Reputation: 333
I want to draw a rectangle that changes from black to red without the user doing anything. The code below should fill the screen, draw one black rectangle then erase it and draw a red rectangle, then just keep looping. But I just get one red rectangle. What am I doing wrong?
import pygame
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
pygame.init()
# Set the width and height of the screen [width, height]
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
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(WHITE)
pygame.draw.rect(screen, BLACK, [300, 200, 100, 100],0)
screen.fill(WHITE)
pygame.draw.rect(screen, RED, [300, 200, 100, 100],0)
pygame.display.flip()
clock.tick(60)
pygame.quit()
Upvotes: 1
Views: 1807
Reputation: 142631
You can use variable with color in rect(...)
and ..
... use own event with pygame.time.set_timer()
to change color in this variable.
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
size = (700, 500)
# --- start ---
pygame.init()
screen = pygame.display.set_mode(size)
# start color
color = RED
# define own event type
CHANGE_COLOR = pygame.USEREVENT + 1
# create event every 250ms
pygame.time.set_timer(CHANGE_COLOR, 250) # 250ms
# --- mainloop ---
done = False
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# check if get event
if event.type == CHANGE_COLOR:
# change color
if color == RED:
color = BLACK
else:
color = RED
screen.fill(WHITE)
pygame.draw.rect(screen, color, [300, 200, 100, 100],0)
pygame.display.flip()
clock.tick(60)
pygame.quit()
... or use pygame.time.get_ticks()
to get current time and check if it is time to change color.
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
size = (700, 500)
# --- start ---
pygame.init()
screen = pygame.display.set_mode(size)
# start color
color = RED
# get current time
current_time = pygame.time.get_ticks()
# first change after 250 ms
change_color_time = current_time + 250
# --- mainloop ---
done = False
clock = pygame.time.Clock()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# get current time
current_time = pygame.time.get_ticks()
# check if it is time to change color
if current_time >= change_color_time:
# set new time to change color again
change_color_time = current_time + 250
# change color
if color == RED:
color = BLACK
else:
color = RED
screen.fill(WHITE)
pygame.draw.rect(screen, color, [300, 200, 100, 100],0)
pygame.display.flip()
clock.tick(60)
pygame.quit()
Upvotes: 1