Pygame Not Respounding

Viewed 25

I am a newbie in Pygame (I use many libraries in python) There is a severe problem I face that is My pygame window is not responding I don't know why but this is happening every time I run the code.

Here is the window

here is the code:

import pygame
pygame.init()

win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")

x = 50
y = 50
width = 20
height = 60
vel = 5

x1 = 100
y1 = 100
width1 = 20
height1 = 60
vel1 = 5

run = True

while run:
    pygame.time.delay(100)



    keys = pygame.key.get_pressed()


    if keys[pygame.K_LEFT] and x > vel:
        x -= vel

    if keys[pygame.K_RIGHT] and x < 245 - vel - width:
        x += vel

    if keys[pygame.K_UP] and y > vel  :
        y -= vel

    if keys[pygame.K_DOWN] and y < 500 - height - vel:
        y += vel
    
win.fill((255,255,255))  # Fills the screen with black
pygame.draw.circle(win , (255,0,0), (x,y), width)
pygame.draw.circle(win , (255,0,0), (x1,y1), width)
pygame.draw.rect(win, (0,0,0), (245, 0, 20, 500))   
pygame.display.update() 

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

Could you just please help to deal wit this problem.

2 Answers

It's just a matter of Indentation. You have implemented an infinite loop, therefore the window does not respond. You need to draw the scene and handle the event in the application loop instead of after the application loop.

import pygame
pygame.init()

win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")

x = 50
y = 50
width = 20
height = 60
vel = 5
x1 = 100
y1 = 100
width1 = 20
height1 = 60
vel1 = 5

run = True
while run:
    pygame.time.delay(100)
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and x > vel:
        x -= vel
    if keys[pygame.K_RIGHT] and x < 245 - vel - width:
        x += vel
    if keys[pygame.K_UP] and y > vel  :
        y -= vel
    if keys[pygame.K_DOWN] and y < 500 - height - vel:
        y += vel

# INDENTATION
#-->|
    
    win.fill((255,255,255))  # Fills the screen with black
    pygame.draw.circle(win , (255,0,0), (x,y), width)
    pygame.draw.circle(win , (255,0,0), (x1,y1), width)
    pygame.draw.rect(win, (0,0,0), (245, 0, 20, 500))   
    pygame.display.update() 

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

Your issue is caused by the indentation of your script.

pygame.display.update() will update the screen, that should be inside the while run!

The 'Fills screen with black' can be done above the while, since you want that just once.

Same story for the for loop that will set run = False, the button and event's is something you want to check every iteration, so move it inside the while


import pygame
pygame.init()

win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")



x = 50
y = 50
width = 20
height = 60
vel = 5

x1 = 100
y1 = 100
width1 = 20
height1 = 60
vel1 = 5

run = True

win.fill((255,255,255))  # Fills the screen with black
pygame.draw.circle(win , (255,0,0), (x,y), width)
pygame.draw.circle(win , (255,0,0), (x1,y1), width)
pygame.draw.rect(win, (0,0,0), (245, 0, 20, 500))

while run:
    # pygame.time.delay(100)



    keys = pygame.key.get_pressed()


    if keys[pygame.K_LEFT] and x > vel:
        x -= vel

    if keys[pygame.K_RIGHT] and x < 245 - vel - width:
        x += vel

    if keys[pygame.K_UP] and y > vel  :
        y -= vel

    if keys[pygame.K_DOWN] and y < 500 - height - vel:
        y += vel


    pygame.display.update()

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

pygame.quit()

Related