Link to code: Game.java
package yourwork;
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import ignorethis.Canvas;
import ignorethis.Sound;
import ignorethis.ValueText;
/**
* NAME :
* COURSE : CompSci 6
* PURPOSE :
* Represents the game of Breakout. It should enforce the rules of the game
* each moment in the game as well as award points.
*/
public class Game
{
// sounds used in the game
private static final Sound BOUNCE_NOISE = new Sound("sounds/boing.wav");
private static final Sound LOSER_NOISE = new Sound("sounds/buzz.wav");
private static final int LABEL_X_OFFSET = 40;
private static final int LABEL_Y_OFFSET = 25;
private static final int NUM_BLOCKS_IN_ROW = 12;
// game state
private BouncingBall myBall;
private Paddle myPaddle;
private List<Block> myBlocks;
private ValueText myScore;
private ValueText myLevel;
// random numbers
private Random myGenerator = new Random();
/**
* Create a game of the given size with the given display for its shapes.
*
* Note, we hold on to our own shapes so that we can more easily enforce
* the games rules (i.e., just checking against the blocks not all shapes).
* Must also add shapes to the display so they can be seen.
*/
public Game (Canvas canvas)
{
addLabels(canvas);
addGamePieces(canvas);
}
/**
* Enforce the rules of the game.
*/
public void update (Canvas canvas)
{
// hit bottom
if (myBall.getBottom() >= canvas.getHeight())
{
LOSER_NOISE.play();
// TODO: lose a life and reset ball
// TODO: if all lives lost, announce game is over
}
// bounce off paddle
if (myPaddle.intersects(new Point(myBall.getCenter().x, myBall.getBottom())))
{
// TODO: make ball go back up if it hits the paddle
}
// TODO: bounce myBall off blocks
for (Block b : myBlocks)
{
// TODO: check to see if ball has hit this block
// TODO: update score, play BOUNCE_NOISE and remove block if necessary
}
}
/**
* Returns a new color with random red, green, and blue values.
*/
protected Color makeRandomColor ()
{
// TODO: return a randomColor (note myGenerator object)
return new Color(127, 127, 127);
}
/**
* Create the ball, paddle, and blocks that will appear in the game.
*
* @param canvas in which things will be drawn
*/
private void addGamePieces (Canvas canvas)
{
myBall = new BouncingBall(
new Point(canvas.getSize().width / 2,
canvas.getSize().height / 2),
new Dimension(16, 16),
new Point(4, -4),
"images/ball.gif");
canvas.add(myBall);
myPaddle = new Paddle(
new Point(canvas.getSize().width / 2,
canvas.getSize().height - 50),
new Dimension(80, 16),
"images/paddle.gif");
canvas.add(myPaddle);
addBlocks(canvas, myLevel.getValue());
}
/**
* Add a different configuration of blocks for each level.
*/
private void addBlocks (Canvas canvas, int level)
{
myBlocks = new ArrayList<Block>();
int width = canvas.getSize().width / NUM_BLOCKS_IN_ROW;
for (int c = 0; c < NUM_BLOCKS_IN_ROW; c++)
{
Block b = new Block(
new Point(width / 2 + c * width, 100),
new Dimension(width, 20),
makeRandomColor(),
1);
myBlocks.add(b);
canvas.add(b);
}
}
/**
* Create the status labels that will appear in the game.
*
* @param canvas in which things will be drawn
*/
private void addLabels (Canvas canvas)
{
myScore = new ValueText(
new Point(LABEL_X_OFFSET, LABEL_Y_OFFSET),
Color.GREEN,
"Score", 0);
canvas.add(myScore);
myLevel = new ValueText(
new Point(canvas.getSize().width - LABEL_X_OFFSET, LABEL_Y_OFFSET),
Color.GREEN,
"Level", 1);
canvas.add(myLevel);
// TODO: add ValueText object for lives status
}
}