1

It is a bit confusing to explain. So, if you have questions, please ask them. I am creating a game using pygame and my "car" moves when you use the "up","down","left", and "right" keys. However, in the way I have it coded, when I release the right key for example but am still holding the left key down, it will change the movement to 0 and not recognize that the left key is still being held down. It makes the movement rather weird. I am trying to make this movement more smooth so that the game is more enjoyable, any ideas are helpful, thank you.

Here is my code:

import pygame

#Initializes pygame
pygame.init()

#Create the screen
screen = pygame.display.set_mode((1000, 800))

#Icon and Caption
icon = pygame.image.load('redcar.png')
pygame.display.set_icon(icon)
pygame.display.set_caption("Placeholder")

# Car

car = pygame.image.load('redcar.png')
x = 500
y = 600
x_change = 0
y_change = 0

def player(x,y):
    screen.blit(car, (x, y))

#Game loop, keep window open unless you quit the window
running = True
while running:
    #RGB
    screen.fill((255, 255, 255))

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

    #If key is pressed, check what key it was
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_change = -1
            if event.key == pygame.K_RIGHT:
                x_change = 1
            if event.key == pygame.K_UP:
                y_change = -1
            if event.key == pygame.K_DOWN:
                y_change = 1
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                x_change = 0

            if event.key == pygame.K_RIGHT:
                x_change = 0

            if event.key == pygame.K_UP:
                y_change = 0

            if event.key == pygame.K_DOWN:
                y_change = 0
    x += x_change
    y += y_change
    player(x,y)
    #For each event, update window
    pygame.display.update()
C W
  • 45
  • 5
  • You could check if the counter key is not being pressed as well: `if event.key == pygame.K_LEFT and not event.key == pygame.K_RIGHT: x_change = 0` and so on for all of them –  Aug 15 '21 at 01:36

1 Answers1

1

You can solve this by using pygame.key.get_pressed(). That will tell you if a key is being held down. It works when any amount of keys are being held down.

Here's a simplified version of your code to demonstrate how that works:

import pygame

pygame.init()
pygame.font.init();
myfont = pygame.font.SysFont('Consolas', 30)

screen = pygame.display.set_mode((1000, 800))

running = True
while running:
    screen.fill((255, 255, 255))

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

    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_UP]:
        textsurface = myfont.render('Up', False, (0, 0, 0))
        screen.blit(textsurface,(0,0))
    if pressed[pygame.K_DOWN]:
        textsurface = myfont.render('Down', False, (0, 0, 0))
        screen.blit(textsurface,(0,50))    
    if pressed[pygame.K_LEFT]:
        textsurface = myfont.render('Left', False, (0, 0, 0))
        screen.blit(textsurface,(0,100))
    if pressed[pygame.K_RIGHT]:
        textsurface = myfont.render('Right', False, (0, 0, 0))
        screen.blit(textsurface,(0,150))          
    
    pygame.display.update()

If I hold Left and Up the window will show:

enter image description here

You could set x_change and y_change to zero before calling get_pressed(), then set those variables within the ifs, depending on which evaluate to true. e.g. Set x_change to -1 if the left key is being pressed.

You may want to think about what happens when the user is pressing up+down (or left+right). That could either cancel out or one key could take precedence.

Robson
  • 2,008
  • 2
  • 7
  • 26