在 Python 海龟游戏中检测碰撞

Detecting collision in Python turtle game

我正在尝试制作一个红海龟追蓝海龟的Python游戏。当红海龟抓到蓝海龟时,我想让它在屏幕上说 'COLLISION' 但它不起作用。当它发生碰撞时,什么也没有发生,它给我一个错误 'Turtle' object is not callable'。

from turtle import Turtle, Screen

playGround = Screen()

playGround.screensize(250, 250)
playGround.title("Turtle Keys")

run = Turtle("turtle")
run.speed("fastest")
run.color("blue")
run.penup()
run.setposition(250, 250)

follow = Turtle("turtle")
follow.speed("fastest")
follow.color("red")
follow.penup()
follow.setposition(-250, -250)

def k1():
    run.forward(45)

def k2():
    run.left(45)

def k3():
    run.right(45)

def k4():
    run.backward(45)

def quitThis():
    playGround.bye()

def follow_runner():
    follow.setheading(follow.towards(run))
    follow.forward(8)
    playGround.ontimer(follow_runner, 10)

playGround.onkey(k1, "Up")  # the up arrow key
playGround.onkey(k2, "Left")  # the left arrow key
playGround.onkey(k3, "Right")  # you get it!
playGround.onkey(k4, "Down")

playGround.listen()

follow_runner()

def is_collided_with(self, run):
    return self.rect.colliderect(run.rect)

runner = run(10, 10, 'my_run')
follower = follow(20, 10)
if follow.is_collided_with(run):
    print 'collision!'

 playGround.mainloop()

这段代码似乎比实际编程更像是一厢情愿:

def is_collided_with(self, run):
    return self.rect.colliderect(run.rect)

runner = run(10, 10, 'my_run')
follower = follow(20, 10)
if follow.is_collided_with(run):
    print 'collision!'

海龟没有 .rect() 方法。您不能使用此 def 语句简单地将 is_collided_with() 方法添加到现有 class。没有 run()follow() 函数。此碰撞测试只会在每次运动后需要时执行一次。让我们尽我们所能挽救并使这项工作成功:

from turtle import Turtle, Screen

playGround = Screen()

playGround.screensize(250, 250)
playGround.title("Turtle Keys")

run = Turtle("turtle")
run.color("blue")
run.penup()
run.setposition(250, 250)

follow = Turtle("turtle")
follow.color("red")
follow.penup()
follow.setposition(-250, -250)

def k1():
    run.forward(45)

def k2():
    run.left(45)

def k3():
    run.right(45)

def k4():
    run.backward(45)

def quitThis():
    playGround.bye()

def is_collided_with(a, b):
    return abs(a.xcor() - b.xcor()) < 10 and abs(a.ycor() - b.ycor()) < 10

def follow_runner():
    follow.setheading(follow.towards(run))
    follow.forward(min(follow.distance(run), 8))

    if is_collided_with(follow, run):
        print('Collision!')
        quitThis()
    else:
        playGround.ontimer(follow_runner, 10)

playGround.onkey(k1, "Up")  # the up arrow key
playGround.onkey(k2, "Left")  # the left arrow key
playGround.onkey(k3, "Right")  # you get it!
playGround.onkey(k4, "Down")

playGround.listen()

follow_runner()

playGround.mainloop()

我根据海龟游标的大小使用10作为碰撞半径,大家可以根据自己的喜好调整。这段代码简单地结束了游戏,并带有一条消息,当发生碰撞时,您可能想要做一些更复杂的事情。您可以考虑将碰撞逻辑作为自己的函数,以便在每次击键后使用,以防跑步者不小心撞到追随者!

def isCollision(t1, t2):
    d = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2) + math.pow(t1.ycor()-t2.ycor(),2))
    if d < 20:
        return True
    else:
        return False

我们在 Turtle 中有一个距离函数,假设 turtle1 位于 x1,y1,turtle2 位于 x2,y2,那么距离将计算为这两点之间的数学 xy 距离。

现在,假设 turtle1 的 "radius" 为 r1,turtle2 的半径为 r2,如果距离小于这两个半径的总和,我们可以通过 chencking 检查碰撞。

所以我认为检查一下就足够了 if (r1+r2)<=turtle1.distance(turtle2.pos())

这些半径可以这样设置:turtle1.r=10,turtle2.r=5,或者作为全局变量,r1=10,r2=5。如果最后一个选项适用,请记住 def 中的全局关键字。

为了检查与给定海龟 turtle1 和海龟列表的碰撞,假设 turtles=[jim,ben,kate,jane],并且所有海龟都有一个半径为 r 的字段,您可以迭代它列表,检查 turtle1 是否与它们中的任何一个发生碰撞:

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

现在为最后一个创建函数:

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

这将检测 turtle1 是否与组中的任何海龟发生碰撞,移除 turtle1 与之发生碰撞的海龟,并将全局点加 1,然后,如果需要,return 如果发生碰撞则为真,否则为假.

这样追随者可以尝试超越一群海龟。希望这可以有所帮助。

这是我能想到的最简单的方法。像这样使用 .pos()

from turtle import *

import turtle

screen = turtle.Screen()

screen.setup(1920, 1080)

blue = turtle.Turtle()

blue.shape = ('turtle')

blue.color = ('blue')


red = turtle.Turtle()

red.shape = ('turtle')

red.color = ('red')



collision = turtle.Turtle()

collision.hideturtle()



if blue.pos() == red.pos():

    collision.goto(0,0)

    collision.showturtle()

    collision.write("COLLISION")
jonny = Turtle()
marie = Turtle()


if (jonny.distance(marie) < 15):
 print('Jonny punch marie')