Block Breaker

1. Introduction to Block Breaker

This is a Java-based Block (Brick) Breaker game built entirely using Swing and AWT. The objective is simple yet engaging: the player controls a paddle to bounce a ball that destroys a wall of bricks. The game ends when the player either breaks all bricks (win) or the ball falls below the paddle (game over).

The game is interactive, animated using a Swing Timer, and incorporates real-time collision detection and sound effects to provide immediate audio feedback on ball bounces and game states.

2. Core Gameplay Features

1. Paddle Control: The player can move the paddle left and right using the keyboard's arrow keys.
2. Ball Launch: The game begins when the UP arrow key is pressed, causing the ball to launch upward from the paddle.
3. Brick Grid: Bricks are arranged in a 2D grid and disappear when hit by the ball.
4. Game States: The game ends either when all bricks are cleared (win) or the ball falls below the screen (loss).
5. Sound Feedback: Audio cues play for paddle hits, wall hits, brick breaks, game over, and win events.

3. How the Game Works Internally

1. Animation Loop: The javax.swing.Timer is set to trigger every 10ms and calls actionPerformed() to update game logic and repaint the screen.
2. Ball Physics: When the ball collides with the paddle, its bounce angle is determined using the horizontal offset from the center of the paddle. The formula uses basic trigonometry to compute the reflection angle:
    - The offset is normalized to a range between -1 and 1.
    - A bounce angle is then calculated as angle = 45° × offset.
    - This angle (in radians) is used to compute the new ball velocity:
        • ballXVelocity = speed × sin(angle)
        • ballYVelocity = -speed × cos(angle)
    - The sine function determines the horizontal (X) movement direction, while the cosine function controls the vertical (Y) movement.
    - This ensures the bounce angle changes depending on where the ball hits the paddle — hitting the edge results in a steeper angle, while hitting the center results in a near-vertical bounce.
3. Collision Detection: Uses Rectangle intersection checks to detect collisions with the paddle, walls, and bricks.
4. Brick Removal: Bricks are stored in an ArrayList. On collision, the brick is removed and the ball's vertical direction is reversed.
5. Paddle Movement: Boolean flags moveLeft and moveRight are toggled via KeyListener and used in each frame to move the paddle.

4. Technical Design Overview

1. Custom JPanel: The entire game view is painted inside a subclass of JPanel, where all rendering happens in the overridden paint() method.
2. Real-Time Graphics: The game is double-buffered using Swing’s default mechanism, ensuring smooth visuals at 100 FPS.
3. Sound Playback: The playSound() method loads .wav files and uses the Clip class from javax.sound.sampled to play audio effects.
4. Brick Generation: Bricks are dynamically generated with calculated positions using looped rows and columns, based on panel width.
5. Game Reset: If the player wins or loses, a JOptionPane displays a message and the application exits using System.exit(0).