Skip to main content

Overview

The Counting Animals main script (main.gd) manages an educational counting game where players watch animals spawn on screen, then answer how many they saw. Features progressive difficulty, multiple animal types per round, and score-based progression. Script Path: minigames/counting_animals/scripts/main.gd Extends: Node2D

Signals

finished_moving
signal
Emitted when animal movement animations complete.

Node References

UI Components

@onready var instruction_label = $CanvasLayer/instruction_label
@onready var number_buttons = $CanvasLayer/number_buttons
@onready var animal_preview = $CanvasLayer/AnimalPreview
@onready var animal_container = $CanvasLayer/animal_container
@onready var score_label = $CanvasLayer/score_label
@onready var intro_panel = $CanvasLayer/intro_panel

Game Over Panel

@onready var panel: Panel = $CanvasLayer/GameOverPanel
@onready var title: Label = $CanvasLayer/GameOverPanel/Title
@onready var points_label: Label = $CanvasLayer/GameOverPanel/Score
@onready var back_btn: TextureButton = $CanvasLayer/GameOverPanel/Buttons/BackButton
@onready var retry_btn: TextureButton = $CanvasLayer/GameOverPanel/Buttons/RetryButton
@onready var backButton: Button = $CanvasLayer/backButton

Game Logic

@onready var spawner = $AnimalSpawner

State Variables

score
int
default:"0"
Current player score. Increases based on difficulty level and rounds completed.
difficulty_level
int
default:"1"
Current difficulty (1-3). Determines how many different animal types spawn per round.
rounds_completed
int
default:"0"
Number of rounds completed at current difficulty level. Used for progression.
correct_streak
int
default:"0"
Consecutive correct answers. Used for alternate difficulty progression.
current_targets
Array
default:"[]"
Array of animal type IDs to count in the current round.
correct_count
int
default:"0"
The correct answer for the current round (total animals of target types).
counting
bool
default:"false"
Whether animals are currently being counted (spawning phase active).
is_paused
bool
default:"false"
Whether the game is currently paused.
last_coins_gained
int
default:"0"
Coins earned in current session for game over display.

Animal Type Mapping

var animal_names := {
    "foca": "focas",
    "nyuron_azul": "Nyuron Azules",
    "nyuron_lengua": "Nyuron con lengua",
    "nyuron_soda": "Nyuron Soda",
    "tortuguita": "Tortuguitas",
    "tortuguita_estrella": "Tortuguitas Estrella"
}

Core Functions

Initialization

_ready
void
Initializes game state and UI.Key Actions:
  • Sets process_mode to ALWAYS for pause functionality
  • Sets up number button connections
  • Connects spawner signals
  • Adds animal_preview to “AnimalPreviewGroup”
  • Shows intro panel
  • Configures UI visibility
setup_number_buttons
void
Connects press signals for numbered answer buttons (btn1-btn10).Logic: Extracts number from button name (“btn1” → 1) and binds to _on_number_pressed

Round Management

start_round
void
Begins a new counting round.Flow:
  1. Clear current targets
  2. Determine number of animal types based on difficulty (1-3)
  3. Select random animal types from spawner’s available animals
  4. Show instruction with target animal images (4 seconds)
  5. Display “¡Empieza!” message (1.5 seconds)
  6. Start spawner with first target type
  7. Wait for spawn_finished signal
_on_spawn_finished
void
Called when spawner finishes spawning animals.Actions:
  1. Set counting = false
  2. Wait 1 second
  3. Calculate correct_count by summing counts for all target types
  4. Show answer choices (number buttons)

Display Functions

show_instruction
void
Displays which animal(s) to count.Parameters:
  • animal_type_or_types (String or Array): Animal type ID(s) to display
Behavior:
  • Sets instruction text to “Animal/es a contar:”
  • Clears and populates animal_container with TextureRect nodes
  • Each animal gets 64×64 texture preview
  • Shows animal_container
_get_texture_for_animal
Texture2D
Returns the preview texture for a given animal type.Parameters:
  • t (String): Animal type ID
Returns: Loaded Texture2D from presentation assets, or null if unknown typePaths: res://minigames/counting_animals/assets/Presentacion/c{AnimalName}.png
show_answer_choices
void
Shows number buttons for player to select their answer.Sets instruction: “¿Cuántas viste en total?”

Answer Handling

_on_number_pressed
void
Processes player’s answer.Parameters:
  • num_str (String): The number selected (“1” through “10”)
