Untitled

 avatar
Kandif
plain_text
2 months ago
1.0 kB
15
Indexable
Never
extends Node2D
 
var point_pos:Vector2
var snake_body = [Vector2(5,10),Vector2(4,10),Vector2(3,10)]
var snake_direction = Vector2(2,0)
var changed_direction = false  
 
func _ready():
	point_pos = place_point()
	draw_point()
	draw_snake()
 
func draw_snake():
	for block in snake_body:
		$Board.set_cell(0,Vector2(block.x,block.y),0,Vector2(7,0))
 
func draw_point():
	$Board.set_cell(0,Vector2(point_pos.x,point_pos.y),0,Vector2(0,0))
 
func place_point():
	randomize()
	var x = randi() % 20
	var y = randi() % 20
	while(Vector2(x,y) in snake_body):
		x = randi() % 20
		y = randi() % 20
	return Vector2(5,5)
 
func delete_tiles(id):
	var cells = $Board.get_used_cells_by_id(id)
	for cell in cells:
		$Board.set_cell(0,Vector2(cell.x,cell.y),-1)	
 
func move_snake():
	delete_tiles(0)
	var body_copy = snake_body.slice(0,snake_body.size()-1)
	var new_head = body_copy[0] + snake_direction
	body_copy.insert(0,new_head)
	snake_body = body_copy
 
func _on_timer_timeout():
	changed_direction = false
	move_snake()
	draw_point()
	draw_snake()
Leave a Comment