limegradient
limegradient

Reputation: 66

pygame window showing nothing on run

I am trying to make a game with pygame, but whenever I run my game, nothing shows up until I close the window, then stuff appears for a second. I don't know what is going on but I think is has to do with my display.update. Here is my code:

import pygame
import colorama
import math
import random #All needed libraries
import time
import os

from colorama import init, Fore, Back, Style #All needed extras
from pygame.locals import (
  K_UP,
  K_DOWN,
  K_LEFT,
  K_RIGHT,
  K_ESCAPE,
  KEYDOWN,
  QUIT,
)

init()
pygame.init()

SCREEN_WIDTH = 400
SCREEN_HEIGHT = 400

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

running = True

while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
        elif event.type == QUIT:
            running = False

screen.fill((255, 255, 255))

surf = pygame.Surface((50, 50))

surf.fill((0, 0, 0))
rect = surf.get_rect()

screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))

rect.update()
pygame.display.update()

why isn't it working?

Upvotes: 1

Views: 142

Answers (1)

Rabbid76
Rabbid76

Reputation: 210968

It is a matter of Indentation. The scene must be drawn in the application loop instead of after the application loop.
Furthermore pygame.Rect.update isn't doing what you'd expect. It is used to change the rectangle. You don't need it at all.

while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
        elif event.type == QUIT:
            running = False

# INDENTATION
#-->|

    screen.fill((255, 255, 255))

    surf = pygame.Surface((50, 50))

    surf.fill((0, 0, 0))
    rect = surf.get_rect()

    screen.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))

    #rect.update()                <--- DELETE
    pygame.display.update()

Upvotes: 1

Related Questions