14 步:使用距离公式,我们可以计算两个精灵之间的距离。这个公式使用我们的 delta_y(上升)和 delta_x(运行)来计算直线距离。

距离公式基于勾股定理(a 2 + b 2 = c 2 ),因此求解 c 将为我们提供距离。

  • ,将Distance Formula拖到代码底部。
  • 现在我们知道 Emma 和 Michael 的距离有多远,如果他们离得太近,我们可以做点什么!

To navigate the page using the TAB key, first press ESC to exit the code editor.

stage.set_background("schoolentrance") stage.wait(2) stage.create_grid_overlay(50, "blue") stage.set_background_color("azure") emma = codesters.Sprite("person1") emma.set_size(0.5) michael = codesters.Sprite("person2") michael.set_size(0.5) emma_x = random.randint(-230,230) emma_y = random.randint(-230,230) emma.go_to(emma_x, emma_y) michael_x = random.randint(-230,230) michael_y = random.randint(-230,230) michael.go_to(michael_x, michael_y) delta_x = emma_x - michael_x delta_y = emma_y - michael_y
  • Run Code
  • 提交作品
  • 下个活动
  • Show Console
  • Reset Code Editor
  • Codesters How To (opens in a new tab)