-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickgame.java
More file actions
617 lines (524 loc) · 14.5 KB
/
Quickgame.java
File metadata and controls
617 lines (524 loc) · 14.5 KB
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;
import javax.imageio.ImageIO;
/*
* TODO:
* - Kollision
* - For Each e: in Paint bumper, kann falsch sein, da gerade die Liste modifiziert wird - anderer thread
* - countdown kommt auch in terrainpaint vor - da updaten
* */
public class Quickgame extends Applet implements Runnable, KeyListener {
private static final long serialVersionUID = -6440197294597461669L;
private long delaytime;
public static int mode;
public static int countdown;
public static Console console;
public static int appletsize_x = 800; // Startgröße des Applets in x - Richtung
public static int appletsize_y = 640; // Größe des Applets in y - Richtung
private Image dbImage; // Double Buffer
private Graphics2D dbg;
private int apl_width;
private int apl_height;
public Image i_hintergrund;
public Image i_bumper;
LinkedList<Bumper> list;
public BufferedImage i_car1;
public BufferedImage i_car2;
public int bumperpos;
private int cam_pos_y;
private Car car1, car2;
private int score1, score2;
private int m11_randomtime=0;
private Boolean m14_randommode=false;
// Renderdata
// Init
public void init() {
repaint(0, 0, 0, 300, 300);
setBackground(new Color(30, 30, 30));
// Größe
resize(appletsize_x, appletsize_y);
// KeyListener
addKeyListener(this);
console = new Console(appletsize_y);
//CLIENT=new Pclient(host);
//Objekte laden
i_hintergrund = getImage(getCodeBase(), "hintergrund.gif");
i_car1 = loadImages("car1.gif");
i_car2 = loadImages("car2.gif");
i_bumper = loadImages("roadblock.gif");
Sound.init(this);
Sound.start_music();
newRound();
}
public BufferedImage loadImages(String input){
URL url;
BufferedImage img;
try {
url = new URL(getCodeBase()+input);
img = ImageIO.read(url);
return img;
}
catch (IOException e) { System.out.print("could not load image");
}
return null;
}
public void start() {
resize(appletsize_x, appletsize_y);
// Neuer Thread
Thread th = new Thread(this);
th.start();
}
public void stop() {
}
public void destroy() {
}
public void run() {
while (true) { //MAIN LOOP
// delay calculation startpoint
long delay = System.nanoTime();
move_cars();
spawnBlocks(200);
collision(car1);
collision(car2);
if(mode==11)
{
randomLenk(car1);
randomLenk(car2);
m11_randomtime--;
}
checkCountdown();
Console.update();
repaint(0, 0, 0, appletsize_x, appletsize_x);
checkVictory();
delaytime = System.nanoTime() - delay;
try { //SChLAF
long delaysmall = delaytime / 1000000;
if (delaysmall >= 0) {
if (delaysmall < 16) {
Thread.sleep(16 - delaysmall);
}
}
}
catch (InterruptedException ex) {
// Do nothing
}
}
}
public void newRound()
{
double angle = Math.random() * Math.PI/2 - Math.PI/4;
car1 = new Car(200, 500, angle);
car2 = new Car(600, 500, -angle);
bumperpos = 0;
cam_pos_y = 0;
list = new LinkedList<Bumper>();
countdown = 180;
}
public void randomLenk(Car i_car)
{
if(m11_randomtime==0)
{
m11_randomtime= (int) (Math.random()*120+50);
}
if(m11_randomtime==1)
{
double j=(Math.random()*Math.PI-Math.PI/2.0);
i_car.set_angle(i_car.get_angle()+j);
}
}
public void Victory(int i)
{
if(i==1)
score1++;
else if(i==2)
score2++;
else
return;
Console.print(score1+" : "+score2, appletsize_x/2-60, 120, 30, 80);
Sound.stop_motor();
newRound();
}
public void checkVictory()
{
//if(Math.abs(car1.get_y()-car2.get_y()) >appletsize_y )
if(cam_pos_y+car1.get_y()>appletsize_y+20 || cam_pos_y+car2.get_y()>appletsize_y+20)
{
if(car1.get_y()<car2.get_y())
Victory(1);
else
Victory(2);
}
}
public void newMode(){
int nextmode = mode;
while(nextmode==mode)
{
nextmode=(int) Math.round(Math.random()*15.5);
}
mode = nextmode;
//mode=14;
if (!m14_randommode)
{
if (mode==3) Sound.change_song(Sound.SONG_WALZER);
else Sound.change_song(Sound.SONG_DEFAULT);
switch(mode) {
case 0:
Console.print("Standard", appletsize_x/2-180, 80, 50, 50);
break;
case 1:
Console.print("Back to the future", appletsize_x/2-250, 80, 50, 50);
break;
case 2:
Console.print("MAX Boing!", appletsize_x/2-140, 80, 50, 50);
break;
case 3:
Console.print("Ice ice baby", appletsize_x/2-140, 80, 50, 50);
break;
case 4:
Console.print("Wohoo See Ya!", appletsize_x/2-180, 80, 50, 50);
break;
case 5:
Console.print("Doppelganger", appletsize_x/2-180, 80, 50, 50);
break;
case 6:
Console.print("Left yields to right!", appletsize_x/2-250, 80, 50, 50);
break;
case 7:
Console.print("Right, oh wait...!", appletsize_x/2-200, 80, 50, 50);
break;
case 8:
Console.print("Standard again", appletsize_x/2-220, 80, 50, 50);
break;
case 9:
Console.print("Pedal to the metal", appletsize_x/2-220, 80, 50, 50);
break;
case 10:
Console.print("The elder scrolls", appletsize_x/2-220, 80, 50, 50);
break;
case 11:
Console.print("Let's play a little game...", appletsize_x/2-280, 80, 50, 50);
break;
case 12:
Console.print("Brickbraker", appletsize_x/2-100, 80, 50, 50);
break;
case 13:
Console.print("Ballet of cars", appletsize_x/2-200, 80, 50, 50);
break;
case 14:
case 15:
Console.print("You don't know, Jack", appletsize_x/2-250, 80, 50, 50);
m14_randommode=true;
newMode();
//Console.print("mode "+mode);
m14_randommode=false;
break;
}
}
}
public void checkCountdown()
{
if(countdown==180)
{
Sound.countdown();
Console.print("3", appletsize_x/2-10, 300, 50, 29);
mode=0;
}
if(countdown==120)
Console.print("2", appletsize_x/2-10, 300, 50, 29);
if(countdown==60)
Console.print("1", appletsize_x/2-10, 300, 50, 29);
if(countdown==1)
{
Sound.start_motor();
Console.print("Go!", appletsize_x/2-20, 300, 50, 30);
newMode();
if(mode==5)
{
double angle=car1.get_angle();
car1=new Car(600, 500, -angle);
car2=new Car(200, 500, angle);
}
}
if(countdown>0) countdown--;
}
public void spawnBlocks(int frequency) //NEU
{
if(bumperpos==0)
{
bumperpos=cam_pos_y-frequency+10;
//Console.print(""+bumperpos);
}
if(cam_pos_y-bumperpos>=frequency)
{
int type=(int) Math.round(Math.random()*5.2);
switch (type){
case 0:
{
list.push(new Bumper((int) (Math.random()*800), -cam_pos_y-200, (int) (Math.random()*100+50), (int) (Math.random()*100+50)));
bumperpos+=frequency/2;
break;
}
case 1:
{
list.push(new Bumper((int) (Math.random()*300), -cam_pos_y-200, (int) (Math.random()*100+50), (int) (Math.random()*100+50)));
list.push(new Bumper((int) (Math.random()*200)+300, -cam_pos_y-200, (int) (Math.random()*100+50), (int) (Math.random()*100+50)));
list.push(new Bumper((int) (Math.random()*300)+500, -cam_pos_y-200, (int) (Math.random()*100+50), (int) (Math.random()*100+50)));
bumperpos+=frequency;
break;
}
case 2:
{
for(int i=0; i<4; i++)
{
list.push(new Bumper((int) (Math.random()*80)+200*i, -cam_pos_y-200, 80, (int) (Math.random()*20+50)));
}
bumperpos+=frequency;
break;
}
case 3:
{
int size=(int) (Math.random()*100+50);
list.push(new Bumper(0, -cam_pos_y-200, size , size));
list.push(new Bumper(appletsize_x-size, -cam_pos_y-150, size , size));
bumperpos+=frequency;
break;
}
case 4:
{
int size=(int) (Math.random()*200+150);
list.push(new Bumper(size, -cam_pos_y-220, appletsize_x-2*size , size/2));
bumperpos+=frequency*1.5;
break;
}
case 5:
{
list.push(new Bumper((int) (Math.random()*220), -cam_pos_y-200, (int) (Math.random()*100+80), (int) (Math.random()*100+50)));
list.push(new Bumper((int) (Math.random()*220)+400, -cam_pos_y-200, (int) (Math.random()*100+80), (int) (Math.random()*100+50)));
bumperpos+=frequency;
break;
}
//Console.print("spawn "+bumperpos);
}
}
}
public void collision(Car i_car)
{
int box_m_x;
int box_m_y;
for(Bumper e: list)
{
if(i_car.get_x()>e.x-i_car.get_radius() && i_car.get_x()<e.x+e.width+i_car.get_radius())
if(i_car.get_y()>e.y-i_car.get_radius() && i_car.get_y()<e.y+e.height+i_car.get_radius())
{
//Console.print("bounce!", i_car.get_x(), i_car.get_y()+cam_pos_y, 12, 50);
Sound.bounce();
box_m_x=e.x+e.width/2;
box_m_y=e.y+e.height/2;
if(Math.floor(i_car.get_y()-box_m_y)==0 || Math.abs((i_car.get_x()-box_m_x)/(i_car.get_y()-box_m_y)) > (e.width/e.height)) //Seitlich
{
if (i_car.get_x() < box_m_x) i_car.bounce_left(e.x-i_car.get_radius()); // von links
else i_car.bounce_right(e.x+e.width+i_car.get_radius()); //von rechts
}
else
{
if (i_car.get_y() < box_m_y) i_car.bounce_top(e.y-i_car.get_radius()); // von oben
else i_car.bounce_bottom(e.y+e.height+i_car.get_radius()); //von unten
}
if(mode==12) // BRICKBRAKER modus
{
if(e.get_time()==0) e.set_time(60);
}
break;
}
}
if(i_car.get_x()<i_car.get_radius()) {
Sound.bounce();
i_car.bounce_right(i_car.get_radius());
} else if (i_car.get_x()>appletsize_x-i_car.get_radius()) {
Sound.bounce();
i_car.bounce_left(appletsize_x-i_car.get_radius());
}
}
public void paint(Graphics2D g) {
terrainpaint(g);
bumperpaint(g);
debugpaint(g);
AffineTransform tx;
AffineTransformOp op;
tx = AffineTransform.getRotateInstance(car1.get_angle());
op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
if(mode!=5)
g.drawImage(i_car1, op, car1.get_x() + (int)(Math.sin(car1.get_angle() - 0.5404) * 29.15), car1.get_y() - (int)(Math.cos(car1.get_angle() - 0.5404) * 29.15)+cam_pos_y);
else
g.drawImage(i_car2, op, car1.get_x() + (int)(Math.sin(car1.get_angle() - 0.5404) * 29.15), car1.get_y() - (int)(Math.cos(car1.get_angle() - 0.5404) * 29.15)+cam_pos_y);
tx = AffineTransform.getRotateInstance(car2.get_angle());
op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
if(mode!=5)
g.drawImage(i_car2, op, car2.get_x() + (int)(Math.sin(car2.get_angle() - 0.5404) * 29.15), car2.get_y() - (int)(Math.cos(car2.get_angle() - 0.5404) * 29.15)+cam_pos_y);
else
g.drawImage(i_car1, op, car2.get_x() + (int)(Math.sin(car2.get_angle() - 0.5404) * 29.15), car2.get_y() - (int)(Math.cos(car2.get_angle() - 0.5404) * 29.15)+cam_pos_y);
Console.paint(g);
}
public void terrainpaint(Graphics2D g)
{
int y= (int) ((car1.get_y()+car2.get_y())/2);
if(mode==10)
{
if(countdown==180) cam_pos_y=-y+450 - (int) (120*(Math.abs((car1.get_y()-car2.get_y())/600.0)));
if(countdown==0)
{
if(-car1.get_y()>cam_pos_y-40) cam_pos_y=-car1.get_y()+40;
else if(-car2.get_y()>cam_pos_y-40) cam_pos_y=-car2.get_y()+40;
else cam_pos_y+=2;
}
}
else
if(cam_pos_y>-appletsize_y) cam_pos_y=-y+450 - (int) (120*(Math.abs((car1.get_y()-car2.get_y())/600.0)));
int y2=(cam_pos_y+2*appletsize_y)%appletsize_y;
g.drawImage(i_hintergrund, 0, y2, this);
g.drawImage(i_hintergrund, 0, y2-appletsize_y, this);
}
public void debugpaint(Graphics g) //NEU
{
g.setFont(new Font("Arial", Font.PLAIN, 12));
// g.setColor(Color.white);
// g.drawString("Pos y: "+cam_pos_y, 20, 20);
// g.drawString("bumper y: "+bumperpos, 90, 20);
// g.drawString("no."+list.size(), 180, 20);
// g.drawString("delay "+(delaytime / 1000), 220, 20 );
g.drawString("delay "+(delaytime / 1000), 20, 20 );
g.drawString(" "+score1+" : "+score2, 20, 60);
}
public void bumperpaint(Graphics g) //NEU
{
for(Bumper e: list)
{
if(-e.y > cam_pos_y-appletsize_y ) //Teste ob es noch im bildschirm ist
{
g.drawImage(i_bumper, e.x, e.y+cam_pos_y, e.width, e.height, this);
if(mode==12 && e.get_time()!=0)
{
int i=e.get_time();
i--;
e.set_time(i);
if(i==1)
{
list.remove(e);
Console.print("pop!",e.x+e.width/2-8, e.y+e.height/2-2+cam_pos_y,12,50);
}
}
}
// g.drawString(""+e.y, e.x, e.y+cam_pos_y+20); //Debug, zeichen position
else
{
list.remove(e);
}
}
}
private void move_cars() {
car1.move();
car2.move();
}
@Override
public void keyPressed(KeyEvent arg0) {
if(countdown==0)
{
switch(arg0.getKeyChar()) {
case 'w':
if(mode!=1) car1.goForward();
else car1.goBrake();
break;
case 'a':
if(mode==7) car1.steerRight();
else car1.steerLeft();
break;
case 's':
if(mode!=1) car1.goBrake();
else car1.goForward();
break;
case 'd':
if(mode==7) car1.steerLeft();
else if(mode!=6) car1.steerRight();
break;
case 'i':
if(mode!=1) car2.goForward();
else car2.goBrake();
break;
case 'j':
if(mode==7) car2.steerRight();
else car2.steerLeft();
break;
case 'k':
if(mode!=1) car2.goBrake();
else car2.goForward();
break;
case 'l':
if(mode==7) car2.steerLeft();
else if(mode!=6) car2.steerRight();
break;
case 'h':
Sound.honk();
break;
}
}
}
@Override
public void keyReleased(KeyEvent arg0) {
switch(arg0.getKeyChar()) {
case 'w':
if(mode==9 && car1.get_forwardspeed()>1)
{
Console.print("Player 1 left the pedal!", appletsize_x/2-120, 500, 18, 50);
Victory(2);
}
case 's':
car1.goRelease();
break;
case 'a':
case 'd':
if(mode!=13) car1.steerRelease();
break;
case 'i':
if(mode==9 && car2.get_forwardspeed()>1)
{
Console.print("Player 2 left the pedal!", appletsize_x/2-120, 500, 18, 50);
Victory(1);
}
case 'k':
car2.goRelease();
break;
case 'j':
case 'l':
if(mode!=13) car2.steerRelease();
break;
}
}
@Override
public void keyTyped(KeyEvent arg0) {
}
public void update(Graphics g) {
if (dbImage == null) {
// Größe des Applets
apl_width = getSize().width;
apl_height = getSize().height;
dbImage = createImage(apl_width, apl_height);
dbg = (Graphics2D) dbImage.getGraphics();
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
dbg.setRenderingHints(rh);
}
//dbg.setColor(getBackground());
//dbg.fillRect(0, 0, apl_width, apl_height);
//dbg.setColor(getForeground());
paint(dbg);
g.drawImage(dbImage, 0, 0, this);
//paint(g);
}
}