Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7827703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:00:03+00:00 2026-06-02T10:00:03+00:00

I am trying to get my walk animation in python using pygame to work

  • 0

I am trying to get my walk animation in python using pygame to work and im getting the following errors:

Traceback (most recent call last):
  File "C:\Users\name\Desktop\game\game.py", line 37, in <module>
    player1 = player()
  File "C:\Users\name\Desktop\game\game.py", line 21, in __init__
    self.img = pygame.image.load(self.ani[1])
IndexError: list index out of range

here is my code:

from pygame.locals import *

pygame.init()
clock = pygame.time.Clock()

height = 400
width = 800
screen = pygame.display.set_mode((width, height), 0, 32)

class player:
    def __init__(self):
        self.x = 200
        self.y = 300
        self.ani_speed_init = 10
        self.ani_speed = self.ani_speed_init
        self.ani = glob.glob("game\redplayer*.png")
        self.ani.sort()
        self.ani_pos = 0
        self.ani_max = len(self.ani) - 1
        self.img = pygame.image.load(self.ani[1])
        self.update(0)

    def update(self, pos):
        if pos != 0:
            self.ani_speed -= 1
            self.x += pos
            if self.ani_speed == 0:
                self.img = pygame.image.load(self.ani[self.ani_pos])
                self.ani_speed = self.ani_speed_init
                if self.ani_pos == self.ani_max:
                    self.ani_pos = 0
                else:
                    self.ani_pos += 1
        screen.blit(self.img,(self.x, self.y))

player1 = player()
pos = 0

i checked the file name,and i dont think thats it, but it could be

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:00:14+00:00Added an answer on June 2, 2026 at 10:00 am

    It looks like you’re off-by-one on the index for self.ani. The first item in the list is index 0, because you only loaded one frame.

    self.img = pygame.image.load(self.ani[0])
    

    I messed with your code on my machine to get it working, here it is. 🙂
    Pre-load each animation frame into a list of prerendered surfaces, then blit the appropriate frame to the screen.
    Also added a simple game loop (press Q to quit) including an FPS clock.

    import pygame
    from pygame.locals import *
    
    pygame.init()
    clock = pygame.time.Clock()
    FPS = 30
    
    width = 800
    height = 600
    
    screen = pygame.display.set_mode((width, height), 0, 32)
    
    class player:
        def __init__(self, init_pos = (10,10), init_ani_speed = 10):
            self.x = init_pos[0]
            self.y = init_pos[1]
    
            self.ani_speed_init = init_ani_speed
            self.ani_speed = self.ani_speed_init
    
            self.ani = []
            # nux-style code here! :)
            # self.ani.append(glob.glob("game\\redplayer_01*.png"))
            # self.ani.append(glob.glob("game\\redplayer_02*.png"))
    
            self.ani.append("./ani/super_neko_01.png")
            self.ani.append("./ani/super_neko_02.png")
    
            self.ani_max = len(self.ani)-1
            self.ani_pos = 0
    
            self.img = []
            for item in self.ani:
                self.img.append(pygame.image.load(item))
    
            self.update(0)
    
    
        def update(self, pos = 0):
            # init: starts at 10, immediately blits self to screen at x,y
            if pos != 0:
                self.ani_speed -= 1 
                self.x += pos # move right by pos pixels
                if self.ani_speed == 0:
                    self.ani_speed = self.ani_speed_init
                    if self.ani_pos < self.ani_max:
                        self.ani_pos += 1
                    else:
                        self.ani_pos = 0
    
            tempSurf = self.img[self.ani_pos]
            screen.blit(tempSurf,(self.x, self.y))
    
    player1 = player()
    
    # Main Game Loop
    quit = False
    while quit == False:
        for event in pygame.event.get():
            if (event.type == KEYUP): 
                        if (event.key == K_q):
                            quit = True
    
        if quit == False:
            player1.update(1)
            pygame.display.update()
        clock.tick(FPS)
    
    pygame.quit()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying get values from a GridView using the following code: foreach (GridViewRow row
I am trying get Struts 2 and Tiles to work and I am using
I am trying get all html links within a string and replace them using
I am trying get all html links within a string and replace them using
Trying to get parameters from a PUT request using HttpServlet#doPut: public void doPut(HttpServletRequest request,
I am trying to run a Walk style animation on my main game sprite.
I'm trying to get simple code working, unfortunately I'm a python beginner. My script
Here is what I am trying to get working: I have the following line
I am just trying to walk through this tutorial http://jimneath.org/2011/03/24/using-redis-with-ruby-on-rails.html#redis_and_rails And when I put
trying to get some data from a plist into the MWPhotoBrowser sample app. Any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.