0
import pygame,sys

pygame.init()

screen = pygame.display.set_mode((800,600))

game_over = False

while not game_over:
        for event in pygame.event.get():

                if event.type == pygame.QUIT:
                        sys.exit()
pygame.draw.rect(screen, (255,0,0),(400,300,50,50))
pygame.display.flip()
3
  • select code and use button {} to correctly format it. And describe problem below code.
    – furas
    Apr 15, 2019 at 1:21
  • you have wrong indentions - you have to draw and flip inside while loop.
    – furas
    Apr 15, 2019 at 1:22
  • You will never reach the code below the while loop.
    – Klaus D.
    Apr 15, 2019 at 1:38

1 Answer 1

0

As @Furas writes, the screen update code is not being called from within the loop. Python uses indentation to designate code blocks, so if the function call (or other code section) is not indented to the correct column, it is literally a completely different set of operations.

Since a piece of sample code is worth a thousand words:

import pygame,sys

pygame.init()

screen = pygame.display.set_mode((800,600))

game_over = False

while not game_over:
    # Handle user-events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True

    # Re-draw the screen
    pygame.draw.rect(screen, (255,0,0), (400,300,50,50))
    pygame.display.flip()

pygame.quit()
sys.exit()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.