How to make an enemy follow the player in pygame?

2019-05-29 00:19发布

问题:

I made part of a game. It runs well but I would like to add enemies in my game. So far I add the image of the enemies in pygame sprites. But how do I make the enemies follow the player? I tried do this but it just made the image of the enemy direct to the player:

def moveEnemy(self):
    enemies.rect.x = player.rect.x
    enemies.rect.y = player.rect.y
    all_sprites_list.add(enemies)
    enemies_list.add(enemies)

I thought this would make the image of the enemy follow the player. Instead it just overlaped the player's image. I read though many pygame sprites examples but the examples saids to replace the 'enemies.rect.x = player.rect.x' with 'enemies.rect.x = -5' or something around that. I also tried this but it just move the image up instead of following the player. Do I have to formulate an equation? If so I do not know how to. How do I make the enemy move but as well make it follow the player? Can someone help me solve this problem? Any help would be appreciated. Windows 7, Python 2.6, Pygame 2.6, sprites.

回答1:

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:

import math

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

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 (dx*self.speed, dy*self.speed) —is greater or equal to the distance between them.



回答2:

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.



回答3:

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.