I Tried To Draw A Rectangle In Pygame But The Colour Is Flickering...why?
So, I'm trying Pygame again (still a beginner), I tried to draw a rectangle, but the colour just flickers. (turquoise on orange surface) Why does this happen? Here's the snippet of
Solution 1:
The pygame.display.update
(or alternatively pygame.display.flip
) function should be called only once per frame (iteration of the while loop) at the end of the drawing section of the code.
Just remove the first pygame.display.update()
call and the program will work correctly.
Some notes about the code: Define your constants (the colors) and create the screen outside of the while loop (that's unrelated to the flickering but it makes no sense to do this in the while loop). Also, better don't use star imports (only from pygame.locals import *
is okay if it's the only star import). And use a clock to limit the frame rate.
import sys
import pygame
from pygame.localsimport *
pygame.init()
# Use uppercase for constants and lowercase for variables (see PEP 8).
SCREENWIDTH = 900
SCREENHEIGHT = 600
SCREENSIZE = [SCREENWIDTH, SCREENHEIGHT]
screen = pygame.display.set_mode(SCREENSIZE)
clock = pygame.time.Clock() # A clock object to limit the frame rate.
BG_COL = [255, 123, 67]
S1_COL = (0, 255, 188)
whileTrue:
for events in pygame.event.get():
if events.type == QUIT:
pygame.quit()
sys.exit()
screen.fill(BG_COL)
pygame.draw.rect(screen, S1_COL, (50, 25, 550, 565), 1)
pygame.display.update()
clock.tick(60) # Limits the frame rate to 60 FPS.
Post a Comment for "I Tried To Draw A Rectangle In Pygame But The Colour Is Flickering...why?"