Вы находитесь на странице: 1из 16

Running head: Programming Test

PROGRAMMING ASSIGNMENT

OMAR ATHMAN

JOMO KENYATTA UNIVERSITY OF AGRICULTURE AND


TECHNOLOGY

07/02/2020
Cooperative Breakout Game

A Cooperative Breakout Game

In this project you will develop a breakout game for Games Unlimited. The goal of the game is
to remove all bricks with a ball that is hit by the player. A brick is removed once it has been hit
by the player’s ball. The ball bounces off the removed brick. The player’s ball is controlled by a
paddle. The ball bounces from all edges but the lower edge. If the ball hits the lower edge of the
playing area, a life is lost. A player has up to three lives. If all bricks are removed from the
player’s screen, the game advances to the next level. If the player has no more lives left, the
game is over. If the high score is in the current top 10, the game asks the player, if an entry to the
high score list is desired. After each played game, the player should see the welcome screen
(detailed below). A typical game screen could look like this:

The game shows in the upper right the current score, the next score the player has to beat to
move up one place in the high score list, and the current rank in the high score list (based on the
current score). If the player is currently not in the top 10, the rank field would just show “-“. In
the lower right, the game displays the current game level, the remaining lives, and the initials of
the player.

The program has to be able to open game levels from a server and to store (and open) them
locally on persistent storage. In addition, the program has to be able to read a high score file
from a server and update it, if the player’s high score is in the top 10 and the player requests an
entry in the high score list.
Cooperative Breakout Game

When the player starts a game a welcome screen lets the player select whether he or she wants to
start a new game, download a new level from the server via a http connection, open a local level,
access the game rules in form of a help screen, see the current (top 10) high scores, or end the
game. If the user has to wait, for example, while downloading a level, the program needs to
show a progress bar. Similarly, in case of exceptional circumstances, such as an unavailable
connection to the server or a game level that cannot be properly initiated (due to a mistake in the
level design), the program should provide appropriate feedback to the player.

Further tasks

Some bricks have extra bonuses: a brick could split the ball into two or three balls, reduce or
enlarge the player’s paddle, or provide an extra life.

Each group has to design at least two game levels. A game level provides a description of the
layout for the bricks and requires a way to store bonus bricks (see above). The game level file
can only use relative measures to facilitate portability. Each brick is specified in relative sizes,
i.e., x % of the screen width and y % of the screen height. The game level file has to include the
speed, direction, and initial position of the ball, and the size of the paddle. You can assume that
the paddle is at rest and horizontally centered. Once several teams have implemented their
version of the game file, we will take the best one as a reference and all other programs should
be able read in this reference game file.

If the ball hits a paddle at rest it just reverses the direction of the ball in x- and y-direction.
However, if the player moves the paddle, the horizontal speed of the paddle is added to the
horizontal component of the ball’s speed.

Extra challenge

Up to now the game can only be played by a single player. For extra marks you can try to design
a collaborative version of this game where another player’s paddle is located at the opposite of
the screen. The game updates between the two players are performed via a Bluetooth connection.
This task is difficult since it requires a permanent synchronization between the two players. If
you like to take the collaborative game a step further, even 4 players could be connected for one
game, i.e., at each side of the game is a paddle of each participating player. The team loses the
game until either one of the players has no live left or, if there is a common number of lives for
all players, no live for the team is left.
Cooperative Breakout Game

Points to Check

• All errors are treated correctly (network, file system)


• Successful download of game level files
• Successfully load, update, and store the high score from/on the server
• Locally load game level files
• Design at least two game level files
• Being able to read the reference format
• Well designed game screen
• Game mechanics OK (brick removal, bonus bricks, bouncing behavior)
• Collision detection OK
• All updates OK (current score, lives, advance to next level, …)
• Nice welcome screen (for example a picture as background) including help screen

• Game works on different resolutions (also in portrait & landscape mode)


• Appropriate feedback (progress bar, successful download of files, …)
• Game fluid and plays well
Cooperative Breakout Game

CODING: I USED JAVA LANGUAGE PROGRAMMING


/*
* File: Breakout.java
* -------------------
* Name:
* Section Leader:
*
* This file will eventually implement the game of Breakout.
*/

