Search code examples
pythonpygame

pygame move rectangle stays at top but move_ip moves


when i change move_ip() to move() in pygame the rectangle stays at the top left
there are no errors
it just opens the pygame window with the rectangle at the top left
image

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface((32, 32))
        self.image.fill(charcolor)
        self.rect = self.image.get_rect()  # Get rect of some size as 'image'.
        self.rect.move(50,400)
        self.velocity = [10, 0]

        def update():
            self.rect.move_ip(*self.velocity)

Solution

  • While move_ip() changes the position of the object itself, move returns a new object with a different position, but does not change the object itself.
    Hence self.rect.move(50,400) does nothing at all, because the return value goes nowhere.

    The statement

    self.rect.move_ip(50,400)
    

    can be replaced by

    self.rect = self.rect.move(50,400)