sportfrenz
sportfrenz

Reputation: 13

My Pygame window only updates when i am moving my cursor around on the window

My Pygame window will only update if my cursor is moving and is above the window.

import pygame

window_length = 1000
window_height = 600
dimensions = (window_length, window_height)
window = pygame.display.set_mode(dimensions)

WHITE = (255, 255, 255)
BLACK = (0,0,0)

main = True

x = 0
y = 0

while main:
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            main = False
            
        x += 1
            
        #redraw background
        pygame.draw.rect(window, BLACK, (0,0, window_length, window_height))
        pygame.draw.rect(window, WHITE, (x, y, 100, 100))
        pygame.display.update()

pygame.quit()

I tried moving things around but nothing worked.

Upvotes: 1

Views: 104

Answers (1)

mazore
mazore

Reputation: 1024

Unindent the code under the event loop. Right now, you're only doing the drawing code when there are events to loops through, like your mouse moving.

It should look like:

while main:

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

    x += 1

    #redraw background
    pygame.draw.rect(window, BLACK, (0,0, window_length, window_height))
    pygame.draw.rect(window, WHITE, (x, y, 100, 100))
    pygame.display.update()

Upvotes: 2

Related Questions