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
549
550 |
package bump_n_jump;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
/*
* Game - the class that contains the main game loop (is a panel).
*/
public class Game extends JPanel implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
private Thread mainLoop;
private static final int DEFAULT_FPS = 50;
private int fps, threadDelay;
private Image offImage;
private Graphics offGraphics;
private Dimension offDimension;
private Image backgroundImage;
private Level level;
private LevelCreator levelCreator;
private int levelNum, livesLeft, starsLeft;
private boolean gameStarted;
private boolean beatTheGame, gameOver;
private int menuScreen;
private static final int MENU = 0, INSTRUCTIONS = 1, CREDITS = 2;
private Image menuImage, instructionsImage, menuImageBeat, menuImageGameOver, credits;
private Image arrow;
private static final int ARROWY1 = 230;
private static final int ARROWY2 = 360;
private static final int ARROWY3 = 505;
private int arrowX = 115, arrowY = ARROWY1;
//Sounds
//private SoundPlayback bounceSnd;
private SoundPlayback dogSnd;
private SoundPlayback trampSnd;
private SoundPlayback deadSnd;
private SoundPlayback starSnd;
private SoundPlayback newLevelSnd;
private SoundPlayback levelMusicSnd;
private SoundPlayback gameplaySnd;
private SoundPlayback openingSnd;
public Game() {
//initialize vars
fps = DEFAULT_FPS;
threadDelay = 1000 / fps;
//key press info.
addKeyListener(this);
requestFocus();
setFocusable(true);
//Initialize Sounds
//bounceSnd = new SoundPlayback("music/Bounce.aif");
dogSnd = new SoundPlayback("music/Dog.aif");
trampSnd = new SoundPlayback("music/Tramp.aif");
deadSnd = new SoundPlayback("music/Dead.aif");
starSnd = new SoundPlayback("music/Star.aif");
newLevelSnd = new SoundPlayback("music/NewLevel.aif");
levelMusicSnd = new SoundPlayback("music/Level_music.aif", true);
gameplaySnd = new SoundPlayback("music/gameplaymusic.aif");
openingSnd = new SoundPlayback("music/openingmusic.aif");
livesLeft = 6;
levelNum = 0;
levelCreator = new LevelCreator();
nextLevel();
//create new thread
mainLoop = new Thread(this);
gameStarted = beatTheGame = gameOver = false;
menuScreen = MENU;
try {
menuImage = ImageIO.read(getClass().getResource("../images/menu.jpg"));
instructionsImage = ImageIO.read(getClass().getResource("../images/instructions.jpg"));
menuImageBeat = ImageIO.read(getClass().getResource("../images/winScreen.jpg"));
menuImageGameOver = ImageIO.read(getClass().getResource("../images/gameOver.jpg"));
credits = ImageIO.read(getClass().getResource("../images/credits.jpg"));
arrow = ImageIO.read(getClass().getResource("../images/arrow.png"));
}
catch (IOException e) {
System.err.println(e.toString());
}
openingSnd.start();
}
private void startEverythingOver() {
livesLeft = 6;
levelNum = 0;
levelCreator = new LevelCreator();
nextLevel();
//create new thread
mainLoop = new Thread(this);
gameStarted = beatTheGame = gameOver = false;
menuScreen = MENU;
gameplaySnd.stop();
openingSnd.start();
menuScreen();
}
/**
* shows the menu screen with options to show instructions, play the game, and quit.
*/
public void menuScreen() {
repaint();
}
public void start() {
mainLoop.start();
}
public void run() {
openingSnd.stop();
gameplaySnd.start();
while (Thread.currentThread() == mainLoop) {
// Remember the starting time
long tm = System.currentTimeMillis();
BallSprite bs = level.getBallSprite();
repaint();
//check collisions between objects:
checkCollisions();
//update everybody's positions
if (bs.isDead()) {
deadSnd.stop();
deadSnd.start();
if (livesLeft == 0) {
// Stop level music at end of game
gameplaySnd.stop();
gameStarted = false;
gameOver = true;
bs.getCircle().setFrame(-10, -10, 0,0);
bs.setIsDead(false);
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {}
repaint();
//mainLoop.stop();
}
else {
livesLeft--;
Level doOver = levelCreator.create(level.filename, level.getBackImageName());
level = doOver;
starsLeft = level.getNumStars();
}
}
level.getBallSprite().updateCoordinates();
level.getBallSprite().updateFrame();
for (Iterator <Dog> iter = level.getDogs().iterator(); iter.hasNext();) {
Dog d = iter.next();
d.updateCoordinates();
d.updateFrame();
}
//if there are no stars left, open the door.
if (!level.isDoorOpen() && starsLeft == 0 && level.getDoor().open()) {
//we get here if we are supposed to open the door and the door is done opening.
level.setDoorOpen(true);
}
// Delay depending on how far we are behind
try {
tm += threadDelay;
Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
}
catch (InterruptedException e) {
System.err.println("An error occurred: " + e.toString());
break;
}
}
}
/**
* steps through all the lists of elements and checks for relevant collisions.
*/
public void checkCollisions() {
BallSprite b = level.getBallSprite();
//check ball collisions with dogs:
for (Iterator <Dog> iter = level.getDogs().iterator(); iter.hasNext();) {
Dog d = iter.next();
if(b.collisionWith(d))
{
//play dog sound when dog collides with wall
dogSnd.stop();
dogSnd.start();
//dogSnd.stop();
}
}
//check ball collisions with ground tiles
for (Iterator <Tile> iter = level.getTiles().iterator(); iter.hasNext();) {
Tile t = iter.next();
b.collisionWithBottom(t);
b.collisionWithSide(t);
if (b.collisionWithTop(t)) {
//plays bounce sound
//bounceSnd.stop();
//bounceSnd.start();
}
}
//check dog collisions with sides of tiles (should reverse them)
for (Iterator <Dog> iter = level.getDogs().iterator(); iter.hasNext();) {
Dog d = iter.next();
for (Iterator <Tile> i = level.getTiles().iterator(); i.hasNext();) {
Tile t = i.next();
d.collisionWithWall(t);
}
}
//check ball collisions with spikes
for (Iterator <Spike> iter = level.getSpikes().iterator(); iter.hasNext();) {
Spike s = iter.next();
b.collisionWith(s);
}
//check collisions with the door
if (level.isDoorOpen() && b.collidesWith(level.getDoor())) {
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {}
nextLevel();
repaint();
}
for (Iterator <Trampoline> iter = level.getTramps().iterator(); iter.hasNext();) {
Trampoline t = iter.next();
if(b.collisionWith(t))
{
//plays trampoline sound
trampSnd.stop();
trampSnd.start();
//trampSnd.stop();
}
}
//collisions with stars
for (Iterator <Star> iter = level.getStars().iterator(); iter.hasNext();) {
Star s = iter.next();
if (b.collidesWith(s)) {
starSnd.stop();
starSnd.start();
//starSnd.stop();
s.setIsExploding(true);
starsLeft--;
}
}
for (Iterator <Warp> iter = level.getWarps().iterator(); iter.hasNext();) {
Warp w = iter.next();
if (b.collidesWith(w)) {
b.setX(w.getExitX());
b.setY(w.getExitY());
//play a woosh sound?
}
}
}
/**
* called every time we do a repaint(); starts the repainting process for all of the things in the level.
*/
public void paintComponent(Graphics g) {
Dimension d = getSize();
//The following was stolen from AnimatedSpriteDemoPanel.java
// Create the offscreen graphics context
if ((offGraphics == null) || (d.width != offDimension.width) || (d.height != offDimension.height)) {
offDimension = d;
offImage = createImage(d.width, d.height);
offGraphics = offImage.getGraphics();
}
// Erase the previous image
offGraphics.setColor(getBackground());
offGraphics.fillRect(0, 0, d.width, d.height);
offGraphics.setColor(Color.black);
if (gameStarted) {
// Paint the frame into the image
paintFrame(offGraphics);
}
else {
paintMenu(offGraphics);
}
// Paint the image onto the screen
g.drawImage(offImage, 0, 0, null);
}
/**
* paints the menu screen.
* @param g
*/
private void paintMenu(Graphics g) {
if (menuScreen == MENU) {
if (!beatTheGame && !gameOver) {
g.drawImage(menuImage, 0, 0, null);
//draw the arrow.
g.drawImage(arrow, arrowX, arrowY, null);
}
else if (beatTheGame) {
mainLoop = null;
g.drawImage(menuImageBeat, 0, 0, null);
}
else if (gameOver) {
mainLoop = null;
g.drawImage(menuImageGameOver, 0, 0, null);
deadSnd.stop();
}
}
else if (menuScreen == INSTRUCTIONS) {
g.drawImage(instructionsImage, 0, 0, null);
}
else if (menuScreen == CREDITS) {
g.drawImage(credits, 0, 0, null);
}
}
/**
* steps through all level elements and repaints them.
* @param g
*/
private void paintFrame(Graphics g) {
//paint the level #, lives, and stars left at the top of the window.
Graphics2D g2D = (Graphics2D)g;
g2D.drawImage(backgroundImage, 0, 0, null);
g2D.setFont(new Font("Helvetica", Font.BOLD, 20));
g2D.drawString("Lives: " + livesLeft, 40, 20);
g2D.drawString("Level " + Integer.toString(levelNum), 300, 20);
g2D.drawString("Stars left: " + Integer.toString(starsLeft), 580, 20);
//draw the door
level.getDoor().drawSprite(g);
for (Iterator <Tile> it = level.getTiles().iterator(); it.hasNext();) {
Tile t = it.next();
//Draw the tile
t.drawSprite(g);
}
for (Iterator <Star> it = level.getStars().iterator(); it.hasNext();) {
Star s = it.next();
//Draw the star
if (s.isShown()) {
if (s.isExploding()) {
s.explode();
}
s.drawSprite(g);
}
}
for (Iterator <Trampoline> it = level.getTramps().iterator(); it.hasNext();) {
Trampoline t = it.next();
//Draw the trampoline
t.drawSprite(g);
}
for (Iterator <Spike> it = level.getSpikes().iterator(); it.hasNext();) {
Spike s = it.next();
//Draw the Spikes
s.drawSprite(g);
}
for (Iterator <Warp> it = level.getWarps().iterator(); it.hasNext();) {
Warp w = it.next();
//Draw the Spikes
w.drawSprite(g);
}
if (level.getDogs() != null) {
for (Iterator <Dog> it = level.getDogs().iterator(); it.hasNext();) {
Dog d = it.next();
//Draw the dog
d.drawSprite(g);
}
}
//draw the ball
level.getBallSprite().drawSprite(g);
}
public void nextLevel() {
//play sound for new level
newLevelSnd.stop();
newLevelSnd.start();
//newLevelSnd.stop();
try {
Thread.sleep(1000L); //sleep one second for emphasis.
}
catch (InterruptedException e) {
System.err.println("An error occurred: " + e.toString());
}
levelNum++;
switch (levelNum) {
case 1:
level = levelCreator.create("levels/level1.txt", "../images/Warp_background.jpg");
starsLeft = level.getNumStars();
backgroundImage = level.getBackImage();
break;
case 2:
level = levelCreator.create("levels/level2.txt", "../images/Warp_background.jpg");
starsLeft = level.getNumStars();
break;
case 3:
level = levelCreator.create("levels/level3.txt", "../images/Warp_background.jpg");
starsLeft = level.getNumStars();
break;
case 4:
level = levelCreator.create("levels/level4.txt", "../images/Spike_background.jpg");
starsLeft = level.getNumStars();
break;
case 5:
level = levelCreator.create("levels/level5.txt", "../images/Spike_background.jpg");
starsLeft = level.getNumStars();
break;
case 6:
level = levelCreator.create("levels/level6.txt", "../images/Spike_background.jpg");
starsLeft = level.getNumStars();
break;
case 7: //beat the game
gameStarted = false;
beatTheGame = true;
repaint();
break;
}
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
level.getBallSprite().setLeftDown(true);
}
else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
level.getBallSprite().setRightDown(true);
}
else if (e.getKeyCode() == KeyEvent.VK_DOWN && !gameStarted && menuScreen == MENU) {
//move the arrow down
if (arrowY == ARROWY1)
arrowY = ARROWY2;
else if (arrowY == ARROWY2)
arrowY = ARROWY3;
repaint();
}
else if (e.getKeyCode() == KeyEvent.VK_UP && !gameStarted && menuScreen == MENU) {
//move arrow up.
if (arrowY == ARROWY3)
arrowY = ARROWY2;
else if (arrowY == ARROWY2)
arrowY = ARROWY1;
repaint();
}
else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
//do action that arrow is on.
if (arrowY == ARROWY1) {
//start the game.
gameStarted = true;
start();
repaint();
}
else if (arrowY == ARROWY2) {
//show instructions
menuScreen = INSTRUCTIONS;
repaint();
}
else if (arrowY == ARROWY3){
menuScreen = CREDITS;
repaint();
}
}
else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
if (menuScreen == INSTRUCTIONS || menuScreen == CREDITS) {
menuScreen = MENU;
arrowY = ARROWY1;
repaint();
}
else if ((gameOver && !gameStarted) || beatTheGame){
gameOver = false;
gameStarted = false;
startEverythingOver();
}
}
else if (e.getKeyCode() == KeyEvent.VK_F12) {
nextLevel();
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
level.getBallSprite().setLeftDown(false);
}
else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
level.getBallSprite().setRightDown(false);
}
}
} |