Python while loop bugs out

Question:

I’m trying to make a game with pygame in which an object moves and i’m trying to get rid of its previous image by drawing a background colored object in its previous position, but for some reason it just wont work, and i’ve extracted the part of the code which fails

import pygame, sys, math
pygame.init()

player = [250, 250]
size = 50
WIDTH = 10*size
HEIGHT = 10*size
speed = 1
prev_pos = player

W = False

screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                W = True
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w:
                W = False
    print(prev_pos)
    if W:
        player[1] = player[1]-speed
    print(f"wtf? {prev_pos}ww")

    pygame.draw.circle(screen, 'black', center=(prev_pos[0], prev_pos[1]), radius=20)
    pygame.draw.circle(screen, 'blue', center=(player[0], player[1]), radius=20)
    
    prev_pos = player
    print(f"prev pos has changed {prev_pos}")

    pygame.display.flip()
    clock.tick(60)

this is the original code you can move with w key

a = [250, 250]
b = a
while True:

    if a[0] > 0:
        print(b)
        a[0] += 20
        print(b)

print(f"b has changed {b}")

and for some reason when a changes it changes b, now i know it might not be a bug but i can’t fix this issue on my own

i tried storing it in another variable, another list and all kinds of stuff but nothing worked

Asked By: Slash

||

Answers:

prev_pos = player does not create a copy of the list, but only a new reference to the same list. You must copy the elements of the list:

prev_pos = player[:]

Complete code

import pygame, sys, math
pygame.init()

player = [250, 250]
size = 50
WIDTH = 10*size
HEIGHT = 10*size
speed = 1
prev_pos = player[:]

W = False

screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                W = True
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w:
                W = False
    print(prev_pos)
    if W:
        player[1] = player[1]-speed
    print(f"wtf? {prev_pos}ww")

    pygame.draw.circle(screen, 'black', center=(prev_pos[0], prev_pos[1]), radius=20)
    pygame.draw.circle(screen, 'blue', center=(player[0], player[1]), radius=20)
    
    prev_pos = player[:]
    print(f"prev pos has changed {prev_pos}")

    pygame.display.flip()
    clock.tick(60)

Also see How can I make a sprite move when key is held down

Answered By: Rabbid76
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.