Currently, I'm trying to make a game and in the game I would like it so if the character is on top of an object, it picks it up. This is what I have so far:
import turtle
import time
default = turtle.clone()
scar = turtle.clone()
wn = turtle.Screen()
wn.setup(500,500)
wn.bgpic('TrumpTowers.gif')
wn.register_shape('default.gif')
wn.register_shape('scar.gif')
wn.register_shape('defaultscar.gif')
def drag(x, y):
default.ondrag(None) # disable handler inside handler
default.goto(x, y)
if default.distance(scar) < 40:
default.shape('defaultscar.gif')
elif default.shape() == 'turtle':
default.shape('circle')
default.ondrag(drag)
turtle.hideturtle()
default.shape('default.gif')
scar.shape('scar.gif')
default.pu()
default.left(90)
default.bk(35)
scar.pu()
scar.left(90)
scar.fd(45)
scar.speed(-1)
default.ondrag(default.goto)
Does anybody know how I would go with fixing this as it looks ok to me, but doesn't actually work!
I see three issues in your code. First, you're calling hideturtle()
on a turtle you're not really using:
turtle.hideturtle()
This is being applied to the default turtle which shouldn't be in play. It only shows up, and gets in the way, because you do:
default = turtle.clone()
scar = turtle.clone()
Instead of:
default = turtle.Turtle()
scar = turtle.Turtle()
The speed()
method does not recognize an argument of -1:
scar.speed(-1)
It will set it to a different value if the argument isn't valid.
Finally, where you're likely having the problem you're asking about, you include the definition of drag(x, y)
I wrote for you but when it comes time to set a handler, you set the wrong function:
default.ondrag(default.goto)
it should be:
default.ondrag(drag)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.