【发布时间】:2020-08-07 02:16:48
【问题描述】:
我一直在努力完成这个工作车间,recolour_snowflake 函数应该返回一个新的雪花,但我的代码返回四个不同的东西。我一直在尝试修复它,但它仍然给了我四种不同的东西。而且,我不明白为什么它总是从侧面而不是顶部开始。
这是我的代码:
def recolour_snowflake(snowflake):
color = [random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255)]
return [color, snowflake[1]]
我试着把我的代码放到这样的地方
def recolour_snowflake(snowflake):
color = [random.randrange(0, 255)]
return [color, snowflake[1]]
我得到的结果是
Traceback (most recent call last):
File "D:/Uni/Sem/pyfiles/animating_snow.py", line 79, in <module>
pygame.draw.circle(screen, snow_list[i][0], snow_list[i][1], 10)
TypeError: invalid color argument
我一直很困惑。我不知道该怎么办了。
这是我拥有的动画的完整代码:
import pygame
import random
# Initialize the game engine
pygame.init()
BLACK = [0, 0, 0]
WHITE = [255, 255, 255]
GREEN = [0, 255, 0]
RED = [255, 0, 0]
colourList = [BLACK, WHITE, GREEN, RED]
colour = random.choice(colourList)
# Set the height and width of the screen
SIZE = [400, 400]
screen = pygame.display.set_mode(SIZE)
pygame.display.set_caption("Snow Animation")
clock = pygame.time.Clock()
# Create an empty list
snow_list = []
# Loop 100 times and add a snow flake in a random x,y position
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):
color = [random.randrange(0, 255)]
return [color, snowflake[1]]
def keep_snowflake(snowflake):
if snowflake == random.randint(1, 30):
return False
else:
return True
count = 0
# Loop until the user clicks the close button.
done = False
while not done:
snow_list = list(filter(keep_snowflake, snow_list))
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT:
# Flag that we are done so we exit this loop
done = True
# change the color of snow flakes
if count == 0:
snow_list = list(map(recolour_snowflake, snow_list))
count += 1
snow_list = list(map(recolour_snowflake, snow_list))
# Set the screen background
screen.fill((100, 100, 100))
# Process each snow flake in the list
for i in range(len(snow_list)):
# Draw the snow flake
pygame.draw.circle(screen, snow_list[i][0], snow_list[i][1], 10)
# Move the snow flake down one pixel
snow_list[i][1][0] += 1
# If the snow flake has moved off the bottom of the screen
if snow_list[i][1][0] > 400:
# Give it a new x position
x = random.randrange(0, 400)
snow_list[i][0][1] = x
# Reset it just above the top
y = random.randrange(-50, -10)
snow_list[i][1][1] = y
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
clock.tick(20)
# If you forget this line, the program will 'hang' on exit.
pygame.quit()
【问题讨论】:
标签: python-3.x function animation pygame