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()