-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiveKick.java
563 lines (442 loc) · 14.2 KB
/
DiveKick.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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// Final Project
// CS201
// DiveKick Applet
// Neil Steiner
// Jana Parsons
import java.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class DiveKick extends Applet implements KeyListener {
// instance variables
FightCanvas fc;
//audio files
AudioClip headshot;
AudioClip ultrakill;
AudioClip doublekill;
AudioClip monsterkill;
AudioClip godlike;
AudioClip humiliation;
AudioClip unstoppable;
AudioClip rampage;
AudioClip firstblood;
//labels
Label player1ControlsDive;
Label player2ControlsDive;
Label player1ControlsKick;
Label player2ControlsKick;
Label clock;
Label player1Score;
Label player2Score;
Label status;
// local color constants
static final Color black = Color.black;
static final Color white = Color.white;
static final Color red = Color.red;
static final Color green = Color.green;
static final Color blue = Color.blue;
static final Color yellow = Color.yellow;
static final Color dred = new Color(160, 0, 100);
static final Color dgreen = new Color(0, 120, 90);
static final Color dblue = Color.blue.darker();
// initialize the applet------------------------------------------------
public void init() {
fc = new FightCanvas(this);
fc.addKeyListener(this);
setFont(new Font("Calibri", Font.BOLD, 24));
setLayout(new BorderLayout());
add("North", topPanel()); // add at top
add("South", bottomPanel()); // add at bottom
add("Center", fc); // add in remaining area (fills space)
// load sounds
headshot = getAudioClip(getCodeBase(), "headshot.au");
ultrakill = getAudioClip(getCodeBase(), "ultrakill.au");
doublekill = getAudioClip(getCodeBase(), "doublekill.au");
monsterkill = getAudioClip(getCodeBase(), "monsterkill.au");
godlike = getAudioClip(getCodeBase(), "godlike.au");
humiliation = getAudioClip(getCodeBase(), "humiliation.au");
unstoppable = getAudioClip(getCodeBase(), "unstoppable.au");
rampage = getAudioClip(getCodeBase(), "rampage.au");
firstblood = getAudioClip(getCodeBase(), "firstblood.au");
}
// helper method to cvreate the top panel
public Panel topPanel() {
Panel topPanel = new Panel();
topPanel.setLayout(new GridLayout(2, 3));
// title
Label titleLabel = new Label("DIVEKICK!!!!!!", Label.CENTER);
titleLabel.setBackground(dblue);
titleLabel.setForeground(white);
// filler
Label blank = new Label("");
blank.setBackground(dblue);
blank.setForeground(dred);
Label blank2 = new Label("");
blank2.setBackground(dblue);
blank2.setForeground(dred);
// player scores
player1Score = new Label("0", Label.CENTER);
player1Score.setBackground(dblue);
player1Score.setForeground(white);
player2Score = new Label("0", Label.CENTER);
player2Score.setBackground(dblue);
player2Score.setForeground(white);
// game status
status = new Label(" ", Label.CENTER);
status.setForeground(red);
status.setBackground(dblue);
// add labels to panel
topPanel.add(player1Score);
topPanel.add(titleLabel); // will eventually become clock
topPanel.add(player2Score);
topPanel.add(blank);
topPanel.add(status);
topPanel.add(blank2);
return topPanel;
}
// helper function to create bottom panel with directions
public Panel bottomPanel() {
Panel bottomPanel = new Panel();
bottomPanel.setLayout(new GridLayout(2, 2));
// create labels
player1ControlsDive = new Label("Dive: Z");
player2ControlsDive = new Label("Dive: Left Arrow", Label.RIGHT);
player1ControlsKick = new Label("Kick: C");
player2ControlsKick = new Label("Kick: Right Arrow", Label.RIGHT);
// colors
player1ControlsDive.setBackground(dblue);
player1ControlsDive.setForeground(white);
player2ControlsDive.setBackground(dblue);
player2ControlsDive.setForeground(white);
player1ControlsKick.setBackground(dblue);
player1ControlsKick.setForeground(white);
player2ControlsKick.setBackground(dblue);
player2ControlsKick.setForeground(white);
// add labels to panel in grid order
bottomPanel.add(player1ControlsDive);
bottomPanel.add(player2ControlsDive);
bottomPanel.add(player1ControlsKick);
bottomPanel.add(player2ControlsKick);
return bottomPanel;
}
public void start() {
fc.start();
}
public void stop() {
fc.stop();
}
// handle key presses---------------------------------------------------
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
int canvasHeight = fc.getSize().height;
// player 1
// movements-------------------------------------------------------
// jump up
// PRE: player must be on the ground
if (keyCode == KeyEvent.VK_Z &&
fc.player1.yPosition == canvasHeight) {
fc.player1.jumpUp();
}
// jump back
// PRE: player must be on the ground
else if (keyCode == KeyEvent.VK_C &&
fc.player1.yPosition == canvasHeight) {
fc.player1.jumpBack();
}
// kick
// PRE: player must be in air
else if (keyCode == KeyEvent.VK_C &&
fc.player1.yPosition != canvasHeight) {
fc.player1.kick();
}
// player 2
// movements----------------------------------------------------------
// jump up
// PRE: player must be on the ground
if (keyCode == KeyEvent.VK_LEFT &&
fc.player2.yPosition == canvasHeight) {
fc.player2.jumpUp();
}
// jump back
// PRE: player must be on the ground
else if (keyCode == KeyEvent.VK_RIGHT &&
fc.player2.yPosition == canvasHeight) {
fc.player2.jumpBack();
}
// kick
// PRE: player must be in air
else if (keyCode == KeyEvent.VK_RIGHT &&
fc.player2.yPosition != canvasHeight) {
fc.player2.kick();
}
}
//keylistener required methods
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
}
// canvas where all the action takes place
class FightCanvas extends Canvas implements Runnable {
// instance variables
Image offscreen;
Dimension offscreensize;
Graphics g2;
DiveKick parent;
Thread t;
Player player1;
Player player2;
// constructor
public FightCanvas(DiveKick dk) {
parent = dk;
//please note that on the browser the players do not initialize to the right positions
//and it is not an issue in any subsequent rounds after the first
//running the game in the applet corrects for this.
player1 = new Player(1, 1, this);
player2 = new Player(4, -1, this);
}
// handles all movement and gameplay
public void run() {
Thread currentThread = Thread.currentThread();
while (currentThread == t) {
Dimension d = getSize();
// handle player movement
if (player1.yPosition <= d.height) {
player1.movePlayer();
}
if (player2.yPosition <= d.height) {
player2.movePlayer();
}
// create floor
if (player1.yPosition > d.height) {
player1.floor();
}
if (player2.yPosition > d.height) {
player2.floor();
}
// create left wall
if (player1.xPosition < 0) {
player1.leftWall();
}
if (player2.xPosition < 0) {
player2.leftWall();
}
// create right wall
if (player1.xPosition + player1.width > d.width) {
player1.rightWall();
}
if (player2.xPosition + player2.width > d.width) {
player2.rightWall();
}
// check for position
// swap/rotate---------------------------------------------------
// player 1 needs to be facing left
if (player1.yPosition == d.height &&
player1.xPosition > player2.xPosition + (player2.width / 2)) {
if (player1.left != -1) {
player1.rotate();
}
}
// player 1 needs to be facing right
if (player1.yPosition == d.height &&
player1.xPosition < player2.xPosition + (player2.width / 2)) {
if (player1.left != 1) {
player1.rotate();
}
}
// player 2 needs to be facing left
if (player2.yPosition == d.height &&
player2.xPosition > player1.xPosition + (player1.width / 2)) {
if (player2.left != -1) {
player2.rotate();
}
}
// player 2 needs to be facing right
if (player2.yPosition == d.height &&
player2.xPosition < player1.xPosition + (player1.width / 2)) {
if (player2.left != 1) {
player2.rotate();
}
}
// check for
// collision-------------------------------------------------------
// COLLISION FOR PLAYER 1
if (player1.isKicking) {
// player 1 must have made a collision AND player 2 must have
// not made a collision
if (player1.collision(player2) && !player2.collision(player1)) {
// player 1 wins round, update all variables
player2.killStreak = 0; // reset player2 kill streak back to 0
// change player 1 score label
parent.player1Score.setText(Integer.toString(player1.scoreCount));
// update status
parent.status.setText("Player 1 wins round " + (player1.scoreCount + player2.scoreCount));
// sounds
if (player1.headStreak >= 1) {
parent.headshot.play();
if (player1.killStreak > 1) {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {};
}
}
if (player2.scoreCount + player1.scoreCount == 1) {
parent.firstblood.play();
}
if (player1.killStreak == 2) {
parent.doublekill.play();
}
if (player1.killStreak == 3) {
parent.ultrakill.play();
}
if (player1.killStreak == 4) {
parent.rampage.play();
}
if (player1.killStreak == 5) {
parent.unstoppable.play();
}
if (player1.killStreak == 6) {
parent.godlike.play();
}
if (player1.killStreak == 7) {
parent.humiliation.play();
}
try {
Thread.sleep(2500);
} catch (InterruptedException e) {};
parent.status.setText(" "); // update status
player1.newRound(1, 1);// reset both player positions
player2.newRound(4, -1);
// both players make a collision
//going to need to update this becayuse I check for that in the player class collision check.
} else if (player1.collision(player2) && player2.collision(player1)) {
// if both players kick and collide at the same time its a
// tie, no score update
parent.status.setText("It's a draw"); // update status
try {
Thread.sleep(2500);
} catch (InterruptedException e) {};
parent.status.setText(" "); // update status
player1.newRound(1, 1);// reset both player positions
player2.newRound(4, -1);
}
}
// player2 collision detection
if (player2.isKicking) {
// player 2 must make a collision and player 1 must NOT make a
// collision
if (player2.collision(player1) && !player1.collision(player2) ) {
player1.killStreak = 0; // set player1 kill streak back to 0
parent.player2Score.setText(Integer.toString(player2.scoreCount)); // change player 2 score label
parent.status.setText("Player 2 wins round " + (player1.scoreCount + player2.scoreCount)); // update status
// sounds
if (player2.headStreak >= 1) {
parent.headshot.play();
if (player2.killStreak > 1) {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {};
}
}
if (player2.scoreCount + player1.scoreCount == 1) {
parent.firstblood.play();
}
if (player2.killStreak == 2) {
parent.doublekill.play();
}
if (player2.killStreak == 3) {
parent.ultrakill.play();
}
if (player2.killStreak == 4) {
parent.rampage.play();
}
if (player2.killStreak == 5) {
parent.unstoppable.play();
}
if (player2.killStreak == 6) {
parent.godlike.play();
}
if (player2.killStreak == 7) {
parent.humiliation.play();
}
try {
Thread.sleep(2500);
} catch (InterruptedException e) {};
parent.status.setText(" "); // update status
// reset both player positions and clock
player1.newRound(1, 1);// reset both player positions
player2.newRound(4, -1);
}
}
// player 1 wins, reset game
if (player1.scoreCount == 7) {
parent.status.setText("PLAYER 1 WINS!");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {};
// reset all variables and create new round
player1.newRound(1, 1); // reset both player positions
player2.newRound(4, -1);
player1.scoreCount = 0;
player2.scoreCount = 0;
player1.killStreak = 0;
player2.killStreak = 0;
parent.player1Score.setText("0");
parent.player2Score.setText("0");
parent.status.setText(" ");
}
// player 2 wins, reset game
if (player2.scoreCount == 7) {
parent.status.setText("PLAYER 2 WINS!");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {};
// reset all variables and create new round
player1.newRound(1, 1); // reset both player positions
player2.newRound(4, -1);
player1.scoreCount = 0;
player2.scoreCount = 0;
player1.killStreak = 0;
player2.killStreak = 0;
parent.player1Score.setText("0");
parent.player2Score.setText("0");
parent.status.setText(" ");
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {};
repaint();
}
}
public void start() {
t = new Thread(this);
t.start();
}
public void stop() {
t = null;
}
public synchronized void update(Graphics g) {
Dimension d = getSize();
// initially (or when size changes) create new image
if ((offscreen == null) || (d.width != offscreensize.width) || (d.height != offscreensize.height)) {
offscreen = createImage(d.width, d.height);
offscreensize = d;
g2 = offscreen.getGraphics();
}
// erase old contents:
g2.setColor(getBackground());
g2.fillRect(0, 0, d.width, d.height);
// now, draw as usual, but use g2 instead of g
g2.setColor(Color.RED);
player1.draw();
g2.setColor(Color.BLUE);
player2.draw();
// finally, draw the image on top of the old one
g.drawImage(offscreen, 0, 0, null);
}
// this is called when window is uncovered or resized
public synchronized void paint(Graphics g) {
update(g);
}
}