首页 > 解决方案 > 为什么 Pygame 从列表索引错误中崩溃?

问题描述

我正在 Pygame 中开发一个游戏,窗口在执行几秒钟后崩溃。我将其追溯到用于“敌人”精灵行走动画的列表。我仍然无法在我的列表中找到它有什么问题。我对我的玩家行走动画做了同样的事情。

这是它抛出的错误:

Traceback (most recent call last):
 File "/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic >platformer/basic_platformer.py", line 188, in <module>
   redrawGameWindow()
 File "/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic >platformer/basic_platformer.py", line 111, in redrawGameWindow
   goblin.draw(ben.window)
 File "/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic >platformer/basic_platformer.py", line 85, in draw
   window.blit(self.walkRight[self.walkCount //3], (self.x, self.y))
IndexError: list index out of range

我已经注释掉了玩家和敌人的类以及行走的列表;“向右走,向左走”。

import sys
import pygame

pygame.init()

pygame.display.set_caption("Ben Meitzen")

#player walking lists
walkRight = [pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic platformer/sprites/tile%s.png' % frame) for frame in range(17, 24)]
walkLeft = [pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic platformer/sprites/tile%s.png' % frame) for frame in range(9, 16)]

bg = pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic platformer/skybackground.png')

char = pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic platformer/sprites/tile1.png')

clock = pygame.time.Clock()

#player class
class player(object):
    def __init__(self,x, y, width, height):         
        self.x = x
        self.y = y
        self.width = width                            
        self.height = height                          
        self.velocity = 11
        self.isJump = False
        self.jumpCount = 7
        self.right = False
        self.left = False
        self.walkCount = 0
        self.window = pygame.display.set_mode((860,538))
        self.standing = True                                            
                                                      
    def draw(self, window):
        
        if self.walkCount + 1 >= 8:  
            self.walkCount = 0      

        if not (self.standing):
        
            if self.left:
                self.window.blit(walkLeft[self.walkCount//1], (self.x, self.y))
                self.walkCount += 1

            elif self.right:
                self.window.blit(walkRight[self.walkCount//1], (self.x, self.y))
                self.walkCount += 1
        
        else:
            if self.right:
                window.blit(walkRight[0], (self.x, self.y))
            else:
                window.blit(walkLeft[0], (self.x, self.y))
                
class projectile(object):

    def __init__(self, x, y, radius, color, facing):
        self.x = x
        self.y = y
        self.color = color
        self.facing = facing
        self.velocity = 40 * facing
        self.radius = radius
    def draw(self, window):
        pygame.draw.circle(window, self.color, (self.x, self.y), self.radius)


#enemy class
class enemy(object):

#enemy walking lists   
 walkRight = [pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic platformer/enemy sprites/enemy%s.png' % frame) for frame in range(17, 24)]
    walkLeft = [pygame.image.load('/Users/luke.redwine/Documents/Python/PyGame/fivr code/basic platformer/enemy sprites/enemy%s.png' % frame) for frame in range(9, 16)]

    def __init__(self, x, y, width, height, end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.end = end
        self.walkCount = 0
        self.velocity = 3
        self.path = [self.x, self.end]
        
    def draw(self, window):
        self.move()
        if self.walkCount + 1 >= 8:
            self.walkcount = 0

        if self.velocity > 0:
            window.blit(self.walkRight[self.walkCount //3], (self.x, self.y))
            self.walkCount += 1
        else:
            window.blit(self.walkLeft[self.walkCount //3], (self.x, self.y))
            self.walkCount += 1

    def move(self):
        if self.velocity > 0:
            if self.x + self.velocity < self.path[1]:
                self.x += self.velocity
            else:
                self.velocity = self.velocity * -1
                self.walkcount = 0
        else:
            if self.x - self.velocity > self.path[0]:
                self.x += self.velocity
            else:
                self.velocity = self.velocity * -1
                self.walkcount = 0

        
#game window-------------------------------------------------------------------------------------------------------------------------------------------------
def redrawGameWindow():

    ben.window.blit(bg,(0,0))
    ben.draw(ben.window)
    goblin.draw(ben.window)
    
    for bullet in bullets:
        bullet.draw(ben.window)

    pygame.display.update()

##main loop##------------------------------------------------------------------------------------------------------------------------------------------------

goblin = enemy(100, 460, 70, 70, 700)
ben = player(30, 460, 64, 64)
bullets = []                    
run = True
                                                    
while run:
    clock.tick(8)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    for bullet in bullets:
        if bullet.x < 860 and bullet.x > 0:
            bullet.x += bullet.velocity

        else:
            bullets.pop(bullets.index(bullet))
    
    keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE]:
        if ben.left:
            facing = -1
        else:
            facing = 1

        if len(bullets) < 5:
            bullets.append(projectile(round(ben.x + ben.width //2), round(ben.y + ben.height -30 //2), 6, (0,0,0), facing))

    if keys[pygame.K_LEFT] and ben.x > ben.velocity:
        ben.x -= ben.velocity
        ben.left = True
        ben.right = False
        ben.standing = False
        
    elif keys[pygame.K_RIGHT] and ben.x < 860 - ben.width - ben.velocity:
        ben.x += ben.velocity
        ben.right = True
        ben.left = False
        ben.standing = False
        
    else:
        ben.standing = True
        ben.walkCount = 0

    if not(ben.isJump):
    
        if keys[pygame.K_UP]:
            ben.isJump = True
            ben.right = False
            ben.left = False

    else:
        if ben.jumpCount >= -7:
            neg = 0.5        


            if ben.jumpCount < 0:
                neg = -0.5

            ben.y -= (ben.jumpCount **2) * 1.5 * neg 
            ben.jumpCount -= 2

        else:
            ben.isJump = False
            ben.jumpCount = 7 
            ben.velocity = 11

    redrawGameWindow()      
#-----------------------------------------------------------------------------------------------
    
pygame.quit()


标签: pythonpython-3.xlistpygame

解决方案


在这部分代码中:

if self.walkCount + 1 >= 8:
    self.walkcount = 0

self.walkCount写为self.walkcount(小写 c)。因此,实际walkCount没有被重置为零,因此列表索引超出范围错误。


推荐阅读