Pygame欢乐打地鼠游戏_unit04 用鼠标打地鼠

14 篇文章 0 订阅
14 篇文章 1 订阅
这是一个使用Pygame库创建的打地鼠游戏代码示例。游戏中,地鼠会在随机的地洞位置出现,玩家通过点击鼠标来打击地鼠。当点击到地鼠时,会播放正确音效,否则播放错误音效。游戏还包括了事件监听、地鼠生成、画面绘制等功能。
摘要由CSDN通过智能技术生成

1. 鼠标点击检测原理

在这里插入图片描述

2. 封装checkPoint()函数

def checkPoint(obj, pos):
    top = obj['y']
    bottom = obj['y'] + obj['height']
    left = obj['x']
    right = obj['x'] + obj['width']
    x, y = pos
    if left <= x <= right and top <= y <= bottom:
        return True

3. 改造beatMouse()函数

def beatMouse(type, arg):
    ......
    # 鼠标点击打击
    elif type == 'mouse':
        if checkPoint(mouse, arg):
            state = True
def eventListen():
    ......
    if event.type == pygame.MOUSEBUTTONDOWN:
        pos = pygame.mouse.get_pos()
        beatMouse('mouse', pos)

完整代码

import pygame
import random

pygame.init()

pygame.display.set_caption('打地鼠')
iconImg = pygame.image.load('./images/a.png')
pygame.display.set_icon(iconImg)

screen = pygame.display.set_mode((500, 500))

bgImg = pygame.image.load('./images/背景.png')

# 生成地鼠自定义事件
ADDMOUSE = 101
pygame.time.set_timer(ADDMOUSE, 2000)

# 生成地鼠图片列表
mouseImgList = [chr(i) + '.png' for i in range(97, 123)]
# 加载被打晕的地鼠图片
beatMouseImg = pygame.image.load('./images/被打晕的地鼠.png')

# 加载打击音效
rightMusic = pygame.mixer.Sound('./music/正确.wav')
errorMusic = pygame.mixer.Sound('./music/错误.wav')

# 预设地洞位置
posList = [[100, 200], [240, 200], [380, 200],
           [100, 350], [240, 350], [380, 350]]


# 随机生成地鼠
def createMouse():
    mouseImg = random.choice(mouseImgList)  # a.png
    mouseObj = pygame.image.load('./images/' + mouseImg)
    mouseRect = mouseObj.get_rect()
    mouse = {
            'letter': mouseImg[0],
            'img': mouseObj,
            'x': 0,
            'y': 0,
            'width': mouseRect.width,
            'height': mouseRect.height,
            'state': False
    }
    # 随机获取地洞位置
    x, y = random.choice(posList)
    mouse['x'] = x - mouse['width'] // 2
    mouse['y'] = y - mouse['height'] // 2
    return mouse


clock = pygame.time.Clock()

mouse = createMouse()


def draw():
    screen.blit(bgImg, (0, 0))
    screen.blit(mouse['img'], (mouse['x'], mouse['y']))


def eventListen():
    global mouse
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        if event.type == ADDMOUSE:
            mouse = createMouse()
        if event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            beatMouse('mouse', pos)
        if event.type == pygame.KEYDOWN:
            beatMouse('key', event.key)


def beatMouse(type, arg):
    state = False
    if type == 'key':
        if arg == ord(mouse['letter']):
            state = True
    elif type == 'mouse':
        if checkPoint(mouse, arg):
            state = True
    if state:
        mouse['img'] = beatMouseImg
        mouse['state'] = True
        rightMusic.play()
    else:
        errorMusic.play()


def checkPoint(obj, pos):
    left = obj['x']
    right = obj['x'] + obj['width']
    top = obj['y']
    bottom = obj['y'] + obj['height']
    x, y = pos
    if left <= x <= right and top <= y <= bottom:
        return True


while True:
    clock.tick(60)
    draw()
    eventListen()
    pygame.display.update()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值