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))

You must log in to read the rest of this article. Please log in or register as a user.