Search code examples
pythonturtle-graphicskey-bindingspython-turtle

how to solve keybinding python turtle?


i am new in python i am trying to make a ping pong game but i cant move my turtle objects with key bindings there is my code import turtle

# inishialize screen
the_ping_pong_windows = turtle.Screen()
# set the screen title
the_ping_pong_windows.title("stage of ping pong")
# set the screen color
the_ping_pong_windows.bgcolor("black")
# set the widht and heigt of the window
the_ping_pong_windows.setup(width=1000, height=600)
# stops the window from updating automatically
the_ping_pong_windows.tracer(0)
# madrab 1
# create a turtle object (SHAPE) eli men 5laloh a2dar a3ml shakl el madrab eli ana 3ayzoh
madrab1 = turtle.Turtle()
# set the speed of the animation dih mi4 el sor3ah eli el mAdrab  dih el sor3ah eli beyrsem biha el turtle object
# el madrab kol ma ytharak 34an a2dar a4ofoh
madrab1.speed(0)
madrab1.shape("square")
# set the madrab's color
madrab1.color("red")
madrab1.shapesize(stretch_wid=5, stretch_len=1)
madrab1.penup()
madrab1.goto(-420, 0)
# madrab 2
madrab2 = turtle.Turtle()
madrab2.speed(0)
madrab2.shape("square")
madrab2.color("blue")
madrab2.shapesize(stretch_wid=5, stretch_len=1)
madrab2.penup()
madrab2.goto(420, 0)
# ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0, 0)


# functions
def madrab1_up():
    y = madrab1.ycor()
    y += 20
    madrab1.sety(y)

def madrab1_down():
    y = madrab1.ycor()
    y -= 20
    madrab1.sety(y)


def madrab2_up():
    y = madrab2.ycor()
    y += 22
    madrab2.sety(y)

def madrab2_down():
    y = madrab2.ycor()
    y -= 20
    madrab1.sety(y)


# keyboard binding
the_ping_pong_windows.listen()
the_ping_pong_windows.onkeypress(madrab1_up,"w")
the_ping_pong_windows.onkeypress(madrab1_down,"g")
the_ping_pong_windows.onkeypress(madrab2_up,"s")
the_ping_pong_windows.onkeypress(madrab1_down,"Up")
the_ping_pong_windows.update()
the_ping_pong_windows.exitonclick()

i do not know how to solve this problem when i click "w" for example the shape dint move and i am not getting any error please if someone have a way to solve this problem tell me thanks in advance


Solution

  • The key listeners are set up correctly, but there's no rendering loop. Calling tracer(0) disables turtle's internal update loop, and it's on you to reimplement manually. .update() is called once in your code, but never again once the key handlers start firing.

    Try calling .update() inside a ontimer cycle like:

    # ...
    
    def tick():
        the_ping_pong_windows.ontimer(tick, 1000 // 30)
    
        # handle movement and drawing here
    
        the_ping_pong_windows.update()
    
    tick()
    the_ping_pong_windows.exitonclick()
    

    Since you're applying position changes in the handler, movement won't be smooth. See How to bind several key presses together in turtle graphics? for an improved design that tracks key presses/releases in handlers and only applies movement during a tick call.