我正在做一个打鼹鼠的游戏,当用户点击鼹鼠图像时会得到一分。我不知道怎样才能做到这一点

2024-06-23 18:38:50 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我把这个打鼹鼠的游戏。我可以让鼹鼠出现在游戏中的随机位置,但是我不知道当玩家点击鼹鼠时如何给他一分。由于鼹鼠随机出现,它可能在同一位置出现两次。我怎样才能阻止这一切的发生。代码如下:

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


Tags: runimageimporteventifcountdisplaypygame
1条回答
网友
1楼 · 发布于 2024-06-23 18:38:50

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

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像素范围内,就成功了

有一个计算点之间直线距离的公式,称为Euclidian Distance。获得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占用。甚至还有一个pre-existing function用于它:pygame.Rect.collidepoint()。但这是留给读者的练习

相关问题 更多 >

    热门问题