在 Python 乌龟游戏中检测碰撞 [英] Detecting collision in Python turtle game

查看:76
本文介绍了在 Python 乌龟游戏中检测碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个红海龟追逐蓝海龟的 Python 游戏.当红海龟抓住蓝海龟时,我希望它在屏幕上显示碰撞"但它不起作用.当它碰撞时,什么也没有发生,它给了我一个错误海龟"对象不可调用.

I am trying to make a Python game where the red turtle chases the blue turtle. When the red turtle catches the blue turtle, I want it to say 'COLLISION' on the screen but it is not working. When it collides, nothing happens and it gives me an error '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()

推荐答案

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

This code seems to be more wishful thinking than actual programming:

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() 函数.这个碰撞测试只会在你每次运动后需要的时候执行一次.让我们尝试挽救我们所能做的事情并使其发挥作用:

Turtles don't have a .rect() method. You can't simply add a is_collided_with() method to an existing class with this def statement. There are no run() and follow() functions. This collision test would only be executed once when you need it after every motion. Let's try to salvage what we can and make this work:

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 作为基于海龟光标大小的碰撞半径,您可以根据需要进行调整.这段代码只是简单地结束游戏,并显示一条消息,当发生碰撞时,您可能想要做一些更复杂的事情.您可以考虑让碰撞逻辑成为自己的函数,以便在每次击键后使用,以防跑步者不小心撞到跟随者!

I use 10 as a collision radius based on the size of a turtle cursor, you can adjust as you see fit. This code simply ends the game, with a message, when a collision occurs, you might want to do something more sophisticated. You could consider making the collision logic its own function to use after each keystroke in case the runner accidentally rams the follower!

这篇关于在 Python 乌龟游戏中检测碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