python - 如何在python中更改动画对象的颜色
问题描述
我有一个将落在不同坐标上的雪的列表,默认颜色是白色。我需要帮助创建一个函数以在 while 循环的每 5 次迭代中更改“雪”的颜色。然后将使用地图使用此功能
尝试为每场雪创建一个函数来改变颜色,但它似乎不起作用。
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)
解决方案
雪名单必须包含颜色和位置的元组。初始颜色为WHITE
:
snow_list = []
for i in range(100):
x = random.randrange(0, 400)
y = random.randrange(0, 400)
snow_list.append((WHITE, [x, y]))
在循环 5 次迭代后随机改变每个雪花的颜色:
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
注意,enumerate
返回一个包含索引和元素的元组。
使用元组列表的颜色来绘制雪花:
while not done:
# [...]
for snow in snow_list:
pygame.draw.circle(screen, snow[0], snow[1], 2)
请参阅下落和重新着色雪花的完整示例:
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)
推荐阅读
- python - read_mrz 错误:在模式“i”中找不到读取指定文件的格式
- linux - 将大行拆分为较小的行并在换行符的开头插入特殊字符
- javascript - 通过 JavaScript 中的自定义函数对数组进行排序
- flutter - 如何修复列中的 ListView 并将 RefreshIndicator 添加到 ListView
- llvm - 如何从 MachineInstr 中检索指令的反汇编?
- tfs - 在 TFS 中自动生成 PBI 的最佳方法是什么?
- python - 从默认字典中提取重复值
- react-native - expo.Google.logInAsync 使我的应用程序崩溃
- javascript - 如何解决 UnhandledPromiseRejectionWarning: Unhandled Promise Rejection: TypeError: Only absolute URLs are supported?节点.js
- r - 如何根据其他变量的匹配名称使用 ifelse 创建多个变量?