Turtle Collision

huangapple go评论64阅读模式
英文:

Turtle Colision

问题

我正在使用turtle模块制作一个游戏,并想要检测原始对象和克隆对象之间的碰撞。我尝试保存克隆对象的位置,然后检查原始对象是否具有相同的位置,但乌龟只有在位置完全相同的情况下才会检测到克隆对象。是否有其他方法可以检测碰撞?

import turtle
import time
import random
points = 0
t = turtle.Turtle()
sc = turtle.Screen()
turtle.bgcolor("black")
t.turtlesize(3)
c = t.clone()
c.goto(200, 200)
c.color("red")
c.turtlesize(1)
t.color("blue")

def turnleft():
    t.left(90)

def turnright():
    t.right(90)

while True:
    t.forward(10)
    time.sleep(0.1)
    sc.onkey(turnleft, "Left")
    sc.onkey(turnright, "Right")
    sc.listen()
    # 这是一个不太有效的碰撞检测器
    if t.distance(c) < 5:
        points = (points + 1)

这是您代码的翻译部分。如果需要更多帮助,请随时告诉我。

英文:

I am making a game using the turtle module, and want to detect colision betwen the original and the clone.I have tried saving the position of the clone, and then checking if the original has the same position, but the turtle only detects the clone if it has the exacly same position. Is there another way to detect colisions?

  import turtle
import time
import random
points = 0
t = turtle.Turtle()
sc = turtle.Screen()
turtle.bgcolor(&quot;black&quot;)
t.turtlesize(3)
c = t.clone()
c.goto(200, 200)
c.color(&quot;red&quot;)
c.turtlesize(1)
t.color(&quot;blue&quot;)
def turnleft():
    t.left(90)
def turnright():
    t.right(90)

while True:
    t.forward(10)
    time.sleep(0.1)
    sc.onkey(turnleft, &quot;Left&quot;)
    sc.onkey(turnright, &quot;Right&quot;)
    sc.listen()
    # here is the no so working collision detector
    if t.pos() == (200, 200):
        points = (points + 1)

答案1

得分: 1

  1. 基于距离的碰撞检测:您可以使用距离方法来计算两只乌龟之间的距离。这非常简单但不太精确。
import turtle
original = turtle.Turtle()
clone = original.clone()

clone.goto(100, 100)
distance = original.distance(clone)
if distance < 20:
    print("检测到碰撞!")
else:
    print("没有碰撞。")
  1. 基于矩形的碰撞检测。使用乌龟的边界框来检测碰撞。
def get_bounds(t):
    x, y = t.pos()
    w, h = t.shapesize()
    w *= 20
    h *= 20
    left = x - w/2
    right = x + w/2
    top = y + h/2
    bottom = y - h/2
    return left, right, top, bottom

def check_collision(t1, t2):
    l1, r1, t1, b1 = get_bounds(t1)
    l2, r2, t2, b2 = get_bounds(t2)
    if r1 >= l2 and l1 <= r2 and t1 >= b2 and b1 <= t2:
        print('检测到碰撞')

请注意,这只是示例代码,我没有充分测试它。我在我的笔记中找到了这段代码。

英文:

I know about 2 ways of detecting collisions:

  1. Distance-based collision detection: You can use the distance method to calculate the distance between two turtles. It's very simple and not verry accurate.
import turtle
original = turtle.Turtle()
clone = original.clone()

clone.goto(100, 100)
distance = original.distance(clone)
if distance &lt; 20:
    print(&quot;Collision detected!&quot;)
else:
    print(&quot;No collision.&quot;)
  1. Rectangle-based collision detection. Use the bounding boxes of the turtles to detect collisions.
def get_bounds(t):
    x, y = t.pos()
    w, h = t.shapesize()
    w *= 20
    h *= 20
    left = x - w/2
    right = x + w/2
    top = y + h/2
    bottom = y - h/2
    return left, right, top, bottom

def check_collision(t1, t2):
    l1, r1, t1, b1 = get_bounds(t1)
    l2, r2, t2, b2 = get_bounds(t2)
    if r1 &gt;= l2 and l1 &lt;= r2 and t1 &gt;= b2 and b1 &lt;= t2:
        print(&#39;collision detected&#39;)

Note that this is a sample code and I didn't test it properly. i found it in my notes.

huangapple
  • 本文由 发表于 2023年2月24日 03:16:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定