索引器:列表索引超出范围(Python 2.7)

索引器:列表索引超出范围(Python 2.7),python,python-2.7,pygame,Python,Python 2.7,Pygame,您好,我最近正在编写一个游戏,我遇到一个IndexError说“IndexError:列表索引超出范围”,我想知道是否有人知道为什么 class MenuScene(MenuClass): def __init__(self, surface, engine): MenuClass.__init__(self, surface) self.MoonSurvival = engine self.cu

您好,我最近正在编写一个游戏,我遇到一个IndexError说“IndexError:列表索引超出范围”,我想知道是否有人知道为什么

    class MenuScene(MenuClass):
        def __init__(self, surface, engine):
            MenuClass.__init__(self, surface)

            self.MoonSurvival = engine

            self.currentScene = 0

            self.scenes = ['CARTER?! CARTER?! ARE YOU THERE?!\nYeah I am here',
            'Look there have been sights of hostile alien activity near moon base 4,\n I need you to go and check it out as this could be a problem.\n\nOk I will, where is my rifle?', \
            'It is just outside your room tell,\nme when you are ready and I will send you there.,\nGood luck Carter.\n\nThe aim of the game is to survive the alien invasion as long as possible,\nThere are some special drops the aliens can drop.\nThese include, health, shield, superhealth and triple-bullets.' \
            '\nBe careful Carter. The aliens do not stay small for long....\n',  \
            'CONTROLS:\nA and D = Left, Right\nSPACE = Jump\nLeft Mouse Button = Shoot']



def renderText(self):
            # split texts at \n (newline)

            texts = self.scenes[self.currentScene].split('\n')

            for i in range(len(texts)):
                textSurface = self.menufont.render(texts[i], 0, (255, 0, 0))

                textRect = textSurface.get_rect()
                textRect.centerx = SCREEN_WIDTH / 2
                textRect.centery = SCREEN_HEIGHT / 2 + i * self.menufont.size(texts[i])[1]

                self.surface.blit(textSurface, textRect)
错误出现在代码的“渲染文本”区域中。下面是场景的nextScene函数

def nextScene(self):
    if self.currentScene < 4:
        # li
        self.currentScene += 1
    elif self.currentScene == 5:
        self.MoonSurvival.resetGame()
        self.MoonSurvival.setState(MENU_GAMEFINISH)
    else:
        self.MoonSurvival.setState(MENU_INGAME)
def nextScene(自):
如果self.currentScene<4:
#李
self.currentScene+=1
elif self.currentScene==5:
self.MoonSurvival.resetGame()
self.MoonSurvival.setState(菜单\游戏完成)
其他:
self.MoonSurvival.setState(菜单名)
错误:

Traceback (most recent call last):
  File "F:\My Game\MoonSurvival.py", line 416, in <module>
    Game().run()
  File "F:\My Game\MoonSurvival.py", line 194, in run
    self.menuScene.draw()
  File "F:\My Game\menus.py", line 168, in draw
    self.renderText()
  File "F:\My Game\menus.py", line 202, in renderText
    texts = self.scenes[self.currentScene].split('\n')
IndexError: list index out of range
[Finished in 5.8s]
回溯(最近一次呼叫最后一次):
文件“F:\My Game\MoonSurvival.py”,第416行,在
Game().run()
文件“F:\My Game\MoonSurvival.py”,第194行,运行中
self.menuScene.draw()
文件“F:\My Game\menus.py”,第168行,在绘图中
self.renderText()
renderText中第202行的文件“F:\My Game\menus.py”
text=self.scenes[self.currentsecene].split('\n')
索引器:列表索引超出范围
[以5.8秒完成]

这么长的一行很难看清,但这一行的末尾没有逗号。当Python看到两个相邻的字符串文本时,它会连接它们的内容并将它们视为一个字符串。加个逗号,看看问题是否解决了。我建议在列表的最后一个元素后面也加一个逗号,这样在以后添加更多场景时就不会忘记逗号。

向我们展示完整的异常回溯,请。当
self.currentScene
实际上没有指定有效的场景时,您是否正在尝试运行
renderText
?我不太确定我是否真的看不出有什么问题:/在方法renderText的开始处,添加
print(self.currentScene,len(self.scenes))
。可能其中有一个不是您所期望的。好吧,让我检查一下,看看这是否有区别,谢谢您只是要添加到这一点,出现错误是因为如果此行末尾没有逗号,您总共只有4个不同的字符串/场景。nextScene()假设有5个,nextScene()中的第一个if条件将在self.currentScene中附加1,即使它是3,这意味着您将尝试访问self.scene[3+1],但这并不存在,因为您只有4个不同的场景:0、1、2和3。
            'It is just outside your room tell,\nme when you are ready and I will send you there.,\nGood luck Carter.\n\nThe aim of the game is to survive the alien invasion as long as possible,\nThere are some special drops the aliens can drop.\nThese include, health, shield, superhealth and triple-bullets.' \