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 Is this your complete script? I get 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:25
  • Please update the code to include your actual code--this isn't runnable. Thanks. It's a bit strange that you're using forward and back on one axis, then setx on the other axis. Why not use rotation and forward 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
  • sorry wn is a screen and I just want a basic wasd movement, w forward, a left, s backwards and d right – Isaac Chick Commented Nov 22, 2024 at 13:39
  • Please edit the question to provide runnable code as a minimal reproducible example. – ggorlen Commented Nov 23, 2024 at 14:51
  • Marked as a duplicate of this one: stackoverflow.com/q/79215316/4788546. – CristiFati Commented Nov 29, 2024 at 9:47
Add a comment  | 

1 Answer 1

Reset to default -2

You 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