snaek finale

 avatar
Kandif
plain_text
2 years ago
5.1 kB
2
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)
var buffered_direction = Vector2.ZERO
var changed_direction = false
var add_point = false

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

func draw_point():
	$Board.set_cell(point_pos.x,point_pos.y,1)
	
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(x,y)
	
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])
			match(head_dir):
				"left":
					$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(2,0))
				"right":
					$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(3,1))
				"top":
					$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(3,0))
				"down":
					$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(2,1))			
		elif block_index == snake_body.size()-1:
			var tail_dir = relation(snake_body[block_index],snake_body[block_index-1])
			match(tail_dir):
				"left":
					$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(1,0))
				"right":
					$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(0,0))
				"top":
					$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(1,1))
				"down":
					$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(0,1))			
		else:
			var previous_block = relation(snake_body[block_index],snake_body[block_index-1])
			var next_block = relation(snake_body[block_index],snake_body[block_index+1])	
			match(previous_block+"_"+next_block):
				"left_top","top_left":
					$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(5,1))
				"right_top","top_right":
					$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(4,1))
				"down_top","top_down":
					$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(6,1))
				"right_left","left_right":
					$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(6,0))
				"left_down","down_left":
					$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(5,0))
				"right_down","down_right":
					$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(4,0))
#$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() - (1 if add_point else 2))
	var new_head = body_copy[0] + snake_direction
	body_copy.insert(0,new_head)
	snake_body = body_copy
	add_point = false
	if buffered_direction!=Vector2.ZERO:
		snake_direction = buffered_direction
		buffered_direction = Vector2.ZERO
		changed_direction = true

func _on_Timer_timeout():
	changed_direction = false
	check_point_eaten()
	check_game_over()
	move_snake()
	draw_point()
	draw_snake()
	
func check_point_eaten():
	if point_pos == snake_body[0]+snake_direction:
		point_pos = place_point()
		add_point = true
		$Score.update_score(snake_body.size()-2)

func check_game_over():
	var head = snake_body[0]+snake_direction
	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()-1):
		if block == head:
			reset()
			
func reset():
	$Score.check_highscore(snake_body.size()-3)
	$Score.update_score(0)	
	delete_tiles(1)
	point_pos = place_point()
	snake_body = [Vector2(5,10),Vector2(4,10),Vector2(3,10)]
	snake_direction = Vector2(1,0)
	
func _process(_delta) :
	if not changed_direction:
		if Input.is_key_pressed(KEY_W) && not snake_direction == Vector2(0,1) :
			snake_direction = Vector2(0,-1)
			changed_direction = true
		elif Input.is_key_pressed(KEY_D) && not snake_direction == Vector2(-1,0) :
			snake_direction = Vector2(1,0)
			changed_direction = true
		elif Input.is_key_pressed(KEY_A) && not snake_direction == Vector2(1,0) :
			snake_direction = Vector2(-1,0)
			changed_direction = true
		elif Input.is_key_pressed(KEY_S) && not snake_direction == Vector2(0,-1) :
			snake_direction = Vector2(0,1)
			changed_direction = true
	else:
		if Input.is_key_pressed(KEY_W) && not snake_direction == Vector2(0,1) :
			buffered_direction = Vector2(0,-1)
		elif Input.is_key_pressed(KEY_D) && not snake_direction == Vector2(-1,0) :
			buffered_direction = Vector2(1,0)
		elif Input.is_key_pressed(KEY_A) && not snake_direction == Vector2(1,0) :
			buffered_direction = Vector2(-1,0)
		elif Input.is_key_pressed(KEY_S) && not snake_direction == Vector2(0,-1) :
			buffered_direction = Vector2(0,1)
			
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 "top"