How do I make the update function in my player class move through indexes for a animation

Viewed 67

Currently when my code runs the update function for the Player class does nothing. I want to update the function to add 1 to the index. This should make the sprite move through an animation. I don't know what to write here.

import pygame
import  sys, os
import random
from colors import *

CLOCK = pygame.time.Clock()
FPS = 1
SIZE = WIDTH, HEIGHT = 576, 480
TILESIZE = 32
CAPTION = pygame.display.set_caption('BRRRRRRRRR')

class Player(pygame.sprite.Sprite):
    def __init__(self, game):
        self.groups = game.sprites
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.images = []
        self.images.append(pygame.image.load("sprites/0.png"))
        self.images.append(pygame.image.load("sprites/1.png"))
        self.images.append(pygame.image.load("sprites/2.png"))
        self.images.append(pygame.image.load("sprites/3.png"))
        self.i = 0
        self.image = self.images[self.i]
        self.rect = pygame.Rect(5,5,32,32)

    def update(self):
        self.i += 1
        print(self.i)
        self.image = self.images[self.i]

class Control(object):
    """NOthing to see here folks"""
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode(SIZE)
        self.img_dir = os.path.dirname(__file__), 'img'
        #self.load_data()
        self.new()
    def new(self):
        self.sprites = pygame.sprite.Group()
        p = Player(self)
    def load_data(self):
        pass
    def update(self):
        self.sprites.update()
    def draw(self):
        self.screen.fill(BGCOLOR)
        self.sprites.draw(self.screen)
        pygame.display.update()
        CLOCK.tick(FPS)
    def events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit(self.screen)
    def display_fps(self):
        self.caption = "{} - FPS: {:.2f}".format(CAPTION, CLOCK.get_fps())
        pygame.display.set_caption(self.caption)
    def run(self):
        self.display_fps()
        self.events()
        self.update()
        self.draw()
if __name__ == "__main__":
    while True:
        run_it = Control()
        run_it.run()
1 Answers

There are 2 issues. You will get IndexError: list index out of range. You need to make sure that self.i is always less than the number of images in the self.images list:

class Player(pygame.sprite.Sprite):
    # [...]

     def update(self):
        self.i += 1
        if self.i >= len(self.images):
            self.i = 0 
        print(self.i)
        self.image = self.images[self.i]

Since the instance of Control is created in the application loop, your game will restart continuously every frame. This means that i always starts at 0. The instance of Control must be created once before the application loop, instead of in the appliciaition loop:

if __name__ == "__main__":
    run_it = Control()
    while True:
        run_it.run()
Related