python+pygame實現代碼雨(黑客帝國既視感)

本文主要介紹瞭python+pygame實現代碼雨,分享給大傢,具體如下:

效果:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time  : 2020/12/29 12:34
# @Author : huni
# @File  : 代碼雨.py
# @Software: PyCharm
import random
import pygame
PANEL_width = 1600
PANEL_highly = 1000
FONT_PX = 15
pygame.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)]
#texts = [
  #font.render(str(letter[i]), True, (0, 255, 0)) for i in range(26)
#]
# 字母版
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)
]
texts = [font.render(str(i), True, (0, 255, 0)) for i in range(2)]
# 按屏幕的寬帶計算可以在畫板上放幾列坐標並生成一個列表
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(40)
  # 重新編輯圖像第二個參數是坐上角坐標
  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()

到此這篇關於python+pygame實現代碼雨(黑客帝國既視感)的文章就介紹到這瞭,更多相關pygame 代碼雨 內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: