首页 > 解决方案 > 如何在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)

标签: pythonpygame

解决方案


雪名单必须包含颜色和位置的元组。初始颜色为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)

推荐阅读