M
M
mcmikey2020-11-17 12:31:49
Python
mcmikey, 2020-11-17 12:31:49

Why is the pygame window not responding?

I wrote a simple code to move the square around the window. But for some reason the square does not move and if you click on the window it stops responding. Looked at other forums but nothing helped.
Here is the code itself -

import pygame

pygame.init()
win = pygame.display.set_mode((500, 500))

pygame.display.set_caption("Adventure")

x = 50 
y = 50
width = 60
height = 40
speed = 5

run = True
while run:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        x -= speed
    if keys[pygame.K_RIGHT]:
        x += speed
    if keys[pygame.K_UP]:
        y -= speed
    if keys[pygame.K_DOWN]:
        y += speed
        
    pygame.draw.rect(win,(0, 0, 255), (x, y, width, height))
    pygame.display.update()

    input()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2020-11-17
@mcmikey

So you don't need to include console input() in the pygame work loop, your window "freezes" because it is constantly waiting for input to the console. There shouldn't be any input.
At the same time, if you are drawing a square, it is worth adding a screen clearer before pygame.draw... so that the "trace" does not drag behind it.
win.fill((0,0,0))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question