栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python实现黑客字幕雨效果

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python实现黑客字幕雨效果

本文实例为大家分享了python实现字幕雨效果的具体代码,供大家参考,具体内容如下

#################################### 
#name : HACKER EMPIRE CAPTION RAIN 
#import modules 
try : 
  import pygame 
  import sys 
  from pygame.locals import * 
  from random import randint 
except : 
  print("Load modules error!!") 
  exit() 
 
 
#define some datas 
SCREEN_WIDTH = 1366 
SCREEN_HEIGHT = 768 
LOW_SPEED = 30 
HIGH_SPEED = 30 
LOW_SIZE = 5 
HIGH_SIZE = 30 
FONT_SIZE = 40 
FONT_NAME = "myfont.ttf" 
FREQUENCE = 50 
times = 0 
 
 
#def random color 
def randomcolor() : 
  return (randint(0,255),randint(0,255),randint(0,255)) 
 
 
def randomspeed() : 
  return randint(LOW_SPEED,HIGH_SPEED) 
 
 
def randomposition() : 
  return (randint(0,SCREEN_WIDTH),randint(0,SCREEN_HEIGHT)) 
 
 
def randomsize() : 
  return randint(LOW_SIZE,HIGH_SIZE) 
 
 
def randomoname() : 
  return randint(0,100000) 
 
 
def randomvalue() : 
  return randint(0,9)#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,randomcolor()) 
    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("HACKER EMPIRE CAPTION RAIN") 
clock = pygame.time.Clock() 
group = pygame.sprite.Group() 
group_count = SCREEN_WIDTH / FONT_SIZE 
 
 
#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_SIZE,-FONT_SIZE))) 
  group.update() 
  group.draw(screen) 
 
  pygame.display.update() 
 
  #save pictures 
  #times += time 
  #if times > 5000 : 
    #pygame.image.save(screen,str(randomoname())+".png") 
 
 
###########################

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/29577.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号