'How to make turtle move continuously without multiple key presses?

I am making a game where the player gets to move on a line at the bottom of the screen.

At this time to move it the user repeatedly hits the arrow keys, but I want to make it move continuously when the key is pressed.

How do I fix this?



Solution 1:[1]

You will want to use the get_pressed() function of key, Link.

import pygame
pygame.init()
# Variables
black_background = (0, 0, 0)
screen = pygame.display.set_mode((1280, 960))
running = True
turtle_image = pygame.image.load('location/to/turtle/image.png')
turtle_rect = turtle_image.get_rect() # Using rect allows for collisions to be checked later
# Set spawn position
turtle_rect.x = 60
turtle_rect.y = 60

while running:
    keys = pygame.key.get_pressed()  #keys pressed
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    if keys[pygame.K_LEFT]:
        turtle_rect.x -= 1
    if keys[pygame.K_RIGHT]:
        turtle_rect.x += 1

    screen.fill(black_background)
    screen.blit(turtle_image, turtle_rect)

Remember, this is just a basic layout and you'll have to read a little more into how you want to accomplish collisions and/or movement with other keys. Intro Here.

Solution 2:[2]

Below is my turtle-based solution. It uses a timer to keep the turtle moving at the bottom of the window but lets the timer expire when nothing's happening (turtle has stopped at an edge) and restarts the timer as needed:

from turtle import Turtle, Screen

CURSOR_SIZE = 20

def move(new_direction=False):
    global direction

    momentum = direction is not None  # we're moving automatically

    if new_direction:
        direction = new_direction

    if direction == 'right' and turtle.xcor() < width / 2 - CURSOR_SIZE:
        turtle.forward(1)
    elif direction == 'left' and turtle.xcor() > CURSOR_SIZE - width / 2:
        turtle.backward(1)
    else:
        direction = None

    if ((not new_direction) and direction) or (new_direction and not momentum):
        screen.ontimer(move, 10)

screen = Screen()
width, height = screen.window_width(), screen.window_height()

turtle = Turtle('turtle', visible=False)
turtle.speed('fastest')
turtle.penup()
turtle.sety(CURSOR_SIZE - height / 2)
turtle.tilt(90)
turtle.showturtle()

direction = None

screen.onkeypress(lambda: move('left'), "Left")
screen.onkeypress(lambda: move('right'), "Right")

screen.listen()
screen.mainloop()

Use the left and right arrows to control the turtle's direction.

Solution 3:[3]

use the function: onkeypress.

here is an example were onkeypress is a higher order function:

def move_forward():
    t.forward(10)

screen.onkeypress(key='w', fun= move_forward)

Solution 4:[4]

You can use onkeypress(fun, key=None)

instead of using onkey() or onkeyrelease(). These two only register the input when the key is released.

onkeypress(fun, key=None) registers it while the key is pressed down.

Here's a link to it in the Turtle documentation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 cdlane
Solution 3 ????????
Solution 4 MwaniN