pygame do you need to use int() for positions 1

pygame do you need to use int() for positions

class Hero(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = load_image('hero.png').convert_alpha()
        self.position = [0, 0]
        self.rect = self.image.get_rect()

    def update(self, dt):
        x = round(self.position[0])
        y = round(self.position[1])
        self.rect.topleft = x, y

Here is what the above code is Doing:
1. We create a new class called Hero.
2. We call the __init__ method of the parent class (Sprite) to initialize the sprite.
3. We load the image and convert it to have an alpha channel.
4. We set the position of the hero to [0, 0].
5. We get the rectangle of the image.
6. We create an update method that will be called every frame.
7. We round the position of the hero to integers.
8. We set the top left of the rectangle to the rounded position.

Similar Posts