-1

我已经发布了一个与此类似的问题,但仍然不知道我应该采取哪些步骤。我试图让计算机检测正方形是否接触圆圈。

我已经看到一些可能有效但无法将其实现到我的脚本中的代码。请在下面提供我编辑的代码,谢谢!

编辑:我试过放一个返回命令,但它仍然不起作用,我在下面发布了我的整个脚本以提供尽可能多的信息。再次感谢!

require 'ruby2d'
set title: 'Merdeka', background: 'olive'
set width: 600, height: 500
s = Square.new(
  x: @x, y: @y,
  size: 25,
  color: 'navy',
  z: 10
)
a = Circle.new(
  x: rand(600), y: rand(500),
  radius: 11,
  sectors: 32,
  color: 'maroon',
  z: 10
)

@x_speed = 0
@y_speed = 0
on :key_down do |event|
  if event.key == 'up'
    @x_speed = 0
    @y_speed = -2
  elsif event.key == 'right'
    @x_speed = 2
    @y_speed = 0
  elsif event.key == 'down'
    @x_speed = 0
    @y_speed = 2
  elsif event.key == 'left'
    @x_speed = -2
    @y_speed = 0
  end
end

update do
  s.x += @x_speed
  s.y += @y_speed
end
def hit(a, s)
  return a.x == s.x && a.y == s.y
end
if hit(a, s)
a.remove
a.add
end
show
4

1 回答 1

3
def hit(a, s)
  return a.contains?(s.x + a.radius ,s.y +a.radius)
end

这会让你靠近,三角形有倾斜的边缘,所以如果你想非常准确,你需要应用一些数学。

于 2019-09-20T20:22:08.563 回答