Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Pygame errror: IndexError: list index out of range


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Crazylegs830   Members   -  Reputation: 226

Like
0Likes
Like

Posted 09 December 2013 - 08:24 PM

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()




Sponsor:

#2 dejaime   Crossbones+   -  Reputation: 4119

Like
2Likes
Like

Posted 09 December 2013 - 09:02 PM

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, 09 December 2013 - 09:02 PM.


#3 Crazylegs830   Members   -  Reputation: 226

Like
0Likes
Like

Posted 09 December 2013 - 09:12 PM

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, 09 December 2013 - 09:16 PM.


#4 KnolanCross   Members   -  Reputation: 1359

Like
0Likes
Like

Posted 10 December 2013 - 09:17 AM

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, 10 December 2013 - 09:17 AM.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS