forked from sweskills/prog-method-assignment-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBreakout.java
executable file
·319 lines (280 loc) · 7.74 KB
/
Breakout.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* File: Breakout.java
* -------------------
* Name:Enoch Mbaebie
* Sweskills Tech Talent Academy
*
* 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 = 400;
public static final int APPLICATION_HEIGHT = 600;
/** Dimensions of game board (usually the same) */
private static final int WIDTH = APPLICATION_WIDTH;
private static final int HEIGHT = APPLICATION_HEIGHT;
/** Dimensions of the paddle */
private static final int PADDLE_WIDTH = 60;
private static final int PADDLE_HEIGHT = 10;
/** 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 = 10;
/** 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;
/** 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;
/** Start value for vy */
private static final double VY_START = 5.0;
/** Animation delay or pause time between ball moves */
private static final int DELAY = 15;
/** Number of times the paddle has to be hit before doubling speed */
private static final int SUCCESSFULL_PADDLEHITS_BEFORE_KICKER = 7;
/**
* instance variables
*/
private GRect paddle;
private GOval ball;
private double vx, vy;
private RandomGenerator rgen = RandomGenerator.getInstance();
private int numberOfBricks;
AudioClip bounceClip = MediaTools.loadAudioClip("bounce.au");
private GLabel label;
private boolean startGame;
private int numberOfPaddleHits;
private GLabel scoreLabel;
private int score;
/* Method: init() */
/** Sets up the Breakout program. */
public void init() {
setupBricks();
setupPaddle();
setupScore();
}
/* Method: run() */
/** Runs the Breakout program. */
public void run() {
for (int i = 0; i < NTURNS; i++) {
displayMessage("press mouse button");
startGame = false;
while (!startGame) {
pause(DELAY * 10);
}
removeMessage();
setupBall();
while ((ball.getY() < HEIGHT) && (numberOfBricks > 0)) {
moveBall();
checkForCollision();
pause(DELAY);
}
if (numberOfBricks == 0) {
displayMessage("Winner!");
return;
}
}
displayMessage("Game Over");
}
/**
* setup bricks
*/
private void setupBricks() {
int y = BRICK_Y_OFFSET;
int firstX = (WIDTH - BRICK_WIDTH * NBRICKS_PER_ROW - BRICK_SEP * (NBRICKS_PER_ROW - 1)) / 2;
Color[] colors = {Color.RED, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.CYAN};
for (int j = 0; j < NBRICK_ROWS; j ++) {
int x = firstX;
Color c = colors[j / 2];
for (int i = 0; i < NBRICKS_PER_ROW; i++) {
GRect brick = new GRect(BRICK_WIDTH, BRICK_HEIGHT);
brick.setLocation(x, y);
brick.setFilled(true);
brick.setColor(c);
add(brick);
x += BRICK_WIDTH + BRICK_SEP;
}
y += BRICK_HEIGHT + BRICK_SEP;
}
numberOfBricks = NBRICK_ROWS * NBRICKS_PER_ROW;
}
/**
* setup paddle
*/
private void setupPaddle() {
paddle = new GRect(PADDLE_WIDTH, PADDLE_HEIGHT);
paddle.setLocation(
(WIDTH - PADDLE_WIDTH) / 2,
HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT
);
paddle.setFilled(true);
add(paddle);
addMouseListeners();
}
/**
* setup score label
*/
private void setupScore() {
score = 0;
scoreLabel = new GLabel("Score: " + score + " ");
scoreLabel.setFont("SansSerif-28");
double x = getWidth() - scoreLabel.getWidth();
double y = getHeight() - scoreLabel.getAscent();
scoreLabel.setLocation(x, y);
add(scoreLabel);
}
/**
* add score value and adjust the label
*/
private void addScore(int value) {
score += value;
scoreLabel.setLabel("Score: " + score + " ");
double x = getWidth() - scoreLabel.getWidth();
double y = scoreLabel.getY();
scoreLabel.setLocation(x, y);
}
/**
* bind paddle to mouse movement
*/
public void mouseMoved(MouseEvent e) {
double x = e.getX();
double minX = PADDLE_WIDTH / 2;
double maxX = WIDTH - PADDLE_WIDTH / 2;
if (x < minX) {
x = minX;
} else if (x > maxX) {
x = maxX;
}
paddle.setLocation(
x - minX,
HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT
);
}
/**
* start game when mouse button has been clicked
*/
public void mouseClicked(MouseEvent e) {
startGame = true;
}
/**
* setup ball
*/
private void setupBall() {
double d = 2 * BALL_RADIUS;
ball = new GOval(d, d);
ball.setFilled(true);
ball.setLocation(WIDTH / 2 - BALL_RADIUS, HEIGHT / 2 - BALL_RADIUS);
add(ball);
vy = VY_START;
vx = rgen.nextDouble(1.0, 3.0);
if (rgen.nextBoolean(0.5)) vx = -vx;
numberOfPaddleHits = 0;
}
/**
* move ball
*/
private void moveBall() {
ball.move(vx, vy);
}
/**
* check for collision and change direction
*/
private void checkForCollision() {
double x = ball.getX();
double y = ball.getY();
double d = 2 * BALL_RADIUS;
if (y < 0) {
vy = -vy;
ball.move(0, -2 * y);
bounceClip.play();
}
if (x > WIDTH - d) {
vx = -vx;
ball.move(-2 * (x - WIDTH + d), 0);
bounceClip.play();
}
if (x < 0) {
vx = -vx;
ball.move(-2 * x, 0);
bounceClip.play();
}
GObject collider = getCollidingObject();
if (collider == paddle) {
double hitPosition = (2 * (x - paddle.getX()) + d - PADDLE_WIDTH) / (d + PADDLE_WIDTH);
if (hitPosition < 0) {
vx = -Math.abs(vx);
} else {
vx = Math.abs(vx);
}
numberOfPaddleHits++;
if (numberOfPaddleHits % SUCCESSFULL_PADDLEHITS_BEFORE_KICKER == 0) {
vx *= 2;
}
vy = -vy;
ball.move(0, -2 * (y + d - HEIGHT + PADDLE_Y_OFFSET + PADDLE_HEIGHT));
bounceClip.play();
} else if (collider instanceof GRect) {
if (collider.getColor() == Color.CYAN) addScore(10);
else if (collider.getColor() == Color.GREEN) addScore(20);
else if (collider.getColor() == Color.YELLOW) addScore(30);
else if (collider.getColor() == Color.ORANGE) addScore(40);
else if (collider.getColor() == Color.RED) addScore(50);
remove(collider);
numberOfBricks--;
vy = -vy;
bounceClip.play();
}
}
/**
* search for colliding object
* @return colliding object || null
*/
private GObject getCollidingObject() {
double x = ball.getX();
double y = ball.getY();
double d = 2 * BALL_RADIUS;
GObject object;
object = getElementAt(x, y);
if (object != null) return object;
object = getElementAt(x + d, y);
if (object != null) return object;
object = getElementAt(x, y + d);
if (object != null) return object;
object = getElementAt(x + d, y + d);
if (object != null) return object;
return null;
}
/**
* print game messages
* @param message
*/
private void displayMessage(String message) {
label = new GLabel(message);
label.setFont("SansSerif-28");
double x = (getWidth() - label.getWidth()) / 2;
double y = (getHeight() + label.getAscent()) / 2;
label.setLocation(x, y);
add(label);
}
/**
* remove game messages
*/
private void removeMessage() {
remove(label);
}
}