Skip to content

Commit 42715aa

Browse files
committed
m
1 parent 409ed3f commit 42715aa

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed

yeke/py-rain/__init__.py

Whitespace-only changes.

yeke/py-rain/code_rain.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import random, pygame
2+
3+
PANEL_width = 400
4+
PANEL_highly = 500
5+
FONT_PX = 15
6+
pygame.init()
7+
# 创建一个窗口
8+
winSur = pygame.display.set_mode((PANEL_width, PANEL_highly))
9+
font = pygame.font.SysFont('123.ttf', 22)
10+
bg_suface = pygame.Surface((PANEL_width, PANEL_highly), flags=pygame.SRCALPHA)
11+
pygame.Surface.convert(bg_suface)
12+
bg_suface.fill(pygame.Color(0, 0, 0, 28))
13+
winSur.fill((0, 0, 0))
14+
letter = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c',
15+
'v', 'b', 'n', 'm']
16+
texts = [
17+
font.render(str(letter[i]), True, (0, 255, 0)) for i in range(26)
18+
]
19+
# 按窗口的宽度来计算可以在画板上放几列坐标并生成一个列表
20+
column = int(PANEL_width / FONT_PX)
21+
drops = [0 for i in range(column)]
22+
while True:
23+
# 从队列中获取事件
24+
for event in pygame.event.get():
25+
if event.type == pygame.QUIT:
26+
exit()
27+
elif event.type == pygame.KEYDOWN:
28+
chang = pygame.key.get_pressed()
29+
if (chang[32]):
30+
exit()
31+
# 暂停给定的毫秒数
32+
pygame.time.delay(30)
33+
# 重新编辑图像
34+
winSur.blit(bg_suface, (0, 0))
35+
for i in range(len(drops)):
36+
text = random.choice(texts)
37+
# 重新编辑每个坐标点的图像
38+
winSur.blit(text, (i * FONT_PX, drops[i] * FONT_PX))
39+
drops[i] += 1
40+
if drops[i] * 10 > PANEL_highly or random.random() > 0.95:
41+
drops[i] = 0
42+
pygame.display.flip()

yeke/py-rain/num_rain.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import random, pygame
2+
3+
FONT_PX = 15
4+
pygame.init()
5+
winSur = pygame.display.set_mode((400, 500))
6+
font = pygame.font.SysFont('arial', 16)
7+
bg_suface = pygame.Surface((400, 500), flags=pygame.SRCALPHA)
8+
pygame.Surface.convert(bg_suface)
9+
bg_suface.fill(pygame.Color(0, 0, 0, 13))
10+
winSur.fill((0, 0, 0))
11+
# 数字
12+
texts = [font.render(str(i), True, (0, 255, 0)) for i in range(10)]
13+
colums = int(400 / FONT_PX)
14+
drops = [0 for i in range(colums)]
15+
while True:
16+
for event in pygame.event.get():
17+
if event.type == pygame.QUIT:
18+
exit()
19+
pygame.time.delay(33)
20+
winSur.blit(bg_suface, (0, 0))
21+
for i in range(len(drops)):
22+
text = random.choice(texts)
23+
winSur.blit(text, (i * FONT_PX, drops[i] * FONT_PX))
24+
drops[i] += 1
25+
if drops[i] * 10 > 400 or random.random() > 0.95:
26+
drops[i] = 0
27+
pygame.display.flip()

yeke/py-rain/pic.gif

8.97 KB
Loading

yeke/py-rain/pic_rain.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from tkinter import *
2+
import random, threading, time, os
3+
4+
# 初始雨滴纵坐标
5+
INIT_HEIGHT = 10
6+
# 雨滴创建
7+
def rainmake(canvas, imagefile):
8+
rainlist = []
9+
for i in range(5):
10+
# 根据图片,创建一排福字
11+
rainlist.append(canvas.create_image(100 + 80 * i, INIT_HEIGHT, anchor=NE, image=imagefile))
12+
return rainlist
13+
14+
# 雨滴下落
15+
def raindown(tk, canvas, imagefile, sec):
16+
# 线程间等待
17+
time.sleep(sec)
18+
rainlist = rainmake(canvas, imagefile)
19+
# 每个福字的纵坐标值
20+
height = [INIT_HEIGHT] * 10
21+
while True:
22+
# 每次移动前稍等一会
23+
time.sleep(0.2)
24+
# 5 个福字一起移动
25+
for i in range(5):
26+
# 如果福字到底了,则不继续移动
27+
if not height[i] == 0:
28+
# 设置下落步调
29+
rnd = random.randint(5, 50)
30+
canvas.move(rainlist[i], 0, rnd)
31+
height[i] = height[i] + rnd
32+
tk.update()
33+
for i,h in enumerate(height):
34+
if h > 400:
35+
# 当福字走到最下方,则删除
36+
canvas.delete(rainlist[i])
37+
tk.update()
38+
# 清空该福的 height
39+
height[i] = 0
40+
print(i,h,height)
41+
# 全到底,则跳出循环
42+
if height == [0] * 5:
43+
print('break:',threading.current_thread().name)
44+
break
45+
46+
def lookloop(tk, canvas, thread):
47+
aliveflg = False
48+
while True:
49+
# 5s 检测一次
50+
time.sleep(5)
51+
for th in thread:
52+
if th.is_alive():
53+
aliveflg = True
54+
else:
55+
aliveflg = False
56+
if aliveflg == False:
57+
break
58+
canvas.create_text(100 , 200, text='雨停了...', fill='red')
59+
canvas.pack()
60+
time.sleep(5)
61+
tk.destroy()
62+
63+
def main():
64+
# 创建窗口对象
65+
tk = Tk()
66+
tk.title('送福雨')
67+
canvas_style = {
68+
'bg':'white',
69+
'height':'500',
70+
'width':'410',
71+
'cursor':'circle'
72+
}
73+
# 创建画布
74+
canvas = Canvas(tk,canvas_style)
75+
canvas.pack()
76+
# 图片素材
77+
if not os.path.exists('pic.gif'):
78+
raise Exception('pic.gif file does not exists.')
79+
imagefile = PhotoImage(file = 'pic.gif')
80+
thread = []
81+
for i in range(100):
82+
thread.append(threading.Thread(target=raindown, args=(tk, canvas, imagefile, i)))
83+
for t in thread:
84+
t.start()
85+
# 新开一个线程监控运行中的线程
86+
threading.Thread(target=lookloop, args=(tk, canvas, thread)).start()
87+
# 进入消息循环
88+
tk.mainloop()
89+
90+
if __name__ == '__main__':
91+
main()

0 commit comments

Comments
 (0)