Skip to main content

Overview

The Food Catch main script (main.gd) manages gameplay for a minigame where players catch falling food items while avoiding trash. It features a lives system, dynamic difficulty scaling, touch controls, and visual effects. Script Path: minigames/food_catch/main.gd Extends: Node2D

Exported Variables

Item Scenes

food_scene
PackedScene
Scene resource for edible food items that increase score when caught.
trash_scene
PackedScene
Scene resource for trash items that decrease lives when caught.
bonus_scene
PackedScene
Scene resource for special bonus items that award extra points.
floating_text_scene
PackedScene
Scene resource for floating text effects displayed when items are caught.

Spawn Configuration

spawn_every
float
default:"0.8"
Base time interval between spawns in seconds. Decreases as difficulty increases.
bonus_probability
float
default:"0.05"
Probability (0-1) of spawning a bonus item instead of regular food/trash.
trash_probability
float
default:"0.35"
Probability (0-1) of spawning trash when not spawning bonus.

Difficulty Scaling

difficulty_interval
float
default:"30.0"
Time in seconds between difficulty increases.
speed_multiplier
float
default:"1.2"
Factor by which falling speed increases with each difficulty stage.
spawn_multiplier
float
default:"0.85"
Factor by which spawn interval decreases with each difficulty stage (makes spawning faster).

Node References

UI Nodes

@onready var score_label: Label = $CanvasLayer/ScoreLabel
@onready var lives_label: Label = $CanvasLayer/LivesLabel
@onready var difficulty_label: Label = $CanvasLayer/DifficultyLabel
@onready var damage_border: ColorRect = $CanvasLayer/DamageBorder
@onready var panel: Panel = $CanvasLayer/GameOverPanel
@onready var backButton: Button = $CanvasLayer/backButton
@onready var intro_panel: Control = $intro_panel

Touch Controls

@onready var touch_left = $CanvasLayer/TouchLeft
@onready var touch_right = $CanvasLayer/TouchRight

Game Objects

@onready var player = $Player
@onready var bg: Node2D = $Background

Audio

@onready var sfx_eat: AudioStreamPlayer = $SFX_Eat
@onready var sfx_bad: AudioStreamPlayer = $SFX_Bad
@onready var sfx_bonus: AudioStreamPlayer = $SFX_Bonus

Particle Effects

@onready var pescao1der: CPUParticles2D = $pescao1der
@onready var pescao1izq: CPUParticles2D = $pescao1izq
@onready var pescao2der: CPUParticles2D = $pescao2der
@onready var bubbles: CPUParticles2D = $bubbles

State Variables

score
int
default:"0"
Current player score. Increases by 10 for food, varies for bonuses.
lives
int
default:"3"
Remaining lives. Game ends when reaches 0.
difficulty_stage
int
default:"0"
Current difficulty level. Increments periodically to increase challenge.
is_paused
bool
default:"false"
Whether the game is currently paused.
last_coins_gained
int
default:"0"
Coins earned in current game session for display on game over.

Core Functions

Initialization

_ready
void
Sets up the game environment.Key Actions:
  • Connects touch input handlers
  • Configures UI follow viewport
  • Sets up button connections
  • Initializes spawn timer (wait_time = spawn_every)
  • Resets global speed multiplier for FallingItem class
  • Shows intro panel and disables player until game starts

Spawning System

_on_spawn_timer_timeout
void
Spawns a random falling item based on probability distribution.Spawn Logic:
  1. Roll random number (0-1)
  2. If < bonus_probability: spawn bonus
  3. Else if < (bonus_probability + trash_probability): spawn trash
  4. Else: spawn food
Spawn Position: Random X between 40 and (screen_width - 40), Y = -40Item Setup:
  • Sets z_index to 20
  • Connects “resolved” or “resolved_bonus” signal
  • Adds to scene tree

Item Resolution

_on_item_resolved
void
Called when food or trash item is caught or missed.Parameters:
  • is_trash (bool): True if item was trash, false if food
Trash Effect:
  • Decreases lives by 1
  • Plays bad sound with pitch variation (0.9-1.0)
  • Flashes lives label red
  • Flashes damage overlay
  • Triggers player damage animation
Food Effect:
  • Increases score by 10
  • Plays eat sound with pitch variation (1.0-1.15)
  • Flashes score label green
  • Shows “+10” floating text
  • Triggers player catch animation
Post-Action:
  • Updates HUD
  • Checks for game over (if lives <= 0)
_on_bonus_resolved
void
Called when bonus item is caught.Parameters:
  • points (int): Points awarded by the bonus
Effect:
  • Increases score by points
  • Plays bonus sound with pitch variation (1.05-1.2)
  • Shows floating text with point value
  • Triggers player catch animation

