我一直在努力完成本工作,而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()
答案 0 :(得分:0)
您的脚本中有一些问题。
但是导致此问题的一个原因是将雪花色元组放入列表中。
雪花是[ ( colour_red, colour_green, colour_blue), ( coord_x, coord_y ) ]
。
但是您的recolour_snowflake()
函数返回了[ [ ( colour_red, colour_green, colour_blue) ], ( coord_x, coord_y ) ]
。因此,当draw.circle()
使用它的颜色参数时-期望有一个颜色元组,您将为其提供一个单个元组的列表。
所以请先解决此问题:
def recolour_snowflake(snowflake):
position = snowflake[1]
# color = [random.randrange(0, 255)]
color = random.randrange(0, 255)
return (color, position)
这是主要问题。
当您将雪花向下移动到屏幕(并且它们横向移动而不是向下移动)时,您也在调整雪花的坐标(等),最好将这部分注释掉,直到其余部分工作为止。