我想知道如何使方形播放器通过箭头键来回移动。我已经尝试过t.setheading()
和t.forward(10)
,但是它们不起作用。
我想我正在使用3.0之前的版本-_-如果无法解决,我可能会重新开始。
我想我正在使用3.0之前的版本
这是Python 2.7的代码短语吗?
我想知道如何让玩家用箭头来回移动 键
下面是我的极简解决方案,应该可以在Python 2或Python 3中使用:
from turtle import Screen, Turtle, mainloop
from functools import partial
screen = Screen()
turtle = Turtle(shape='square')
turtle.setheading(0)
screen.onkey(partial(turtle.forward, 10), "Right")
screen.onkey(partial(turtle.backward, 10), "Left")
screen.listen()
mainloop()