用Python画一棵带音乐的雪夜圣诞树
Python 音乐 圣诞树 一棵
2023-06-13 09:11:26 时间
本文我们用 Python 来画一棵带音乐效果的雪夜圣诞树,基本思路如下:
- 用 Python 画一棵圣诞树作为背景图
- 在圣诞树背景图中添加雪落效果及音乐
下面来看一下具体实现。
首先,我们来画一棵圣诞树,主要用到的 Python 库为 turtle,主要代码实现如下:
n = 80.0
turtle.setup(700, 700, 0, 0)
turtle.speed("fastest")
turtle.screensize(bg='black')
turtle.left(90)
turtle.forward(3 * n)
turtle.color("orange", "yellow")
turtle.begin_fill()
turtle.left(126)
for i in range(5):
turtle.forward(n / 5)
turtle.right(144)
turtle.forward(n / 5)
turtle.left(72)
turtle.end_fill()
turtle.right(126)
turtle.color("dark green")
turtle.backward(n * 4.8)
def tree(d, s):
if d <= 0: return
turtle.forward(s)
tree(d - 1, s * .8)
turtle.right(120)
tree(d - 3, s * .5)
turtle.right(120)
tree(d - 3, s * .5)
turtle.right(120)
turtle.backward(s)
tree(15, n)
turtle.backward(n / 2)
for i in range(200):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
turtle.up()
turtle.forward(b)
turtle.left(90)
turtle.forward(a)
turtle.down()
if random.randint(0, 1) == 0:
turtle.color('tomato')
else:
turtle.color('wheat')
turtle.circle(2)
turtle.up()
turtle.backward(a)
turtle.right(90)
turtle.backward(b)
time.sleep(60)
看一下效果:
接着将圣诞树作为背景图添加雪落效果及音乐,主要用到的 Python 库为 pygame,主要代码实现如下:
# 初始化 pygame
pygame.init()
#设置屏幕宽高,根据背景图调整
bg_img = "bg.png"
bg_size = (609, 601)
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("雪夜圣诞树")
bg = pygame.image.load(bg_img)
# 雪花列表
snow_list = []
for i in range(150):
x_site = random.randrange(0, bg_size[0]) # 雪花圆心位置
y_site = random.randrange(0, bg_size[1]) # 雪花圆心位置
X_shift = random.randint(-1, 1) # x 轴偏移量
radius = random.randint(4, 6) # 半径和 y 周下降量
snow_list.append([x_site, y_site, X_shift, radius])
# 创建时钟对象
clock = pygame.time.Clock()
# 添加音乐
track = pygame.mixer.music.load('my.mp3') # 加载音乐文件
pygame.mixer.music.play() # 播放音乐流
pygame.mixer.music.fadeout(600000) # 设置音乐结束时间
done = False
while not done:
# 消息事件循环,判断退出
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.blit(bg, (0, 0))
# 雪花列表循环
for i in range(len(snow_list)):
# 绘制雪花,颜色、位置、大小
pygame.draw.circle(screen, (255, 255, 255), snow_list[i][:2], snow_list[i][3] - 3)
# 移动雪花位置(下一次循环起效)
snow_list[i][0] += snow_list[i][2]
snow_list[i][1] += snow_list[i][3]
# 如果雪花落出屏幕,重设位置
if snow_list[i][1] > bg_size[1]:
snow_list[i][1] = random.randrange(-50, -10)
snow_list[i][0] = random.randrange(0, bg_size[0])
# 刷新屏幕
pygame.display.flip()
clock.tick(30)
# 退出
pygame.quit()
看一下最终效果:
这里就不放视频了,大家如果想听一下音乐效果可以自己获取源码执行一下。
相关文章
- python海龟作图红绿灯_海龟作图—用Python绘图
- Python元组_python输出元组
- Anaconda:初学Python、入门机器学习的首选
- 【说站】python中pyquery的CSS选择器用法
- 如何用python画心形_用python制作音乐
- python小实例-生成不重复的、x个中文姓名
- Python使用免费天气API,获取全球任意地区的天气情况
- Python抛出异常_python抛出异常的作用
- python中copy.deepcopy_Python eval
- python绘制条形柱状图_Python柱状图
- 人生苦短,我用Python-手把手教你如何使用python写串口调试助手
- python请求缓存库-让你多次请求不在被封IP
- 【测试开发】python系列教程:Python 推导式
- Python生成条形码
- python-Python与MongoDB数据库-使用Python执行MongoDB查询(二)
- Python使用platform库获取系统信息:操作系统信息、硬件信息、python环境信息
- python:面向对象高级编程详解编程语言
- 编写Python开发文档的重要性
- 使用 Dask 在 Python 中进行并行计算
- Python脚本实现Linux系统管理及自动化部署(python写linux)
- Linux系统下安装Python模块指南(linux安装python模块)