I tried to draw a rectangle in Pygame, but the color is flashing … why?

So, I tried Pygame again (still a beginner), I tried to draw a rectangle, but the color just flickered. (Turquoise on the orange surface) Why does this happen?

This is the code snippet:

from pygame import *
from sys import *

while True:
init()

for events in event.get():
if events.type == QUIT:
quit()
exit ()

SCREENWIDTH = 900
SCREENHEIGHT = 600
SCREENSIZE = [SCREENWIDTH, SCREENHEIGHT]
SCREEN = display.set_mode(SCREENSIZE)
bg_col = [ 255, 123, 67]
s1_col = (0, 255, 188)
SCREEN.fill(bg_col)
display.update()
draw.rect(SCREEN, s1_col, (50, 25, 550, 565), 1) #problem area?
display.update()

Thank you all:)

pygame.display.update (or instead of pygame.display.flip) function should be called once every frame at the end of the drawing part of the code (Iteration of the while loop).

Just delete the first pygame.display.update() call and the program will work normally.

Some notes about the code Matters: Define constants (colors) and create the screen outside of the while loop (this has nothing to do with flickering, but it makes no sense to do this in the while loop). Also, it is best not to use star imports (only from pygame.locals import * Only if it is the only star import). Use the clock to limit the frame rate.

import sys

import pygame
from pygame.locals import *


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)

while True:
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.

So, I tried Pygame again (still a beginner), I tried to draw a rectangle, but the color just flickered. (Turquoise on the orange surface) Why does this happen?

This is the code snippet:

from pygame import *
from sys import *

while True:
init()

for events in event.get():
if events.type == QUIT:
quit()
exit ()

SCREENWIDTH = 900
SCREENHEIGHT = 600
SCREENSIZE = [SCREENWIDTH, SCREENHEIGHT]
SCREEN = display.set_mode(SCREENSIZE)
bg_col = [ 255, 123, 67]
s1_col = (0, 255, 188)
SCREEN.fill(bg_col)
display.update()
draw.rect(SCREEN, s1_col, (50, 25, 550, 565), 1) #problem area?
display.update()

Thank you all:)

pygame.display.update (or instead of pygame.display.flip) function should be called once per frame at the end of the drawing part of the code (iteration of the while loop).

Just delete the first pygame.display.update() call, and the program will work normally.

Some notes about the code: define a constant (color) and place it in the while loop Create the screen outside (this has nothing to do with flickering, but it makes no sense to do this in a while loop). Also, it is best not to use star imports (only from pygame.locals import *, if it is the only star import) . And use the clock to limit the frame rate.

import sys

import pygame
from pygame.locals import *


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)

while True:
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.

Leave a Comment

Your email address will not be published.