简体   繁体   English

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

[英]IndexError: list index out of range (Python 2.7)

Hello I have recently been programming a game and I have come across and IndexError saying 'IndexError: list index out of range' and was wondering if anyone knew why? 您好,我最近一直在编写游戏,但遇到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)

The error appears in the render text area of the code. 错误出现在代码的渲染文本区域中。 Here is the nextScene function for the scenes. 这是场景的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)

The Error: 错误:

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]
            '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.' \

It's hard to see with such a long line, but this line does not have a comma on the end. 这么长的行很难看到,但是该行的末尾没有逗号。 When Python sees two string literals next to each other, it concatenates their contents and treats them as one string. 当Python看到两个字符串文字彼此相邻时,它将它们的内容串联起来,并将它们视为一个字符串。 Put in a comma and see if the problem goes away. 放入逗号,看看问题是否消失。 I recommend putting a comma after the last element of the list too, so you don't forget the comma when you go to add more scenes later. 我建议也将逗号放在列表的最后一个元素之后,这样以后再添加更多场景时就不要忘记逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM