前言
本文章向大家介绍用 Python 实现雪花飘落效果,运行以下代码,你将会看到一个美丽的雪花效果。你可以根据自己的需求,调整代码中的参数值以及其他细节。
第一种
普通雪花代码:
import turtleimport randomdef draw_snowflake(length, depth):if depth == 0:turtle.forward(length)returnlength /= 3.0draw_snowflake(length, depth-1)turtle.left(60)draw_snowflake(length, depth-1)turtle.right(120)draw_snowflake(length, depth-1)turtle.left(60)draw_snowflake(length, depth-1)turtle.speed(0)turtle.penup()turtle.goto(-200, 200)turtle.pendown()for i in range(3):draw_snowflake(400, 4)turtle.right(120)turtle.hideturtle()turtle.done()
第二种
随机下落的雪花:
import pygame, random# 初始化 Pygamepygame.init()# 创建屏幕screen = pygame.display.set_mode((800, 600))pygame.display.set_caption("Python 雪景")# 定义颜色WHITE = (255, 255, 255)# 创建雪花列表snow_list = []for i in range(300):x = random.randrange(0, 800)y = random.randrange(0, 600)snow_list.append([x, y])# 创建时钟对象clock = pygame.time.Clock()# 循环标志done = False# 游戏循环while not done:# 处理事件for event in pygame.event.get():if event.type == pygame.QUIT:done = True# 填充屏幕为白色screen.fill(WHITE)# 循环雪花列表for i in range(len(snow_list)):# 绘制雪花pygame.draw.circle(screen, WHITE, snow_list[i], 2)# 移动雪花snow_list[i][1] += 1# 雪花超出屏幕,重置位置if snow_list[i][1] > 600:y = random.randrange(-50, -10)snow_list[i][1] = yx = random.randrange(0, 800)snow_list[i][0] = x# 更新屏幕pygame.display.flip()# 控制帧率clock.tick(60)# 退出 Pygamepygame.quit()
第三种
随机颜色代码:
import turtleimport randomcolors = ["blue", "purple", "cyan", "white", "yellow", "orange"]turtle.speed(0)turtle.bgcolor("black")for i in range(10):color = random.choice(colors)turtle.color(color)turtle.pensize(i / 2 + 1)turtle.forward(100)turtle.right(120)turtle.done()
使用了turtle模块和random模块,会在黑色背景上生成随机颜色的雪花。你可以根据需要调整colors列表中的颜色,来生成你想要的效果。
第四种
包含三种大小的Python雪花代码:
import turtleimport random# Set up the screenwn = turtle.Screen()wn.bgcolor("black")wn.title("Snow")# Create a variable for the number of snowflakesnum_snowflakes = 100# Create a list of colorscolors = ["white", "lightgray", "gray"]# Create a function to draw snowflakesdef draw_snowflake(size):# Draw a hexagonfor i in range(6):turtle.forward(size)turtle.right(60)turtle.forward(size)turtle.right(120)# Draw a dot in the centerturtle.dot(size // 2)# Create a loop to draw the snowflakesfor i in range(num_snowflakes):# Move to a random position on the screenx = random.randint(-300, 300)y = random.randint(-300, 300)turtle.penup()turtle.goto(x, y)turtle.pendown()# Draw a snowflake with a random size and colorsize = random.randint(5, 25)color = random.choice(colors)turtle.color(color)draw_snowflake(size)# Hide the turtleturtle.hideturtle()# Keep the screen open until it is closed manuallyturtle.done()
使用了turtle模块来绘制雪花。首先设置画布的背景色和标题。接着定义了一个draw_snowflake函数,用于绘制雪花。在主循环中,使用random模块来生成随机位置、大小和颜色的雪花,最后隐藏了画笔,使得只显示雪花。
第五种
用Python的turtle库来画雪花形状:
import turtle# 设置画布和画笔wn = turtle.Screen()wn.bgcolor("black")wn.title("Snowflake")pen = turtle.Turtle()pen.speed(0)pen.color("white")# 画雪花形状def snowflake(size):if size <= 10:pen.forward(size)returnelse:snowflake(size/3)pen.left(60)snowflake(size/3)pen.right(120)snowflake(size/3)pen.left(60)snowflake(size/3)# 画三个不同大小的雪花pen.penup()pen.goto(-200, 0)pen.pendown()snowflake(300)pen.penup()pen.goto(0, 0)pen.pendown()snowflake(200)pen.penup()pen.goto(200, 0)pen.pendown()snowflake(100)# 关闭画布wn.exitonclick()
这个代码将会在黑色背景上绘制白色雪花。使用递归函数来生成雪花的形状,大小为参数“size”。