Glitches with if statement

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Holy

Hello,
I’m new to this engine (plus haven’t programmed for a long time)

I’m trying to create a simple snake game like I made back in my days to learn the language
but I’m stuck with the movement of the snake

I have a 2d array that contains the grid for the game and I init it like this

for i in range(hight):
		Cells.append([])
		Cells[i].resize(width)
		
		for x in range(width):
			if	(i == 0): Cells[i][x] = -1
			elif (i == (hight - 1)) :Cells[i][x] = -1
			elif(x == 0) : Cells[i][x] = -1
			elif (x == (width - 1)) :Cells[i][x] = -1
			else:
				Cells[i][x] = 0
			print("Cells[" + String(i) +"]["+String(x)+"] = " + String(Cells[i][x]))
	Cells[5][5] = lenght

after this this line of code works

if(move_dir == 3) :
    Cells[x-1][y] = lenght 

but this line

if(move_dir == 3) :
    Cells[x+1][y] = lenght 

just loops it self and does not even gets to draw nothing :confused:

this is the full code while I was trying to solve the issue (the code tag does not work… )

extends Node2D
var width = 16.0
var hight = 9.0

var Cells = []

var move_dir = 0 # 0 left , 1 down , 2 right , 4 u
var lenght = 1
var step = 0
func _ready():
	for i in range(hight):
		Cells.append([])
		Cells[i].resize(width)
		
		for x in range(width):
			if	(i == 0): Cells[i][x] = -1
			elif (i == (hight - 1)) :Cells[i][x] = -1
			elif(x == 0) : Cells[i][x] = -1
			elif (x == (width - 1)) :Cells[i][x] = -1
			else:
				Cells[i][x] = 0
			print("Cells[" + String(i) +"]["+String(x)+"] = " + String(Cells[i][x]))
	Cells[5][5] = lenght
	pass # Replace with function body.

func _draw():
	
	var vec_pos = Vector2(0,0)
	var vec_size = Vector2( get_viewport().size.x / width -1 , get_viewport().size.y / hight - 1 )
	for i in range(hight):
		for x in range(width):
			if Cells[i][x] == -1:
				var _i  = (get_viewport().size.y* i) / hight
				var _x  = (get_viewport().size.x * x) /width
				vec_pos = Vector2(_x , _i )
				var rct = Rect2(vec_pos, vec_size)
				draw_rect(rct,Color.white)    
			elif Cells[i][x] > 0:
				var _i  = (get_viewport().size.y * i) / hight
				var _x  = (get_viewport().size.x * x) /width
				vec_pos = Vector2(_x , _i )
				var rct = Rect2(vec_pos, vec_size)
				draw_rect(rct,Color.blue)            
				print("x :"+ str(x))  
	pass
	
func _process(delta):
	if(Input.is_action_pressed("ui_up")) : move_dir = 3 
	if(Input.is_action_pressed("ui_down")) : move_dir = 1
	if(Input.is_action_pressed("ui_left")) : move_dir = 0
	if(Input.is_action_pressed("ui_right")) : move_dir = 2
	step += delta
	if(step < 5) : return
	step = 0
	for x in range(hight):
		for y in range(width):
			if Cells[x][y] == lenght:
				if(move_dir == 3) :
					 Cells[x-1][y] = lenght 
				elif(move_dir == 1) :
					var x1 = 0
					if( x == hight -1):
						 x1=0 
					else:
						x1 = x-1
					print(x1)
					Cells[x1][y] = lenght
				elif(move_dir == 0) :
					 Cells[x][y-1] = lenght
				elif(move_dir == 2):
					var y1 = 0
					if( y == width -1):
						 y1=0  
					else:
						y1 = y+1
					Cells[x][y1] = lenght
				Cells[x][y] -=1
			elif Cells[x][y] > 0: 
				Cells[x][y] -= 1
	update()

Ok, I was looking throught the code few more time and I found the issue , needed to put

if(move_dir == 3) :
Cells[x+1][y] = lenght +1 

Holy | 2019-09-17 10:43