pygame行走的小猫多帧动画演示程序

"""行走的小猫多帧动画演示程序.py。一只步态优雅的小猫在海面上行走。用的是MIT scratch小猫。
这是本人曾经学习pygame的一个练习程序,在这个程序中新建了一个叫Sprite的类。
它有两个帧图序列,当它的x速度大于0的时候,就用右边的帧序列,反之用左边的帧序列."""

import pygame
import time,glob
from random import randint
from pygame.locals import *

class Sprite():
    def __init__(self,framesRight,framesLeft,x,y,w,h):
        self.frames_right = framesRight
        self.frames_left = framesLeft
        self.造型数量 = len(self.frames_left)
        self.造型编号=0
        self.造型切换间隔时长=0.1   # 以秒为单位
        self.移动间隔时长=0.1       # 以秒为单位

        self.rect = pygame.Rect(x,y,w,h)
        self.xspeed=randint(-5,5)
        self.yspeed=0
        self.begintime = time.time()
        self.begintime2 = time.time()
        
    def 下一个造型(self):
        if (time.time()-self.begintime) >= self.造型切换间隔时长:
            self.造型编号 = self.造型编号 + 1
            self.造型编号 = self.造型编号 % self.造型数量
            self.begintime = time.time()
    def move(self):
        if (time.time()-self.begintime2) >= self.移动间隔时长:

            self.rect.move_ip(self.xspeed,self.yspeed)
            if self.rect.left<0 or self.rect.right>screen_width:
                self.xspeed = - self.xspeed
            self.begintime2 = time.time()
        
    def draw(self):
        if self.xspeed>0:
            screen.blit(self.frames_right[self.造型编号],self.rect)
        else:            
            screen.blit(self.frames_left[self.造型编号],self.rect)

pygame.init()
screen_width,screen_height=480,360
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("行走的小猫帧动画演示程序--作者:李兴球")
背景 = pygame.image.load("海洋背景.png")
framesRight = [pygame.image.load(image) for image in glob.glob("cats/*.png")]
framesLeft = [pygame.transform.flip(f,True,False) for f in framesRight]
font = pygame.font.Font("c:/windows/fonts/msyh.ttf",30)
textImage =font.render("行走的小猫",True,(0,2,255))
(tx,ty,tw,th) = textImage.get_rect()
textpos=(screen_width//2 - tw //2,screen_height//2 - th/2 -100)

#新建一个小猫测试
cat = Sprite(framesRight,framesLeft,100,200,80,90)
running = True
while running:
    for event in pygame.event.get():
        if event.type==QUIT:running = False        
    cat.move()
    cat.下一个造型()    
    screen.blit(背景,(0,0))
    cat.draw()
    screen.blit(textImage,textpos)
    pygame.display.update()
pygame.quit()