forked from zacoppotamus/Breakout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.java
548 lines (504 loc) · 13.6 KB
/
Board.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/**
The Board class includes the game logic as well
as game objects, such as the brick, paddle and ball,
and all the methods to draw components to screen
*/
import javax.swing.JPanel;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;
import java.util.*;
public class Board extends JPanel
{
private Paddle paddle;
private ArrayList<Brick> bricks;
private Ball ball;
private Timer timer;
private boolean inGame = false;
private boolean gamePaused = false;
private boolean skippedMainMenu = false;
private boolean playerIsDead = false;
private int points = 0;
private int lives = 3;
private int level = 1;
private Commons commons;
private String bg = "/landingScreen.jpg";
/**
* Constructor. Configures refresh rate and initiates threads
*/
public Board()
{
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);
timer = new Timer();
timer.scheduleAtFixedRate(new ScheduleTask(), 100, 8);
}
public void addNotify()
{
super.addNotify();
gameInit();
}
/**
* Initializes game logic and variables
* Bricks are initialized for the first
* level and randomly initialized for every other
* level
*/
public void gameInit()
{
paddle = new Paddle(120,360);
ball = new Ball(150, 100, 5);
commons = new Commons();
bricks = new ArrayList<Brick>();
ball.reset();
if (level == 1)
initializeBricks();
else
randomizeBricks();
}
/**
* Draws all components to screen
* depending on the game state.
* Game states include main menu, pause menu,
* game over screen and game screen.
*
* @param g
*/
public void paint(Graphics g)
{
super.paint(g);
ImageIcon img = new ImageIcon(this.getClass().getResource(bg));
Image image = img.getImage();
g.drawImage(image, 0, 0, null);
if (inGame)
{
drawGameState(g);
}
else
{
if (playerIsDead)
{
drawGameState(g);
drawGameOver(g);
}
else if (gamePaused)
{
drawGameState(g);
drawGamePaused(g);
}
else if (!skippedMainMenu)
{
drawTitles(g);
}
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
/**
* Draw all game components (paddle, bricks, ball, points, lives)
*
* @param g
*/
public void drawGameState(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
drawPaddle(g2);
drawBricks(g2);
drawBall(g2);
drawPoints(g);
drawLives(g);
}
/**
* Draw main menu. Set font, coordinates
* and color for every string drawn to screen.
*
* @param g
*/
public void drawTitles(Graphics g)
{
String drawnString = "Breakout";
Font font = new Font("Bank Gothic", Font.BOLD, 35);
FontMetrics metr = this.getFontMetrics(font);
Color letterColor = new Color(248,207,144);
g.setColor(letterColor);
g.setFont(font);
g.drawString(drawnString,
(commons.getWidth() - metr.stringWidth(drawnString)) / 2,
(int)(commons.getHeight() * 0.15));
drawnString = "AN IZAC JOINT";
font = new Font("OCR A Std", Font.BOLD, 10);
g.setFont(font);
g.drawString(drawnString, 6, (int)(commons.getHeight() * 0.98));
drawnString = "PRESS <SPACE> TO PLAY";
font = new Font("OCR A Std", Font.BOLD, 15);
metr = this.getFontMetrics(font);
g.setFont(font);
g.drawString(drawnString,
(commons.getWidth() - metr.stringWidth(drawnString)) / 2,
(int)(commons.getHeight() * 0.65));
}
/**
* Event Listener. Determines when SPACE
* is pressed to start the game and transition
* to the inGame game state. Also used to restart
* the game in the game over state
*
* @param e
*/
public void spaceToStart(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE)
{
if (playerIsDead)
{
lives = 3;
points = 0;
level = 1;
playerIsDead = false;
inGame = true;
gameInit();
}
if (!skippedMainMenu)
{
inGame = true;
skippedMainMenu = true;
}
if (gamePaused)
{
gamePaused = !gamePaused;
inGame = true;
}
}
}
/**
* Event listener. Pauses game when
* ESC is pressed
*
* @param e
*/
public void pauseGame(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_ESCAPE)
{
if (skippedMainMenu)
{
inGame = !inGame;
gamePaused = !gamePaused;
}
}
}
/**
* Draw points to screen.
*
* @param g
*/
public void drawPoints(Graphics g)
{
String score = Integer.toString(points);
String totalPoints = "";
int i = score.length();
while (i < 8)
{
totalPoints += "0";
i++;
}
totalPoints += score;
Font font = new Font("OCR A Std", Font.BOLD, 12);
g.setColor(Color.WHITE);
g.setFont(font);
g.drawString(totalPoints,
(int)(commons.getWidth() * 0.75),
(int)(commons.getHeight() * 0.98));
}
/**
* Draw number of lives left
* to screen.
*
* @param g
*/
public void drawLives(Graphics g)
{
Font font = new Font("OCR A Std", Font.PLAIN, 12);
g.setColor(Color.WHITE);
g.setFont(font);
g.drawString("LIVES "+lives,
7,
(int)(commons.getHeight() * 0.98));
}
/**
* Draw game over message to screen
*
* @param g
*/
public void drawGameOver(Graphics g)
{
Font font = new Font("OCR A Std", Font.BOLD, 30);
g.setColor(Color.WHITE);
g.setFont(font);
g.drawString("GAME OVER",
(int)(commons.getWidth() * 0.17),
(int)(commons.getHeight() * 0.5));
font = new Font("OCR A Std", Font.PLAIN, 11);
g.setFont(font);
g.drawString("Press <SPACE> to start a new game",
(int)(commons.getWidth() * 0.07),
(int)(commons.getHeight() * 0.55));
}
/**
* Draw game paused message to screen
*
* @param g
*/
public void drawGamePaused(Graphics g)
{
Font font = new Font("OCR A Std", Font.BOLD, 35);
g.setColor(Color.WHITE);
g.setFont(font);
g.drawString("PAUSED",
(int)(commons.getWidth() * 0.25),
(int)(commons.getHeight() * 0.5));
font = new Font("OCR A Std", Font.PLAIN, 11);
g.setFont(font);
g.drawString("Press <SPACE> to return to game",
(int)(commons.getWidth() * 0.1),
(int)(commons.getHeight() * 0.55));
}
/**
* Convert paddle to rectangle and
* draw it to screen.
*
* @param g2
*/
public void drawPaddle(Graphics2D g2)
{
g2.setColor(Color.WHITE);
Rectangle paddleRect = paddle.paddleAsRect();
g2.draw(paddleRect);
g2.fill(paddleRect);
}
/**
* Convert ArrayList of bricks to rectangles and
* draw them to screen.
*
* @param g2
*/
public void drawBricks(Graphics2D g2)
{
//Rectangle brickRect[] = new Rectangle[40];
ArrayList<Rectangle> brickRect;
brickRect = new ArrayList<Rectangle>();
int i = 0;
g2.setColor(Color.WHITE);
for (Brick a : bricks)
{
brickRect.add(bricks.get(i).brickAsRect());
if (!bricks.get(i).isDestroyed())
{
g2.draw(brickRect.get(i));
g2.fill(brickRect.get(i));
}
i++;
}
}
/**
* Convert bal to Ellipse and draw it to screen.
*
* @param g2
*/
public void drawBall(Graphics2D g2)
{
Ellipse2D.Double ballShape = ball.ballAsEllipse();
g2.setColor(Color.WHITE);
g2.fill(ballShape);
g2.draw(ballShape);
}
/**
* Initialize brick position and add
* bricks to the ArrayList for the first
* level.
*/
public void initializeBricks()
{
commons.setBWidth(38);
for (int i=20; i<commons.getHeight()*0.2; i += commons.getBHeight()+6)
{
for (int j=2; j<commons.getWidth()-15; j += commons.getBWidth()+5)
{
bricks.add(new Brick(j,i));
}
}
}
/**
* Randomly initialize brick position and add
* bricks to the ArrayList for every
* next level.
*/
public void randomizeBricks()
{
Random r = new Random();
int minBWidth = 38;
int maxBWidth = 65;
int newBWidth = r.nextInt(maxBWidth - minBWidth + 1) + minBWidth;
commons.setBWidth(newBWidth);
int randomInt;
for (int i=20; i < commons.getHeight()*0.4; i+=commons.getBHeight()+6)
{
for (int j=10; j<commons.getWidth()-15; j += commons.getBWidth()+5)
{
// Generate int between 0 or 1
randomInt = r.nextInt(2);
if (randomInt == 1)
bricks.add(new Brick(j,i));
}
}
}
/**
* Check if ball intersects paddle
* and change its speed and direction
* accordingly.
*/
public void collisionCheck()
{
if ((ball.ballAsEllipse()).intersects(paddle.paddleAsRect()))
{
ball.changeVerticalDirection();
adjustBallSpeedRelativeToPaddleIntersection();
}
// If ball hits a brick, remove it
removeBrick();
checkIfPlayerIsDead();
checkForVictory();
}
/**
* Remove brick from the ArrayList
* when it is hit by the ball.
*/
public void removeBrick()
{
int i = 0;
//ArrayList copy to avoid ConcurrentModificationError
ArrayList<Brick> bricksCopy = new ArrayList<Brick>(bricks);
for (Brick a : bricks)
{
if ((ball.ballAsEllipse()).intersects(bricks.get(i).brickAsRect()))
{
ball.changeVerticalDirection();
bricks.get(i).destroyBrick();
//System.out.println("Brick "+i+" is destroyed!");
points+=100;
try
{
bricksCopy.remove(i);
}
catch (Exception IndexOutOfBoundsException)
{
}
}
i++;
}
bricks = bricksCopy;
}
/**
* Determine if player is dead by checking
* the number of lives left, and deduce a
* life when ball hits bottom of screen.
*/
public void checkIfPlayerIsDead()
{
if (lives == 0)
{
inGame = false;
playerIsDead = true;
return;
}
if (ball.getY() + (2 * ball.getRadius()) == commons.getHeight())
{
inGame = !inGame;
gamePaused = !gamePaused;
lives -= 1;
ball.reset();
paddle = new Paddle(120,360);
}
}
/**
* Determine if game must proceed
* to next level.
*/
public void checkForVictory()
{
if (bricks.size() == 0)
{
level += 1;
gameInit();
}
}
/**
* The paddle is split into 4 positions
* which determine the resulting horizontal
* speed of the ball when it hits each one.
* This allows for better player control over
* the ball and enables better gameplay.
*/
public void adjustBallSpeedRelativeToPaddleIntersection()
{
Ellipse2D.Double ballShape = ball.ballAsEllipse();
Rectangle paddleRect = paddle.paddleAsRect();
// Split paddle in four parts thus allowing player to control
// direction of the ball
// Get leftmost position of paddle and ball
float pLeft = paddle.getXLeft();
float bLeft = ball.getX();
if (bLeft > pLeft + (0.75*commons.getPWidth()))
{
ball.setHorizontalSpeed(2);
}
else if (bLeft > pLeft + (0.5*commons.getPWidth()))
{
ball.setHorizontalSpeed(1);
}
else if (bLeft > pLeft + (0.25*commons.getPWidth()))
{
ball.setHorizontalSpeed(-1);
}
else
{
ball.setHorizontalSpeed(-2);
}
}
/**
* Key Listener.
*/
private class TAdapter extends KeyAdapter
{
public void keyReleased(KeyEvent e)
{
paddle.keyReleased(e);
}
public void keyPressed(KeyEvent e)
{
paddle.keyPressed(e);
spaceToStart(e);
pauseGame(e);
}
}
class ScheduleTask extends TimerTask
{
public void run()
{
if (inGame)
{
paddle.move();
ball.move();
collisionCheck();
}
repaint();
}
}
}