0

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]
4
  • Show us the complete exception traceback, please. Commented Dec 11, 2013 at 7:55
  • Are you trying to run renderText when self.currentScene doesn't actually specify a valid scene? Commented Dec 11, 2013 at 7:57
  • I'm not too sure I can't really see what is wrong :/ Commented Dec 11, 2013 at 7:57
  • At the start of the method renderText, add print(self.currentScene, len(self.scenes)). Probably one of them is not what you expect. Commented Dec 11, 2013 at 8:34

1 Answer 1

1
            '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. 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok let me check that to see if it makes a difference, thank you
Just to add to this, the error occurs because without the comma at the end of this line, you only have a total of 4 different strings/scenes. Your nextScene() assumes there's 5 - the first if-condition in your nextScene() will append 1 to self.currentScene even if it's 3, meaning you'll try to access self.scene[3+1], but that does not exist because you've only got 4 different scenes: 0, 1, 2 and 3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.