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?
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.
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]
renderTextwhenself.currentScenedoesn't actually specify a valid scene?print(self.currentScene, len(self.scenes)). Probably one of them is not what you expect.