HUD Management

_update_hud
void
Updates score and lives display labels.Format:
  • Score: “Puntos:
  • Lives: “Vidas:
_hud_damage_flash
void
Flashes the lives label red to indicate damage taken.Animation: 0.06s fade to red, 0.22s fade back to original color
_hud_pop_flash
void
Flashes the score label green to indicate points gained.Animation: 0.06s fade to green (#60e55a), 0.22s fade back

Visual Effects

_flash_damage_overlay
void
Flashes a red damage overlay using shader material.Animation:
  • 0.08s: intensity 0.0 → 0.5
  • 0.25s: intensity 0.5 → 0.0
_spawn_floating_text
void
Creates floating text near the player position.Parameters:
  • text (String): Text to display (e.g., “+10”, “+50”)
  • color (Color): Text color
Position: Player position + Vector2(20 + random(-10,10), -30)

Audio

_play_varied
void
Plays audio with random pitch variation for variety.Parameters:
  • player (AudioStreamPlayer): The audio player to use
  • pmin (float): Minimum pitch scale (default: 0.95)
  • pmax (float): Maximum pitch scale (default: 1.05)
Behavior: Stops current playback if playing, sets random pitch, then plays

Difficulty System

_on_DifficultyTimer_timeout
void
Increases game difficulty periodically.Changes:
  • Increments difficulty_stage by 1
  • Multiplies falling speed: FallingItem.global_speed_multiplier = pow(speed_multiplier, difficulty_stage)
  • Reduces spawn interval: spawn_timer.wait_time *= spawn_multiplier
  • Shows “¡Más rápido!” message
_show_difficulty_message
void
Displays an animated difficulty message.Parameters:
  • text (String): Message to display
Animation:
  • Fade in + scale down (1.4 → 1.0) over 0.2s
  • Hold for 1.0s
  • Fade out over 0.4s

Game Over

_check_game_over
void
Checks if lives have reached 0 and triggers game over sequence.Flow:
  1. Stop spawn timer
  2. Play player hide/damage animation
  3. Wait 0.8 seconds
  4. Save high score to ScoreManager (“food_catch” key)
  5. Calculate and award coins (score * 0.1)
  6. Show game over panel
_show_game_over
void
Displays the game over panel with results.Actions:
  • Sets panel title to “¡Fin del Juego!”
  • Shows final score and coins earned
  • Hides touch controls and back button

Pause System

pause_game
void
Pauses all game activity.Pauses:
  • Spawn timer and difficulty timer
  • Player processing and animation
  • All falling items and their animations
  • All particle systems
Shows: Pause panel with current score
resume_game
void
Resumes game from paused state.Resumes:
  • All timers
  • Player processing and animation
  • All falling items
  • All particles (with custom speed scales for fish particles: 0.4)
Hides: Pause panel
pause_all_particles
void
Pauses or resumes all particle effects.Parameters:
  • pause (bool): True to pause, false to resume
Effect: Sets emitting state and speed_scale for all CPUParticles2D nodes
pause_all_falling_items
void
Pauses or resumes all falling items in the scene.Parameters:
  • pause (bool): True to pause, false to resume
Detection: Checks for “FallingItem” in node name or classEffect: Sets process_mode and animation speed_scale

Touch Controls

_on_touch_input
void
Handles touch/mouse input for player movement.Parameters:
  • event (InputEvent): Input event
  • dir (float): Direction (-1 for left, 1 for right)
Behavior:
  • On press: Sets player.touch_dir to dir
  • On release: Sets player.touch_dir to 0.0
_on_back_pressed
void
Returns to main menu, resetting screen orientation to portrait.
_on_retry_pressed
void
Reloads the current scene to restart the game.
_on_backButton_pressed
void
Toggles pause state.

ScoreManager Integration

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

Common Patterns

Global Speed Control

Falling items use a class-level static multiplier:
# On ready
FallingItem.global_speed_multiplier = 1.0

# On difficulty increase
FallingItem.global_speed_multiplier = pow(speed_multiplier, difficulty_stage)

Probability-Based Spawning

var roll := randf()
if roll < bonus_probability:
    scene = bonus_scene
elif roll < bonus_probability + trash_probability:
    scene = trash_scene
else:
    scene = food_scene

HUD Flash Animation

Label flashes provide immediate visual feedback:
var t := create_tween()
t.tween_property(label, "modulate", flash_color, 0.06)
t.tween_property(label, "modulate", base_color, 0.22)
The game uses landscape orientation during play but switches to portrait when returning to menu.
The FallingItem class must be accessible globally and support the global_speed_multiplier static variable.

Build docs developers (and LLMs) love