[Python] How to bind several key presses together in turtle graphics?

Discussão em 'Python' iniciado por Stack, Julho 23, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to make a connect-the-dot python game. I want the game to register 2 button presses. Example: if the user presses Up and Right arrow key, the turtle goes 45 degrees north east.

    here is my code:

    import turtle

    flynn=turtle.Turtle()
    win=turtle.Screen()
    win.bgcolor("LightBlue")
    flynn.pensize(7)
    flynn.pencolor("lightBlue")

    win.listen()

    def Up():
    flynn.setheading(90)
    flynn.forward(25)

    def Down():
    flynn.setheading(270)
    flynn.forward(20)

    def Left():
    flynn.setheading(180)
    flynn.forward(20)

    def Right():
    flynn.setheading(0)
    flynn.forward(20)

    def upright():
    flynn.setheading(45)
    flynn.forward(20)

    win.onkey(Up, "Up")
    win.onkey(Down,"Down")
    win.onkey(Left,"Left")
    win.onkey(Right,"Right")

    Continue reading...

Compartilhe esta Página