我正在尝试寻找乌龟的位置,该位置由用户使用wasd键控制。我注意到的是,在while (True)
块内的打印语句中,乌龟的位置似乎从未改变。你们有什么建议(以下相关代码供参考)?
注意:我知道while(True)
是错误的形式;这只是一个峰值。
import turtle
def Setupcontrols(turtle, window):
window.onkey(lambda: turtle.sety(turtle.ycor()+15), 'w')
window.onkey(lambda: turtle.setx(turtle.xcor()-15), 'a')
window.onkey(lambda: turtle.setx(turtle.xcor()+15), 'd')
window.onkey(lambda: turtle.sety(turtle.ycor()-15), 's')
window.listen()
def Setupuser(myTurtle,window):
window.bgcolor("white")
window.setup (width=400, height=400, startx=0, starty=0)
myTurtle.speed(2)
myTurtle.shape('turtle')
myTurtle.color("blue")
myTurtle.penup()
myTurtle.delay(0)
myTurtle.left(90)
window.exitonclick()
def main():
wn=turtle.Screen()
Gameturtle=turtle.Turtle()
Setupuser(Gameturtle, wn)
Setupcontrols(Gameturtle, wn)
while (True):
print(Gameturtle.position())
main()
我相信您只能将功能用于onkey命令。示例:
import turtle
t = turtle.Turtle()
screen = turtle.Screen()
def moveForward():
t.forward(1)
screen.onkey(moveForward,'right')
screen.listen()
希望这会有所帮助!