【发布时间】:2016-02-28 17:10:21
【问题描述】:
我是 Python 新手,正在尝试创建海龟形状,一旦用户单击键盘上的向左或向右箭头键,形状应该朝那个方向移动,但是什么也没有发生。
我正在尝试使用左右箭头键移动播放器,但它不起作用,请帮助和建议。
#Create the player turtle
player = turtle.Turtle()
player.color("blue")
player.shape("triangle")
player.penup()
player.speed(0)
player.setposition(0, -235)
player.setheading(90)
playerspeed = 15
#Move the player Left and Right
def move_left():
x = player.xcor()
x -= playerspeed
if x < -200:
x = - 200
player.setx(x)
def move_right():
x = player.xcor()
x +- playerspeed
if x < -200:
x = - 280
player.setx(x)
#Create Keyboard Bindings
turtle.listen()
turtle.onkey(move_left,"Left")
turtle.onkey(move_right, "Right")
【问题讨论】:
-
一方面,在
move_right()中,您说x +- playerspeed而不是x += playerspeed。我假设这是一个错字,但谁知道呢。 -
我已经更正了这个,但是玩家仍然没有移动。
标签: python python-2.7