How to make an enemy follow the player in pygame?

我是研究僧i 提交于 2019-12-06 08:28:00

You need to reduce the distance between the enemy and the player by changing the enemy's position. This can be done by finding the difference between their positions and then using that vector to compute a normalized (unit length) direction vector. With that, change the enemy's position by adding its speed times the direction vector to it.

One way of doing this would be by adding a method like the following to your Enemy class. The mathmatical operations can be done using the built-in math module or the pygame.math module. The latter also has support for a 2D Vector2 class, so it would probably be the better one to actually use.

import math
import pygame
from pygame.locals import *

class Enemy(object):
    ...
    def move_towards_player(self, player):
        # Find direction vector (dx, dy) between enemy and player.
        dx, dy = player.rect.x - self.rect.x, player.rect.y - self.rect.y
        dist = math.hypot(dx, dy)
        dx, dy = dx / dist, dy / dist  # Normalize.
        # Move along this normalized vector towards the player at current speed.
        self.rect.x += dx * self.speed
        self.rect.y += dy * self.speed

    # Same thing using only pygame utilities
    def move_towards_player2(self, player):
        # Find direction vector (dx, dy) between enemy and player.
        dirvect = pygame.math.Vector2(player.rect.x - self.rect.x,
                                      player.rect.y - self.rect.y)
        dirvect.normalize()
        # Move along this normalized vector towards the player at current speed.
        dirvect.scale_to_length(self.speed)
        self.rect.move_ip(dirvect)

You will need to add checks to determine whether the enemy object will overshoot and thereby hit the player along the way if it moves this distance, and react accordingly. Hint: A collision will occur whenever the amount to be moved—the length of the speed vector, i.e. the object's speed—is greater or equal to the distance between them.

At a high level you need to work out the vector from the enemy to the player. The x and y components of the direction vector would look like:

enemies.rect.x - player.rect.x
enemies.rect.y - player.rect.y

You then add a multiple of the direction to the enemy position to make it move towards the player.

However: You'll notice that this vector will have a large magnitude when the player and enemy are far apart and a small magnitude when they are closer together. So to avoid having the enemy move at super-fast speeds, you would could normalise the direction vector and multiply by a speed s to keep it under control.

Here, px & py are the position (globals) of the object the enemy is following/chasing.

class Enemy(object):
    def __init__(self,x,y):  # initial position
        self.x = x 
        self.y = y
    def move(self, speed=5): # chase movement
        # Movement along x direction
        if self.x > px:
            self.x -= speed
        elif self.x < px:
            self.x += speed
        # Movement along y direction
        if self.y < py:
            self.y += speed
        elif self.y > py:
            self.y -= speed

If you want, you can customize the speed along x & y directions too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!