Ninjapenguin05
Ninjapenguin05

Reputation: 1

list won't change values for pygame rectangles

I have been trying to recreate snake in pygame for fun but the list won't update for the rectangle positions, anyone know what I'm doing wrong with this code???

I passed in a list within a list (snake_body) to a function with this code however the list within a list doesn't change its integer values

if event.key == pygame.K_UP or pygame.K_w:
    snake_body[0][1] -= 50

if event.key == pygame.K_DOWN or pygame.K_s:
    snake_body[0][1] += 50

if event.key == pygame.K_RIGHT or pygame.K_d:
    snake_body[0][0] += 50

if event.key == pygame.K_LEFT or pygame.K_a:
    snake_body[0][0] -= 50

    print(snake_body)

>>> [[370, 50, 30, 30]]

but the up output should be >>> [[320, 50, 30, 30]]

anything would be helpful as I have been stuck on this for a day and it is no longer fun.

Upvotes: 0

Views: 42

Answers (1)

Rabbid76
Rabbid76

Reputation: 210998

See How to test multiple variables for equality against a single value?. A condition like

if event.key == pygame.K_UP or pygame.K_w:

does not do what you expect. It always evaluates to True. The correct syntax is:

if event.key == pygame.K_UP or event.key == pygame.K_w:

Upvotes: 2

Related Questions