Python龟gam中的碰撞检测

2024-05-15 15:03:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个红海龟追蓝海龟的Python游戏。当红海龟抓到蓝海龟时,我想让它在屏幕上说“碰撞”,但它不起作用。当它碰撞的时候,什么都没有发生,它给了我一个错误‘Turtle’对象是不可调用的’。

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()

Tags: rundefscreencolorrunnerspeed海龟turtle
3条回答

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

现在,假设turtle1的“半径”是r1,turtle2的半径是r2,如果距离小于这两个半径的和,我们可以通过chenking检查碰撞。

所以我想检查一下就足够了 如果(r1+r2)<;=turtle1.距离(turtle2.pos())

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

要检查与给定海龟、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添加到全局点,然后,如果需要,在发生碰撞时返回True,否则返回False。

这样跟随者就可以试图超过一群海龟。希望这能有所帮助。

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

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()方法添加到现有类中。没有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

相关问题 更多 >