分享

简单用Python写出一个黑客帝国的瀑布流代码页面

 李济宏 2023-01-18 发布于上海

  执行时如果报错,提示少calibrii.ttf字体,那就百度一下,下载这个字体,放在python代码目录下即可。

# !/usr/bin/env python

# -*- coding:utf-8 -*-

# @Time : 2023.1

# @Author : Amadeus

# @Note : 类似"黑客帝国"中的代码雨效果


#导入系统文件库

import pygame

import random

from pygame.locals import *

from random import randint


#定义一些窗体参数及加载字体文件

SCREEN_WIDTH = 1500    # 窗体宽度

SCREEN_HEIGHT = 800    # 窗体宽度

LOW_SPEED = 5      # 字体移动最低速度

HIGH_SPEED = 15      # 字体移动最快速度

FONT_COLOR = (00,150,00)  # 字体颜色

FONT_SIZE = 25       # 字体尺寸

FONT_NOM = 25       # 显示字体数量 从0开始

FONT_NAME = "calibrii.ttf" # 注意字体的文件名必须与真实文件完全相同(注意ttf的大小写),且文件名不能是中文

FREQUENCE = 20       # 时间频度

times = 0         # 初始化时间



# 定义随机参数

def randomspeed() :

  return randint(LOW_SPEED,HIGH_SPEED)

def randomposition() :

  return randint(0,SCREEN_WIDTH),randint(0,SCREEN_HEIGHT)

def randomoname() :

    # 序列

  array = ["T","o","n","y","A","m","d","u","s","1","2","5"]

  # 随机选取一个元素

  return random.choice(array)

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 = randomoname()

    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("Amadeus的黑客帝国")

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() 

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多