0

Essentially, what I'm doing is taking the enemies list (on line 1), which is holding a list of coordinates and iterate through each pair in the enemies list at the bottom.

I want to go through each enemy in the list, get the y coordinate, add 10 and then go to the next enemy and add 10, so on and so forth. For some reason, it adds 10 ONCE and then stops, and the enemies do not fall down the screen. I don't know why this is happening. Why is it not running through the for loop anymore? Thank you so much for any help.

NOTE: I removed some code at the top for the sake of being less confusing. The update() function is just the pygame flip function.

enemies = [[100,0], [150,0]]
while True:

    for enemy in enemies:

        x = enemy[0]
        y = enemy[1]
        y += 10
        pygame.draw.rect(screen, (255,0,0), (x, y,10,10))

    # uses flip to update the screen
    update()
    # FPS
    clock.tick(20)

1 Answer 1

1

You're trying to modify a local variable, not the value in the list. You need to write:

enemy[1] += 10

Since integers are immutable (they cannot be changed), the line y = enemy[1] can be thought of as "copy the value from enemy[1] into y".

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

1 Comment

Also, this is a great case for learning how to use a debugger. Then you would be able to step through the code and confirm that the loop actually iterates twice.

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.