python-pygame作品之黑客帝国代码雨
生活随笔
收集整理的這篇文章主要介紹了
python-pygame作品之黑客帝国代码雨
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天,我又來為大家介紹pygame小程序了。
推薦你先看看之前的那一篇:
python-pygame小游戲之球球大作戰
今天我要來介紹介紹我自己做的小程序——黑客帝國字母雨!大家來看看吧!
---------------------------------------------------開始寫代碼了!---------------------------------------------------------
一、初始化
首先導入pygame和random模塊后創建一個窗口,定義好常用的變量。
import random import pygamePANEL_width = 600 PANEL_highly = 500 FONT_PX = 15pygame.init()# 創建一個可是窗口 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))二、字母雨的創建
想要實現字母雨,首先要創造好帶有內容的列表。
# 數字版 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']然后按屏幕的寬帶計算可以在畫板上放幾列坐標并生成另一個列表,完成字母表的創建
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()最后還要寫上顯示字母的代碼。
while True:# 從隊列中獲取事件for event in pygame.event.get():if event.type == pygame.QUIT: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] += 1if drops[i] * 10 > PANEL_highly or random.random() > 0.95:drops[i] = 0pygame.display.flip()簡簡單單的字母雨程序,就被你我搞定了!
四、完整代碼
奉上全部代碼:
import random import pygamePANEL_width = 600 PANEL_highly = 500 FONT_PX = 15pygame.init() # 創建一個可是窗口 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))# 數字版 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) #]# 按屏幕的寬帶計算可以在畫板上放幾列坐標并生成一個列表 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()# 將暫停一段給定的毫秒數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] += 1if drops[i] * 10 > PANEL_highly or random.random() > 0.95:drops[i] = 0pygame.display.flip()五、效果圖
?六、知識總結
通過這次編程實踐,我對python和pygame有了新的了解。其實python一點也不難。推薦大家可以學習python哦~
總結
以上是生活随笔為你收集整理的python-pygame作品之黑客帝国代码雨的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: visio 模板_Mac软件推荐:免费的
- 下一篇: 杨辉三角形(Python)