前言
本文章向大家介绍用 Python 实现雪花飘落效果,运行以下代码,你将会看到一个美丽的雪花效果。你可以根据自己的需求,调整代码中的参数值以及其他细节。
第一种
普通雪花代码:
import turtle
import random
def draw_snowflake(length, depth):
if depth == 0:
turtle.forward(length)
return
length /= 3.0
draw_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
# 初始化 Pygame
pygame.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] = y
x = random.randrange(0, 800)
snow_list[i][0] = x
# 更新屏幕
pygame.display.flip()
# 控制帧率
clock.tick(60)
# 退出 Pygame
pygame.quit()
第三种
随机颜色代码:
import turtle
import random
colors = ["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 turtle
import random
# Set up the screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Snow")
# Create a variable for the number of snowflakes
num_snowflakes = 100
# Create a list of colors
colors = ["white", "lightgray", "gray"]
# Create a function to draw snowflakes
def draw_snowflake(size):
# Draw a hexagon
for i in range(6):
turtle.forward(size)
turtle.right(60)
turtle.forward(size)
turtle.right(120)
# Draw a dot in the center
turtle.dot(size // 2)
# Create a loop to draw the snowflakes
for i in range(num_snowflakes):
# Move to a random position on the screen
x = 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 color
size = random.randint(5, 25)
color = random.choice(colors)
turtle.color(color)
draw_snowflake(size)
# Hide the turtle
turtle.hideturtle()
# Keep the screen open until it is closed manually
turtle.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)
return
else:
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”。
文章出处登录后可见!
已经登录?立即刷新