Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
877 views
in Technique[技术] by (71.8m points)

python - How to make an enemy follow the player in pygame?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...