Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.1 kB
2
Indexable
Never
extends Node2D

const snake = 0
const point = 1

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

func _ready():
	point_pos = place_point()

func place_point():
	var x = randi() % 20
	var y = randi() % 20
	return Vector2(x,y)

func draw_point():
	$Board.set_cell(0,Vector2(point_pos.x, point_pos.y), point, Vector2(0,0))

func draw_snake():
	for block_index in snake_body.size():
		var block = snake_body[block_index]
		if block_index == 0:
			var head_dir = relation(snake_body[0], snake_body[1])
			if head_dir == 'left':
				$Board.set_cell(0, Vector2(block.x, block.y), snake, Vector2(2,0))
			if head_dir == 'right':
				$Board.set_cell(0, Vector2(block.x, block.y), snake, Vector2(3,1))
			if head_dir == 'up':
				$Board.set_cell(0, Vector2(block.x, block.y), snake, Vector2(3,0))
			if head_dir == 'down':
				$Board.set_cell(0, Vector2(block.x, block.y), snake, Vector2(2,1))
		else:
			$Board.set_cell(0, Vector2(block.x, block.y), snake, Vector2(7,0))

func move_snake():
	if add_point:
		delete_tiles(snake)
		var body_copy = snake_body.slice(0, snake_body.size())
		var new_head = body_copy[0] + snake_direction
		body_copy.insert(0, new_head)
		snake_body = body_copy
		add_point = false
	else:
		delete_tiles(snake)
		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():
	move_snake()
	draw_point()
	draw_snake()
	check_point_eaten()

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 _input(event):
	if Input.is_action_just_pressed("ui_down") and snake_direction != Vector2(0,-1):
		snake_direction = Vector2(0,1)
	if Input.is_action_just_pressed("ui_up") and snake_direction != Vector2(0,1):
		snake_direction = Vector2(0,-1)
	if Input.is_action_just_pressed("ui_left") and snake_direction != Vector2(1,0):
		snake_direction = Vector2(-1,0)
	if Input.is_action_just_pressed("ui_right") and snake_direction != Vector2(-1,0):
		snake_direction = Vector2(1,0)

func check_point_eaten():
	if point_pos == snake_body[0]:
		point_pos = place_point()
		add_point = true

func check_game_over():
	var head = snake_body[0]
	if head.x > 19 or head.x < 0 or head.y < 0 or head.y > 19:
		reset()
	for block in snake_body.slice(1, snake_body.size()):
		if block == head:
			reset()

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

func _process(_delta):
	check_game_over()

func relation(first_block:Vector2, second_block:Vector2):
	var block_relation = second_block - first_block
	if block_relation == Vector2(-1, 0):
		return 'left'
	if block_relation == Vector2(1, 0):
		return 'right'
	if block_relation == Vector2(0, 1):
		return 'down'
	if block_relation == Vector2(0, -1):
		return 'up'
Leave a Comment