pygame打地鼠小游戏

"""pygame打地鼠小游戏,这是有游戏封面与配音的一个版本,作者:李兴球,风火轮少儿编程 ,www.scratch8.net。在游戏中新建了锤子类和地鼠类。在游戏中用了少许中文变量,方便初学者理解程序。"""

import pygame
from pygame.locals import *
from random import randint

class Hamster():
    def __init__(self,x,y,w,h,image0,image1):
        self.images = [image0,image1]
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.status = randint(0,1)     # 状态,隐藏或显示,image0表示隐藏
        self.rect = pygame.Rect(self.x,self.y,self.w,self.h)
        
    def show(self):
        self.status = 1
        
    def hide(self):
        self.status= 0
        
    def draw(self):
        screen.blit(self.images[self.status],(self.x,self.y))
        
    def collide(self,hammer):
        """地鼠和锤子的矩形重叠"""
        return self.rect.colliderect(hammer.rect) and self.status == 1

class Hammer():
    def __init__(self,x,y,w,h,image0,image1):
        self.images = [image0,image1]
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.status = 0                 # 表示没敲下的状态
         
    def setpos(self,x,y):
        self.x = x
        self.y = y
        self.rect = pygame.Rect(self.x,self.y,self.w,self.h)# 由于锤子跟着鼠标移动,所以它的rect属性要不断重设
        
    def 切换造型(self,no):
        self.status = no
        
    def draw(self):
        screen.blit(self.images[self.status],(self.x,self.y))

print("主程序开始...")        
pygame.init()
屏幕宽度=480
屏幕高度=360
screen = pygame.display.set_mode((屏幕宽度,屏幕高度))
pygame.display.set_caption("打地鼠小游戏_作者:李兴球_风火轮少儿编程_www.scratch8.net")

pygame.mixer.init()
HandClap = pygame.mixer.Sound("HandClap.wav")    # 实例化击打音效
pygame.mixer.music.load("My Musicfmusic1.wav")   # 播放动听的背景音乐
pygame.mixer.music.play(-1,0)

hamster0 = pygame.image.load("地鼠隐藏.png")      
hamster1 = pygame.image.load("地鼠显示.png") 
hammer0 = pygame.image.load("锤子-没敲.png")
hammer1 = pygame.image.load("锤子-敲下.png")

锤子  = Hammer(0,0,80,80,hammer0,hammer1)            
篮子=[Hamster(10,10,80,80,hamster0,hamster1),Hamster(90,109,80,80,hamster0,hamster1),Hamster(290,222,80,80,hamster0,hamster1)]
篮子.append(Hamster(310,59,80,80,hamster0,hamster1))
篮子.append(Hamster(50,234,80,80,hamster0,hamster1))

print("显示封面",KEYDOWN)
封面图像= pygame.image.load("封面.png")
退出循环=False
while not 退出循环:    
    for event in pygame.event.get(): 
        if event.type==QUIT:
            pygame.quit()
        if event.type==KEYDOWN:
            print(event.key,pygame.K_SPACE)
            if event.key == pygame.K_SPACE:退出循环=True
    screen.blit(封面图像,(0,0))
    pygame.display.update()

print("进入打地鼠游戏中...")
clock = pygame.time.Clock()
击=False
while True:
    "遍历每一个事件"
    for event in pygame.event.get():        
        if event.type==QUIT:
            pygame.quit()
        if event.type==MOUSEBUTTONDOWN:
            击=True
        if event.type==MOUSEBUTTONUP:
            击=False
     
    screen.fill((0,0,0))    
    
    for 地鼠 in 篮子:
        if randint(0,40)==0:
            
            if randint(0,1000)>600:
                地鼠.show()
            else:
                地鼠.hide()
        地鼠.draw()
        if 击 == True:            
            锤子.切换造型(1)
            if  地鼠.collide(锤子):
                地鼠.hide()
                HandClap.play()
                print("击中")      # 加个计数器,数量到达一定的时候显示Game Over,你能完成吗?
        else:
            锤子.切换造型(0)
    mpos = pygame.mouse.get_pos()
    锤子.setpos(mpos[0],mpos[1])   # 设置到鼠标的位置
    锤子.draw()
    pygame.display.update()
    clock.tick(30)