Sprite doesn't move right when rect.x change is <1

Viewed 160

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
2 Answers

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.

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

Related