简体   繁体   English

乌龟的定义对我来说不错,但不起作用

[英]turtle definition looks ok to me but doesn't work

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: 首先,您在未真正使用的乌龟上调用hideturtle()

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: speed()方法无法识别参数-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: 最后,在您可能遇到问题的地方,包括我为您编写drag(x, y)的定义,但是到了设置处理程序的时候,您设置了错误的函数:

default.ondrag(default.goto)

it should be: 它应该是:

default.ondrag(drag)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM