1

Here is my code:

import game, pygame, sys
from gameobjects import *
from imageloader import *




def main():
    screen = pygame.display.set_mode( (800, 600) )
    background = Background("images/Nebula1.bmp", screen.get_width(), screen.get_height())
    letter1 = pygame.image.load("1.bmp")
    letter2 = pygame.image.load("2.bmp")
    letter3 = pygame.image.load("3.bmp")
    letter4 = pygame.image.load("4.bmp")
    letter5 = pygame.image.load("5.bmp")
    letter6 = pygame.image.load("6.bmp")
    letter7 = pygame.image.load("7.bmp")
    letter8 = pygame.image.load("8.bmp")
    letter9 = pygame.image.load("9.bmp")
    letter10 = pygame.image.load("10.bmp")
    Letters = [letter1, letter2, letter3, letter4, letter5, letter6, letter7, letter8, letter9, letter10]
    letter = 0

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        screen.blit(background.image, (0,0))

        for i in Letters:
            screen.blit( Letters[letter], (330 + 14 * letter, 190) )
            letter += 1


        pygame.display.flip()
All the items in the Letters list are pygame.Surfaces retrieved through using pygame.image.load(). Every time I run this code, I get an error that looks like this:

    Traceback (most recent call last):
      File "main.py", line 75, in <module>
        main()
      File "main.py", line 32, in main
        screen.blit( Letters[letter], ((330 + 14 * letter, 190)) )
    IndexError: list index out of range

Why is this happening? It seems perfectly valid to me. What am I doing wrong? What I want to do is paste each letter on the screen 14 pixels over from the last one, because each one is 14 pixels wide. Any help? (I am new to python so if I made a dumb mistake, forgive me.)

1 Answer 1

2

You have to set letter = 0 for each iteration. Because its outside of the while loop, letter is only set to 0 on initialization. Afterwards it throws an IndexError as on the second iteration of the for i in letters, as letter is now equal to the length of the letters because of the last iteration!

Use enumerate() to safety and easily increment:

for i,val in enumerate(Letters):
        screen.blit( Letters[i], (330 + 14 * letter, 190) )

or alternatively, include letter = 0 inside the while loop:

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    screen.blit(background.image, (0,0))

    letter = 0
    for i in Letters:
        screen.blit( Letters[i], (330 + 14 * letter, 190) )
        letter += 1
Sign up to request clarification or add additional context in comments.

10 Comments

It ran, but I ran into another problem. When I run it, the letters don't really print to the screen, they just really quickly slide across it.... Do you know why that might be happening?
I'm not quite sure what you mean. Since these are two different questions, you should ask another one about the new issue. If my answer solved you first problem, can you please accept my answer?
What I meant was that the for loop doesn't do what it was supposed to do, that was what I wanted an answer to. That was the question, cuz I still have an error, just not the same error.
Where is the bad for loop in the question? Please don't edit away the error, how is anyone supposed to understand what this answer is fixing?
Oh oops sorry I was adding the rest of the code, I'll change the error back. It was in the copy paste.
|

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.