如何在Pygame中检查鼠标单击是否在圆形内?

4
import pygame

pygame.init()

white = 255,255,255
cyan = 0,255,255

gameDisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption('Circle Click Test')

stop = False

while not stop:
    gameDisplay.fill(white)

    pygame.draw.circle(gameDisplay,cyan,(400,300),(100))

    for event in pygame.event.get():

        if event.type == pygame.MOUSEBUTTONDOWN:
            ####################################################  

        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    pygame.display.update()

我在屏幕上有一个圆形,我想检查用户是否在圆形内点击。我知道如何用矩形做到这一点,我认为用圆形会类似。非常感谢任何帮助,我对pygame非常新手。

以下是我用于矩形的代码:

import pygame

pygame.init()

white = 255,255,255
cyan = 0,255,255

gameDisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption('Circle Click Test')

rectangle = pygame.Rect(400,300,200,200)

stop = False

while not stop:
    gameDisplay.fill(white)

    pygame.draw.rect(gameDisplay, cyan,rectangle,4)

    for event in pygame.event.get():

        if event.type == pygame.MOUSEBUTTONDOWN:
            click = rectangle.collidepoint(pygame.mouse.get_pos())

            if click == 1:
                print 'CLICKED!'

        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    pygame.display.update()

你能贴上你的矩形代码吗? - arivero
展示一下你的矩形碰撞代码以及你对圆形试验所做的工作。 - msw
2个回答

3

使用距离公式:

################################################################################
# Imports ######################################################################
################################################################################

from pygame.locals import *
import pygame, sys, math

################################################################################
# Screen Setup #################################################################
################################################################################

pygame.init()
scr = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Box Test')

################################################################################
# Game Loop ####################################################################
################################################################################

while True:
    pygame.display.update(); scr.fill((200, 200, 255))
    pygame.draw.circle(scr, (0, 0, 0), (400, 300), 100)

    x = pygame.mouse.get_pos()[0]
    y = pygame.mouse.get_pos()[1]

    sqx = (x - 400)**2
    sqy = (y - 300)**2

    if math.sqrt(sqx + sqy) < 100:
        print 'inside'

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

################################################################################
################################################################################
################################################################################

@RomanFormicola 我实际上在鼠标索引方面犯了一个错误。现在看看。 - Malik Brahimi

1
你可以像这样取样像素 检测pygame上的形状点击 否则,使用勾股定理获取到中心的距离。
正如Malik所示,勾股定理对于圆形非常有效,但对于一般的纯色形状,您可以这样做:
if event.type == pygame.MOUSEBUTTONDOWN:
  click = gameDisplay.get_at(pygame.mouse.get_pos()) == cyan

  if click == 1:
      print 'CLICKED!'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接