"""pygame制作的太空猫捉太空鼠小游戏"""
import pygame
from pygame.locals import *
from random import randint,choice
import time
pygame.init()
screenWidth,screenHeight=480,360
screen = pygame.display.set_mode((screenWidth,screenHeight))
pygame.display.set_caption("pygame太空猫捉太空鼠小游戏_作者:李兴球")
class Rat():
def __init__(self,leftImage,rightImage):
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.rect = self.image.get_rect()
self.rect.x = randint(80, screenWidth-80)
self.rect.y = randint(80,screenHeight-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>=screenWidth:
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>=screenHeight:
self.yspeed = -self.yspeed
def collide(self,cat):
return self.rect.colliderect(cat.rect)
def draw(self):
screen.blit(self.image,self.rect)
class Cat():
def __init__(self,leftImage,rightImage):
self.images = [leftImage,rightImage] #左图与右图
self.imageIndex = 1 #开始时面向右的方向
self.image = self.images[self.imageIndex] #确定使用哪个图形
self.xspeed = 0 #0左图对应-1往左移动,1右图对应1往右移动
self.yspeed = 0
self.rect = self.image.get_rect()
self.rect.x = screenWidth//2 - self.rect.width//2
self.rect.y = screenHeight//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]
screen.blit(self.image,self.rect)
def play():
pygame.mixer.music.load("Cave.wav")
pygame.mixer.music.play(-1,0)
def main():
老鼠音 = 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(鼠左图像,鼠右图像))
猫左图像 = pygame.image.load("太空猫_左.png")
猫右图像 = pygame.image.load("太空猫_右.png")
cat = Cat(猫左图像,猫右图像)
clock = pygame.time.Clock()
运行中 = True
while 运行中:
if randint(0,100) ==0 : rats.append(Rat(鼠左图像,鼠右图像))
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
screen.blit(背景图,(0,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()
rat.draw()
else:
rats.remove(rat)
老鼠音.play()
cat.move()
cat.draw()
pygame.display.update()
pygame.display.set_caption("太空猫捉太空鼠" + str(rat.rect) + str(cat.yspeed) + str(cat.xspeed))
clock.tick(60)
pygame.quit()
if __name__ == "__main__":
pygame.mixer.init()
play()
main()