SNAKE FINAL ULTIMATE

 avatar
Kandif
plain_text
a year ago
5.7 kB
4
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 changed_direction = false
var buffered_direction = Vector2.ZERO
var add_point = false
var number_of_points = 500
var score = 0

func _ready():
	for i in number_of_points:
		$Board.set_cellv(place_point(),1)
	point_pos = place_point()
	draw_point()
	draw_snake()
	
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[1],snake_body[0])
			match(head_dir):
				"left":
					$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(3,1))
				"right":
					$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(2,0))
				"top":
					$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(2,1))
				"down":
					$Board.set_cell(block.x,block.y,0,false,false,false,Vector2(3,0))			
		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))

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 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 removable = 2
	if add_point:
		removable = 1
		add_point = false
	var body_copy = snake_body.slice(0,snake_body.size() - removable)
	var new_head = body_copy[0] + snake_direction
	body_copy.insert(0,new_head)
	snake_body = body_copy
	if buffered_direction!=Vector2.ZERO:
		snake_direction = buffered_direction
		buffered_direction = Vector2.ZERO
		changed_direction = true
	
func _on_Timer_timeout():
	if not $Label2.visible:
		changed_direction = false
		check_point_eaten()
		check_game_over()
		move_snake()
		draw_point()
		draw_snake()
	
func check_point_eaten():
	var head = snake_body[0] + snake_direction
	if $Board.get_cell(head.x,head.y) == 1:
		point_pos = place_point()
		add_point = true
		score += 1
		$Label.update_score(score)
	if point_pos == snake_body[0] + snake_direction:
		point_pos = place_point()
		add_point = true
		score += 1
		$Label.update_score(score)
		
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()
	if head in snake_body.slice(0,snake_body.size() - 2):
		reset()
		
func reset():
	snake_body = [Vector2(4,10),Vector2(3,10),Vector2(2,10)]
	snake_direction = Vector2(1,0)
	delete_tiles(1)
	point_pos = place_point()
	for i in number_of_points:
		$Board.set_cellv(place_point(),1)
	$Label.check_highscore(score)
	score = 0
	$Label2.visible = true
	
func _process(delta):
	if $Label2.visible and Input.is_key_pressed(KEY_ENTER):
		$Label2.visible = false
		$Label.update_score(0)
	if not changed_direction:
		if Input.is_key_pressed(KEY_W) and not snake_direction == Vector2(0,1):
			changed_direction = true
			snake_direction = Vector2(0,-1)
		elif Input.is_key_pressed(KEY_S) and not snake_direction == Vector2(0,-1):
			changed_direction = true
			snake_direction = Vector2(0,1)
		elif Input.is_key_pressed(KEY_A) and not snake_direction == Vector2(1,0):
			changed_direction = true
			snake_direction = Vector2(-1,0)
		elif Input.is_key_pressed(KEY_D) and not snake_direction == Vector2(-1,0):
			changed_direction = true
			snake_direction = Vector2(1,0)			
	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"