用python實(shí)現(xiàn)代碼雨(電影黑客帝國里的效果,代碼可直接運(yùn)行)

用python實(shí)現(xiàn)代碼雨:

# -*- coding:utf-8 -*-

import random

import pygame

from pygame.locals import *

from sys import exit

PANEL_width = 1920

PANEL_highly = 1080

FONT_PX = 40

pygame.init()

# 創(chuàng)建一個(gè)可視窗口, 如果不想全屏運(yùn)行,可以把 FULLSCREEN 參數(shù)去掉

winSur = pygame.display.set_mode((PANEL_width, PANEL_highly), FULLSCREEN, 32)

font = pygame.font.SysFont("SimHei", 35)

bg_suface = pygame.Surface((PANEL_width, PANEL_highly), flags=pygame.SRCALPHA)

pygame.Surface.convert(bg_suface)

bg_suface.fill(pygame.Color(0, 0, 0, 20))

winSur.fill((0, 0, 0))

# 數(shù)字版

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

# # 二進(jìn)制版

# letter = ['1', '0', '1', '1', '1', '0', '0', '0', '1', '0', '1', '0', '1', '0', '0', '1', '1', '0', '0', '0', '1', '1'

#? ? ? ? ? ,'1', '0', '1', '0', '0', '1', '0', '1']

# # 漢字版,但看不到字

# letter = ['我', '愛', '你', '我', '愛你', '我愛你', '我非常愛你', '我愛你', '我愛', '我', '愛', '你',

#? ? ? ? ? '我愛你', '愛', '我', '愛你', '我', '我愛', '愛你', '你']

#

# #? 字母版

letter = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v'

? ? ? ? ? ,'w', 'x', 'y', 'z']

texts = [

? ? font.render(str(letter[i]), True, (0, 255, 0)) for i in range(20)

]

# 按屏幕的寬帶計(jì)算可以在畫板上放幾列坐標(biāo)并生成一個(gè)列表

column = int(PANEL_width / FONT_PX)

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

while True:

? ? # 從隊(duì)列中獲取事件, 如果退出程序, 按兩下確認(rèn)鍵即可

? ? 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()

? ? # 將暫停一段給定的毫秒數(shù)

? ? pygame.time.delay(30)

? ? # 重新編輯圖像第二個(gè)參數(shù)是坐上角坐標(biāo)

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

? ? for i in range(len(drops)):

? ? ? ? text = random.choice(texts)

? ? ? ? # 重新編輯每個(gè)坐標(biāo)點(diǎn)的圖像

? ? ? ? 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()

實(shí)現(xiàn)效果如下:

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容