pygame婷婷的舞蹈_小女孩跳舞动画

"""婷婷的舞蹈.py,伴随着舞台音响灯光,这是在演示一个小女孩在舞台上跳舞的小动画"""

__author__ = "李兴球"
__date__ = "2018年7月"

import pygame
from pygame.locals import *
import time
   
class Sprite():
    def __init__(self,images,x,y,间隔,screen):
        self.images = images
        self.造型总数 = len(images)
        self.index = 0
        self.image = self.images[self.index]  #初始造型
        self.造型切换间隔 = 间隔
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.start = time.time()
        self.screen = screen

    def 下一个造型(self):
        逝去的时间 = time.time() -self.start
        
        if 逝去的时间  >= self.造型切换间隔:
            self.index = self.index + 1
            self.index = self.index  % self.造型总数
            self.image = self.images[self.index]
            self.start = time.time()
    def draw(self):
        self.screen.blit(self.image,self.rect)

def 播放背景音乐():
    pygame.mixer.init()
    pygame.mixer.music.load("Cave.wav")
    pygame.mixer.music.play(-1,0)
    
def main():
        
    pygame.init()
    screen = pygame.display.set_mode((480,360))
    pygame.display.set_caption("婷婷的舞蹈_小女孩跳舞pygame小动画_作者:李兴球")
    
    背景列表 = []
    for i in range(8):
        背景列表.append(pygame.image.load("背景" + str(i) + ".png"))

    girl_images = []
    for i in range(4):
        girl_images.append(pygame.image.load("girl" + str(i) + ".gif"))     
    
    播放背景音乐()
    
    bg = Sprite(背景列表,0,0,0.5,screen)
    girl = Sprite(girl_images,190,80,0.6,screen)
    clock = pygame.time.Clock()
    running = True
    while running:
        for event in pygame.event.get():
            if event.type==QUIT:running = False
            
        bg.下一个造型()        
        girl.下一个造型()
        bg.draw()
        girl.draw()
        pygame.display.update()
        clock.tick(30)
    pygame.quit()

if __name__=="__main__":
    main()