如何检测鼠标是否在圆圈上?

时间:2021-04-13 08:08:06

标签: ruby libgosu

我在窗口上画了一个圆,如何定义它的边界?到目前为止,我可以将它放在一个不可见的矩形中,所以我检测到了鼠标,但这不是我想要的

CIRCLE_STEP = 10

def draw_circle(cx,cy,r,color)
    0.step(360, CIRCLE_STEP) do |a1|
    a2 = a1 + CIRCLE_STEP
    $window.draw_line cx + Gosu.offset_x(a1, r), cy + Gosu.offset_y(a1, r), color, cx + Gosu.offset_x(a2, r), cy + Gosu.offset_y(a2, r), color, 10
    end
  end

def update
   if mouse_over_button($window.mouse_x, $window.mouse_y, 180)
     @color = Gosu::Color::GREEN
   else
     @color = Gosu::Color.argb(255, 240, 232, 196) 
   end
 end

def mouse_over_button(mouse_x, mouse_y, shift)
    mouse_x.between?(get_rect_width, get_rect_width + shift) && mouse_y.between?(get_rect_height, get_rect_height + shift)
  end

 def get_rect_width()
   $window.width / 2  
 end
 
 def get_rect_height()
   $window.height / 2 + 200  
 end

还有其他更有效的方法吗?

1 个答案:

答案 0 :(得分:1)

使用圆的屏幕位置的中心,然后获取鼠标的屏幕位置。然后按圆半径偏移。

Gosu::Window#mouse_

my = Gosu::Window#mouse_x
mx = Gosu::Window#mouse_y

if (my - circle_y).abs + (mx - circle_x).abs < circle_radius
  # mouse is over circle
end
相关问题