【发布时间】:2021-08-26 13:04:35
【问题描述】:
我正在尝试制作一个红海龟追逐蓝海龟的 Python 游戏。当红海龟抓住蓝海龟时,我想让它在屏幕上说“碰撞”,但它不起作用。当它发生碰撞时,什么也没有发生,它给了我一个错误'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()
【问题讨论】:
-
或许你可以尝试检查
run的位置是否与follow匹配? -
我在那段代码中没有看到任何关于 pygame.如果你正在使用 pygame,为什么不使用它的 sprite 功能,让 pygame 检测碰撞?
标签: python python-3.x turtle-graphics