2015-04-23 103 views
2
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很陌生。如何检查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() 
+0

可以粘贴您的矩形代码? – arivero

+0

向我们展示您的矩形命中代码以及您尝试过的圈子。 – msw

回答

2

使用距离公式:

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

################################################################################ 
################################################################################ 
################################################################################ 
+0

谢谢,这表现很好 –

+0

@RomanFormicola我实际上在鼠标索引时发生了错误。现在看。 –

0

,你可以品尝这样 detect click on shape pygame 像素以其他方式使用毕达哥拉斯摆脱中心的距离。

由于马利克表示,毕达哥拉斯行之有效的圈子,但是对于一般的纯色形状,你可以这样做:

if event.type == pygame.MOUSEBUTTONDOWN: 
    click = gameDisplay.get_at(pygame.mouse.get_pos()) == cyan 

    if click == 1: 
     print 'CLICKED!' 
相关问题