Skip to content

starsentry/java_2d_game

Repository files navigation

Java 2D Space Shooter Game

A classic 2D space shooter game built with Java Swing, featuring smooth gameplay, particle effects, and sound effects.

game

🎮 Game Overview

This is a 2D space shooter game where you control a spaceship and fight against enemy rockets. The game features:

  • Smooth 60 FPS gameplay with optimized rendering
  • Realistic physics with momentum and collision detection
  • Particle explosion effects with alpha blending
  • Sound effects for shooting, hits, and explosions
  • Health system with visual HP bars
  • Score tracking system

🎯 Gameplay Features

Controls

  • A/D Keys: Rotate spaceship left/right
  • Space: Accelerate forward
  • J Key: Fire small bullets (rapid fire)
  • K Key: Fire large bullets (powerful shots)
  • Enter: Restart game after game over

Game Mechanics

  • Player Ship: 50 HP, can be destroyed by enemy rockets
  • Enemy Rockets: Spawn every 3 seconds from both sides, 20 HP each
  • Bullets: Two types - small (5 damage) and large (20 damage)
  • Collision Detection: Precise shape-based collision using Java Area class
  • Explosion Effects: Multi-layered particle effects with different colors and sizes

🏗️ Project Structure

├── Main.java              # Main game window and initialization
├── PanelGame.java         # Core game logic, rendering, and game loop
├── Player.java            # Player spaceship with movement and shooting
├── Rocket.java            # Enemy rocket AI and behavior
├── Bullet.java            # Projectile system for both player and enemies
├── Effect.java            # Particle explosion effects
├── HP.java                # Health point system
├── HpRender.java          # Health bar rendering
├── Key.java               # Keyboard input handling
├── ModelBoom.java         # Explosion particle model
├── Sound.java             # Audio system for game sounds
├── shoot.wav              # Shooting sound effect
├── hit.wav                # Hit sound effect
└── destroy.wav            # Destruction sound effect

🚀 Technical Features

Graphics & Rendering

  • Double buffering for smooth animation
  • Anti-aliasing for crisp graphics
  • Affine transformations for sprite rotation
  • Alpha compositing for particle effects
  • Custom shape collision detection

Game Engine

  • Multi-threaded architecture with separate threads for:
    • Main game loop (60 FPS)
    • Keyboard input handling
    • Bullet physics
    • Enemy spawning
  • Frame rate limiting to maintain consistent 60 FPS
  • Memory management with automatic cleanup of off-screen objects

Audio System

  • Java Sound API integration
  • Non-blocking audio playback
  • Multiple sound channels for different game events

🎨 Visual Effects

The game features sophisticated particle effects:

  • Multi-layered explosions with different particle sizes
  • Color-coded effects (yellow, cyan, red, white particles)
  • Alpha fading for realistic explosion dissipation
  • Randomized particle distribution for natural-looking effects

🎵 Audio Design

Three distinct sound effects enhance the gameplay:

  • Shoot Sound: Triggered when firing bullets
  • Hit Sound: Played when bullets hit enemies
  • Destroy Sound: Played when enemies or player are destroyed

🛠️ Requirements

  • Java 8+ (uses modern Java features)
  • Java Swing (included in JDK)
  • Java Sound API (included in JDK)

🚀 How to Run

  1. Compile the project:

    javac *.java
  2. Run the game:

    java Main
  3. Enjoy the game! Use the controls above to play.

🎯 Game Objectives

  • Survive as long as possible against waves of enemy rockets
  • Score points by destroying enemy rockets
  • Avoid collisions with enemy rockets to maintain your health
  • Use strategy - small bullets for rapid fire, large bullets for powerful shots

🔧 Development Notes

This project demonstrates several advanced Java programming concepts:

  • Object-oriented design with proper inheritance and encapsulation
  • Game loop architecture with frame rate control
  • Multi-threading for responsive gameplay
  • Graphics programming with Java 2D API
  • Audio programming with Java Sound API
  • Mathematical calculations for physics and collision detection

📝 License

This project is open source and available for educational purposes.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages