Reputation: 35
I want to fill my window with a uniform color that slowly changes in each frame.
import pygame
import random
pygame.init()
screen = pygame.display.set_mode([400,400])
color = random.randint(0,255)
screen.fill((color))
r_color=10
r_color_change=5
animating=True
while animating:
for event in pygame.event.get():
if event.type == pygame.QUIT:
animating = False
r_color += r_color_change
if r_color > 255 or r_color < 0:
r_color_change *= -1
pygame.display.flip()
Upvotes: 1
Views: 373
Reputation: 210948
A color consists of 3 color channels, red, green and blue. (see RGB color model). You need to continuously fill
the entire screen with the new color in the application loop:
animating=True
while animating:
# [...]
screen.fill((r_color, 0, 0))
Additionally you should clam the r_color
value to the range [0, 255]:
screen.fill((r_color, 0, 0))
And control the frames per second with pygame.time.Clock.tick
:
clock = pygame.time.Clock()
animating=True
while animating:
clock.tick(60)
# [...]
Complete example:
import pygame
import random
pygame.init()
screen = pygame.display.set_mode([400,400])
clock = pygame.time.Clock()
r_color = random.randint(0,255)
r_color_change=5
animating=True
while animating:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
animating = False
r_color += r_color_change
if r_color > 255 or r_color < 0:
r_color_change *= -1
r_color = max(0, min(255, r_color))
screen.fill((r_color, 0, 0))
pygame.display.flip()
Upvotes: 3