如何在python中更改动画对象的颜色 [英] How to change color of animated object in python

查看:78
本文介绍了如何在python中更改动画对象的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将落在不同坐标上的雪的列表,默认颜色是白色.我需要帮助创建一个函数来改变 while 循环的每第 5 次迭代的雪"的颜色.然后将使用地图使用此函数

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)

推荐答案

雪列表必须包含颜色和位置的元组.初始颜色为WHITE:

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]))

在循环 5 次迭代后随机改变每个雪花的颜色:

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

注意,enumerate 返回包含索引和元素的元组.

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)

这篇关于如何在python中更改动画对象的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