如何阻止我的 python 乌龟移动?
How do I stop my python turtle from moving?
所以我正在制作一个 python 乌龟游戏,玩家通过按相应的箭头键左右移动乌龟。乌龟不能以任何方式上下移动。只有左和右。但是,当我的乌龟达到某个 xcor 值时,我希望乌龟停止移动,即使我仍在按下该箭头键也是如此。但是仍然可以用另一个箭头键向相反的方向移动。
def playerRight():
player.goto(player.xcor() + 8,player.ycor())
if player.xcor() >= 200:
def playerLeft():
player.goto(player.xcor() - 8,player.ycor())
if player.xcor() <= -200:
screen.onkey(playerRight,'Right')
screen.onkey(playerLeft,'Left')
screen.listen()
但我不知道要在我的条件中放入什么。非常感谢您的答复!谢谢!
也许尝试更改您的代码,仅在海龟不会走得太远时才移动它,就像这样。
def playerRight():
if player.xcor() <= 192:
player.goto(player.xcor() + 8,player.ycor())
现在它只会在不会走得太远的情况下向右移动。然后对 playerLeft()
函数
做同样的事情
你的不平等也有错误的方式(<你需要的地方>)
所以我正在制作一个 python 乌龟游戏,玩家通过按相应的箭头键左右移动乌龟。乌龟不能以任何方式上下移动。只有左和右。但是,当我的乌龟达到某个 xcor 值时,我希望乌龟停止移动,即使我仍在按下该箭头键也是如此。但是仍然可以用另一个箭头键向相反的方向移动。
def playerRight():
player.goto(player.xcor() + 8,player.ycor())
if player.xcor() >= 200:
def playerLeft():
player.goto(player.xcor() - 8,player.ycor())
if player.xcor() <= -200:
screen.onkey(playerRight,'Right')
screen.onkey(playerLeft,'Left')
screen.listen()
但我不知道要在我的条件中放入什么。非常感谢您的答复!谢谢!
也许尝试更改您的代码,仅在海龟不会走得太远时才移动它,就像这样。
def playerRight():
if player.xcor() <= 192:
player.goto(player.xcor() + 8,player.ycor())
现在它只会在不会走得太远的情况下向右移动。然后对 playerLeft()
函数
你的不平等也有错误的方式(<你需要的地方>)