当前位置: 首页 > 图灵资讯 > 行业资讯> 用Python实现的代码雨,超赞!

用Python实现的代码雨,超赞!

来源:图灵python
时间: 2025-02-10 13:41:47

说到电影《黑客帝国》,我相信大多数人都看过或听说过。电影中有一个场景数字雨。如果你看过这部电影,你应该对这个经典场景印象深刻。我们使用这篇文章 Python 这种效果是以三种形式实现的:数字、字母和图片。

1. 数字

首先,为了实现数字雨,我们需要创建一个窗口来显示内容,创建和使用窗口 pygame 代码实现如下:库:

FONT_PX=15
pygame.init()
winSur=pygame.display.set_mode((500,600))
font=pygame.font.SysFont('fangsong',20)
bg_suface=pygame.Surface((500,600),flags=pygame.SRCALPHA)
pygame.Surface.convert(bg_suface)
bg_suface.fill(pygame.Color(0,0,0,13))
winSur.fill(0,0,0,0)
#数字
texts=[font.render(str(i),True,(0,255,0))foriinrange(10)]
colums=int(500/FONT_PX)
drops=[0forinrange(colums)]
whileTrue:
foreventinpygame.event.get():
ifevent.type==pygame.QUIT:
exit()
pygame.time.delay(33)
winSur.blit(bg_suface,(0,0))
foriinrange(len(drops)):
text=random.choice(texts)
winSur.blit(text,(i*FONT_PX,drops[i]*FONT_PX))
drops[i]+=1
ifdrops[i]*10>600orandom.random()>0.95:
drops[i]=0
pygame.display.flip()

实现效果如下:

20200513195342725.gif

2. 字母

然后,我们将实现字母雨。实现的方法基本上是用字母替换上述实现数字雨的数字。代码实现如下:

PANEL_width=400
PANEL_highly=500
FONT_PX=15
pygame.init()
#创建窗口
winSur=pygame.display.set_mode((PANEL_width,PANEL_highly))
font=pygame.font.SysFont('123.ttf',22)
bg_suface=pygame.Surface((PANEL_width,PANEL_highly),flags=pygame.SRCALPHA)
pygame.Surface.convert(bg_suface)
bg_suface.fill(pygame.Color(0,0,0,28)
winSur.fill(0,0,0))
letter=['q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c',
'v','b','n','m']
texts=[
font.render(str(letter[i]),True,(0,255,0))foriinrange(26)
]
#根据窗户的宽度,可以在画板上放置几列坐标并生成列表
column=int(PANEL_width/FONT_PX)
drops=[0forinrange(column)]
whileTrue:
#从队列中获取事件
foreventinpygame.event.get():
ifevent.type==pygame.QUIT:
exit()
elifevent.type==pygame.KEYDOWN:
chang=pygame.key.get_pressed()
if(chang[32]):
exit()
#暂停给定的毫秒数
pygame.time.delay(30)
#重新编辑图像
winSur.blit(bg_suface,(0,0))
foriinrange(len(drops)):
text=random.choice(texts)
#重新编辑每个坐标点的图像
winSur.blit(text,(i*FONT_PX,drops[i]*FONT_PX))
drops[i]+=1
ifdrops[i]*10>PANEL_highlyorrandom.random()>0.95:
drops[i]=0
pygame.display.flip()

实现效果如下:

20200513195610469.gif

3. 图片

最后,我们用图片来达到这个效果,我们用雨滴来达到这个效果。我们在这里使用它 tkinter 创建窗口,代码实现如下:

#初始雨滴纵坐标
INIT_HEIGHT=10
#雨滴创建
defrainmake(canvas,imagefile):
rainlist=[]
foriinrange(5):
#根据图片,创建一排雨滴
rainlist.append(canvas.create_image(100+80*i,INIT_HEIGHT,anchor=NE,image=imagefile))
returnrainlist
#雨滴下落
defraindown(tk,canvas,imagefile,sec):
#线程间等待
time.sleep(sec)
rainlist=rainmake(canvas,imagefile)
#每个雨滴的纵坐标值
height=[INIT_HEIGHT]*10
whileTrue:
#每次移动前等一会儿
time.sleep(0.2)
#五滴雨滴一起移动
foriinrange(5):
#如果雨滴字到底,不要继续移动
ifnotheight[i]==0:
#设置下落步调
rnd=random.randint(5,50)
canvas.move(rainlist[i],0,rnd)
height[i]=height[i]+rnd
tk.update()
fori,hinenumerate(height):
ifh>400:
#当雨滴字走到底部时,删除它
canvas.delete(rainlist[i])
tk.update()
#清空雨滴的heightht
height[i]=0
print(i,h,height)
#全到底,则跳出循环
ifheight==[0]*5:
print('break:',threading.current_thread().name)
break
deflookloop(tk,canvas,thread):
aliveflg=False
whileTrue:
#5s检测一次
time.sleep(5)
forthinthread:
ifth.is_alive():
aliveflg=True
else:
aliveflg=False
ifaliveflg==False:
break
canvas.create_text(100,200,text='雨停了...',fill='red')
canvas.pack()
time.sleep(5)
tk.destroy()

实现效果如下:

20200513195715106.gif

更多Python知识,请关注Python视频教程!!