Reputation: 29
I have a list of snows that will fall on different coordinates, the default color is white. I need help with creating a function to change color of 'snows' every 5th iteration of while loop. This function will then be used using a map
Tried creating a function for each snow to change colors, but it doesn't seem to be working.
WHITE = [255, 255, 255]
SILVER = [192, 192, 192]
GOLD = (255,223,0)
RED = [255, 0, 0]
colour_list = [WHITE, SILVER, GOLD, RED]
colour = random.choice(colour_list)
snow_list = []
#assigning random coordinates for each snowflake
for i in range(100):
x = random.randrange(0, 600)
y = random.randrange(0, 600)
snow_list.append([x, y])
# colour changing function
def recolour_snowflake(snowflake):
snowflake.append(colour)
pygame.draw.circle(screen, colour, snowflake, 2)
return snowflake
done = False
while not done:
#change colour every 7th iteration
if count == 7:
snow_list = list(map(recolour_snowflake, snow_list))
count=0
count += 1
# display each snow
for i in range(len(snow_list)):
# Draw the snow
pygame.draw.circle(screen, WHITE, snow_list[i], 3)
Upvotes: 2
Views: 3156
Reputation: 210880
The snow list has to contain tuples of color and position. The initial color is WHITE
:
snow_list = []
for i in range(100):
x = random.randrange(0, 400)
y = random.randrange(0, 400)
snow_list.append((WHITE, [x, y]))
Randomly change the color of each snowflake after 5 iterations of the loop:
def recolour_snowflake(snowflake):
colour = random.choice(colour_list)
return (colour, snowflake[1])
count = 0
done = False
while not done:
# [...]
if count == 5:
snow_list = list(map(recolour_snowflake, snow_list))
count=0
count += 1
Note, enumerate
returns a tuple containing the index and the element.
Use the color of the tuple list to draw the snowflakes:
while not done:
# [...]
for snow in snow_list:
pygame.draw.circle(screen, snow[0], snow[1], 2)
See the full example with falling and recoloring snow flakes:
import pygame
import random
pygame.init()
size = (400,400)
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
BLACK = [0, 0, 0]
WHITE = [255, 255, 255]
SILVER = [192, 192, 192]
GOLD = [255, 223, 0]
RED = [255, 0, 0]
colour_list = [WHITE, SILVER, GOLD, RED]
colour = random.choice(colour_list)
snow_list = []
for i in range(100):
x = random.randrange(0, 400)
y = random.randrange(0, 400)
snow_list.append((WHITE, [x, y]))
def recolour_snowflake(snowflake):
colour = random.choice(colour_list)
return (colour, snowflake[1])
def animate_snowflake(snowflake):
x, y = snowflake[1]
y += random.randrange(1,3)
if y > 400:
x, y = (random.randrange(0, 400), random.randrange(-50, -10))
return (snowflake[0], [x, y])
count = 0
done = False
while not done:
# handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# change the color of the snow flakes
if count == 5:
snow_list = list(map(recolour_snowflake, snow_list))
count = 0
count += 1
# Process each snow flake in the list
snow_list = list(map(animate_snowflake, snow_list))
# Set the screen background
screen.fill(BLACK)
# draw the snow flakes
for snow in snow_list:
pygame.draw.circle(screen, *snow, 2)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
clock.tick(20)
Upvotes: 2