我正在尝试制作一个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()
答案 0 :(得分:2)
这段代码似乎比实际编程更加一厢情愿:
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()
方法。您不能简单地使用此is_collided_with()
语句向现有类添加def
方法。没有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作为碰撞半径,您可以根据需要进行调整。这段代码只是结束游戏,带有消息,当发生碰撞时,你可能想要做一些更复杂的事情。您可以考虑在每次击键后使碰撞逻辑成为自己的功能,以防转轮意外撞击跟随者!
答案 1 :(得分:1)
我们在Turtle中有一个距离函数,所以让我们说turtle1在x1,y1,而turtle2在x2,y2,那么距离将被计算为这两个点之间的数学xy距离。
现在,让我们说turtle1有一个&#34; radius&#34;如果距离小于这两个半径的总和,则r1和turtle2的半径为r2,我们可以通过chencking来检查碰撞。
所以我认为检查就足够了 if(r1 + r2)&lt; = 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添加到全局点,然后,如果需要,如果发生碰撞则返回True,否则返回False。
这样,追随者可能会试图超越一群海龟。希望这可以有所帮助。
答案 2 :(得分:0)
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
答案 3 :(得分:0)
这是我能想到的最简单的方法。只需使用.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")
答案 4 :(得分:0)
jonny = Turtle()
marie = Turtle()
if (jonny.distance(marie) < 15):
print('Jonny punch marie')