Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Turtle Long Press Lag
#1
Below is some code from a tutorial for making pong with Turtle. I noticed that the short presses of "w" and "s" move the left paddle instantaneously, whereas there is a 1 second delay for the paddle to move when I long press (or hold down one of the keys). Is there a way to make long press responses equally fast, or is it just something that has to be accepted with using Turtle?

#Jimmy. 5-13-20 Pong, base on tutorial by Christian Thompson
#His twitter is @TokyoEdTech
import turtle

wn = turtle.Screen()

wn = turtle.Screen()
wn.title("Pong by Jimmy")
wn.bgcolor("black")
wn.setup(width=800, height=600)
wn.tracer(0)  # stops window from update, requiring manual update.

# Paddle A
paddle_a = turtle.Turtle()
paddle_a.speed(0)  # sets animation speed to maximum.
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=5, stretch_len =1)
paddle_a.penup()  #something to do with not drawing lines. (which Turtle object do by default).
paddle_a.goto(-350,0)



# Paddle B
paddle_b = turtle.Turtle()
paddle_b.speed(0)  # sets animation speed to maximum.
paddle_b.shape("square")
paddle_b.color("white")
paddle_b.shapesize(stretch_wid=5, stretch_len =1)
paddle_b.penup()  #something to do with not drawing lines. (which Turtle object do by default).
paddle_b.goto(350,0)


# Ball
ball = turtle.Turtle()
ball.speed(0)  # sets animation speed to maximum.
ball.shape("square")
ball.color("white")
ball.penup()  #something to do with not drawing lines. (which Turtle object do by default).
ball.goto(0,0)

# Functions
def paddle_a_up():
    y = paddle_a.ycor()
    y += 20
    paddle_a.sety(y)

def paddle_a_down():
    y = paddle_a.ycor()
    y += -20
    paddle_a.sety(y)

def paddle_b_up():
    y = paddle_b.ycor()
    y += 20
    paddle_b.sety(y)

def paddle_b_down():
    y = paddle_a.ycor()
    y += -20
    paddle_b.sety(y)[url=https://www.youtube.com/watch?v=DplYMapxi3Y]https://www.youtube.com/watch?v=DplYMapxi3Y[/url]

# Keyboard Binding
wn.listen()
wn.onkeypress(paddle_a_up,"w")
wn.onkeypress(paddle_a_down,"s")

#Main game loop
while True:
    wn.update()
Reply
#2
That is the key repeat delay, the delay between the initial key press and repeating the key press when a key is held down. It has nothing to do with Turtle.

You can change the key repeat for your system, but it will affect all programs.
Reply
#3
You can go into the original Python files for turtle, find the code responsible, and change it, but then it will affect everything that you do with turtle. I suppose you could edit the original files to make a parameter for the wait, true or false. Then you just add a simple if statement. Also it would be best to make the parameter a default so that the function can still run without an extra argument being needed.
Reply
#4
(Jun-05-2020, 01:10 AM)deanhystad Wrote: That is the key repeat delay, the delay between the initial key press and repeating the key press when a key is held down. It has nothing to do with Turtle.

You can change the key repeat for your system, but it will affect all programs.

Interesting you say that, because I don't notice that same delay when I make similar games in pygame. Perhaps because I loop through the game loop in my pygame instance 60 times a second?
Reply
#5
Pygame probably binds key down/key up events and uses their own internal timer to get a better key-repeat for gaming. For typing you want the delay.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020