Overview
SpaceEscape is the main game class that extends libGDX’s Game class. It manages the complete game lifecycle including rendering, input handling, state transitions, and resource management.
The game features an Angry Birds adaptation where players control a red bird that shoots at falling pig enemies. The game includes three main states (MENU, PLAYING, GAME_OVER) and features a complete scoring system based on survival time and enemies destroyed.
Class Declaration
com.pmm.games
Authors: Adrián Álvarez y Alejandro Medina
Version: 1.1 - Edición Angry Birds
Angry Birds Adaptations
This game is a complete thematic transformation from a space shooter to an Angry Birds-themed game:Visual Elements
- Player: Red bird (
red.png) representing the controllable character - Obstacles: Pigs (
cerdo.png) as enemies to destroy - Bullets: Red projectiles (
bala.jpg) representing bird attacks
Audio Theme
- Menu Music: Angry Birds main theme (
angry-birds.mp3) - Game Music: Epic battle music (
battle.mp3) - Sound Effects:
- Shoot: Launch sound (
piuw.mp3) - Explosion: Pig death sound (
muerteCerdo.mp3) - Motor: Tension drone sound (
tension-drones.mp3)
- Shoot: Launch sound (
Game Mechanics
- Asteroids are now pigs falling from above
- Player is a bird defending its position
- Score = survival time + 10 points per pig destroyed
- Visual effects include shooting flash and adjusted hitboxes
Cross-Platform Controls
- Touch: Swipe to move
- Keyboard: Arrow keys for movement, SPACE to shoot
- Mouse: Right-click as alternative shooting method
Key Fields
Graphics and Rendering
Sprite batch for rendering graphics
Background texture for the game (
noche.jpg)Font for rendering text on screen
Game logo for the menu screen (
space.jpg)Single white pixel texture for visual effects (flash)
Game State Management
Current game state (MENU, PLAYING, or GAME_OVER)
Next game state for transition handling
Indicates if the game state has changed and needs processing
Indicates if the game is paused
Game Objects
Controllable player instance (the red bird)
List of active obstacles (pigs) in the game
Texture used for obstacles (
cerdo.png)Texture used for player bullets (
bala.jpg)Timing and Scoring
Timer for generating new obstacles (spawns every 0.5 seconds)
Total time elapsed during the current game
Total player score
Counter for asteroids (pigs) destroyed by the player
Audio
Background music for the menu screen (Angry Birds theme)
Background music during gameplay (battle music)
Sound effect for pig explosions (
muerteCerdo.mp3)Visual Effects
Controls whether the flash effect is active
Timer for the flash effect duration (0.1 seconds)
Screens
Score screen to display results
Indicates if the score screen should be displayed
Methods
create()
- Creates sprite batch for rendering
- Loads all textures (background, logo, player, obstacles, bullets)
- Initializes fonts and UI elements
- Creates player at centered position (x: center, y: 50) with size 64x64 and speed 5f
- Initializes empty obstacle array
- Loads Angry Birds theme music for menu (volume 0.5, looping)
- Loads battle music for gameplay (volume 1.5, looping)
- Loads pig death sound effect
- Creates white pixel texture for flash effects
- Initializes score counters to 0
render()
- Clears screen with dark blue color (0.15, 0.15, 0.2)
- Calls
actualizarObjetos()to update game logic - Calls
gestionarInputs()to handle user input - Renders current state via
representacionEstado() - Processes state changes if
gameStateChangedis true
- GAME_OVER → MENU: Clears obstacles, stops game music, plays menu music, resets score/counters/timers
- MENU → PLAYING: Pauses menu music, plays game music, activates player, resets game time and spawn timer, clears obstacles
dispose()
- Sprite batch
- All textures (background, player, obstacles, bullets, white pixel)
- Fonts
- Player object
- Background music (menu and game)
- Sound effects
gestionarInputs()
- Touch screen → Start game
- SPACE or ENTER → Start game
- Touch screen → Move player towards touch position (with 5-pixel dead zone)
- Arrow keys → Move player (LEFT, RIGHT, UP, DOWN)
- SPACE → Shoot bullet and trigger flash effect
- Right mouse button → Shoot bullet and trigger flash effect
- ENTER → Calculate final score and show score screen
- ESCAPE → End game (transition to GAME_OVER)
- ENTER → Return to menu, stop game music, show score screen
actualizarObjetos()
- Gets delta time for frame-independent updates
- If PLAYING state:
- Increments game time
- Increments spawn timer
- Spawns new obstacle every 0.5 seconds
- Updates player
- Updates all obstacles and removes off-screen ones
- Detects bullet-asteroid collisions
- Updates flash effect timer
detectarColisionesBalasAsteroides()
- Iterates through all bullets (reverse order)
- For each bullet, checks collision with all obstacles
- On collision:
- Calls
obstacle.destroy() - Plays explosion sound at 70% volume
- Increments
asteroidsDestroyedcounter - Deactivates and removes bullet
- Breaks inner loop to prevent multiple hits
- Calls
calcularPuntuacionFinal()
addObstacle()
- Width: 62 pixels
- Height: 48 pixels
- X position: Random between 0 and screen width minus obstacle width
- Y position: Top of screen (screen height)
- Speed: 3f pixels per frame
representacionEstado()
- Draws game logo at full screen
- Displays instructions: “Pulsa ESPACIO o ENTER para comenzar”
- Shows controls: “FLECHAS para mover, ESPACIO para disparar”
- Shows score viewing option: “ENTER para ver puntuación durante el juego”
- Plays menu music
- Draws night background
- Renders flash effect if active (white overlay at 60% opacity)
- Displays UI text:
- White: “Pulsa ESC para terminar”, “ENTER para ver puntuación”
- Yellow: Time, asteroids destroyed count, current score (top-right)
- Red: “Jugando…” centered
- Renders player and all bullets
- Renders all obstacles
- Checks player-obstacle collisions → triggers GAME_OVER
- Renders score screen if
showScoreScreenis true
- Clears screen with red color
- Displays in yellow:
- “GAME OVER” title
- “Puntuación final: ”
- “Pulsa ENTER para volver al menú”
Usage Example
State Flow Diagram
Related Classes
- GameState - Enum defining the three game states
Player- Player character class with movement and shootingObstacle- Obstacle (pig) class with movement and collisionBullet- Bullet projectile classScoreScreen- Score display screen