我有一个游戏,如果乌龟进入另一只乌龟的距离小于40,它将改变乌龟的背景。对我来说似乎没有任何问题,但这会导致游戏崩溃。
def drag(x, y):
default.ondrag(None) # disable handler inside handler
default.goto(x, y)
if default.distance(scar) < 40:
default.shape('defaultscar.gif')
scar.hideturtle()
if default.distance(mini) < 40:
wn.bigpic('TrumpTowers25.gif')
default.ondrag(drag)
如果需要,这是完整的代码:
from turtle import Screen, Turtle
def get_mouse_click_coor(x, y):
print(x, y)
def drag(x, y):
default.ondrag(None) # disable handler inside handler
default.goto(x, y)
if default.distance(scar) < 40:
default.shape('defaultscar.gif')
scar.hideturtle()
if default.distance(mini) < 40:
wn.bigpic('TrumpTowers25.gif')
default.ondrag(drag)
wn = Screen()
wn.setup(500, 500)
wn.bgpic('TrumpTowers.gif')
wn.register_shape('default.gif')
wn.register_shape('scar.gif')
wn.register_shape('defaultscar.gif')
wn.register_shape('mini.gif')
wn.register_shape('defaultgliding.gif')
scar = Turtle('scar.gif', visible=False)
scar.shapesize(4)
scar.color('pink')
scar.penup()
scar.left(90)
scar.forward(50)
scar.showturtle()
mini = Turtle('mini.gif', visible=False)
mini.shapesize(4)
mini.color('pink')
mini.penup()
mini.forward(60)
mini.showturtle()
default = Turtle('default.gif', visible=False)
default.shapesize(2)
default.speed(1)
default.penup()
default.left(90)
default.backward(50)
default.showturtle()
default.ondrag(drag)
banshee = Turtle('defaultgliding.gif', visible=False)
banshee.shapesize(2)
banshee.speed(-1)
banshee.penup()
banshee.left(90)
banshee.forward(200)
banshee.left(90)
banshee.foward(200)
banshee.showturtle()
wn.mainloop()