0% found this document useful (0 votes)
42 views18 pages

Acn Microproject

A basic breakout game in Java can be created with the following elements: 1) Set up the game window and initialize game elements like the paddle, ball, and bricks. 2) Implement a game loop to continuously update and render the game state. 3) Detect collisions between the ball and other elements to bounce the ball and remove bricks. 4) Track the player's score and implement win/loss conditions like clearing all bricks or losing all lives.

Uploaded by

tajayaan18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views18 pages

Acn Microproject

A basic breakout game in Java can be created with the following elements: 1) Set up the game window and initialize game elements like the paddle, ball, and bricks. 2) Implement a game loop to continuously update and render the game state. 3) Detect collisions between the ball and other elements to bounce the ball and remove bricks. 4) Track the player's score and implement win/loss conditions like clearing all bricks or losing all lives.

Uploaded by

tajayaan18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

INTRODUCTION

A breakout game in Java is a classic arcade-style game where the player controls a paddle at the bottom of the
screen to bounce a ball and break a wall of bricks. The goal is to clear all the bricks from the screen by hitting
them with ODUCTION

the ball. Here's a simple outline of how you might create a basic breakout game in Java:

Setting Up the Game Window:

Use a graphics library like Swing or JavaFX to create a window for the game.

Set up the dimensions, background, and any necessary game settings.

Game Elements:

Create classes for game entities such as the paddle, ball, and bricks.

Define their properties such as position, size, and speed.

Game Loop:

Implement a game loop that continuously updates and renders the game.

Update the ball's position based on its velocity.

Check for collisions between the ball and the paddle, as well as the ball and the bricks.

User Input:

Capture user input (keyboard) to move the paddle left and right.

Update the paddle's position accordingly within the game loop.

Collision Detection and Response:

Detect collisions between the ball and game elements (paddle, bricks, walls).

Adjust the ball's direction when it hits different surfaces (angles for bricks, reflection for paddle).

Brick Destruction:

When the ball collides with a brick, remove the brick from the game.

Update the player's score based on the number of bricks destroyed.

Winning and Losing:

Check conditions to determine if the player has won (all bricks are destroyed) or lost (ball goes below the
paddle).

Graphics and Rendering:

Render the game elements on the screen using the chosen graphics library.

Update the display within the game loop to reflect changes in position and state.

Sound Effects and Music:

Enhance the gameplay experience with sound effects for collisions, brick destruction, and other events.

Add background music to make the game more engaging.

Game Over and Restart:

Implement logic to handle game over and allow the player to restart the game.
Remember that this is a simplified overview, and creating a complete and polished breakout game involves
more details and considerations. You can find tutorials, resources, and sample code online to help you
implement each aspect of the game. As you gain more experience, you can also explore adding additional
features like power-ups, different levels, and more complex gameplay mechanics.

METHODOLOGY

Creating a breakout game as a microproject in Java can be simplified while still covering the essential aspects.
Here's a streamlined methodology:

Setup and Initialization: Set up the game window and initialize the game components such as the ball, paddle,
and bricks.

Game Loop: Implement a game loop that repeatedly updates the game state and renders it on the screen.
Input Handling: Capture user input to control the paddle's movement using keyboard events.

Collision Detection: Detect collisions between the ball, paddle, and bricks. Update ball direction and remove
bricks upon collision.

Physics and Movement: Implement basic physics to control the ball's movement, bouncing off walls, paddle,
and bricks.

Brick Generation: Create a simple grid of bricks using arrays or collections. Each brick can have a basic state
(e.g., present or destroyed).

Scoring: Update and display the player's score based on the number of bricks destroyed.

Win/Loss Condition: Implement conditions to determine when the player wins (all bricks destroyed) or loses
(ball goes below the paddle).

Graphics and Rendering: Draw the game elements (ball, paddle, bricks) onto the screen using basic graphics
functions.

Game Over and Restart: Provide an option to restart the game after winning or losing.

Simple User Interface: Create a basic user interface with buttons for starting, restarting, and exiting the game.

Comments and Documentation: Add comments to your code explaining important sections and functions. Write
a brief documentation outlining how to play the game and control the paddle.

