python实现黑客字幕雨效果

yipeiwu_com7年前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") 
 
 
###########################

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python 3.6.2 安装配置方法图文教程

python 3.6.2 安装配置方法图文教程

Windows下Python(pip)环境搭建(3.6)图解,供大家参考,具体内容如下 1、下载最新的Python安装:3.6.2 2、安装时不要选择默认,自定义安装(customiz...

详解python多线程、锁、event事件机制的简单使用

线程和进程 1、线程共享创建它的进程的地址空间,进程有自己的地址空间 2、线程可以访问进程所有的数据,线程可以相互访问 3、线程之间的数据是独立的 4、子进程复制线程的数据 5、子进...

windows下添加Python环境变量的方法汇总

如果安装的时候没有选择Add Python.exe to Path 导致在命令提示符下执行python 提示 'python' 不是内部或外部命令,也不是可运行的程序或批处理文件。 可...

Python获取Redis所有Key以及内容的方法

一、获取所有Key # -*- encoding: UTF-8 -*- __author__ = "Sky" import redis pool=redis.Connection...

Python OpenCV利用笔记本摄像头实现人脸检测

Python OpenCV利用笔记本摄像头实现人脸检测

本文实例为大家分享了Python OpenCV利用笔记本摄像头实现人脸检测的具体代码,供大家参考,具体内容如下 1.安装opencv 首先参考其他文章安装pip。 之后以管理员身份运行命...