Skip to content

Commit 841db50

Browse files
committed
m
1 parent 440e2e5 commit 841db50

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed

yeke/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
+ [py-teacher](https://github.com/JustDoPython/python-examples/tree/master/yeke/py-teacher) :用 Python 为老师送上节日的祝福
1818
+ [py-minesweep](https://github.com/JustDoPython/python-examples/tree/master/yeke/py-minesweep) :用 Python 实现扫雷小游戏
1919
+ [py-ascii](https://github.com/JustDoPython/python-examples/tree/master/yeke/py-ascii) :Python 实现图片转字符画,静态图、GIF 都能转
20+
+ [py-ascii](https://github.com/JustDoPython/python-examples/tree/master/yeke/py-snow) :用 Python 实现带音乐的雪花飘落雪景图
2021

2122
---
2223

yeke/py-snow/__init__.py

Whitespace-only changes.

yeke/py-snow/bg.jpeg

39.1 KB
Loading

yeke/py-snow/my.mp3

4.31 MB
Binary file not shown.

yeke/py-snow/snow_sight.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import pygame
2+
import random
3+
4+
# 初始化 pygame
5+
pygame.init()
6+
# 设置屏幕宽高,根据背景图调整
7+
bg_img = "bg.jpeg"
8+
bg_size = (900, 500)
9+
screen = pygame.display.set_mode(bg_size)
10+
pygame.display.set_caption("雪景图")
11+
bg = pygame.image.load(bg_img)
12+
# 雪花列表
13+
snow_list = []
14+
for i in range(150):
15+
x_site = random.randrange(0, bg_size[0]) # 雪花圆心位置
16+
y_site = random.randrange(0, bg_size[1]) # 雪花圆心位置
17+
X_shift = random.randint(-1, 1) # x 轴偏移量
18+
radius = random.randint(4, 6) # 半径和 y 周下降量
19+
snow_list.append([x_site, y_site, X_shift, radius])
20+
# 创建时钟对象
21+
clock = pygame.time.Clock()
22+
# 添加音乐
23+
track = pygame.mixer.music.load('my.mp3') # 加载音乐文件
24+
pygame.mixer.music.play() # 播放音乐流
25+
pygame.mixer.music.fadeout(600000) # 设置音乐结束时间
26+
done = False
27+
while not done:
28+
# 消息事件循环,判断退出
29+
for event in pygame.event.get():
30+
if event.type == pygame.QUIT:
31+
done = True
32+
screen.blit(bg, (0, 0))
33+
# 雪花列表循环
34+
for i in range(len(snow_list)):
35+
# 绘制雪花,颜色、位置、大小
36+
pygame.draw.circle(screen, (255, 255, 255), snow_list[i][:2], snow_list[i][3] - 3)
37+
# 移动雪花位置(下一次循环起效)
38+
snow_list[i][0] += snow_list[i][2]
39+
snow_list[i][1] += snow_list[i][3]
40+
# 如果雪花落出屏幕,重设位置
41+
if snow_list[i][1] > bg_size[1]:
42+
snow_list[i][1] = random.randrange(-50, -10)
43+
snow_list[i][0] = random.randrange(0, bg_size[0])
44+
# 刷新屏幕
45+
pygame.display.flip()
46+
clock.tick(20)
47+
# 退出
48+
pygame.quit()

0 commit comments

Comments
 (0)