Overview
GameState is an enumeration that represents the different states of the game. Each state defines a specific phase in the game flow, controlling what is displayed on screen and how user input is handled.
Enum Declaration
com.pmm.games
Author: juegos
Values
MENU
- Displays the game logo at full screen
- Shows game instructions and controls
- Plays Angry Birds theme music on loop
- Waits for player input to transition to PLAYING state
- Touch screen anywhere → Start game
- Press SPACE or ENTER → Start game
- Game logo background
- Instructions text: “Pulsa ESPACIO o ENTER para comenzar”
- Controls guide: “Controles: FLECHAS para mover, ESPACIO para disparar”
- Score viewing hint: “ENTER para ver puntuación durante el juego”
PLAYING
- Night sky background is rendered
- Player (red bird) is active and controllable
- Obstacles (pigs) spawn every 0.5 seconds and fall from the top
- Bullets can be fired to destroy obstacles
- Score is calculated in real-time
- Collision detection is active
- Battle music plays on loop
- Arrow keys → Move player (UP, DOWN, LEFT, RIGHT)
- Touch screen → Move player towards touch position
- SPACE or right mouse button → Shoot bullet
- ENTER → Show score screen (game continues in background)
- ESCAPE → End game (transition to GAME_OVER)
- Night background (
noche.jpg) - Player sprite and bullets
- Falling obstacle sprites (pigs)
- Real-time score display (top-right)
- Time counter (seconds)
- Asteroids destroyed counter
- Current points:
time + (asteroids × 10) - Status text: “Jugando…” (centered)
- Flash effect on shooting
- Obstacles spawn every 0.5 seconds at random X positions
- Player-obstacle collision → Immediate GAME_OVER
- Bullet-obstacle collision → Obstacle destroyed, +1 to counter, explosion sound
- Score formula:
1 point per second + 10 points per pig destroyed
GAME_OVER
- Red screen background
- Final score is calculated and displayed
- Game music stops
- All game objects remain in memory but are no longer updated
- Waits for player input to return to menu
- Press ENTER → Return to MENU state
- Red background (full screen)
- “GAME OVER” title (yellow, centered)
- Final score: “Puntuación final: ” (yellow)
- Instructions: “Pulsa ENTER para volver al menú” (yellow)
- All obstacles are cleared from the array
- Game music is stopped
- Menu music starts playing
- Score is reset to 0
- Asteroids destroyed counter is reset to 0
- Game time is reset to 0
- Spawn timer is reset to 0
State Transitions
The game uses a state machine pattern with explicit state transitions:Transition Details
MENU → PLAYING
Trigger: User touches screen or presses SPACE/ENTERActions:
- Pause menu music
- Play game music
- Activate player
- Reset game time to 0
- Reset spawn timer to 0
- Clear all obstacles
PLAYING → GAME_OVER
Trigger: Player presses ESCAPE or collides with obstacleActions:
- Calculate final score:
(int)gameTime + (asteroidsDestroyed × 10) - Keep all objects in memory
- Wait for user input
GAME_OVER → MENU
Trigger: User presses ENTERActions:
- Stop game music
- Play menu music
- Clear all obstacles from array
- Reset score to 0
- Reset asteroidsDestroyed to 0
- Reset gameTime to 0
- Reset spawnTimer to 0
- Show score screen
- Set gamePaused to true
Implementation Example
Here’s howGameState is used in the SpaceEscape class:
State-Specific Behavior
Input Handling by State
Rendering by State
Best Practices
Related Classes
- SpaceEscape - Main game class that uses GameState for flow control