Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python while loop bugs out

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading