温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python 实现 字符雨

发布时间:2020-08-09 01:58:54 来源:网络 阅读:183 作者:专注地一哥 栏目:编程语言

本教程很简单吧,除了复制代码之外,希望你也抽点时间去看下“注意”,教程很简单

注意

本项目中,需要用到文件库“pygame”,不会的小伙伴,可以参考我的PyCharm教程里面有详细的讲解代理/IB如何添加库;

#导入系统文件库

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI