Python海龟游戏中的碰撞检测

2024-04-20 05:11:46 发布

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

我正在尝试制作一个Python游戏,其中红海龟追逐蓝海龟。当红海龟抓住蓝海龟时,我希望它在屏幕上显示“碰撞”,但它不工作。当它发生碰撞时,什么也不会发生,它会给我一个错误“海龟”对象是不可调用的

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条回答
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

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

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

所以我想检查一下就足够了 如果(r1+r2)<=turtle1.距离(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添加到全局点,然后,如果需要,如果发生碰撞,则返回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作为基于海龟光标大小的碰撞半径,您可以根据需要进行调整。这段代码只是以一条消息结束游戏,当发生碰撞时,您可能想做一些更复杂的事情。你可以考虑在每个击键之后使用碰撞逻辑来使用它,以防流氓意外地欺负跟随者!p>

相关问题 更多 >