首页 > 解决方案 > 我正在制作一个 Whack a Mole 游戏,当用户单击鼹鼠图像时,他会得到一个点。我无法弄清楚如何做到这一点

问题描述

所以我正在把这个重击变成一个鼹鼠游戏。我可以让鼹鼠出现在游戏中的随机位置,但是我不明白当玩家点击鼹鼠时如何奖励他一分。由于痣是随机出现的,它可能会在同一位置出现两次。我将如何阻止这种情况发生。这是代码:

import pygame
from pygame.locals import MOUSEBUTTONDOWN
import random
import time

pygame.init()

# constants
width = 300
height = 300
z = 2
Radius = 21

# loading mole image
mole_image = pygame.image.load("mole image2.png")
modified_image = pygame.transform.scale(mole_image, (40, 40))

# burrow and mole positions
burrow_x = -50
burrow_y = 50
count = int(0)
burrow_positions_list = []
mole_positions_list = []
while count != 9:
    count += 1
    burrow_x += 100
    if burrow_x == 350:
        burrow_x -= 300
        burrow_y += 100
    tuple1 = (burrow_x, burrow_y)
    tuple2 = (burrow_x - 20, burrow_y - 20)
    burrow_positions_list.append(tuple1)
    mole_positions_list.append(tuple2)

# colours
white = (255, 255, 255)
blue = (0, 0, 255)
black = (0, 0, 0)

# setting up the display
display = pygame.display.set_mode((width, height))
pygame.display.set_caption("Whack A Mole")


# creating burrows for the moles
def Burrows():
    circle_count = int(-1)
    while circle_count != len(burrow_positions_list) - 1:
        circle_count += 1
        pygame.draw.circle(display, black, burrow_positions_list[circle_count], 30)


def Moles():
    display.blit(modified_image, random.choice(mole_positions_list))
    time.sleep(z)


# running pygame until quit
run = True
while run:
    # speeding up mole blitting
    z -= 0.05
    if z < 0.4:
        z += 0.05
    display.fill(white)
    Burrows()
    Moles()
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()


pygame.quit()

标签: pythonpygame

解决方案


您已经有了洞穴位置列表:

burrow_positions_list[]

在每个圆上绘制一个半径为 30 的圆。首先让我们将 30 转换为常数。

BURROW_RADIUS = WINDOW_WIDTH // 10  # scales with window size, default 30

您已经知道单击鼠标时的位置:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        run = False
    elif event.type == MOUSEBUTTONDOWN:
        pos = pygame.mouse.get_pos()          # <-- HERE

所以现在我们需要将两者结合在一起。我们知道洞穴中心在哪里,所以如果鼠标点击在BURROW_RADIUS这个点的像素范围内,那就是命中。

有一个公式可以计算点之间的线距离,称为欧几里得距离。对于 2 点,这很容易:

def twoPointDistance( point_a, point_b ):
    x1, y1 = point_a
    x2, y2 = point_b
    x_squared = (x2 - x1) * (x2 - x1)
    y_squared = (y2 - y1) * (y2 - y1)
    length = math.sqrt( x_squared + y_squared )
    return length

所以现在我们有两个点 -burrow_pointmouse_click_point,以及一种确定距离的方法。所以当点击发生时,我们只需要看看它是否足够接近。

elif event.type == MOUSEBUTTONDOWN:
    mouse_click_point = pygame.mouse.get_pos()          # Mouse was clicked
    # Loop through every burrow point, checking the distance
    for i, burrow_point in enumerate( burrow_positions_list ):
        if ( twoPointDistance( mouse_click_point, burrow_point ) < BURROW_RADIUS ):
            # Burrow was clicked
            print( "Burrow %d was clicked" % ( i ) )

就是这样。

然而...

预先计算每个洞穴周围的边界正方形,并且只检查点是否在这个边界内(这只是几个简单的</ >检查)而不是平方根的复杂数学运算,这将大大减少 CPU 密集度。它甚至还有一个预先存在的功能pygame.Rect.collidepoint(). 但这留给读者作为练习!


推荐阅读