• Advertisement
Sign in to follow this  

Pygame errror: IndexError: list index out of range

This topic is 1501 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I was just making a "Stickman" class for the player and I get this error :IndexError: list index out of range

Here is the code:

import pygame, sys, glob
from pygame import *

h=400
w=800

screen = pygame.display.set_mode((w,h))

clock = pygame.time.Clock()

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("C:\Users\user\Desktop\Python Programs\Sprite Example\Entities\Stcik_Man_Walk*.png")
        self.ani.sort()
        self.ani_pos=0
        self.ani_max = len(self.ani)-1
        self.img = pygame.image.load(self.ani[0])
        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

while 1:
    screen.fill((255,0,0))
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN and event.key == K_RIGHT:
            pos = 1
        elif event.type == KEYUP and event.key == K_RIGHT:
            pos= 0

    player1.update(pos)

    pygame.display.update()


Share this post


Link to post
Share on other sites
Advertisement

It looks like you're going over the end of a list, but I really can't understand what your code is doing (no experience with python).

 

From this line, I assume self.ani has only one record (the name of the file).

self.ani = glob.glob("C:\Users\user\Desktop\Python Programs\Sprite Example\Entities\Stcik_Man_Walk*.png")

But here, it looks like you're trying to read other positions from self.ani that currently doesn't exist.

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

So, maybe if you changed this last line to the code below, it would stop going over the end of the list (stay on the only record in it)

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

I probably don't know what I'm talking about since I know nothing about python.

Edited by dejaime

Share this post


Link to post
Share on other sites

It looks like you're going over the end of a list, but I really can't understand what your code is doing (no experience with python).

 

From this line, I assume self.ani has only one record (the name of the file).

self.ani = glob.glob("C:\Users\user\Desktop\Python Programs\Sprite Example\Entities\Stcik_Man_Walk*.png")

But here, it looks like you're trying to read other positions from self.ani that currently doesn't exist.

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

So, maybe if you changed this last line to the code below, it would stop going over the end of the list (stay on the only record in it)

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

I probably don't know what I'm talking about since I know nothing about python.

I fixed it now was a simple misspell error.

Edited by Crazylegs830

Share this post


Link to post
Share on other sites

It is complaining that you tried to access an element that doesn't exist in an list.

In the stack trace it will show you exactly where is the problem, but - unless it is an internal pygame call - the only points that it can be are here:

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

And here:

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

You can either use pdb to debug it or print those lists to check what is inside and how many elements they have.

Edited by KnolanCross

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement