How to avoid button mashing in Godot 4 for snake movement?

While going through a Snake game tutorial for Godot 4, I noticed a crucial error in the snake’s movement. If the player presses buttons randomly, there’s a high chance the snake can collide with its tail without actually turning (you can see this in the video). How can I avoid this kind of snake behavior? Below, I’ve written a snippet of the snake’s movement code, but since the project is open-source, I’ve included the link to the project’s GitHub and the tutorial video I’m analyzing. Thanks in advance to anyone willing to help!

YouTube tutorial link → “https://youtu.be/DlRP-UBR-2A?si=IGgZD6Vxx3Wvme3Q

GitHub project link → “GitHub - russs123/snake_tutorial: Snake game made in Godot

Part script for snake movement →

func move_snake():
if can_move:
#update movement from keypresses
if Input.is_action_just_pressed(“move_down”) and move_direction != up:
move_direction = down
can_move = false
if not game_started:
start_game()
if Input.is_action_just_pressed(“move_up”) and move_direction != down:
move_direction = up
can_move = false
if not game_started:
start_game()
if Input.is_action_just_pressed(“move_left”) and move_direction != right:
move_direction = left
can_move = false
if not game_started:
start_game()
if Input.is_action_just_pressed(“move_right”) and move_direction != left:
move_direction = right
can_move = false
if not game_started:
start_game()

func move_snake():
  if can_move:
    #update movement from keypresses
    if Input.is_action_just_pressed(“move_down”) and move_direction != up:
      move_direction = down
      can_move = false
    if Input.is_action_just_pressed(“move_up”) and move_direction != down:
      move_direction = up
      can_move = false
    if Input.is_action_just_pressed(“move_left”) and move_direction != right:
      move_direction = left
      can_move = false
    if Input.is_action_just_pressed(“move_right”) and move_direction != left:
      move_direction = right
      can_move = false
    if not can_move and not game_started:
      start_game()

I optimized this code a little bit. The problem with mashing is there could be multiple buttons that set the value change move_direction and which ever order is last will take precedence.

Do we want the first button pressed to take precedence or the last?
in order to take FI (First In) or LI (Last In) we need to use the _unhandled_input(event): function.

after we selected the direction. we need to confirm its a valid direction with the snakes physical state.

last in:

func _unhandled_input(event):
 if can_move:
    #update movement from keypresses
    if event.pressed:
      if event.is_action(“move_down”) :
        move_direction = down
        can_move = false
      if event.is_action(“move_up”):
        move_direction = up
        can_move = false
      if event.is_action(“move_left”) :
        move_direction = left
        can_move = false
      if event.is_action(“move_right”):
        move_direction = right
        can_move = false

      #start game on first move
      if not can_move and not game_started:
        start_game()

func turn_snake():
  match (snake_direction)
     up:
        if move_direction != down:
           snake_direction = move_direction
     down:
        if move_direction != up:
          snake_direction = move_direction
      right:
        if move_direction != left:
          snake_direction = move_direction
      left:
        if move_direction != right:
           snake_direction = move_direction