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)
>>> [[int, int, int, int]]
anything would be helpful as I have been stuck on this for a day and it is no longer fun.
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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.