admin管理员组文章数量:1122846
So I have a movement script for my turtle (called mc)
from turtle import *
def w():
mc.shape(FFM1)
mc.forward(40)
mc.shape(FFM2)
mc.forward(40)
mc.shape(FFS)
def s():
mc.shape(BFM1)
mc.bk(40)
mc.shape(BFM2)
mc.bk(40)
mc.shape(BFS)
def a():
mc.shape(LFM1)
mc.setx(mc.xcor() - 40)
mc.shape(LFM2)
mc.setx(mc.xcor() - 40)
mc.shape(LFS)
def d():
mc.shape(RFM1)
mc.setx(mc.xcor() + 40)
mc.shape(RFM2)
mc.setx(mc.xcor() + 40)
mc.shape(RFS)
wn.onkey(w, "w")
wn.onkey(s, "s") # "Back"
# ^^^^
wn.onkey(a, "a")
wn.onkey(d, "d")
listen()
mainloop()
This works but if I press, for example, d more than once while turtle is still moving it carries on moving however when it stops it teleports back to where it should have gone from the first move.
I've tried putting a time.sleep(2) in the function but it doesn't do anything and just freezes the turtle for two seconds and then carries on breaking
So I have a movement script for my turtle (called mc)
from turtle import *
def w():
mc.shape(FFM1)
mc.forward(40)
mc.shape(FFM2)
mc.forward(40)
mc.shape(FFS)
def s():
mc.shape(BFM1)
mc.bk(40)
mc.shape(BFM2)
mc.bk(40)
mc.shape(BFS)
def a():
mc.shape(LFM1)
mc.setx(mc.xcor() - 40)
mc.shape(LFM2)
mc.setx(mc.xcor() - 40)
mc.shape(LFS)
def d():
mc.shape(RFM1)
mc.setx(mc.xcor() + 40)
mc.shape(RFM2)
mc.setx(mc.xcor() + 40)
mc.shape(RFS)
wn.onkey(w, "w")
wn.onkey(s, "s") # "Back"
# ^^^^
wn.onkey(a, "a")
wn.onkey(d, "d")
listen()
mainloop()
This works but if I press, for example, d more than once while turtle is still moving it carries on moving however when it stops it teleports back to where it should have gone from the first move.
I've tried putting a time.sleep(2) in the function but it doesn't do anything and just freezes the turtle for two seconds and then carries on breaking
Share Improve this question asked Nov 21, 2024 at 14:10 Isaac ChickIsaac Chick 71 bronze badge 5 |1 Answer
Reset to default -2You can add a variable and a condition to allow only one movement at a time:
from turtle import *
moving = False
def w():
global moving
if not moving:
moving = True
mc.shape(FFM1)
mc.forward(40)
mc.shape(FFM2)
mc.forward(40)
mc.shape(FFS)
moving = False
def s():
global moving
if not moving:
moving = True
mc.shape(BFM1)
mc.bk(40)
mc.shape(BFM2)
mc.bk(40)
mc.shape(BFS)
moving = False
def a():
global moving
if not moving:
moving = True
mc.shape(LFM1)
mc.setx(mc.xcor() - 40)
mc.shape(LFM2)
mc.setx(mc.xcor() - 40)
mc.shape(LFS)
moving = False
def d():
global moving
if not moving:
moving = True
mc.shape(RFM1)
mc.setx(mc.xcor() + 40)
mc.shape(RFM2)
mc.setx(mc.xcor() + 40)
mc.shape(RFS)
moving = False
wn.onkey(w, "w")
wn.onkey(s, "s")
wn.onkey(a, "a")
wn.onkey(d, "d")
listen()
mainloop()
本文标签: pythonHow to make listen only do 1 command at onceStack Overflow
版权声明:本文标题:python - How to make listen only do 1 command at once? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736310111a1934259.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
NameError: name 'wn' is not defined. Did you mean: 'w'?
. What Python version do you use? Do you use Windows? – jabaa Commented Nov 21, 2024 at 14:25setx
on the other axis. Why not use rotation andforward
exclusively? I'm not sure what sort of movement you want to create (I suggest clarifying how you want behavior to be, exactly), but this answer provides a good starter for full real-time smooth movement, if that's what you're looking for. – ggorlen Commented Nov 21, 2024 at 15:42