让病毒0.5 秒生成一个。
import pygame
from random import randint, choice
from time import sleep, time
from os import system, remove
from os. path import exists
from pygame. locals import *
pygame. init( )
canvas = pygame. display. set_mode( ( 980 , 700 ) )
canvas. fill( ( 255 , 255 , 255 ) )
pygame. display. set_caption( '抗击病毒' )
bg = pygame. image. load( 'images/hospital.jpg' )
doctor = pygame. image. load( 'images/doctor.png' )
virus1 = pygame. image. load( 'images/virus.png' )
virus2 = pygame. image. load( 'images/virus.jpg' )
virus3 = pygame. image. load( 'images/virus.jpeg' )
boss1 = pygame. image. load( 'images/boss1.png' )
boss2 = pygame. image. load( 'images/boss2.png' )
lose = pygame. image. load( 'images/lose.jpg' )
bullet = pygame. image. load( 'images/bullet.png' )
win = pygame. image. load( 'images/win.jpg' )
bag1 = pygame. image. load( 'images/bag.png' )
def isActionTime ( lastTime, interval) :
if lastTime == 0 :
return True
currentTime = time( )
return currentTime - lastTime >= interval
def conPaint ( ) :
canvas. blit( bg, ( 0 , 0 ) )
for virus in Game. virus:
virus. paint( )
def event ( ) :
for event in pygame. event. get( ) :
if event. type == QUIT:
pygame. quit( )
exit( )
def conEnter ( ) :
if not isActionTime( Game. lastTime, Game. interval) :
return
Game. lastTime = time( )
Game. virus. append( Virus( ) )
def conStep ( ) :
for virus in Game. virus:
virus. step( )
def control ( ) :
conPaint( )
event( )
conEnter( )
conStep( )
pygame. display. update( )
class Virus ( ) :
def __init__ ( self) :
self. width = 33
self. height = 33
self. img = choice( Game. virus_imgs)
self. x = 1000 - self. width
self. y = randint( 0 , 700 - self. height)
self. speed = randint( 1 , 10 )
def paint ( self) :
canvas. blit( self. img, ( self. x, self. y) )
def step ( self) :
self. x -= self. speed
class Game ( ) :
virus_imgs = [ virus1, virus2, virus3]
virus = [ ]
lastTime = time( )
interval = 0.5
while True :
control( )