Correct Answer:
  1. Increment correct_streak and rounds_completed
  2. Show “¡Correcto! Eran en total.” message
  3. Add score based on difficulty
  4. Check difficulty progression
  5. Wait 2 seconds
  6. Start next round
Incorrect Answer:
  1. Show “Fallaste eran en total.” message
  2. Wait 2 seconds
  3. Trigger game over

Scoring System

add_score
void
Adds points based on difficulty level and rounds completed.Point Formula:
  • Level 1: 10 + ((rounds_completed - 1) × 5)
  • Level 2: 40 + ((rounds_completed - 1) × 20)
  • Level 3: 150 + ((rounds_completed - 1) × 30)
Updates: score_label with “Puntaje:

Difficulty Progression

check_difficulty_progression
void
Checks if player should advance to next difficulty level.Progression Rules:
  • Level 1 → 2: After 3 rounds completed
  • Level 2 → 3: After 5 rounds completed
On Advancement:
  • Resets rounds_completed to 0
  • Shows transition message
  • Increases number of animal types per round
show_transition_message
void
Displays a temporary difficulty advancement message.Parameters:
  • text (String): Message to display
Duration: 2.5 seconds

Pause System

pause_game
void
Pauses the game.Actions:
  • Disables spawner processing
  • Calls spawner.pause() if available
  • Pauses all spawned animals
  • Disables UI element processing
  • Shows pause panel
  • Hides animal preview and instruction
resume_game
void
Resumes from pause.Actions:
  • Re-enables spawner processing
  • Calls spawner.resume() if available
  • Resumes all spawned animals
  • Re-enables UI element processing
  • Hides pause panel
  • Shows animal preview and instruction
pause_all_animals
void
Pauses or resumes all animals in the “spawned_animals” group.Parameters:
  • pause (bool): True to pause, false to resume
Effect: Sets process_mode and AnimatedSprite2D speed_scale for each animal

Game Over

show_game_over
void
Displays game over screen and saves progress.Flow:
  1. Stop spawner
  2. Set counting = false
  3. Pause scene tree
  4. Hide all UI elements
  5. Clean up animal preview nodes
  6. Save high score to ScoreManager (“counting_animals” key)
  7. Calculate and award coins (score × 2.0)
  8. Show game over panel with results
_on_play_pressed
void
Starts game when intro play button is pressed.Animation: Fades out intro panel over 0.8s, then shows score label and starts first round
_on_back_pressed
void
Returns to main menu.Actions:
  • Unpauses tree
  • Resets is_paused flag
  • Resets screen orientation to portrait
  • Changes to main menu scene
_on_retry_pressed
void
Restarts the game by reloading the scene.
_on_backButton_pressed
void
Toggles pause state.

ScoreManager Integration

var score_manager = get_node("/root/ScoreManager")
if score_manager:
    score_manager.save_high_score("counting_animals", final_score)
    score_manager.add_coins(coins_earned)
Game Key: "counting_animals" Coin Conversion: Final score × 2.0

Integration with AnimalSpawner

The script relies on an AnimalSpawner node with these capabilities:
spawner.animals
Array[PackedScene]
Array of available animal scenes. Script instantiates them to read type_id.
spawner.start_spawning
void
Begins spawning animals of specified type.Parameters:
  • type_id (String): Animal type to spawn
spawner.stop_spawning
void
Stops the spawning process.
spawner.get_correct_count_for
int
Returns the number of animals of specified type that were spawned.Parameters:
  • type_id (String): Animal type (lowercase)
Returns: Count of that animal type
spawner.spawn_finished
signal
Emitted when spawning phase completes.

Common Patterns

Progressive Difficulty

Difficulty increases both number of animal types and points per round:
var num_types = clamp(difficulty_level, 1, 3)
current_targets = all_types.slice(0, num_types)

Multi-Type Counting

Player must count total across multiple animal types:
for t in current_targets:
    correct_count += spawner.get_correct_count_for(t.to_lower())

Answer Button Setup

Buttons are named “btn1” through “btn10” for easy extraction:
var num = int(btn.name.replace("btn", ""))
btn.pressed.connect(_on_number_pressed.bind(str(num)))
The game uses process_mode = PROCESS_MODE_ALWAYS on main node and CanvasLayer to allow pause menu interaction while game is paused.
Animals must have a type_id variable to be properly identified and counted. Spawner must track counts per type.

Build docs developers (and LLMs) love