Ryan P
Ryan P

Reputation: 45

Continuous Movement in Pygame

I am trying to make a ship on the surface move continuously on screen but it only accepts one key press at a time. I have tried all solutions online and they aren't working.

import pygame

#initialize the pygame module
pygame.init()
#set the window size 
screen =  pygame.display.set_mode((1280, 720))

#change the title of the window
pygame.display.set_caption("Space Invaders")

#change the icon of the window
icon = pygame.image.load("alien.png")
pygame.display.set_icon(icon)

#add the ship to the window
shipx = 608
shipy = 620

def ship(x, y):
    ship = pygame.image.load("spaceship.png").convert()
    screen.blit(ship, (x,y))
   
running = True
while running:
    #background screen color
    screen.fill((0, 0, 0))

    #render the ship on the window
    ship(shipx,shipy)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            shipx -= 30
        if keys[pygame.K_RIGHT]:
            shipx += 30

    pygame.display.update()

I'm still new to Pygame. How can I fix this?

Upvotes: 3

Views: 634

Answers (3)

ChubbyORBoy
ChubbyORBoy

Reputation: 1

For me, if I need to move an object in Pygame continuously, a velocity variable can be assigned to control the speed.

Here is part of the code for my robot movement program inside of the game_on loop:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False

    # Detect keyboard input on your computer check if it is the direction keys
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            print("Left arrow is pressed")
            robotX_speed = -60
            
        if event.key == pygame.K_RIGHT:
            print("Right arrow is pressed")
            robotX_speed = 60
            
        if event.key == pygame.K_UP:
            print("Up arrow is pressed")
            robotY_speed = -60
            
        if event.key == pygame.K_DOWN:
            print("Down arrow is pressed")
            robotY_speed = 60
    
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                print("Keystoke L/R has been released")
                robotX_speed = 0
        
            if event.type == pygame.K_DOWN or event.key == pygame.K_UP:
                print("Keystoke Up/Down has been released")
                robotY_speed = 0

# update the coordinates in the while loop
robotX += robotX_speed
robotY += robotY_speed

Hope these codes can help you!

Upvotes: 0

Benjamin
Benjamin

Reputation: 3448

The problem that I found is that your application is replying only on one key pressed, not on a continuous movement. When you set the pygame.key.set_repeat function like in the example below, everything should be running smoothly.

import sys
import pygame

#initialize the pygame module
pygame.init()
#set the window size 
screen =  pygame.display.set_mode((1280, 720))

# Images
ship_img = pygame.image.load("spaceship.png")
ship_rect = ship_img.get_rect()

def draw():
    screen.blit(ship_img, ship_rect)
   
pygame.key.set_repeat(10)

while True:
    #background screen color
    screen.fill((0, 0, 0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            ship_rect = ship_rect.move((-30, 0))
        if keys[pygame.K_RIGHT]:
            ship_rect = ship_rect.move((30, 0))

    #render the ship on the window
    draw()

    pygame.display.update()

Upvotes: 4

Rabbid76
Rabbid76

Reputation: 210909

Its a matter of Indentation. pygame.key.get_pressed() has to be done in the application loop rather than the event loop. Note, the event loop is only executed when event occurs, but the application loop is executed in every frame:

running = True
while running:
    # [...]

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        
    #<--| INDENTATION
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        shipx -= 30
    if keys[pygame.K_RIGHT]:
        shipx += 30

    # [...]

Upvotes: 5

Related Questions