[Python] How can you prevent the holding down of a key in python turtle?

Discussão em 'Python' iniciado por Stack, Novembro 26, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to make a game with 2 turtles that move in python turtle. I have used the onkey() command tomake the turtles move. I want to make them move at the same time because the game is kind of like a race but when ever you hold down a key, the other turtle stops moving and this gives an unfair advantage. Here is the code used for moving the turtle

    screen.onkey(p1_up, 'w')
    screen.onkey(p1_down, 's')
    screen.onkey(p1_left, 'a')
    screen.onkey(p1_right, 'd')
    screen.listen()

    screen.onkey(p2_up, 'Up')
    screen.onkey(p2_down, 'Down')
    screen.onkey(p2_left, 'Left')
    screen.onkey(p2_right, 'Right')
    screen.listen()


    and here is an example of a function if it may help

    def p1_left():
    p1.setheading(180)
    p1.forward(20)
    p1.check_collision()
    p1.check_victory()


    I want to make it so that if you hold down any of the keys, the turtle either moves only one step or just doesn't move. I am new to coding so I haven't tried anything except for using onkeypress() and the onkeyrelease() commands

    Continue reading...

Compartilhe esta Página