Snake 2 lesson

 avatar
Kandif
python
2 years ago
910 B
33
Indexable
Never
extends Node2D

var point_pos:Vector2
var snake_body = [Vector2(5,10),Vector2(4,10),Vector2(3,10)]
var snake_direction = Vector2(1,0)

func _ready():
	point_pos = place_point()
	draw_point()
	draw_snake()
	

func draw_point():
	$Board.set_cell(point_pos.x,point_pos.y,1,false,false,false,Vector2(0,0))
	
func place_point():
	randomize()
	var x = randi() % 20
	var y = randi() % 20
	return Vector2(x,y)
	
func draw_snake():
	for block in snake_body:
		$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(7,0))
	
func delete_tiles(id):
	var cells = $Board.get_used_cells_by_id(id)
	for cell in cells:
		$Board.set_cell(cell.x,cell.y,-1)
		
func move_snake():
	delete_tiles(0)
	var body_copy = snake_body.slice(0,snake_body.size() - 2)
	var new_head = body_copy[0] + snake_direction
	body_copy.insert(0,new_head)
	snake_body = body_copy

func _on_Timer_timeout():
	move_snake()
	draw_point()
	draw_snake()