日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python数字雨_用Python实现黑客帝国代码雨效果(3种方式)

發布時間:2023/12/31 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python数字雨_用Python实现黑客帝国代码雨效果(3种方式) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

說起電影《黑客帝國》,相信大部分人都看過或聽說過,影片中有一個場景數字雨,如果你看過電影的話,應該對這個經典場景印象深刻,本文我們利用 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))

# 數字

texts = [font.render(str(i), True, (0, 255, 0)) for i in range(10)]

colums = int(500 / FONT_PX)

drops = [0 for i in range(colums)]

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

exit()

pygame.time.delay(33)

winSur.blit(bg_suface, (0, 0))

for i in range(len(drops)):

text = random.choice(texts)

winSur.blit(text, (i * FONT_PX, drops[i] * FONT_PX))

drops[i] += 1

if drops[i] * 10 > 600 or random.random() > 0.95:

drops[i] = 0

pygame.display.flip()

實現效果如下:

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)) for i in range(26)

]

# 按窗口的寬度來計算可以在畫板上放幾列坐標并生成一個列表

column = int(PANEL_width / FONT_PX)

drops = [0 for i in range(column)]

while True:

# 從隊列中獲取事件

for event in pygame.event.get():

if event.type == pygame.QUIT:

exit()

elif event.type == pygame.KEYDOWN:

chang = pygame.key.get_pressed()

if (chang[32]):

exit()

# 暫停給定的毫秒數

pygame.time.delay(30)

# 重新編輯圖像

winSur.blit(bg_suface, (0, 0))

for i in range(len(drops)):

text = random.choice(texts)

# 重新編輯每個坐標點的圖像

winSur.blit(text, (i * FONT_PX, drops[i] * FONT_PX))

drops[i] += 1

if drops[i] * 10 > PANEL_highly or random.random() > 0.95:

drops[i] = 0

pygame.display.flip()

實現效果如下:

3. 圖片

最后,我們使用圖片來實現這一效果,圖片我們就使用雨滴吧,這里我們使用 tkinter 創建窗口,代碼實現如下:# 初始雨滴縱坐標

INIT_HEIGHT = 10

# 雨滴創建

def rainmake(canvas, imagefile):

rainlist = []

for i in range(5):

# 根據圖片,創建一排雨滴

rainlist.append(canvas.create_image(100 + 80 * i, INIT_HEIGHT, anchor=NE, image=imagefile))

return rainlist

# 雨滴下落

def raindown(tk, canvas, imagefile, sec):

# 線程間等待

time.sleep(sec)

rainlist = rainmake(canvas, imagefile)

# 每個雨滴的縱坐標值

height = [INIT_HEIGHT] * 10

while True:

# 每次移動前稍等一會

time.sleep(0.2)

# 5 個雨滴一起移動

for i in range(5):

# 如果雨滴字到底了,則不繼續移動

if not height[i] == 0:

# 設置下落步調

rnd = random.randint(5, 50)

canvas.move(rainlist[i], 0, rnd)

height[i] = height[i] + rnd

tk.update()

for i,h in enumerate(height):

if h > 400:

# 當雨滴字走到最下方,則刪除

canvas.delete(rainlist[i])

tk.update()

# 清空該雨滴的 height

height[i] = 0

print(i,h,height)

# 全到底,則跳出循環

if height == [0] * 5:

print('break:',threading.current_thread().name)

break

def lookloop(tk, canvas, thread):

aliveflg = False

while True:

# 5s 檢測一次

time.sleep(5)

for th in thread:

if th.is_alive():

aliveflg = True

else:

aliveflg = False

if aliveflg == False:

break

canvas.create_text(100 , 200, text='雨停了...', fill='red')

canvas.pack()

time.sleep(5)

tk.destroy()

實現效果如下:

源碼在公眾號 Python小二 后臺回復 200513 獲取。

總結

以上是生活随笔為你收集整理的python数字雨_用Python实现黑客帝国代码雨效果(3种方式)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。