如何使用 Gosu Ruby 检测鼠标位置是否超过圆圈位置?
How can I detect if mouse position is over the circle position using Gosu Ruby?
我在 window 上画了一个圆,如何定义它的边界?到目前为止,我可以将它放在一个不可见的矩形中,这样我就可以检测到鼠标,但这不是我想要的
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
还有其他更有效的方法吗?
使用圆的屏幕中心位置,然后获取鼠标的屏幕位置。然后偏移圆半径。
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
我在 window 上画了一个圆,如何定义它的边界?到目前为止,我可以将它放在一个不可见的矩形中,这样我就可以检测到鼠标,但这不是我想要的
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
还有其他更有效的方法吗?
使用圆的屏幕中心位置,然后获取鼠标的屏幕位置。然后偏移圆半径。
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