System sends to applications details about pressed key ("occasions") solely when key modifications state from not-pressed to pressed or from pressed to not-pressed (launched). It would not ship occasions if you maintain down key.
It’s important to use onkeypressed() to set arrow_up_pressed = True and onkeyreleased() to reset arrow_up_pressed = False and ontimer() to run repeatedly code which checks if arrow_up_pressed is True and transfer object up. The identical it’s best to do with arrow_down_pressed, and so forth.
Or you should utilize variable velocity as an alternative of arrow_up_pressed and arrow_down_pressed so you’ll be able to assing +15 or -15 to the identical variable (or 0 when keys are launched). And once more you want ontimer to run repeatedly code which add velocity to place.
In instance I exploit second methodology.
Minimal working code
import turtle
def paddle_a_up():
international a_speed_y
a_speed_y = +15
def paddle_a_down():
international a_speed_y
a_speed_y = -15
def paddle_a_left():
international a_speed_x
a_speed_x = -15
def paddle_a_right():
international a_speed_x
a_speed_x = +15
def paddle_a_stop_y():
international a_speed_y
a_speed_y = 0
def paddle_a_stop_x():
international a_speed_x
a_speed_x = 0
def update_frame():
x, y = paddle_a.place()
y += a_speed_y
x += a_speed_x
paddle_a.goto(x, y)
# right here replace place for different objects - ie. transfer ball
# run once more after 50ms
wn.ontimer(update_frame, 50) # 50ms means ~20 FPS (Frames Per Second) (1000/50 = 20)
# --- predominant ---
# default values at begin
a_speed_x = 0
a_speed_y = 0
wn = turtle.Display()
paddle_a = turtle.Turtle()
# run first time after 50ms
wn.ontimer(update_frame, 50) # 50ms means ~20 FPS (Frames Per Second) (1000ms / 50ms = 20)
# binds
wn.onkeypress(paddle_a_up, "Up")
wn.onkeypress(paddle_a_down, "Down")
wn.onkeypress(paddle_a_left, "Left")
wn.onkeypress(paddle_a_right, "Proper")
wn.onkeyrelease(paddle_a_stop_y, "Up")
wn.onkeyrelease(paddle_a_stop_y, "Down")
wn.onkeyrelease(paddle_a_stop_x, "Left")
wn.onkeyrelease(paddle_a_stop_x, "Proper")
wn.hear()
wn.mainloop()
BTW: The identical approach you would need to do that in PyGame or Pyglet.
EDIT:
You get higher outcome should you add/substract worth to velocity as an alternative of assigning – as a result of if you press left and proper on the identical time then it can cease transfer (as a result of velocity +15 and -15 will provides 0), and if you launch just one – ie. left – then it can once more transfer proper. In earlier model if you launch one however you continue to hold pressed different then it would not transfer once more.
import turtle
def paddle_a_up_pressed():
international a_speed_y
a_speed_y += 15
def paddle_a_down_pressed():
international a_speed_y
a_speed_y -= 15
def paddle_a_left_pressed():
international a_speed_x
a_speed_x -= 15
def paddle_a_right_pressed():
international a_speed_x
a_speed_x += 15
def paddle_a_up_released():
international a_speed_y
a_speed_y -= 15
def paddle_a_down_released():
international a_speed_y
a_speed_y += 15
def paddle_a_left_released():
international a_speed_x
a_speed_x += 15
def paddle_a_right_released():
international a_speed_x
a_speed_x -= 15
def update_frame():
x, y = paddle_a.place()
x += a_speed_x
y += a_speed_y
paddle_a.goto(x, y)
# run once more after 50ms
wn.ontimer(update_frame, 50) # 50ms means ~20 FPS (Frames Per Second) (1000/50 = 20)
# --- predominant ---
# default values at begin
a_speed_x = 0
a_speed_y = 0
wn = turtle.Display()
paddle_a = turtle.Turtle()
# run first time after 50ms
wn.ontimer(update_frame, 50) # 50ms means ~20 FPS (Frames Per Second) (1000/50 = 20)
# binds
wn.onkeypress(paddle_a_up_pressed, "Up")
wn.onkeypress(paddle_a_down_pressed, "Down")
wn.onkeypress(paddle_a_left_pressed, "Left")
wn.onkeypress(paddle_a_right_pressed, "Proper")
wn.onkeyrelease(paddle_a_up_released, "Up")
wn.onkeyrelease(paddle_a_down_released, "Down")
wn.onkeyrelease(paddle_a_left_released, "Left")
wn.onkeyrelease(paddle_a_right_released, "Proper")
wn.hear()
wn.mainloop()