Since this is a microproject, focus on simplicity and core functionality. You can use Java's Swing library for
graphics and event handling, which is relatively straightforward. Start with the most basic features and gradually
enhance the game as time allows. This approach will allow you to learn and demonstrate the key concepts of
game development within the scope of a microproject.
PROGRAM

Main program:

import acm.graphics.*;

import acm.program.*;

import acm.util.*;

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class Breakout extends GraphicsProgram {

public void init() {

setBricks();

add(paddle);

add(ball,WIDTH/2,500);
lifeBar.setColor(Color.WHITE);

add(lifeBar,20,60);

addMouseListeners();

setBackground(Color.BLACK);

public void run() {

while(gameOver != true) {

pause(20);

checkCollision(ball);

ball.move();

if (prize != null) {

prize.drop();

if (prize.leftScreen()) remove(prize);

addMouseListeners();

private void setBricks() {

for (int row = 0; row < NBRICK_ROWS; row++) {

Color rowColor = checkColor(row);

for (int col = 0; col < NBRICKS_PER_ROW; col++) {

add(new Brick(col,row,rowColor));

public void mouseClicked(MouseEvent e) {

removeBrick(getElementAt(e.getX(),e.getY()));

public void mouseMoved(MouseEvent e) {


paddle.followMouse(e.getX());

private Color checkColor(int row) {

switch (row) {

case 0:

case 1: return Color.RED;

case 2:

case 3: return Color.ORANGE;

case 4:

case 5: return Color.YELLOW;

case 6:

case 7: return Color.GREEN;

case 8:

case 9: return Color.CYAN;

return Color.CYAN;

private void removeBrick(GObject brick) {

/* add test to see if removing brick is possible */

if (brick != null && brick instanceof Brick) {

remove(brick);

if (((Brick) brick).isSpecial()) callPrize(brick.getX(),brick.getY());

if (Brick.decreaseBricks()) callGameOver(true);

private void removeLife() {

lives--;

lifeBar.setLabel("Lives: " + lives);

if (lives <= 0) callGameOver(false);


}

public void callPrize(double x, double y) {

prize = new Prize(x,y,Color.MAGENTA, paddle);

add(prize);

public void callGameOver(boolean won) {

GRect gameOverScreen = new GRect(0,0,WIDTH,HEIGHT);

gameOverScreen.setFilled(true);

gameOverScreen.sendToFront();

GLabel gameOverLabel = new GLabel("");

if (won == true) {

gameOverScreen.setColor(Color.WHITE);

gameOverLabel.setColor(Color.BLUE);

gameOverLabel.setLabel("WOO YOU WON!!!");

if (lives == 3) gameOverLabel.setLabel("HOLY SHIT PERFECT SCORE!!!");

} else {

gameOverScreen.setColor(Color.RED);

gameOverLabel.setColor(Color.WHITE);

gameOverLabel.setLabel("HAH WHAT A LOSER");

add(gameOverScreen);

add(gameOverLabel,WIDTH/2-gameOverLabel.getWidth()/2,HEIGHT/2+gameOverLabel.getAscent()/2);

gameOver = true;

private void checkCollision(Ball ball) {

double r = ball.getRadius();

ball.move();

double x = ball.getX();

double y = ball.getY();

ball.moveBack();
GObject collider = null;

int dir = 0;

// Check walls + keep within bounds (keep from getting stuck)

if (x-r <= 0) {

ball.setLocation(r,y);

ball.changeDirection(1);

} else if (x+r >= WIDTH) {

ball.setLocation(WIDTH-r,y);

ball.changeDirection(1);

if (y-r <= 0) {

ball.setLocation(x,r);

ball.changeDirection(2);

// less life

} else if (y+r >= HEIGHT) {

removeLife();

ball.restart();

//Check elements

//Check right

if (getElementAt(x+r,y) != null && getElementAt(x+r,y) != ball && !(getElementAt(x+r,y) instanceof


GLabel) && !(getElementAt(x+r,y) instanceof Prize)) {

ball.changeDirection(1);

collider = getElementAt(x+r,y);

//Check left

if (getElementAt(x-r,y) != null && getElementAt(x-r,y) != ball && !(getElementAt(x-r,y) instanceof


GLabel) && !(getElementAt(x-r,y) instanceof Prize)) {

ball.changeDirection(1);

collider = getElementAt(x-r,y);

}
//Check bottom

if (getElementAt(x,y+r) != null && getElementAt(x,y+r) != ball && !(getElementAt(x,y+r) instanceof


GLabel) && !(getElementAt(x,y+r) instanceof Prize)) {

ball.changeDirection(2);

collider = getElementAt(x,y+r);

//Check top

if (getElementAt(x,y-r) != null && getElementAt(x,y-r) != ball && !(getElementAt(x,y-r) instanceof


GLabel) && !(getElementAt(x,y-r) instanceof Prize)) {

ball.changeDirection(2);

collider = getElementAt(x,y-r);

ball.changeDirection(dir);

if (collider instanceof Brick) removeBrick(collider);

if (collider == paddle) {

ball.setDirection(paddle.collide(x,r));

/* Init things */

private Paddle paddle = new Paddle();

private Ball ball = new Ball(BALL_RADIUS);

/** Width and height of application window in pixels */

public static final int APPLICATION_WIDTH = 400;

public static final int APPLICATION_HEIGHT = 600;

/** Dimensions of game board (usually the same) */

public static final int WIDTH = APPLICATION_WIDTH;

public static final int HEIGHT = APPLICATION_HEIGHT;

/** Offset of the paddle up from the bottom */

public static final int PADDLE_Y_OFFSET = 30;


/** Number of bricks per row */

public static final int NBRICKS_PER_ROW = 10;

/** Number of rows of bricks */

private static final int NBRICK_ROWS = 10;

/** Radius of the ball in pixels */

private static final int BALL_RADIUS = 10;

/** Offset of the top brick row from the top */

public static final int BRICK_Y_OFFSET = 70;

/** Number of turns */

private static final int NTURNS = 3;

/** Points */

private int lives = NTURNS;

/** Game is Over */

private boolean gameOver = false;

/* Prize on Board */

Prize prize = null;

private GLabel lifeBar = new GLabel("Lives: " + lives);

Ball Class

import acm.graphics.*;

import acm.util.*;

import java.awt.*;
public class Ball extends GCompound {

public Ball(double radius) {

r = radius;

GOval ball = new GOval(-r,-r,r*2,r*2);

ball.setFilled(true);

ball.setColor(new Color(200,200,200));

GOval light = new GOval(-r+3,-r+3,r*2-6,r-2);

light.setFilled(true);

light.setColor(new Color(255,255,255,150));

add(ball);

add(light);

direction = rgen.nextDouble(220,320);

direction = 290;

public void move() {

this.movePolar(velocity,direction);

public void moveBack() {

this.movePolar(velocity, (direction+180)%360);

public void restart() {

this.setVisible(false);

pause(100);

this.setVisible(true);

pause(100);
this.setVisible(false);

pause(100);

this.setLocation(Breakout.WIDTH/2,Breakout.HEIGHT/2);

this.setVisible(true);

direction = rgen.nextDouble(220,320);

public double getRadius() {

return r;

public void changeDirection(int xory) {

if (xory == 1) direction -= (direction-270)*2;

if (xory == 2) direction -= (direction-180)*2;

public double getDirection() {

return direction;

public static void setVelocity(double v) {

velocity = v;

public void setDirection(double d) {

direction = d;

private double r;

private double direction;

private static final double STARTING_VELOCITY = 5.0;

private static double velocity = STARTING_VELOCITY;


private RandomGenerator rgen = RandomGenerator.getInstance();

Brick Class

import acm.graphics.*;

import acm.util.*;

import java.awt.*;

public class Brick extends GCompound {

public Brick(int colNum, int rowNum, Color color) {

if (rgen.nextBoolean(0.01)) {

color = Color.MAGENTA;

special = true;

GRect fill = new GRect(0,0,BRICK_WIDTH, BRICK_HEIGHT);

fill.setFilled(true);

fill.setColor(color);

GRect light = new GRect(0,0,BRICK_WIDTH,BRICK_HEIGHT/2);

light.setFilled(true);

light.setColor(new Color(255,255,255,120+10*rowNum));

if (special == true) light.setColor(new Color(255,255,255,120));

add(fill);

add(light);

// Find and set location

double x = START_X + colNum * (BRICK_WIDTH + BRICK_SEP);

double y = START_Y + rowNum * (BRICK_HEIGHT + BRICK_SEP);

setLocation(x,y);
// Add to bricks remaining;

bricksRemaining++;

public static int getBricksRemaining() {

return bricksRemaining;

/* decreases bricks remaining and checks for end */

public static boolean decreaseBricks() {

bricksRemaining--;

if (bricksRemaining < 75) Ball.setVelocity(7); //Set the velocity higher if

if (bricksRemaining < 50) Ball.setVelocity(9);

if (bricksRemaining < 25) Ball.setVelocity(11);

return (bricksRemaining > 0) ? false : true;

public boolean isSpecial() {

return special;

/** Separation between bricks */

public static final int BRICK_SEP = 4;

/** Width of a brick */

public static final int BRICK_WIDTH =

(Breakout.WIDTH - (Breakout.NBRICKS_PER_ROW - 1) * BRICK_SEP) /


Breakout.NBRICKS_PER_ROW;

/** Height of a brick */

public static final int BRICK_HEIGHT = 12;


/** Starting point X and Y values for first brick */

private static final double START_X = (Breakout.WIDTH - ((Breakout.NBRICKS_PER_ROW *


BRICK_WIDTH) + (Breakout.NBRICKS_PER_ROW - 1) * BRICK_SEP)) / 2 ;

private static final int START_Y = Breakout.BRICK_Y_OFFSET;

private boolean special = false;

private static int bricksRemaining = 0;

private RandomGenerator rgen = RandomGenerator.getInstance();

Paddle Class

import acm.graphics.*;

import java.awt.*;

public class Paddle extends GCompound {

public Paddle() {

fill = new GRect(0,0, PADDLE_STARTING_WIDTH, PADDLE_HEIGHT);

fill.setFilled(true);

fill.setColor(Color.GRAY);

light = new GRect(0,PADDLE_HEIGHT/2, PADDLE_STARTING_WIDTH, PADDLE_HEIGHT/2);

light.setFilled(true);

light.setColor(new Color(0,0,0,50));

add(fill);

add(light);

this.setLocation((Breakout.WIDTH - PADDLE_STARTING_WIDTH) / 2, PADDLE_Y);

public void followMouse(double mouseX) {

if (mouseX < paddleWidth/2) {

this.setLocation(0, PADDLE_Y);
} else if (mouseX > Breakout.WIDTH - paddleWidth/2) {

this.setLocation(Breakout.WIDTH - paddleWidth,PADDLE_Y);

} else {

this.move(mouseX-paddleWidth/2 - this.getX(), 0);

public double collide(double ballX, double r) {

double paddleX = this.getX();

setColor(Color.RED);

double hitSpot = ballX - paddleX;

double maxPaddle = paddleWidth + r;

double minPaddle = -r;

double paddleRange = maxPaddle - minPaddle;

double minAngle = 160;

double maxAngle = 20;

double angleRange = maxAngle - minAngle;

double newDirection = ((hitSpot * angleRange) / paddleRange) + minAngle;

return newDirection;

private void widen() {

paddleWidth += 30;

this.move(-15, 0);

fill.setSize(paddleWidth, PADDLE_HEIGHT);

light.setSize(paddleWidth, PADDLE_HEIGHT / 2);

public void checkForPrize(Prize prize, double x, double y) {

if ((y >= PADDLE_Y) && (y <= PADDLE_Y + PADDLE_HEIGHT) && x > this.getX() && x <
this.getX() + paddleWidth) {

prize.pickUp();
this.widen();

/** Dimensions of the paddle */

private static final int PADDLE_STARTING_WIDTH = 60;

private static final int PADDLE_HEIGHT = 15;

private GRect fill;

private GRect light;

private int paddleWidth = PADDLE_STARTING_WIDTH;

/** Dimensions of the paddle */

public static final int PADDLE_Y = Breakout.HEIGHT - Breakout.PADDLE_Y_OFFSET;

Prize Class

impo

You might also like