当前位置:   article > 正文

5种Python雪花飘落代码(建议收藏)_python雪花满天飞代码

python雪花满天飞代码

前言

本文章向大家介绍用 Python 实现雪花飘落效果,运行以下代码,你将会看到一个美丽的雪花效果。你可以根据自己的需求,调整代码中的参数值以及其他细节。


第一种

普通雪花代码:

  1. import turtle
  2. import random
  3. def draw_snowflake(length, depth):
  4. if depth == 0:
  5. turtle.forward(length)
  6. return
  7. length /= 3.0
  8. draw_snowflake(length, depth-1)
  9. turtle.left(60)
  10. draw_snowflake(length, depth-1)
  11. turtle.right(120)
  12. draw_snowflake(length, depth-1)
  13. turtle.left(60)
  14. draw_snowflake(length, depth-1)
  15. turtle.speed(0)
  16. turtle.penup()
  17. turtle.goto(-200, 200)
  18. turtle.pendown()
  19. for i in range(3):
  20. draw_snowflake(400, 4)
  21. turtle.right(120)
  22. turtle.hideturtle()
  23. turtle.done()

第二种

随机下落的雪花:

  1. import pygame, random
  2. # 初始化 Pygame
  3. pygame.init()
  4. # 创建屏幕
  5. screen = pygame.display.set_mode((800, 600))
  6. pygame.display.set_caption("Python 雪景")
  7. # 定义颜色
  8. WHITE = (255, 255, 255)
  9. # 创建雪花列表
  10. snow_list = []
  11. for i in range(300):
  12. x = random.randrange(0, 800)
  13. y = random.randrange(0, 600)
  14. snow_list.append([x, y])
  15. # 创建时钟对象
  16. clock = pygame.time.Clock()
  17. # 循环标志
  18. done = False
  19. # 游戏循环
  20. while not done:
  21. # 处理事件
  22. for event in pygame.event.get():
  23. if event.type == pygame.QUIT:
  24. done = True
  25. # 填充屏幕为白色
  26. screen.fill(WHITE)
  27. # 循环雪花列表
  28. for i in range(len(snow_list)):
  29. # 绘制雪花
  30. pygame.draw.circle(screen, WHITE, snow_list[i], 2)
  31. # 移动雪花
  32. snow_list[i][1] += 1
  33. # 雪花超出屏幕,重置位置
  34. if snow_list[i][1] > 600:
  35. y = random.randrange(-50, -10)
  36. snow_list[i][1] = y
  37. x = random.randrange(0, 800)
  38. snow_list[i][0] = x
  39. # 更新屏幕
  40. pygame.display.flip()
  41. # 控制帧率
  42. clock.tick(60)
  43. # 退出 Pygame
  44. pygame.quit()

第三种

随机颜色代码:

  1. import turtle
  2. import random
  3. colors = ["blue", "purple", "cyan", "white", "yellow", "orange"]
  4. turtle.speed(0)
  5. turtle.bgcolor("black")
  6. for i in range(10):
  7. color = random.choice(colors)
  8. turtle.color(color)
  9. turtle.pensize(i / 2 + 1)
  10. turtle.forward(100)
  11. turtle.right(120)
  12. turtle.done()

使用了turtle模块和random模块,会在黑色背景上生成随机颜色的雪花。你可以根据需要调整colors列表中的颜色,来生成你想要的效果。


第四种

包含三种大小的Python雪花代码:

  1. import turtle
  2. import random
  3. # Set up the screen
  4. wn = turtle.Screen()
  5. wn.bgcolor("black")
  6. wn.title("Snow")
  7. # Create a variable for the number of snowflakes
  8. num_snowflakes = 100
  9. # Create a list of colors
  10. colors = ["white", "lightgray", "gray"]
  11. # Create a function to draw snowflakes
  12. def draw_snowflake(size):
  13. # Draw a hexagon
  14. for i in range(6):
  15. turtle.forward(size)
  16. turtle.right(60)
  17. turtle.forward(size)
  18. turtle.right(120)
  19. # Draw a dot in the center
  20. turtle.dot(size // 2)
  21. # Create a loop to draw the snowflakes
  22. for i in range(num_snowflakes):
  23. # Move to a random position on the screen
  24. x = random.randint(-300, 300)
  25. y = random.randint(-300, 300)
  26. turtle.penup()
  27. turtle.goto(x, y)
  28. turtle.pendown()
  29. # Draw a snowflake with a random size and color
  30. size = random.randint(5, 25)
  31. color = random.choice(colors)
  32. turtle.color(color)
  33. draw_snowflake(size)
  34. # Hide the turtle
  35. turtle.hideturtle()
  36. # Keep the screen open until it is closed manually
  37. turtle.done()

使用了turtle模块来绘制雪花。首先设置画布的背景色和标题。接着定义了一个draw_snowflake函数,用于绘制雪花。在主循环中,使用random模块来生成随机位置、大小和颜色的雪花,最后隐藏了画笔,使得只显示雪花。


第五种

用Python的turtle库来画雪花形状:

  1. import turtle
  2. # 设置画布和画笔
  3. wn = turtle.Screen()
  4. wn.bgcolor("black")
  5. wn.title("Snowflake")
  6. pen = turtle.Turtle()
  7. pen.speed(0)
  8. pen.color("white")
  9. # 画雪花形状
  10. def snowflake(size):
  11. if size <= 10:
  12. pen.forward(size)
  13. return
  14. else:
  15. snowflake(size/3)
  16. pen.left(60)
  17. snowflake(size/3)
  18. pen.right(120)
  19. snowflake(size/3)
  20. pen.left(60)
  21. snowflake(size/3)
  22. # 画三个不同大小的雪花
  23. pen.penup()
  24. pen.goto(-200, 0)
  25. pen.pendown()
  26. snowflake(300)
  27. pen.penup()
  28. pen.goto(0, 0)
  29. pen.pendown()
  30. snowflake(200)
  31. pen.penup()
  32. pen.goto(200, 0)
  33. pen.pendown()
  34. snowflake(100)
  35. # 关闭画布
  36. wn.exitonclick()

这个代码将会在黑色背景上绘制白色雪花。使用递归函数来生成雪花的形状,大小为参数“size”。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/36916
推荐阅读
相关标签
  

闽ICP备14008679号