Why is my enemy movement script not working in pygame

Viewed 47

I've been working on movement for the enemy in my game for a day or so and can't seem to get it working

the problem is it will chase the character for a little bit until it hits x or y zero and will stick there forever, and sometimes it will just stick in place when it hits the player

any help will be greatly appreciated

import pygame
import math

ALPHA = (0, 255, 0)
path = "data/chr/squid.png"

def findDist(coords1, coords2):
  try:
    return math.sqrt(pow(coords1[0] - coords2[0], 2) + pow(coords1[1] - coords2[1], 2))
  except:
    print("invalid dist input, ignoring")

class Squid(pygame.sprite.Sprite):
  #player object

  def __init__(self, X, Y):
    pygame.sprite.Sprite.__init__(self)
    self.speed = .01
    self.images = []
    for i in range(1, 5):
      img = pygame.image.load(path).convert()
      img.convert_alpha()  # optimise alpha
      img.set_colorkey(ALPHA)  # set alpha
      self.images.append(img)
      self.image = self.images[0]
      self.rect = self.image.get_rect()
      self.rect.x = X
      self.rect.y = Y
  

  def chasePlayer(self, 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 
    self.rect.x += dx * self.speed
    self.rect.y += dy * self.speed
    
1 Answers

Since pygame.Rect is supposed to represent an area on the screen, a pygame.Rect object can only store integral data.

The coordinates for Rect objects are all integers. [...]

The fraction part of the coordinates gets lost when the new position of the object is assigned to the Rect object. If this is done every frame, the position error will accumulate over time.

If you want to store object positions with floating point accuracy, you have to store the location of the object in separate variables respectively attributes and to synchronize the pygame.Rect object. round the coordinates and assign it to the location (e.g. .topleft) of the rectangle.

x, y = # floating point coordinates
rect.topleft = round(x), round(y)

Read about Classes. If you want to spawn multiple enemies, you must create multiple Instance Objects of the Squid class. e.g.:

enemies = []
for _ in range(5):
    x = random.randrange(screen_width)
    y = random.randrange(screen_height)
    enemies.append(Squid(x, y))

Squid class:

class Squid(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.speed = .01
        self.image = pygame.image.load(path).convert()
        self.image.convert_alpha()  # optimise alpha
        self.image.set_colorkey(ALPHA)  # set alpha
        self.x = x
        self.y = y
        self.rect = self.image.get_rect(topleft = (round(x), round(y)))

    def chasePlayer(self, 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 
        self.x += dx * self.speed
        self.y += dy * self.speed
        self.rect.x = round(self.x)
        self.rect.y = round(self.y)
Related