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
Emitted when animal movement animations complete.
Node References
UI Components
Game Over Panel
Game Logic
State Variables
Current player score. Increases based on difficulty level and rounds completed.
Current difficulty (1-3). Determines how many different animal types spawn per round.
Number of rounds completed at current difficulty level. Used for progression.
Consecutive correct answers. Used for alternate difficulty progression.
Array of animal type IDs to count in the current round.
The correct answer for the current round (total animals of target types).
Whether animals are currently being counted (spawning phase active).
Whether the game is currently paused.
Coins earned in current session for game over display.
Animal Type Mapping
Core Functions
Initialization
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
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
Begins a new counting round.Flow:
- Clear current targets
- Determine number of animal types based on difficulty (1-3)
- Select random animal types from spawner’s available animals
- Show instruction with target animal images (4 seconds)
- Display “¡Empieza!” message (1.5 seconds)
- Start spawner with first target type
- Wait for spawn_finished signal
Called when spawner finishes spawning animals.Actions:
- Set counting = false
- Wait 1 second
- Calculate correct_count by summing counts for all target types
- Show answer choices (number buttons)
Display Functions
Displays which animal(s) to count.Parameters:
animal_type_or_types(String or Array): Animal type ID(s) to display
- 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
Returns the preview texture for a given animal type.Parameters:
t(String): Animal type ID
res://minigames/counting_animals/assets/Presentacion/c{AnimalName}.pngShows number buttons for player to select their answer.Sets instruction: “¿Cuántas viste en total?”
Answer Handling
Processes player’s answer.Parameters:
num_str(String): The number selected (“1” through “10”)
- Increment correct_streak and rounds_completed
- Show “¡Correcto! Eran en total.” message
- Add score based on difficulty
- Check difficulty progression
- Wait 2 seconds
- Start next round
- Show “Fallaste eran en total.” message
- Wait 2 seconds
- Trigger game over
Scoring System
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)
Difficulty Progression
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
- Resets rounds_completed to 0
- Shows transition message
- Increases number of animal types per round
Displays a temporary difficulty advancement message.Parameters:
text(String): Message to display
Pause System
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
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
Pauses or resumes all animals in the “spawned_animals” group.Parameters:
pause(bool): True to pause, false to resume
Game Over
Displays game over screen and saves progress.Flow:
- Stop spawner
- Set counting = false
- Pause scene tree
- Hide all UI elements
- Clean up animal preview nodes
- Save high score to ScoreManager (“counting_animals” key)
- Calculate and award coins (score × 2.0)
- Show game over panel with results
Navigation
Starts game when intro play button is pressed.Animation: Fades out intro panel over 0.8s, then shows score label and starts first round
Returns to main menu.Actions:
- Unpauses tree
- Resets is_paused flag
- Resets screen orientation to portrait
- Changes to main menu scene
Restarts the game by reloading the scene.
Toggles pause state.
ScoreManager Integration
"counting_animals"
Coin Conversion: Final score × 2.0
Integration with AnimalSpawner
The script relies on anAnimalSpawner node with these capabilities:
Array of available animal scenes. Script instantiates them to read type_id.
Begins spawning animals of specified type.Parameters:
type_id(String): Animal type to spawn
Stops the spawning process.
Returns the number of animals of specified type that were spawned.Parameters:
type_id(String): Animal type (lowercase)
Emitted when spawning phase completes.
Common Patterns
Progressive Difficulty
Difficulty increases both number of animal types and points per round:Multi-Type Counting
Player must count total across multiple animal types:Answer Button Setup
Buttons are named “btn1” through “btn10” for easy extraction:The game uses
process_mode = PROCESS_MODE_ALWAYS on main node and CanvasLayer to allow pause menu interaction while game is paused.