import acm.graphics.*;
import acm.program.*;
import acm.util.*;

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Breakout extends GraphicsProgram {

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


public static final int APPLICATION_WIDTH = 450;
public static final int APPLICATION_HEIGHT = 650;

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


private static final int WIDTH = APPLICATION_WIDTH;
Cooperative Breakout Game

private static final int HEIGHT = APPLICATION_HEIGHT;

/** Dimensions of the paddle */


private static final int PADDLE_WIDTH = 70;
private static final int PADDLE_HEIGHT = 20;

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


private static final int PADDLE_Y_OFFSET = 30;

/** Number of bricks per row */


private static final int NBRICKS_PER_ROW = 15;

/** Number of rows of bricks */


private static final int NBRICK_ROWS = 10;

/** Separation between bricks */


private static final int BRICK_SEP = 4;

/** Width of a brick */


private static final int BRICK_WIDTH =
(WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;

/** Height of a brick */


private static final int BRICK_HEIGHT = 8;

/** Radius of the ball in pixels */


private static final int BALL_RADIUS = 10;
Cooperative Breakout Game

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


private static final int BRICK_Y_OFFSET = 70;

/** Number of turns */


private static final int NTURNS = 3;

/**Ball velocity*/
private double vx, vy;

/**Random number generator for vx*/


private RandomGenerator rgen = RandomGenerator.getInstance();

/** Animation delay or paust time between ball moves */


private static final int DELAY = 10;

/* Method: run() */
/** Runs the Breakout program. */
public void run() {
for(int i=0; i < NTURNS; i++) {
setUpGame();
playGame();
if(brickCounter == 0) {
ball.setVisible(false);
printWinner();
break;
}
if(brickCounter > 0) {
removeAll();
Cooperative Breakout Game

}
}
if(brickCounter > 0) {
printGameOver();
}
}

private void setUpGame() {


drawBricks( getWidth()/2, BRICK_Y_OFFSET);
drawPaddle();
drawBall();
}

//adding an individual brick object


private GRect brick;

//drawing all the bricks necessary for the game


private void drawBricks(double cx, double cy) {

/*need to have several columns in each row


* so there need to be two for loops,
* one for loop for the rows and one for loop for the columns.
*/

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

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


Cooperative Breakout Game

/* To get the x coordinate for the starting width:


* start at the center width,
* subtract half of the bricks (width) in the row,
* subtract half of the separations (width) between the bricks
in the row,
* now you're at where the first brick should be,
* so for the starting point of the next bricks in the column, you
need to:
* add a brick width
* add a separation width
*/

double x = cx - (NBRICKS_PER_ROW*BRICK_WIDTH)/2 -
((NBRICKS_PER_ROW-1)*BRICK_SEP)/2 + column*BRICK_WIDTH +
column*BRICK_SEP;

/* To get the y coordinate of the starting height:


* start at the given length from the top for the first row,
* then add a brick height and a brick separation for each of
the following rows
*/

double y = cy + row*BRICK_HEIGHT + row*BRICK_SEP;

brick = new GRect( x , y , BRICK_WIDTH , BRICK_HEIGHT );


add (brick);
brick.setFilled(true);

//Setting colors depending on which row the bricks are in


Cooperative Breakout Game

if (row < 2) {
brick.setColor(Color.RED);
}
if (row == 2 || row == 3) {
brick.setColor(Color.BLACK);
}
if (row == 4 || row == 5) {
brick.setColor(Color.YELLOW);
}
if (row == 6 || row == 7) {
brick.setColor(Color.GREEN);
}
if (row == 8 || row == 9) {
brick.setColor(Color.CYAN);
}
}
}
}

//adding individual paddle object


private GRect paddle;

//paddle set-up
private void drawPaddle() {
//starting the paddle in the middle of the screen
double x = getWidth()/2 - PADDLE_WIDTH/2;
//the paddle height stays consistent throughout the game
//need to make sure to subtract the PADDLE_HEIGHT,
Cooperative Breakout Game

//since the rectangle gets drawn from the top left corner
double y = getHeight() - PADDLE_Y_OFFSET - PADDLE_HEIGHT;
paddle = new GRect (x, y, PADDLE_WIDTH, PADDLE_HEIGHT);
paddle.setFilled(true);
add (paddle);
addMouseListeners();
}

//making the mouse track the paddle


