Why is nothing drawn in PyGame at all?

Posted on

Solving problem is about exposing yourself to as many situations as possible like Why is nothing drawn in PyGame at all? and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about Why is nothing drawn in PyGame at all?, which can be followed any time. Take easy to follow this discuss.

Why is nothing drawn in PyGame at all?

i have started a new project in python using pygame and for the background i want the bottom half filled with gray and the top black. i have used rect drawing in projects before but for some reason it seems to be broken? i don’t know what i am doing wrong. the weirdest thing is that the result is different every time i run the program. sometimes there is only a black screen and sometimes a gray rectangle covers part of the screen, but never half of the screen.

import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAY=pygame.display.set_mode((800,800))
pygame.display.set_caption("thing")
pygame.draw.rect(DISPLAY, (200,200,200), pygame.Rect(0,400,800,400))
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
Asked By: MinerBat

||

Answer #1:

You need to update the display.
You are actually drawing on a Surface object. If you draw on the Surface associated to the PyGame display, this is not immediately visible in the display. The changes become visibel, when the display is updated with either pygame.display.update() or pygame.display.flip().

See pygame.display.flip():

This will update the contents of the entire display.

While pygame.display.flip() will update the contents of the entire display, pygame.display.update() allows updating only a portion of the screen to updated, instead of the entire area. pygame.display.update() is an optimized version of pygame.display.flip() for software displays, but doesn’t work for hardware accelerated displays.

The typical PyGame application loop has to:

import pygame
from pygame.locals import *
pygame.init()
DISPLAY = pygame.display.set_mode((800,800))
pygame.display.set_caption("thing")
clock = pygame.time.Clock()
run = True
while run:
    clock.tick(60)
    # handle events
    for event in pygame.event.get():
        if event.type == QUIT:
            run = False
    # clear display
    DISPLAY.fill(0)
    # draw scene
    pygame.draw.rect(DISPLAY, (200,200,200), pygame.Rect(0,400,800,400))
    # update display
    pygame.display.flip()
pygame.quit()
exit()

repl.it/@Rabbid76/PyGame-MinimalApplicationLoop See also Event and application loop

Answered By: Rabbid76
The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.