python編寫《黑客帝國I》中的代碼雨

首先說明配置環境:

1、使用python版本:python-3.7.3

2、使用工具:PyCharm

3、代碼實現:

"""
 chuangjianshijian:2019/4/28
 chuangjianneirong:daimayu(代碼雨)
"""

import random
import pygame
from pygame.locals import *
from sys import exit

PANEL_width = 1920
PANEL_highly = 1080
FONT_PX = 20

pygame.init()

# 創建一個可是窗口
winSur = pygame.display.set_mode((PANEL_width, PANEL_highly), FULLSCREEN, 32)

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, 16))

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

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

# # 二進制版
# 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)
]

# 按屏幕的寬帶計算可以在畫板上放幾列座標並生成一個列表
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()

 

整個代碼包含了,三個版本,一個是二進制的版本、一個是26個字母的版本、漢字版以及二進制和字母混合的版本,其中漢字版只能顯示爲“口”,一直沒找到錯誤的原因,編碼設置也做過,沒變化,如果大家誰解決了這個問題,發消息回覆一下,謝謝,你想用哪一個就用哪一個,其中可以調代碼雨的速度以及顯示屏上的顯示條數等,如有什麼需要改進的請大家評論出來。大家一起進步!

4、運行顯示:

二進制版

漢字版

字母版

個人認爲字母版看着舒服點,嘻嘻

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章