-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBreakout.java
309 lines (270 loc) · 9.07 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
/*
* 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 = 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;
/** Speed of ball */
private static final int BALL_SPEED = 3;
/** 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;
/** Delay time for game */
private static final int DELAY = 10;
/* Method: init() */
/** Sets up the Breakout program. */
public void init() {
setBricks();
setPaddle();
setBall();
setLabels();
addMouseListeners();
}
// Sets up the bricks for the game
private void setBricks() {
brickXOffset = (WIDTH - BRICK_WIDTH * NBRICKS_PER_ROW - BRICK_SEP * (NBRICKS_PER_ROW - 1)) / 2;
for(int row = 0; row < NBRICK_ROWS; row++) {
for(int rowBrick = 0; rowBrick < NBRICKS_PER_ROW; rowBrick++) {
GRect brick = new GRect(BRICK_WIDTH, BRICK_HEIGHT);
brick.setFilled(true);
brick.setColor(getColor(row));
add(brick, getBrickPoint(row, rowBrick));
}
}
}
// Returns color for brick based on which row brick is on
private Color getColor(int row) {
if(row < 2) {
return Color.RED;
} else if (row < 4) {
return Color.ORANGE;
} else if (row < 6) {
return Color.YELLOW;
} else if (row < 8) {
return Color.GREEN;
} else {
return Color.CYAN;
}
}
// Returns point where each brick should be placed
private GPoint getBrickPoint(int row, int rowBrick) {
int brickX = brickXOffset + (BRICK_WIDTH + BRICK_SEP) * rowBrick;
int brickY = BRICK_Y_OFFSET + (BRICK_HEIGHT + BRICK_SEP) * row;
return new GPoint(brickX, brickY);
}
// Sets up the paddle for the game
private void setPaddle() {
paddle = new GRect(PADDLE_WIDTH, PADDLE_HEIGHT);
paddle.setFilled(true);
add(paddle, (WIDTH - PADDLE_WIDTH) / 2, HEIGHT - PADDLE_Y_OFFSET - PADDLE_HEIGHT);
}
// Sets up the ball for the game
private void setBall() {
ball = new GOval(BALL_RADIUS * 2, BALL_RADIUS * 2);
ball.setFilled(true);
add(ball, paddle.getX() + paddle.getWidth() / 2 - BALL_RADIUS, paddle.getY() - BALL_RADIUS * 2 - 1);
}
// Initializes ball velocities
private void setBallVelocities() {
ballVX = rgen.nextDouble(1, BALL_SPEED);
if(rgen.nextBoolean()) ballVX = -ballVX;
ballVY = -BALL_SPEED;
}
// Sets up labels in game
private void setLabels() {
setCenterLabel("Click Paddle to Begin!");
livesLeft = NTURNS;
setLivesLeftLabel();
}
// Sets up label to show message in the center of the game
private void setCenterLabel(String label) {
centerLabel = new GLabel(label);
centerLabel.setFont("CenturyGothic-26-bold");
add(centerLabel, (WIDTH - centerLabel.getWidth()) / 2, (HEIGHT + centerLabel.getHeight()) / 2);
}
// Sets up label to show how many lives are left on the top left of the game
private void setLivesLeftLabel() {
livesLeftLabel = new GLabel("Lives Left: " + livesLeft);
livesLeftLabel.setFont("CenturyGothic-15-bold");
add(livesLeftLabel, 3, 3 + livesLeftLabel.getAscent());
}
// Stores point in program where mouse clicks
public void mousePressed(MouseEvent e) {
if(livesLeft > 0) {
ptClicked = new GPoint(e.getPoint());
objClicked = getElementAt(ptClicked);
if(objClicked == paddle && ballVX == 0) {
if(centerLabel.isVisible()) {
centerLabel.setVisible(false);
remove(centerLabel);
}
setBallVelocities();
}
}
}
// Drags paddle if it is clicked on
public void mouseDragged(MouseEvent e) {
if(objClicked == paddle && livesLeft > 0) {
if (ballVX == 0) setBallVelocities();
if (0 > e.getX() - PADDLE_WIDTH / 2){
paddle.setLocation(0, paddle.getY());
} else if (WIDTH < e.getX() + PADDLE_WIDTH / 2) {
paddle.setLocation(WIDTH - PADDLE_WIDTH, paddle.getY());
} else {
paddle.setLocation(e.getX() - PADDLE_WIDTH / 2, paddle.getY());
}
ptClicked = new GPoint(e.getPoint());
}
}
/* Method: run() */
/** Runs the Breakout program. */
public void run() {
while(livesLeft > 0) {
ball.move(ballVX, ballVY);
checkIfBallHits();
if(winCounter == (NBRICK_ROWS * NBRICKS_PER_ROW)) break;
pause(DELAY);
}
remove(ball);
displayMessage();
}
// Check if ball hits anything in program
private void checkIfBallHits() {
checkBallHitsWall();
checkBallFallsOut();
checkBallHitsObj();
}
// Checks if ball hits wall
private void checkBallHitsWall() {
// Case 1: if ball hits top left corner, ball will bounce off
if ((ball.getX() < 0) && (ball.getY() < 0)) {
ballVX = -ballVX;
ballVY = -ballVY;
// Case 2: if ball hits top right corner, ball will bounce off
} else if ((ball.getX() + BALL_RADIUS * 2 > WIDTH) && (ball.getY() < 0)) {
ballVX = -ballVX;
ballVY = -ballVY;
// Case 3: if ball hits left or right wall, ball will bounce off
} else if ((ball.getX() + BALL_RADIUS * 2 > WIDTH + BALL_SPEED) || (ball.getX() < -BALL_SPEED)) {
ballVX = -ballVX;
// Case 4: if ball hits top wall, ball will bounce off
} else if (ball.getY() < -BALL_SPEED) {
ballVY = -ballVY;
}
}
// Checks if ball falls out under paddle
private void checkBallFallsOut() {
if(ball.getY() > HEIGHT) {
livesLeft--;
remove(livesLeftLabel);
setLivesLeftLabel();
if(livesLeft > 0) ball.setLocation(paddle.getX() + paddle.getWidth() / 2 - BALL_RADIUS, paddle.getY() - BALL_RADIUS * 2);
ballVX = 0;
ballVY = 0;
}
}
// Checks if the ball hits paddle or bricks
private void checkBallHitsObj() {
for(int ballSide = 0; ballSide < 4; ballSide++) {
objHit = getElementAt(ballSidePoint(ballSide));
if(objHit != null && objHit != livesLeftLabel) {
if(objHit != paddle && objHit != livesLeftLabel) {
remove(objHit);
winCounter++;
} else {
checkIfStuck(ballSide);
}
changeVelocity(ballSide);
}
}
}
// Checks each four sides of the ball if it hits any object
private GPoint ballSidePoint(int ballSide) {
switch(ballSide) {
// Checks right side of ball
case 0:
return new GPoint(ball.getX() + BALL_RADIUS * 2 + 1, ball.getY() + BALL_RADIUS);
// Checks left side of ball
case 1:
return new GPoint(ball.getX() - 1, ball.getY() + BALL_RADIUS);
// Checks bottom side of ball
case 2:
return new GPoint(ball.getX() + BALL_RADIUS, ball.getY() + BALL_RADIUS * 2 + 1);
// Checks top side of ball
default:
return new GPoint(ball.getX() + BALL_RADIUS, ball.getY() - 1);
}
}
// Changes the velocity of the ball based on which side hits
private void changeVelocity(int ballSide) {
if(ballSide < 2) {
ballVX = -ballVX;
} else {
ballVY = -ballVY;
}
}
// Checks if ball is stuck in paddle
private void checkIfStuck(int ballSide) {
if(ballSide == 0 && ball.getX() + BALL_RADIUS * 2 > paddle.getX()) {
ball.setLocation(paddle.getX() - BALL_RADIUS * 2 - BALL_SPEED, ball.getY());
} else if(ballSide == 1 && ball.getX() < paddle.getX() + PADDLE_WIDTH) {
ball.setLocation(paddle.getX() + PADDLE_WIDTH + BALL_SPEED, ball.getY());
} else if(ballSide == 2 && ball.getY() + BALL_RADIUS * 2 > paddle.getX()) {
ball.setLocation(ball.getX(), paddle.getY() - BALL_RADIUS * 2 - BALL_SPEED);
} else if(ballSide == 3 && ball.getY() < paddle.getY() + PADDLE_HEIGHT) {
ball.setLocation(ball.getX(), paddle.getY() + PADDLE_HEIGHT + BALL_SPEED);
}
}
// Displays message whether player wins or loses
private void displayMessage() {
if(livesLeft > 0) {
setCenterLabel("You Win!");
livesLeft = 0;
} else {
setCenterLabel("Game Over!");
}
}
// Private instance variables
private GRect paddle;
private GOval ball;
private GPoint ptClicked;
private GObject objClicked, objHit;
private GLabel livesLeftLabel, centerLabel;
private int brickXOffset, livesLeft, winCounter;
private double ballVX, ballVY;
private RandomGenerator rgen = new RandomGenerator();
}