天天看點

Python 實作 黑客帝國中的字元雨

Python 實作 黑客帝國中的字元雨
#  !/usr/bin/env  python
#  -*- coding:utf-8 -*-
# @Time   :  2020.2
# @Author :  綠色羽毛
# @Email  :  [email protected]
# @Blog   :  https://blog.csdn.net/ViatorSun
# @Note   :  類似"黑客帝國"中的代碼雨效果


#導入系統檔案庫
import pygame
import random
from pygame.locals import *
from random import randint



#定義一些窗體參數及加載字型檔案
SCREEN_WIDTH  = 900         # 窗體寬度
SCREEN_HEIGHT = 600         # 窗體寬度
LOW_SPEED  = 4              # 字型移動最低速度
HIGH_SPEED = 10             # 字型移動最快速度
FONT_COLOR = (00,150,00)    # 字型顔色
FONT_SIZE = 5               # 字型尺寸
FONT_NOM  = 20              # 顯示字型數量  從0開始
FONT_NAME = "calibrii.ttf"  # 注意字型的檔案名必須與真實檔案完全相同(注意ttf的大小寫),且檔案名不能是中文
FREQUENCE = 10              # 時間頻度
times = 0                   # 初始化時間


# 定義随機參數
def randomspeed() :
    return randint(LOW_SPEED,HIGH_SPEED)
def randomposition() :
    return randint(0,SCREEN_WIDTH),randint(0,SCREEN_HEIGHT)
def randomoname() :
    return randint(0,100000)
def randomvalue() :
    return randint(0,100)              # this is your own display number range


#class of sprite
class Word(pygame.sprite.Sprite) :
    def __init__(self,bornposition) :
        pygame.sprite.Sprite.__init__(self)
        self.value = randomvalue()
        self.font = pygame.font.Font(FONT_NAME,FONT_SIZE)
        self.image = self.font.render(str(self.value),True,FONT_COLOR)
        self.speed = randomspeed()
        self.rect = self.image.get_rect()
        self.rect.topleft = bornposition

    def update(self) :
        self.rect = self.rect.move(0,self.speed)
        if self.rect.top > SCREEN_HEIGHT :
            self.kill()


#init the available modules
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
pygame.display.set_caption("ViatorSun CodeRain")
clock = pygame.time.Clock()
group = pygame.sprite.Group()
group_count = int(SCREEN_WIDTH / FONT_NOM)


#mainloop
while True :
    time = clock.tick(FREQUENCE)
    for event in pygame.event.get() :
        if event.type == QUIT :
            pygame.quit()
            exit()

    screen.fill((0,0,0))
    for i in range(0,group_count) :
        group.add(Word((i * FONT_NOM,-FONT_NOM)))

    group.update()
    group.draw(screen)
    pygame.display.update()
    

           

不想下載下傳字型的,把47行FONT_NAME改為None。