我目前正在尝试数字化我发明的棋盘游戏(回复:https://github.com/zutn/King_of_the_Hill)。为了使其工作,我需要检查是否已单击此board上的其中一个图块(弧)。到目前为止,我没有放弃pygame.arc函数进行绘图。如果我使用点击位置的x,y位置,我无法想出一条出路来确定要比较的弧的确切轮廓。我想过使用颜色检查,但这只会告诉我是否有任何瓷砖被点击。那么有没有一种方便的方法来测试是否在pygame中点击了弧线,还是我必须使用精灵或完全不同的东西?此外,在稍后的步骤中,将包括位于瓷砖上的单元。这将使得具有角度计算的解决方案在下面更加困难。
答案 0 :(得分:1)
这是一个简单的弧类,它将检测弧中是否包含一个点,但它只适用于圆弧。
import pygame
from pygame.locals import *
import sys
from math import atan2, pi
class CircularArc:
def __init__(self, color, center, radius, start_angle, stop_angle, width=1):
self.color = color
self.x = center[0] # center x position
self.y = center[1] # center y position
self.rect = [self.x - radius, self.y - radius, radius*2, radius*2]
self.radius = radius
self.start_angle = start_angle
self.stop_angle = stop_angle
self.width = width
def draw(self, canvas):
pygame.draw.arc(canvas, self.color, self.rect, self.start_angle, self.stop_angle, self.width)
def contains(self, x, y):
dx = x - self.x # x distance
dy = y - self.y # y distance
greater_than_outside_radius = dx*dx + dy*dy >= self.radius*self.radius
less_than_inside_radius = dx*dx + dy*dy <= (self.radius- self.width)*(self.radius- self.width)
# Quickly check if the distance is within the right range
if greater_than_outside_radius or less_than_inside_radius:
return False
rads = atan2(-dy, dx) # Grab the angle
# convert the angle to match up with pygame format. Negative angles don't work with pygame.draw.arc
if rads < 0:
rads = 2 * pi + rads
# Check if the angle is within the arc start and stop angles
return self.start_angle <= rads <= self.stop_angle
以下是该类的一些示例用法。使用它需要一个中心点和半径而不是一个矩形来创建弧。
pygame.init()
black = ( 0, 0, 0)
width = 800
height = 800
screen = pygame.display.set_mode((width, height))
distance = 100
tile_num = 4
ring_width = 20
arc = CircularArc((255, 255, 255), [width/2, height/2], 100, tile_num*(2*pi/7), (tile_num*(2*pi/7))+2*pi/7, int(ring_width*0.5))
while True:
fill_color = black
for event in pygame.event.get():
# quit if the quit button was pressed
if event.type == pygame.QUIT:
pygame.quit(); sys.exit()
x, y = pygame.mouse.get_pos()
# Change color when the mouse touches
if arc.contains(x, y):
fill_color = (200, 0, 0)
screen.fill(fill_color)
arc.draw(screen)
# screen.blit(debug, (0, 0))
pygame.display.update()