python-pygame作品之黑客帝国代码雨
今天,我又來(lái)為大家介紹pygame小程序了。
推薦你先看看之前的那一篇:
python-pygame小游戲之球球大作戰(zhàn)
今天我要來(lái)介紹介紹我自己做的小程序——黑客帝國(guó)字母雨!大家來(lái)看看吧!
---------------------------------------------------開(kāi)始寫(xiě)代碼了!---------------------------------------------------------
一、初始化
首先導(dǎo)入pygame和random模塊后創(chuàng)建一個(gè)窗口,定義好常用的變量。
import random import pygamePANEL_width = 600 PANEL_highly = 500 FONT_PX = 15pygame.init()# 創(chuàng)建一個(gè)可是窗口 winSur = pygame.display.set_mode((PANEL_width, PANEL_highly)) font = pygame.font.SysFont("123.ttf", 25) 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))二、字母雨的創(chuàng)建
想要實(shí)現(xiàn)字母雨,首先要?jiǎng)?chuàng)造好帶有內(nèi)容的列表。
# 數(shù)字版 texts = [font.render(str(i), True, (0, 255, 0)) for i in range(10)]# 字母版 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']然后按屏幕的寬帶計(jì)算可以在畫(huà)板上放幾列坐標(biāo)并生成另一個(gè)列表,完成字母表的創(chuàng)建
column = int(PANEL_width / FONT_PX) drops = [0 for i in range(column)]完成后就要開(kāi)始進(jìn)入正題了!
三、主程序運(yùn)行
首先與往常一樣,先寫(xiě)防卡程序。
while True:# 從隊(duì)列中獲取事件for event in pygame.event.get():if event.type == pygame.QUIT:exit()最后還要寫(xiě)上顯示字母的代碼。
while True:# 從隊(duì)列中獲取事件for event in pygame.event.get():if event.type == pygame.QUIT: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] += 1if drops[i] * 10 > PANEL_highly or random.random() > 0.95:drops[i] = 0pygame.display.flip()簡(jiǎn)簡(jiǎn)單單的字母雨程序,就被你我搞定了!
四、完整代碼
奉上全部代碼:
import random import pygamePANEL_width = 600 PANEL_highly = 500 FONT_PX = 15pygame.init() # 創(chuàng)建一個(gè)可是窗口 winSur = pygame.display.set_mode((PANEL_width, PANEL_highly)) font = pygame.font.SysFont("123.ttf", 25) 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))# 數(shù)字版 texts = [font.render(str(i), True, (0, 255, 0)) for i in range(10)]# 字母版 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) #]# 按屏幕的寬帶計(jì)算可以在畫(huà)板上放幾列坐標(biāo)并生成一個(gè)列表 column = int(PANEL_width / FONT_PX) drops = [0 for i in range(column)]while True:# 從隊(duì)列中獲取事件for event in pygame.event.get():if event.type == pygame.QUIT: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] += 1if drops[i] * 10 > PANEL_highly or random.random() > 0.95:drops[i] = 0pygame.display.flip()五、效果圖
?六、知識(shí)總結(jié)
通過(guò)這次編程實(shí)踐,我對(duì)python和pygame有了新的了解。其實(shí)python一點(diǎn)也不難。推薦大家可以學(xué)習(xí)python哦~
總結(jié)
以上是生活随笔為你收集整理的python-pygame作品之黑客帝国代码雨的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: visio 模板_Mac软件推荐:免费的
- 下一篇: Linux安装以及固定ip