From fefd1aa0153c6c1dd78d6c53f62d334b84d9d208 Mon Sep 17 00:00:00 2001 From: TechieDamien Date: Thu, 31 Jul 2025 11:44:20 +0100 Subject: [PATCH] Adds growth value to fruit to allow for more rapid growth of the snake --- Scenes/fruit.tscn | 16 ++++++++++++++++ Scripts/fruit.gd | 9 +++++++++ Scripts/snake_part.gd | 10 +++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/Scenes/fruit.tscn b/Scenes/fruit.tscn index 2b392b2..a4b6a9a 100644 --- a/Scenes/fruit.tscn +++ b/Scenes/fruit.tscn @@ -16,3 +16,19 @@ shape = SubResource("RectangleShape2D_hf2pu") [node name="Sprite2D" type="Sprite2D" parent="."] texture = SubResource("PlaceholderTexture2D_kum7e") + +[node name="Label" type="Label" parent="."] +anchors_preset = 8 +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +offset_left = -20.0 +offset_top = -11.5 +offset_right = 20.0 +offset_bottom = 11.5 +grow_horizontal = 2 +grow_vertical = 2 +text = "1" +horizontal_alignment = 1 +vertical_alignment = 1 diff --git a/Scripts/fruit.gd b/Scripts/fruit.gd index 545a567..090e6f1 100644 --- a/Scripts/fruit.gd +++ b/Scripts/fruit.gd @@ -5,6 +5,13 @@ const TILE_SIZE = 20 var all_positions : Array[Vector2] = [] +const possible_growth_amounts = [1,1,1,3,3,5,10] + +var growth_amount : int = 1 + +@onready +var label : Label = $Label + func _ready() -> void: var level : Area2D = get_tree().get_first_node_in_group("Level") var x_bounds : Vector2 = level.position.x * Vector2.ONE + Vector2(-TILE_SIZE, TILE_SIZE) * level.scale.x / 2 @@ -31,3 +38,5 @@ func respawn() -> void: var chosen_position = possible_positions.pick_random() position = chosen_position + growth_amount = possible_growth_amounts.pick_random() + label.text = str(growth_amount) diff --git a/Scripts/snake_part.gd b/Scripts/snake_part.gd index 0611979..1dcc087 100644 --- a/Scripts/snake_part.gd +++ b/Scripts/snake_part.gd @@ -30,6 +30,7 @@ var timer_ref : Timer = $"../Timer" var next_part : SnakePart = null var skip_next_move_propagation : bool = false +var queued_growth : int = 0 signal do_movement(new_dir) @@ -69,6 +70,8 @@ func process_movement(new_direction : Vector2) -> void: # The head needs to check if we are about to collide with something if part_type == PartTypes.HEAD: check_movement() + if queued_growth: + extend() # Update the position of this part position = (position + current_direction * TILE_SIZE).snapped(Vector2.ONE * TILE_SIZE) @@ -106,11 +109,13 @@ func check_movement() -> void: match object_in_path.part_type: PartTypes.BODY: lose_game() + PartTypes.DEAD: + lose_game() PartTypes.TAIL: ouroboros() if object_in_path is Fruit: + queued_growth += object_in_path.growth_amount object_in_path.respawn() - extend() func extend() -> void: # Remove old connection to previous next part @@ -134,6 +139,9 @@ func extend() -> void: # Mark the next movement to not propagate the movement down the chain skip_next_move_propagation = true + # Mark the growth step as done + queued_growth -= 1 + func lose_game() -> void: # You lose!