Let’s take a look into the last code to explain a little bit what I did in the class Player, before we go on.
If you want to see the entire code go to the previos post (at the end of this post).
class Player(pg.sprite.Sprite): def __init__(self, px, py, action): super().__init__() lst = glob("cat\\{}*".format(action)) img = [f for f in lst if len(f) == len(lst[0])] img.extend([f for f in lst if len(f) != len(lst[0])]) self.sprites = [pg.image.load(x) for x in img] self.current = 0 self.image = self.sprites[self.current] self.rect = self.image.get_rect() self.rect.topleft = [px, py] self.wait = 0 def update(self, pause=4): self.wait += 1 if self.wait == pause: self.wait = 0 self.current += 1 if self.current >= len(self.sprites): self.current = 0 self.image = self.sprites[self.current]
Video
Index of Pygame First Game
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame