SNAKE 3 START
Kandif
plain_text
a year ago
4.2 kB
16
Indexable
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 remover = 1 func _ready(): 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(0,Vector2(block.x,block.y),0,Vector2(7,0)) "right": $Board.set_cell(0,Vector2(block.x,block.y),0,Vector2(7,0)) "top": $Board.set_cell(0,Vector2(block.x,block.y),0,Vector2(7,0)) "down": $Board.set_cell(0,Vector2(block.x,block.y),0,Vector2(7,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(0,Vector2(block.x,block.y),0,Vector2(7,0)) "right": $Board.set_cell(0,Vector2(block.x,block.y),0,Vector2(7,0)) "top": $Board.set_cell(0,Vector2(block.x,block.y),0,Vector2(7,0)) "down": $Board.set_cell(0,Vector2(block.x,block.y),0,Vector2(7,0)) 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(0,Vector2(block.x,block.y),0,Vector2(7,0)) "right_top","top_right": $Board.set_cell(0,Vector2(block.x,block.y),0,Vector2(7,0)) "down_top","top_down": $Board.set_cell(0,Vector2(block.x,block.y),0,Vector2(7,0)) "right_left","left_right": $Board.set_cell(0,Vector2(block.x,block.y),0,Vector2(7,0)) "left_down","down_left": $Board.set_cell(0,Vector2(block.x,block.y),0,Vector2(7,0)) "right_down","down_right": $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),1,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(x,y) func delete_tiles(id): var cells = $Board.get_used_cells_by_id(0,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()-remover) remover = 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 check_point_eaten() check_game_over() move_snake() draw_point() draw_snake() func check_point_eaten(): if $Board.get_cell_source_id(0,snake_body[0]+snake_direction) == 2: point_pos = place_point() remover = 0 func check_game_over(): var head = snake_body[0] + snake_direction if head.x<2 or head.x>16 or head.y<2 or head.y>16: reset() if head in snake_body.slice(0,snake_body.size() - 1): reset() func reset(): snake_body = [Vector2(5,10),Vector2(4,10),Vector2(3,10)] snake_direction = Vector2(1,0) delete_tiles(1) point_pos = place_point() func _process(delta): if not changed_direction: if Input.is_key_pressed(KEY_W) && snake_direction!=Vector2.DOWN: snake_direction = Vector2.UP changed_direction = true elif Input.is_key_pressed(KEY_A) && snake_direction!=Vector2.RIGHT: snake_direction = Vector2.LEFT changed_direction = true elif Input.is_key_pressed(KEY_S) && snake_direction!=Vector2.UP: snake_direction = Vector2.DOWN changed_direction = true elif Input.is_key_pressed(KEY_D) && snake_direction!=Vector2.LEFT: snake_direction = Vector2.RIGHT changed_direction = true 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"
Editor is loading...
Leave a Comment