oceandye
oceandye

Reputation: 319

Pygame drawing not showing in Pygame window

So I was testing out pygame and I wanted to draw a simple rectangle. There are no error messages when I run the code but the rectangle doesn't show up in the window. What I see is a blank white Pygame window pop up. Does anyone know why? Currently using Python3 and Pygame 1.9.4 on my mac. Here is my code,

import pygame
import pygame.font
pygame.init()

# Colours
BLACK   = (  0,  0,  0)
WHITE   = (255,255,255)
GREEN   = (  0,255,  0)
RED     = (255,  0,  0)
BLUE    = (  0,  0,255)

# Dimensions of screen
size = (400,500)
WIDTH = 500
HEIGHT = 400
screen = pygame.display.set_mode(size)

# Loop Switch
done = False

# Screen Update Speed (FPS)
clock = pygame.time.Clock()

# ------- Main Program Loop -------
while not done:
    # --- Main Event Loop ---
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5)


    screen.fill(GREEN)
    pygame.display.flip()

    #Setting FPS
    clock.tick(60)

#Shutdown
pygame.quit()

Upvotes: 0

Views: 5193

Answers (9)

HAMED AMIRI
HAMED AMIRI

Reputation: 1

You used the incorrect code for drawing the rectangle. Here is the correct code for drawing a rectangle:

pygame.draw.rect(your surface name, color, (x, y, length, width))

Upvotes: 0

user18970005
user18970005

Reputation:

you should add pygame.display.update() in the while not done loop. pygame.display.update updates the screen. You have this problem because you drew all of the drawings but did not update the screen.

Upvotes: 0

explorer
explorer

Reputation: 31

Each of the previously given answers fail to properly elaborate why this issue occurs. It is not about the order of drawing, filling operations; it is about your timing on calling the pygame.display.flip function, or shortly: updating the screen. Your code draws a rectangle, fills the screen with green, and then updates the screen. What it should have done instead is draw the rectangle, update the screen and then fill the screen with green. That way the screen is updated after you draw the rectangle before the screen is filled with green, therefore you can see it:

import pygame
import pygame.font
pygame.init()

# Colours
BLACK   = (  0,  0,  0)
WHITE   = (255,255,255)
GREEN   = (  0,255,  0)
RED     = (255,  0,  0)
BLUE    = (  0,  0,255)

# Dimensions of screen
size = (400,500)
WIDTH = 500
HEIGHT = 400
screen = pygame.display.set_mode(size)

# Loop Switch
done = False

# Screen Update Speed (FPS)
clock = pygame.time.Clock()

# ------- Main Program Loop -------
while not done:
    # --- Main Event Loop ---
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    #In each case you draw the rectangle and then fill the screen with green

    pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5)
    pygame.display.flip()

    screen.fill(GREEN)
    

    #Setting FPS
    clock.tick(60)

#Shutdown
pygame.quit()

In a nutshell, you should update after you draw the rectangle.

Upvotes: 0

Karan Mishra
Karan Mishra

Reputation: 26

The error is obvious as first you are drawing a shape then covering it with color . Your code is right but need some rearrangement.

screen.fill("your color") # First you should fill the screen with color

pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5) # Then u should draw any shape

Upvotes: 0

Joe The Bro
Joe The Bro

Reputation: 105

You do not want to fill the screen with green every 60 ticks

To fix this, simply put screen.fill(GREEN) outside of the Main loop.

The only time you want screen.fill inside your while loop, is when your adding movement into your program.

I strongly suggest you make a function called draw and draw things outside of your while loop.

Upvotes: 4

user11305446
user11305446

Reputation:

The problem is that you draw the shape and after that you 'fill' (cover) it with green, so try something like that:

    screen.fill(GREEN) #first fill the screen with green
    pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5) #and after that draw the rectangle

Upvotes: 0

Eero Ristolainen
Eero Ristolainen

Reputation: 11

You should first cover the screen with green and then draw your shape because otherwise it will get covered.

Upvotes: -1

user11217727
user11217727

Reputation:

I have found the problem: first of, Glitchd is correct, but you forget to update:

import pygame
import pygame.font
pygame.init()

# Colours
BLACK   = (  0,  0,  0)
WHITE   = (255,255,255)
GREEN   = (  0,255,  0)
RED     = (255,  0,  0)
BLUE    = (  0,  0,255)

# Dimensions of screen
size = (400,500)
WIDTH = 500
HEIGHT = 400
screen = pygame.display.set_mode(size)

# Loop Switch
done = False

# Screen Update Speed (FPS)
clock = pygame.time.Clock()

# ------- Main Program Loop -------
while not done:
    # --- Main Event Loop ---
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    screen.fill(GREEN)
    pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5)


    pygame.display.flip()
    pygame.display.update()

    #Setting FPS
    clock.tick(60)

#Shutdown
pygame.quit()

Upvotes: 2

Glitchd
Glitchd

Reputation: 361

U are drawing a shape and then covering it up with green, swap

pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5)
screen.fill(GREEN)

Those 2 around

Upvotes: 1

Related Questions