Python implementation characters rain *** empire

This tutorial is very simple, in addition to copy the code, I hope you take the time to look at "attention", the tutorial is very simple

note

The project, the need to use the file repository "pygame", not a small partner, I can refer to PyCharm course there are detailed explanations agent / IB how to add a library;

# Import system file library

import pygame

import random

from pygame.locals import *

from random import randint

# Define form parameters and load the font file

Form width SCREEN_WIDTH = 900 #

Form width SCREEN_HEIGHT = 600 #

LOW_SPEED = 4 # font move the minimum speed

HIGH_SPEED = 10 # fastest moving fonts

FONT_COLOR = (00,150,00) # font color

FONT_SIZE = 5 # font size

FONT_NOM = 20 # to display the number of fonts from 0

FONT_NAME = "calibrii.ttf" # Note that the font file name must be identical to the real file (note the capitalization of ttf), and the file name can not be Chinese

FREQUENCE = 10 # Time Frequency

times = 0 # initialization time

Random parameter definitions

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

Guess you like

Origin blog.51cto.com/14511863/2472599
Recommended