使用箭头键移动后,乌龟的位置不会改变

问题描述 投票:0回答:1

我正在尝试寻找乌龟的位置,该位置由用户使用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()
python-2.7 turtle-graphics keyboard-events
1个回答
0
投票

我相信您只能将功能用于onkey命令。示例:

import turtle
t = turtle.Turtle()
screen = turtle.Screen()

def moveForward():
   t.forward(1)

screen.onkey(moveForward,'right')
screen.listen()

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.