Overview
The Turtle Run main script (Main.gd) manages the core gameplay loop for the endless runner minigame where players control a turtle avoiding obstacles and collecting bonuses. It handles spawning, scrolling, scoring, difficulty progression, and pause/resume functionality.
Script Path: minigames/turtle_run/scripts/Main.gd
Extends: Node2D
Signals
Emitted when the player wants to return to the main menu.
Exported Variables
Base scrolling speed of the parallax background in pixels per second.
Scene resource for floating text effects shown when collecting bonuses.
Scene resource for obstacles that the player must avoid.
Scene resource for bonus items that award points.
Y-coordinates for the three vertical lanes where obstacles and bonuses spawn.
Node References
UI Nodes
Container Nodes
Audio Nodes
State Variables
Current player score. Increases continuously based on time survived and speed multiplier.
Difficulty multiplier that increases every 6 seconds, affecting scroll speed and score rate.
Whether the game is currently paused.
Whether the intro panel is currently displayed (game not started).
Tracks coins earned in the last game session for display on game over screen.
Core Functions
Initialization
Initializes UI, connects signals, sets up intro panel, and configures process modes for pause functionality.Key Actions:
- Sets up game over panel and button connections
- Adds node to “turtle_game” group
- Configures spawn timer (starts stopped until intro completes)
- Disables turtle processing until game starts
Game Loop
Main game loop that runs every frame.Parameters:
delta(float): Time elapsed since last frame in seconds
- Updates parallax scrolling based on speed multiplier
- Increases score continuously (delta * 10 * speed_multiplier)
- Updates speed multiplier every 6 seconds (multiplies by 1.1)
- Updates turtle animation speed to match game speed
- Skips processing if paused or in intro state
Spawning System
Called when spawn timer expires. Spawns either an obstacle (90% chance) or bonus (10% chance) in a random lane.Logic:
- Randomizes next spawn interval between 0.5-1.5 seconds
- Avoids spawning in the same lane twice in a row
- Uses
randf() < 0.9to determine obstacle vs bonus
Instantiates an obstacle at the specified Y position on the right edge of the screen.Parameters:
y_pos(float): Y-coordinate for the obstacle (typically from lanes array)
Vector2(viewport_width + 50, y_pos)Instantiates a bonus item at the specified Y position on the right edge of the screen.Parameters:
y_pos(float): Y-coordinate for the bonus (typically from lanes array)
Vector2(viewport_width + 50, y_pos)Scoring
Adds bonus points to the current score.Parameters:
amount(int): Points to add
Game Over
Handles game over state when turtle hits an obstacle.Flow:
- Stops spawn timer and scrolling
- Disables processing for all obstacles, bonuses, and turtle
- Plays hit sound effect
- Waits 0.8 seconds
- Saves high score to ScoreManager (“turtle_runner” key)
- Calculates and awards coins (score * 0.1)
- Shows game over panel
Displays the game over panel with final score and coins earned.
Pause System
Pauses the game.Actions:
- Sets
is_paused = true - Pauses spawn timer
- Stores current parallax offset
- Disables turtle processing and animation
- Pauses all obstacles and bonuses
- Pauses audio streams
- Shows pause panel
Resumes the game from paused state.Actions:
- Sets
is_paused = false - Resumes spawn timer
- Re-enables turtle processing and animation
- Resumes all obstacles and bonuses
- Resumes audio streams
- Hides pause panel
Helper function to pause/resume all obstacles.Parameters:
pause(bool): True to pause, false to resume
Helper function to pause/resume all bonus items.Parameters:
pause(bool): True to pause, false to resume
Audio & Visual Effects
Displays floating text effect at a position.Parameters:
pos(Vector2): World position for the texttext(String): Text to display (default: “+50”)color(Color): Text color (default: yellow)
Plays the bonus collection sound effect.
Plays the hit/collision sound effect.
Navigation
Handles back button press. Resets screen orientation to portrait and returns to main menu.
Reloads the current scene to restart the game.
Toggles pause state when the in-game back button is pressed.
ScoreManager Integration
The script integrates with the globalScoreManager singleton:
"turtle_runner"
Coin Conversion: Final score × 0.1
Common Patterns
Difficulty Progression
The game increases difficulty by incrementingspeed_multiplier by 10% every 6 seconds:
Process Mode Management
UI elements that need to remain interactive during pause usePROCESS_MODE_ALWAYS:
Usage Example
Typical scene structure:The script checks
is_paused or is_intro at the start of most functions to prevent actions during paused or intro states.