pygame制作的太空猫捉太空鼠小游戏

"""pygame制作的太空猫捉太空鼠小游戏,用上下左右方向箭头号操作一只猫在太空中去碰撞老鼠的小游戏."""

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

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

class Rat():
     
    def __init__(self,leftImage,rightImage,screen):
        self.images = [leftImage,rightImage]     # 左图与右图
        self.imageIndex = randint(0,1)           # 图像索引
        self.image = self.images[self.imageIndex]# 确定使用哪个图形
        self.xspeed = self.imageIndex * 2 -1     # 0左图对应-1往左移动,1右图对应1往右移动
        self.yspeed = choice([-2,2])
        self.screen = screen
        self.sw = screen.get_width()       # 屏幕宽度
        self.sh = screen.get_height()      # 屏幕高度
        self.rect = self.image.get_rect()
        self.rect.x = randint(80, self.sw-80)
        self.rect.y = randint(80,self.sh-80)
        self.status = 1                    # 1表示移动中,0表示正在暂停
        self.moveStartTime = time.time()
        self.moveDelay = 0.01              # 移动延时         
        self.delete = 0
        
    def move(self):
        if self.status==1:
            if time.time() - self.moveStartTime > self.moveDelay:
                self.rect.x = self.rect.x + self.xspeed
                self.rect.y = self.rect.y + self.yspeed
                
                # 以下是超出边界进行换造型与xspeed的翻转
                if self.rect.x<=0 or self.rect.right>=self.sw:
                    self.imageIndex = 1-self.imageIndex
                    self.xspeed = self.imageIndex * 2 -1
                    self.image = self.images[self.imageIndex]
                if self.rect.y <=0 or self.rect.bottom>=self.sh:
                    self.yspeed = -self.yspeed
                     
    def collide(self,cat):
        return self.rect.colliderect(cat.rect)
 
    def draw(self):
        self.screen.blit(self.image,self.rect)


class Cat():
    def __init__(self,leftImage,rightImage,screen):
        self.images = [leftImage,rightImage]     # 左图与右图
        self.imageIndex = 1                      # 开始时面向右的方向
        self.image = self.images[self.imageIndex]# 确定使用哪个图形
        self.xspeed = 0                          # 0左图对应-1往左移动,1右图对应1往右移动
        self.yspeed = 0
        self.screen = screen
        self.sw = screen.get_width()
        self.sh = screen.get_height()
        self.rect = self.image.get_rect()
        self.rect.x = self.sw//2 - self.rect.width//2
        self.rect.y = self.sh//2 - self.rect.height//2
        
    def move(self):
        self.rect.move_ip(self.xspeed,self.yspeed)
   
    def draw(self):
        self.image = self.images[self.imageIndex]
        self.screen.blit(self.image,self.rect)
        
def playmusic():    
    pygame.mixer.music.load("Cave.wav")
    pygame.mixer.music.play(-1,0)
        
def main():
    
    pygame.init()
    screen_width,screeh_height=480,360
    screen = pygame.display.set_mode((screen_width,screeh_height))
    pygame.display.set_caption("pygame太空猫捉太空鼠小游戏_作者:李兴球 www.scratch8.net")

    老鼠音 = pygame.mixer.Sound("老鼠音.wav")
    背景图 = pygame.image.load("moon.png")
    鼠左图像 = pygame.image.load("太空鼠_左.png")
    鼠右图像 = pygame.image.load("太空鼠_右.png")
    rats=[]
    for i in range(30):
       rats.append(Rat(鼠左图像,鼠右图像,screen))        
    
    猫左图像 = pygame.image.load("太空猫_左.png")
    猫右图像 = pygame.image.load("太空猫_右.png")
    
    cat = Cat(猫左图像,猫右图像,screen)
    
    clock = pygame.time.Clock()
    运行中 = True
    while 运行中:
        if randint(0,100) ==0 : rats.append(Rat(鼠左图像,鼠右图像,screen))
        for event in pygame.event.get():
            if event.type == QUIT:运行中 = False
        keys = pygame.key.get_pressed()
         
        if keys[K_UP] ==1:
            cat.yspeed=-4
        elif keys[K_DOWN] ==1 :
            cat.yspeed=4
        else:
            cat.yspeed=0
            
        if keys[K_RIGHT] ==1:
            cat.imageIndex = 1             
            cat.xspeed=4
        elif keys[K_LEFT] ==1 :
            cat.imageIndex = 0
            cat.xspeed=-4
        else:
            cat.xspeed=0       

        for rat in rats:
            if randint(0,100)==0:
                rat.status= 1 - rat.status             
                # 设计让老鼠有时会换方向
                if randint(0,1) ==1:
                    rat.imageIndex = 1 - rat.imageIndex    # 图像索引
                    rat.image = rat.images[rat.imageIndex] # 确定使用哪个图形
                    rat.xspeed = -rat.xspeed               # 0左图对应-1往左移动,1右图对应1往右移动                    
            if rat.collide(cat):rat.delete=1
            if rat.delete==0:    
               rat.move()               
            else:
               rats.remove(rat)
               老鼠音.play()               
        cat.move()
        
        # 重画图形
        screen.blit(背景图,(0,0))
        cat.draw()        
        [rat.draw() for rat in rats]
        
        pygame.display.update()       
        clock.tick(60)
    pygame.quit()



if __name__ == "__main__":
    pygame.mixer.init()
    playmusic()
    main()