Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 649
Sandman Avatar asked Dec 15 '22 04:12

Sandman


2 Answers

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.

like image 143
martineau Avatar answered Dec 27 '22 01:12

martineau


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.

like image 30
YXD Avatar answered Dec 27 '22 01:12

YXD