2

It is updated using pygame groups in run But when my x change is smaller than 1, my sprite just doesn't move However when it is a negative value and larger than -1, it will move as intended.


class Enemy(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('img/blob.png')
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

        
        
    def update(self):
        self.rect.x += 0.5
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Nano
  • 23
  • 3

2 Answers2

2

pygame.Rect only accepts integer values. floating points will get rounded, and for very small values will equal 0 which is why it won't move unless it's bigger than -1 in your case.

From the documentation

The coordinates for Rect objects are all integers.

ShadowMitia
  • 2,411
  • 1
  • 19
  • 24
  • I see, thank you for the comment. Is there an easy solution to this? Also how come negative floats work? – Nano Aug 17 '21 at 08:38
  • The solution is to just use integers. Negative floats work because they are converted to an integer and rounded. So -1.5 would for example -1. (Or -2 I'm not sure). But if you give it -0.1 for example, it will most likely round to 0, which is why It won't move anything. – ShadowMitia Aug 17 '21 at 08:46
  • you can define the x and y coordinates of the object in a simpler way :' self.rect.center = (x,y)',and you have to call the update function inside your main loop:) – Django Aug 17 '21 at 12:30
0

Are you calling the function with Enemy.update() ? If so try to add self.rect.x by 1 instead of 0.5

LuckyLuke
  • 1
  • 1