使玩家在Python乌龟图形中来回移动

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

我想知道如何使方形播放器通过箭头键来回移动。我已经尝试过t.setheading()t.forward(10),但是它们不起作用。

我想我正在使用3.0之前的版本-_-如果无法解决,我可能会重新开始。

python turtle-graphics
1个回答
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()
© www.soinside.com 2019 - 2024. All rights reserved.