Overview
Space Birds uses a state machine with three primary states to manage game flow. Each state has distinct visuals, controls, and behaviors.State Machine
The game uses the
GameState enum with three states:- MENU: Starting point and main menu
- PLAYING: Active gameplay
- GAME_OVER: End screen with final score
State Diagram
The game starts in MENU state when launched and cycles through these states until the application is closed.
MENU State
Description
The initial state where players can start a new game. This is the default state when the game first launches or after returning from GAME_OVER.Visual Elements
| Element | Description |
|---|---|
| Background | Space logo image (space.jpg) filling entire screen |
| Text Line 1 | ”Pulsa ESPACIO o ENTER para comenzar” |
| Text Line 2 | ”Controles: FLECHAS para mover, ESPACIO para disparar” |
| Text Line 3 | ”ENTER para ver puntuación durante el juego” |
| Music | Angry Birds theme (angry-birds.mp3) looping at 50% volume |
Available Controls
SPACE Key
Starts the game → PLAYING
ENTER Key
Starts the game → PLAYING
Touch Screen
Starts the game → PLAYING
State Initialization
When entering MENU state:- Background menu music starts playing
- Game logo is displayed
- Control instructions are shown
- Player object exists but is not active
The MENU state does not update obstacles or process gameplay logic. It’s purely a waiting state.
PLAYING State
Description
The active gameplay state where the player controls the bird, shoots bullets, avoids pigs, and accumulates score.Visual Elements
| Element | Location | Description |
|---|---|---|
| Background | Full screen | Night sky image (noche.jpg) |
| Player Bird | Bottom center | Red bird (64×64px) that moves based on input |
| Pigs | Falling from top | Enemy pigs (62×48px) spawning every 0.5s |
| Bullets | Moving upward | Red projectiles (10×20px) from player |
| Timer | Top-right | ”Tiempo: Xs” showing survival time |
| Kill Count | Top-right | ”Asteroides: X” showing pigs destroyed |
| Score | Top-right | ”Puntos: X” showing current total |
| Status | Center-bottom | ”Jugando…” in red text |
| Instructions | Top-left | ESC and ENTER key reminders |
| Flash Effect | Full screen | White flash when shooting (0.1s duration) |
Active Systems
Movement System
Processes keyboard, touch, and mouse input for player movement within screen boundaries
Shooting System
Handles bullet creation with 0.3s cooldown, bullet movement, and removal when off-screen
Spawn System
Generates new pigs every 0.5 seconds at random horizontal positions
Collision System
Detects bullet-pig and bird-pig collisions
Available Controls
| Input | Action |
|---|---|
| Arrow Keys | Move bird (↑↓←→) |
| SPACE | Shoot bullet |
| Right Click | Shoot bullet (alternative) |
| Touch | Move bird toward touch position |
| ENTER | Show score screen overlay |
| ESC | Quit to game over |
State Updates
Every frame during PLAYING:- Delta time is calculated
- Game time increases
- Spawn timer increases (pigs spawn at 0.5s intervals)
- Player updates (position, bullets, cooldown)
- Obstacles update (position, removal if off-screen)
- Collision detection runs for bullets and bird
- Flash effect timer decreases if active
Music
Battle music (battle.mp3) loops at 150% volume during PLAYING state.GAME_OVER State
Description
The end state displayed when the player dies (collision) or quits (ESC). Shows final score and allows return to menu.Visual Elements
| Element | Description |
|---|---|
| Background | Red screen (solid red fill) |
| Title Text | ”GAME OVER” in yellow (center-top) |
| Score Text | ”Puntuación final: X” in yellow (center) |
| Instruction | ”Pulsa ENTER para volver al menú” (center-bottom) |
Score Calculation
When transitioning to GAME_OVER, the final score is calculated:The score is locked in at the moment of game over and cannot change in this state.
Available Controls
ENTER Key Only
Pressing ENTER returns to MENU state
State Cleanup
The GAME_OVER state does not perform cleanup. When ENTER is pressed and the game returns to MENU, the cleanup happens:- All obstacles are cleared from memory
- Game music stops
- Menu music starts
- Score resets to 0
- Kill counter resets to 0
- Game time resets to 0
- Spawn timer resets to 0
State Transitions
MENU → PLAYING
Triggers: SPACE key, ENTER key, or Touch input Actions:- Menu music pauses
- Battle music starts
- Player activates (engine sound plays)
- Game time resets to 0
- Spawn timer resets to 0
- All obstacles cleared
- State changes to PLAYING
This transition prepares a fresh game session with clean counters and cleared obstacles.
PLAYING → GAME_OVER
Triggers:- Player collision with pig (automatic)
- ESC key pressed (manual quit)
- Final score calculated
- State changes to GAME_OVER
- Game music continues playing until ENTER pressed
- Red screen displayed
- Score shown
When a collision occurs, the game immediately transitions to GAME_OVER without any “death animation” or delay.
GAME_OVER → MENU
Trigger: ENTER key Actions:- Game music stops
- Menu music starts
- All obstacles cleared
- Score resets to 0
- Kill counter resets to 0
- Game time resets to 0
- Spawn timer resets to 0
- State changes to MENU
State Management Implementation
State Change Mechanism
The game uses a two-variable system:Transition Process
Trigger Detected
When a state transition is triggered (button press, collision), the game sets:
nextGameStateto the target stategameStateChangedtotrue
State Change Processed
At the end of the render cycle, if
gameStateChanged is true:- Cleanup/initialization code runs
gameState=nextGameStategameStateChanged=false
This deferred transition system prevents state changes mid-frame, which could cause rendering glitches or logic errors.
Special State Features
Score Screen Overlay
While in PLAYING state, pressing ENTER shows a score screen overlay:- Does NOT change the game state
- Pauses background music
- Shows detailed score information
- Can be dismissed to resume
- Gameplay continues in background (not paused)
Pause State
The game has agamePaused boolean variable, but it’s only used for the score screen overlay feature:
There is no built-in pause function that stops gameplay. Pressing ESC immediately ends the game.
State-Specific Music
| State | Music | Volume | Looping |
|---|---|---|---|
| MENU | angry-birds.mp3 | 50% | Yes |
| PLAYING | battle.mp3 | 150% | Yes |
| GAME_OVER | (continues previous) | - | - |