public void mouseMoved(MouseEvent e) {
/* The mouse tracks the middle point of the paddle.
* If the middle point of the paddle is between half paddle width of the screen
* and half a paddle width before the end of the screen,
* the x location of the paddle is set at where the mouse is minus half a paddle's
width,
* and the height remains the same
*/
if ((e.getX() < getWidth() - PADDLE_WIDTH/2) && (e.getX() >
PADDLE_WIDTH/2)) {
paddle.setLocation(e.getX() - PADDLE_WIDTH/2, getHeight() -
PADDLE_Y_OFFSET - PADDLE_HEIGHT);
}

//adding an individual ball object


private GOval ball;
Cooperative Breakout Game

//ball set-up
private void drawBall() {
double x = getWidth()/2 - BALL_RADIUS;
double y = getHeight()/2 - BALL_RADIUS;
ball = new GOval(x, y, BALL_RADIUS, BALL_RADIUS);
ball.setFilled(true);
add(ball);
}

private void playGame() {


waitForClick();
getBallVelocity();
while (true) {
moveBall();
if (ball.getY() >= getHeight()) {
break;
}
if(brickCounter == 0) {
break;
}
}
}

private void getBallVelocity() {


vy = 4.0;
vx = rgen.nextDouble(1.0, 3.0);
Cooperative Breakout Game

if (rgen.nextBoolean(0.5)) {
vx = -vx;
}

private void moveBall() {


ball.move(vx, vy);
//check for walls
//need to get vx and vy at the point closest to 0 or the other edge
if ((ball.getX() - vx <= 0 && vx < 0 )|| (ball.getX() + vx >= (getWidth() -
BALL_RADIUS*2) && vx>0)) {
vx = -vx;
}
//We don't need to check for the bottom wall, since the ball can fall through the
wall at that point
if ((ball.getY() - vy <= 0 && vy < 0 )) {
vy = -vy;
}

//check for other objects


GObject collider = getCollidingObject();
if (collider == paddle) {
/* We need to make sure that the ball only bounces off the top part of the
paddle
* and also that it doesn't "stick" to it if different sides of the ball hit the
paddle quickly and get the ball "stuck" on the paddle.
* I ran "println ("vx: " + vx + ", vy: " + vy + ", ballX: " + ball.getX() + ",
ballY: " +ball.getY());"
* and found that the ball.getY() changes by 4 every time, instead of 1,
Cooperative Breakout Game

* so it never reaches exactly the the height at which the ball hits the
paddle (paddle height + ball height),
* therefore, I estimate the point to be greater or equal to the height at
which the ball hits the paddle,
* but less than the height where the ball hits the paddle minus 4.
*/

if(ball.getY() >= getHeight() - PADDLE_Y_OFFSET -


PADDLE_HEIGHT - BALL_RADIUS*2 && ball.getY() < getHeight() -
PADDLE_Y_OFFSET - PADDLE_HEIGHT - BALL_RADIUS*2 + 4) {
vy = -vy;
}
}
//since we lay down a row of bricks, the last brick in the brick wall is assigned the
value brick.
//so we narrow it down by saying that the collier does not equal to a paddle or
null,
//so all that is left is the brick
else if (collider != null) {
remove(collider);
brickCounter--;
vy = -vy;
}
pause (DELAY);
}

private GObject getCollidingObject() {

if((getElementAt(ball.getX(), ball.getY())) != null) {


Cooperative Breakout Game

return getElementAt(ball.getX(), ball.getY());


}
else if (getElementAt( (ball.getX() + BALL_RADIUS*2), ball.getY()) != null ){
return getElementAt(ball.getX() + BALL_RADIUS*2, ball.getY());
}
else if(getElementAt(ball.getX(), (ball.getY() + BALL_RADIUS*2)) != null ){
return getElementAt(ball.getX(), ball.getY() + BALL_RADIUS*2);
}
else if(getElementAt((ball.getX() + BALL_RADIUS*2), (ball.getY() +
BALL_RADIUS*2)) != null ){
return getElementAt(ball.getX() + BALL_RADIUS*2, ball.getY() +
BALL_RADIUS*2);
}
//need to return null if there are no objects present
else{
return null;
}

private void printGameOver() {


GLabel gameOver = new GLabel ("Game Over", getWidth()/2, getHeight()/2);
gameOver.move(-gameOver.getWidth()/2, -gameOver.getHeight());
gameOver.setColor(Color.RED);
add (gameOver);
}

private int brickCounter = 100;


Cooperative Breakout Game

private void printWinner() {


GLabel Winner = new GLabel ("Winner!!", getWidth()/2, getHeight()/2);
Winner.move(-Winner.getWidth()/2, -Winner.getHeight());
Winner.setColor(Color.RED);
add (Winner);
}
}

Вам также может понравиться