forked from sweskills/prog-method-assignment-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBreakout.java
executable file
·305 lines (243 loc) · 8.62 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
/*
* 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.*;
import javax.swing.*;
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;
private static final double GRAVITY = 3.0;
private static final int DELAY = 30;
private RandomGenerator rgen = RandomGenerator.getInstance();
private double vx, vy;
private double vySTART = +3.0;
private double vxSTART = 0.0;
private GRect paddle;
private GOval ball;
/* Method: run() */
/** Runs the Breakout program. */
public void run() {
addActionListeners();
JButton but = new JButton("Score");
add(but, SOUTH);
for(int i=0; i < NTURNS; i++) {
buildGame();
playGame();
if(brickCounter == 0) {
ball.setVisible(false);
printWinner();
break;
}
if(brickCounter > 0) {
removeAll();
}
}
if(brickCounter > 0) {
printGameOver();
}
}
private void buildGame() {
setSize(WIDTH, HEIGHT);
buildBricks(0, BRICK_Y_OFFSET);
buildPaddle();
buildBall();
}
//drawing all the bricks necessary for the game
private void buildBricks(double ax, double ay) {
Color color= null;
/*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++){
/* 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 x = ax + (BRICK_WIDTH + BRICK_SEP) * row;
/* 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 y = ay + (BRICK_HEIGHT + BRICK_SEP) * column;
GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
brick.setFilled(true);
add(brick);
if(column<2){
brick.setColor(Color.RED);
} else if (column <=3) {
brick.setColor(Color.ORANGE);
} else if (column<=5){
brick.setColor(Color.YELLOW);
} else if (column<=7){
brick.setColor(Color.GREEN);
} else {
brick.setColor(Color.CYAN);
}
}
}
}
//paddle set-up
private void buildPaddle() {
setSize(WIDTH, HEIGHT);
//starting the paddle in the middle of the screen
double x = (WIDTH -PADDLE_WIDTH)/2;
//the paddle height stays consistent throughout the game
//need to make sure to subtract the PADDLE_HEIGHT,
//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
*/
double y = (getHeight() - PADDLE_Y_OFFSET - PADDLE_HEIGHT);
if (e.getX() >= 0 && e.getX() < (getWidth() - PADDLE_WIDTH)){
paddle.setLocation(e.getX(), y);
} else if (e.getX() > WIDTH) {
paddle.setLocation(WIDTH, y);
}
}
//ball set-up
private void buildBall() {
ball = new GOval(getWidth()/2 - BALL_RADIUS, getHeight()/2 - BALL_RADIUS, 10, 10);
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 = 5.0;
vx = rgen.nextDouble(1.0, 3.0);
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() <= 0) || (ball.getX() >= (getWidth() - BALL_RADIUS))) {
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() <= 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,
* 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_HEIGHT) {
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) {
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);
}
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;
private void printWinner() {
GLabel Winner = new GLabel ("Winner!!", getWidth()/2, getHeight()/2);
Winner.move(-Winner.getWidth()/2 +vx, -Winner.getHeight());
Winner.setColor(Color.RED);
add (Winner);
}
}