擅长:python、mysql、java
<p>在Turtle中,我们有一个距离的函数,假设turtle1在x1,y1,turtle2在x2,y2,那么这个距离将被计算为这两点之间的数学xy距离。</p>
<p>现在,假设turtle1的“半径”是r1,turtle2的半径是r2,如果距离小于这两个半径的和,我们可以通过chenking检查碰撞。</p>
<p>所以我想检查一下就足够了
<strong>如果(r1+r2)<;=turtle1.距离(turtle2.pos())</strong></p>
<p>这些半径可以设置为:turtle1.r=10,turtle2.r=5,或者作为全局变量,r1=10,r2=5。如果最后一个选项适用,请记住def中的global关键字。</p>
<p>要检查与给定海龟、turtle1和海龟列表的碰撞,请说turtles=[jim,ben,kate,jane],所有海龟都有一个半径r的字段,您可以遍历此列表,以检查turtle1是否与其中任何海龟碰撞:</p>
<pre><code>collsion=False
for turtle in turtles:
if (turtle.r+turtle1.r)<=turtle1.distance(turtle.pos()):
collision=True
# turtle.reset() # removes the turtle
# points+=1, or whatever
</code></pre>
<p>现在来做最后一个函数:</p>
<pre><code>def group_collide(t1,group):
global points
turtle1=t1
collide=False
for turtle in group:
if (turtle.r+turtle1.r)<=turtle1.distance(turtle(pos)):
collide=True
turtle.reset()
points+=1
# if needed:
return collide
</code></pre>
<p>这将检测turtle1是否与组中的任何一个碰撞,移除turtle1碰撞的海龟,并将1添加到全局点,然后,如果需要,在发生碰撞时返回True,否则返回False。</p>
<p>这样跟随者就可以试图超过一群海龟。希望这能有所帮助。</p>