【问题标题】:How can I return a new snowflakes?我怎样才能退回新的雪花?
【发布时间】: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


    【解决方案1】:

    您的脚本中存在一些问题。

    但导致此问题的原因是将雪花颜色元组放入列表中。

    雪花是[ ( 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)
    

    因为这是主要问题。

    在将雪花沿屏幕向下移动时(它们是侧向移动,而不是向下移动),您也在修改雪花的坐标(等),最好将这部分注释掉,直到其余部分正常工作为止。

    【讨论】:

      猜你喜欢
      • 2020-02-27
      • 2017-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      相关资源
      最近更新 更多