Pygame Error getting list index out of range but i dont know how to fix (I'm new)

Viewed 83

This was shown Traceback (most recent call last):

File "C:/Users/PC GAMER/Desktop/pygame sprite/oo.py", line 46, in <module>
   redrawGameWindow()
File "C:/Users/PC GAMER/Desktop/pygame sprite/oo.py", line 35, in redrawGameWindow
   win.blit(walk_right[walk_Count//3], (x, y))
IndexError: list index out of range

I am extremely new to python in general and pygame I don't really understand what's wrong with this although I know what its trying to say that the item in the list that I'm trying to get is not available but I don't really know what to do if possible please kindly explain how to fix this thankyou for taking your time to read this as my English is not great and thank you for your time in general

import pygame
pygame.init()

Clock = pygame.time.Clock()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("Hello")

x = 100
y = 400
width = 64
height = 64
vel = 10

walk_right = [pygame.image.load("R1.png"), pygame.image.load("R2.png"), pygame.image.load("R3.png"), 
pygame.image.load("R4.png"), pygame.image.load("R5.png"), pygame.image.load("R6.png"), 
pygame.image.load("R7.png"), pygame.image.load("R8.png")]
walk_left = [pygame.image.load("L1.png"), pygame.image.load("L2.png"), pygame.image.load("L3.png"), 
pygame.image.load("L4.png"), pygame.image.load("L5.png"), pygame.image.load("L6.png"), 
pygame.image.load("L7.png"), pygame.image.load("L8.png")]
bg = pygame.image.load("bg.jpg")
char = pygame.image.load("standing.png")

left = False
right= False
walk_Count = 0
jump_count = 10
jump = False
run = True


def redrawGameWindow():
    global walk_Count

    if walk_Count + 1 >= 27:
        walk_Count = 0
    if left:
        win.blit(walk_left[walk_Count//3], (x, y))
    elif right:
        win.blit(walk_right[walk_Count//3], (x, y))
        walk_Count += 1
    else:
        win.blit(char, (x, y))
    pygame.display.update()
    win.blit(bg, (0, 0))



#Mainloop
while run:
    redrawGameWindow()

    Clock.tick(27)
    for event in pygame.event.get():


        if event.type == pygame.QUIT:
            run = False


    keys = pygame.key.get_pressed()


    if keys[pygame.K_RIGHT] and x < 450:
        right = True
        left = False
        x += vel

    elif keys[pygame.K_LEFT] and x > -10:
        x -= vel
        left = True
        right = False
    else:
        right = False
        left = False
        walk_Count = 0
    if not jump:
        if keys[pygame.K_SPACE]:
            left = False
            right = False
            jump = True
    else:
        if jump_count >= -10:
            neg = 1
            if jump_count < 0:
                    neg = -1

            y -= (jump_count ** 2) /2 * neg
            jump_count -= 1

        else:
            jump = False
            jump_count = 10

pygame.quit()
3 Answers

You probably forgot that list indices start at zero and not at 1.

Your variable walk_Count can take a max value of 26, as the bellow:

if walk_Count + 1 >= 27:
    walk_Count = 0

But if you see these lines:

if left:
    win.blit(walk_left[walk_Count//3], (x, y))
elif right:
    win.blit(walk_right[walk_Count//3], (x, y))
    walk_Count += 1

if walk_Count can take a max value of 26 then walk_Count//3 max value is 8. If we try to access walk_righ[8] or walk_left[8] it gives an error because those indices don't exist.

To solve that you can change this part of the code (change 27 to 24):

if walk_Count + 1 >= 24:
    walk_Count = 0

walk_right and walk_left just contain 8 images. Therefore you must reset walk_Count when walk_Count + 1 is greater or equal 24:

def redrawGameWindow():
    global walk_Count

    if walk_Count + 1 >= 24:
        walk_Count = 0 

However, I recommend to check the length of the list. Test if walk_Count // is greater or equal the length of the list. The length of the list can be get with len():

def redrawGameWindow():
    global walk_Count

    if left:
        if walk_Count // 3 >= len(walk_left):
            walk_Count = 0
        win.blit(walk_left[walk_Count//3], (x, y))
        walk_Count += 1
    
    elif right:
        if walk_Count // 3 >= len(walk_right):
            walk_Count = 0
        win.blit(walk_right[walk_Count//3], (x, y))
        walk_Count += 1
    
    else:
        win.blit(char, (x, y))

Or even more generally:

def redrawGameWindow():
    global walk_Count

    image_list = [char]
    if left:
        image_list = walk_left
    elif right:
        image_list = walk_right

    if walk_Count // 3 >= len(image_list):
        walk_Count = 0
    
    win.blit(image_list[walk_Count // 3], (x, y))
    walk_Count += 1

Maybe because walk_left[] size is 8 and 26//3=8. Array's indexes are starting in 0. To solve this problem replace [walk_Count//3]->[walk_Count//3-1]

Related