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设计】。

相关文章

eclipse创建python项目步骤详解

eclipse创建python项目步骤详解

eclipse安装Python插件之后,主要是为了方便Python代码就可以再Eclipse进行代码脚本,使用Eclipse开发Python脚本,主要写代码会有提示功能,提高写代码效率,...

Python语言的面相对象编程方式初步学习

词语练习 class:告诉python创造一个新的东西 object:两个意思:最基本的东西和任何实例化的东西。 instance:创建一个类得到的东西。 def:在类...

如何在Python中编写并发程序

GIL 在Python中,由于历史原因(GIL),使得Python中多线程的效果非常不理想.GIL使得任何时刻Python只能利用一个CPU核,并且它的调度算法简单粗暴:多线程中,让每...

举例详解Python中循环语句的嵌套使用

 Python编程语言允许在一个循环内嵌套另一个循环。下面将介绍几个例子来说明这一概念。 语法 在Python中嵌套循环语句的语法如下: for iterating_var...

win与linux系统中python requests 安装

在 windows 系统下,只需要输入命令 pip install requests ,即可安装。 在 linux 系统下,只需要输入命令 sudo  pip install...