Q:

How do I stop a sprite from moving diagonally using Pygame?

I am making a snake game. Anytime I press an arrow key to move in one direction then press a key in another direction, the snake will diagonally. (E.g. if I first press right then press up.) This happens even if the previous key is released. How can I stop this?
# x and y marks the player's position

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
        x_change = -10
    if event.key == pygame.K_RIGHT:
        x_change = 10
    if event.key == pygame.K_UP:
        y_change = -10
    if event.key == pygame.K_DOWN:
        y_change = 10
    x += x_change
    y += y_change

A:

I have added y_change = 0 and x_change = 0 to reset the values to only keep orthogonal movement.
if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
        x_change = -10
        y_change = 0
    if event.key == pygame.K_RIGHT:
        x_change = 10
        y_change = 0
    if event.key == pygame.K_UP:
        y_change = -10
        x_change = 0
    if event.key == pygame.K_DOWN:
        y_change = 10
        x_change = 0
    x += x_change
    y += y_change

