I am unable to understand what went wrong in my movement code for the nokia classic snake game remake. Please help me

Viewed 46

Please help me. As we know in the snake game, if we are currently moving to the right: we cannot move to the left, we have to move either up or down or persist with moving right. And so on respectively for the other directions. So I came up with the following code:

extends Area2D

export var SPEED=100
var input_vector= Vector2.ZERO
var canMove = {'left':false, 'right':false, 'up':false, 'down':false}
var moving = {'left':false, 'right':false, 'up':false, 'down':false}


func _physics_process(delta):
    
    position += input_vector*SPEED*delta
    
    if Input.is_action_just_pressed("ui_right"):
        input_vector.x=1
        input_vector.y=0
    if Input.is_action_just_pressed("ui_left"):
        input_vector.x=-1
        input_vector.y=0
    if Input.is_action_just_pressed("ui_up"):
        input_vector.x=0
        input_vector.y=-1
    if Input.is_action_just_pressed("ui_down"):
        input_vector.x=0
        input_vector.y=1

    
    if input_vector.x<0:
        moving['left']=true
    if input_vector.x>0:
        moving['right']=true

    if input_vector.y<0:
        moving['up']=true
    if input_vector.y>0:
        moving['down']=true


    if moving['left']==true:
        canMove['right']=false
        canMove['up']=true
        canMove['down']=true
    if moving['right']==true:
        canMove['left']=false
        canMove['up']=true
        canMove['down']=true
    if moving['up']==true:
        canMove['right']=true
        canMove['left']=true
        canMove['down']=false
    if moving['down']==true:
        canMove['right']=true
        canMove['left']=true
        canMove['up']=false


    if canMove['left']==false:
        input_vector.x= abs(input_vector.x)
    if canMove['right']==false:
        input_vector.x= -abs(input_vector.x)

    if canMove['up']==false:
        input_vector.y= abs(input_vector.y)
    if canMove['down']==false:
        input_vector.y= -abs(input_vector.y)
1 Answers

After position += input_vector*SPEED*delta. The first block writes input_vector based on Input.

The second block writes moving based on input_vector. Since we are writing input_vector based on Input, these blocks combined write moving based on input_vector.

The third block writes canMove based on moving. By the same logic as above, you are writing canMove based on Input. When are you checking canMove? last. This is not what you want. You want to check canMove first, before writing input_vector and moving.

In fact, you can combine the checks, for example:

if Input.is_action_just_pressed("ui_right") and canMove['right']:

I don't think this is a good way to represent moving. Instead, I'd suggest to use an Enum. That is:

enum Direction {UP, DOWN, LEFT, RIGHT, NONE}
var moving = Direction.NONE

You may also get rid of canMove. The rule is that you can't move to the direction opposite to where you are moving. Thus, you can check moving directly:

    if Input.is_action_just_pressed("ui_right") and moving != Direction.LEFT:
        input_vector.x=1
        input_vector.y=0
        moving = Direction.RIGHT
    if Input.is_action_just_pressed("ui_left") and moving != Direction.RIGHT:
        input_vector.x=-1
        input_vector.y=0
        moving = Direction.LEFT
    if Input.is_action_just_pressed("ui_up") and moving != Direction.DOWN:
        input_vector.x=0
        input_vector.y=-1
        moving = Direction.UP
    if Input.is_action_just_pressed("ui_down") and moving != Direction.UP:
        input_vector.x=0
        input_vector.y=1
        moving = Direction.DOWN

That block should be enough.

Related