|
|
# 左半部分
pygame.draw.circle(
win, self.color, (center_x - 5, center_y), self.radius - 5)
pygame.draw.circle(
win, BLACK, (center_x - 5, center_y), self.radius - 5, 1)
# 右半部分
pygame.draw.circle(
win, self.color, (center_x + 5, center_y), self.radius - 5)
pygame.draw.circle(
win, BLACK, (center_x + 5, center_y), self.radius - 5, 1)
# 中间的线
pygame.draw.line(win, BLACK,
(center_x - self.radius//2, center_y),
(center_x + self.radius//2, center_y), 2)
def check_slice(self, start_pos, end_pos):
if self.sliced:
return False
# 检查线段(鼠标轨迹)是否与圆(水果)相交
x1, y1 = start_pos
x2, y2 = end_pos
x0, y0 = self.x, self.y
# 线段向量
dx = x2 - x1
dy = y2 - y1
# 线段长度的平方
length_sq = dx*dx + dy*dy
# 计算投影比例
if length_sq == 0: # 鼠标没移动
return False
t = ((x0 - x1)*dx + (y0 - y1)*dy) / length_sq
t = max(0, min(1, t)) # 限制在0-1之间
# 线段上最近的点
closest_x = x1 + t*dx
closest_y = y1 + t*dy
# 计算最近点到圆心的距离
distance = math.sqrt((closest_x - x0)**2 + (closest_y - y0) ** 2)
return distance <= self.radius
|
|