如何使敌人跟随玩家

时间:2020-01-18 21:45:35

标签: python tkinter

我找到了有关pygame的信息,但是我无法在tkinter中对此进行解释。
图标-https://imgur.com/a/PQx9tpW
pastebin-https://pastebin.com/ykUzj9Pc

def key_pressed(event):
    global e
    if event.keysym == 'Up':
        move_wrap(player, (0, -step))
    if event.keysym == 'Down':
        move_wrap(player, (0, step))
    if event.keysym == 'Left':
        move_wrap(player, (-step, 0))
    if event.keysym == 'Right':
        move_wrap(player, (step, 0))
    check_move()
    # for enemy in enemies:
    #     direction = enemy[1]() # вызвать функцию перемещения у "врага"
    #     move_wrap(enemy[0], direction) # произвести  перемещение 
    #     print(e)
    for enemy in enemies:
        direction = enemy[1]()
        move_wrap(enemy[0], direction)
        list_ = [(step, 0), (-step, 0), (0, step), (0, -step)]
        predictions = []
        for i in list_:
            predictions.append([canvas.coords(enemy[0])[0] + i[0], canvas.coords(enemy[0])[1] + i[1]])
        for i in predictions:
            if i[0] - canvas.coords(player)[0] > i[1] - canvas.coords(player)[1]:
                if predictions.index(i) == 1:
                    move_wrap(enemy[0], (step, 0))
                elif predictions.index(i) == 2:
                    move_wrap(enemy[0], (-step, 0))
                elif predictions.index(i) == 3:
                    move_wrap(enemy[0], (0, step))
                elif predictions.index(i) == 4:
                    move_wrap(enemy[0], (0, -step))
        predictions.clear()

我有一个敌人清单。在此列表中,我有一个带有(敌人,random_move函数)的元组。

1 个答案:

答案 0 :(得分:0)

要跟随player,您必须计算enemyplayer之间的向量,并对向量进行归一化(因此其长度将为1),然后可以将其乘以敌人的速度得到它必须移动的距离

player_x, player_y = 0, 0
enemy_x,  enemy_y  = 100, 100


diff_x = player_x - enemy_x
diff_y = player_y - enemy_y

distance = (diff_x**2 + diff_y**2)**0.5  # Pythagoras: a**2 + b**2 = c**2

normal_x = diff_x/distance
normal_y = diff_y/distance

print(normal_x, normal_y)

enemy_speed = 5

enemy_move_x = enemy_speed * normal_x
enemy_move_y = enemy_speed * normal_y

print(enemy_move_x, enemy_move_y)

您可能还必须检查distance是否不为零,因为您可能会得到ZeroDivisionError: division by zero


编辑:最小的工作示例-使用键WASD移动绿色圆圈,随后将出现红色圆圈。

import tkinter as tk
import random

def follow(player_x, player_y, enemy_x, enemy_y, enemy_speed=5):
    diff_x = player_x - enemy_x
    diff_y = player_y - enemy_y

    distance = (diff_x**2 + diff_y**2)**0.5  # Pythagoras: a**2 + b**2 = c**2

    if distance <= enemy_speed:
        return diff_x, diff_y

    normal_x = diff_x/distance
    normal_y = diff_y/distance

    enemy_move_x = enemy_speed * normal_x
    enemy_move_y = enemy_speed * normal_y

    return enemy_move_x, enemy_move_y

def on_key_press(event):
    global player_move_x
    global player_move_y

    if event.char == 'w':
        player_move_y -= 5
    if event.char == 's':
        player_move_y += 5
    if event.char == 'a':
        player_move_x -= 5
    if event.char == 'd':
        player_move_x += 5

def on_key_release(event):
    global player_move_x
    global player_move_y

    if event.char == 'w':
        player_move_y -= -5
    if event.char == 's':
        player_move_y += -5
    if event.char == 'a':
        player_move_x -= -5
    if event.char == 'd':
        player_move_x += -5

def update_game():
    global player_x
    global player_y
    global enemy_x
    global enemy_y

    player_x += player_move_x
    player_y += player_move_y
    canvas.move(player_id, player_move_x, player_move_y)

    enemy_move_x, enemy_move_y = follow(player_x, player_y, enemy_x, enemy_y, enemy_speed)
    enemy_x += enemy_move_x
    enemy_y += enemy_move_y
    canvas.move(enemy_id, enemy_move_x, enemy_move_y)

    root.after(100, update_game)

# --- main ---

player_x = 250
player_y = 250
player_move_x = 0
player_move_y = 0

enemy_x = random.randint(0, 500)
enemy_y = random.randint(0, 500)
enemy_speed = 5

root = tk.Tk()

canvas = tk.Canvas(root, width=500, height=500, bg='gray')
canvas.pack()
root.bind('<KeyPress>', on_key_press)
root.bind('<KeyRelease>', on_key_release)

player_id = canvas.create_oval(player_x-5, player_y-5, player_x+5, player_y+5, fill='green')
enemy_id = canvas.create_oval(enemy_x-5, enemy_y-5, enemy_x+5, enemy_y+5, fill='red')

update_game()

root.mainloop()