-
Notifications
You must be signed in to change notification settings - Fork 2
/
Jail.java
80 lines (67 loc) · 2.33 KB
/
Jail.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.List;
/**
* Breakout of the Jail.
*
* @author Cody A. Ray
* @version December 8, 2012
*/
public class Jail extends World
{
private static final Brick[] BRICK_COLUMN = new Brick[] {
new Brick(Color.RED, 7), new Brick(Color.RED, 7),
new Brick(Color.ORANGE, 5), new Brick(Color.ORANGE, 5),
new Brick(Color.GREEN, 3), new Brick(Color.GREEN, 3),
new Brick(Color.YELLOW, 1), new Brick(Color.YELLOW, 1)
};
private static final int BRICK_ROWS = BRICK_COLUMN.length;
private static final int BRICK_COLS = 12;
private static final int BALL_MAX_SPEED = 2;
private final Counter scoreCounter;
/**
* Create a new Jail to Breakout.
*/
public Jail()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
// create a black background
GreenfootImage background = getBackground();
background.setColor(Color.BLACK);
background.fill();
Paddle paddle = new Paddle();
addObject(paddle, getWidth()/2, getHeight()*7/8);
addBricks(BRICK_ROWS, BRICK_COLS);
Ball ball = new Ball(1, 2);
addObject(ball, getWidth()/2, getHeight()/2);
scoreCounter = new Counter("Score: ");
addObject(scoreCounter, 60, 380);
// Paddle should act first to hit ball before going out of bounds
setActOrder(Paddle.class);
}
private void addBricks(int rows, int cols)
{
int rowHeight = Brick.HEIGHT;
int colWidth = Brick.LENGTH;
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
// shift down and right 1/2 brick, so we don't cut off half the top row and left col
int width = (int) ((0.5 + col) * colWidth);
int height = (int) ((0.5 + row) * rowHeight);
addObject(new Brick(BRICK_COLUMN[row]), width, height);
}
}
}
public void addScore(int value)
{
scoreCounter.add(value);
}
public void gameOver()
{
addObject(new ScoreBoard(scoreCounter.getValue()), getWidth()/2, getHeight()/2);
Greenfoot.stop();
}
}