python实现黑客字幕雨效果

yipeiwu_com6年前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实现PS滤镜特效之扇形变换效果示例

Python实现PS滤镜特效之扇形变换效果示例

本文实例讲述了Python实现PS滤镜特效之扇形变换效果。分享给大家供大家参考,具体如下: 这里用 Python 实现 PS 滤镜中的一种几何变换特效,称为扇形变换,将图像扭曲成一个扇形...

Django在win10下的安装并创建工程

Django在win10下的安装并创建工程

Django的核心(1.4+)可以运行在从2.5到2.7之间的任何Python版本。 我的电脑是操作系统是window10 ,内存是4G。 1。下载django 官网地址:https...

在Python中使用HTML模版的教程

在Python中使用HTML模版的教程

Web框架把我们从WSGI中拯救出来了。现在,我们只需要不断地编写函数,带上URL,就可以继续Web App的开发了。 但是,Web App不仅仅是处理逻辑,展示给用户的页面也非常重要。...

Python网络编程使用select实现socket全双工异步通信功能示例

本文实例讲述了Python网络编程使用select实现socket全双工异步通信功能。分享给大家供大家参考,具体如下: 在前面一篇《Python网络编程之TCP套接字简单用法》中,我们实...

flask中过滤器的使用详解

过滤器 过滤器的本质就是函数。有时候我们不仅仅只是需要输出变量的值,我们还需要修改变量的显示,甚至格式化、运算等等,而在模板中是不能直接调用 Python 中的某些方法,那么这就用到了...