forked from steward379/assign5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assign5.pde
610 lines (537 loc) · 13 KB
/
assign5.pde
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
// constant variables
final int GAME_START = 0;
final int GAME_RUN = 1;
final int GAME_LOSE = 2;
final int SOURCE_NONE = 0;
final int SOURCE_FIGHTER = 1;
final int SOURCE_BULLET = 2;
final int HIT_NONE = 0;
final int HIT_ENEMY = 1;
final int HIT_TREASURE = 2;
final int HP_POINT_DEFAULT = 20;
final int HP_POINT_MAX = 100;
final int HP_POINT_HIT = 20;
final int HP_POINT_ENERGY = 10;
final int ENEMY_SCORE = 20;
// Fixed Position
final int HP_X = 10;
final int HP_Y = 10;
final int HP_RED_X = HP_X + 5;
final int HP_RED_Y = HP_Y + 2;
final int BUTTON_X = 202;
final int BUTTON_Y = 280;
final int BUTTON_WIDTH = 255;
final int BUTTON_HEIGHT = 123;
final int DEFAULT_FIGHTER_X = 500;
final int DEFAULT_FIGHTER_Y = 240;
final int SCORE_X = 10;
final int SCORE_Y = 470;
// image size
final int FIGHTER_SIZE = 50;
final int ENEMY_SIZE = 60;
final int ENEMY_GAP = (ENEMY_SIZE / 2);
final int TREASURE_SIZE = 40;
final int HP_WIDTH = 202;
final int HP_HEIGHT = 20;
final int BULLET_WIDTH = 32;
final int BULLET_HEIGHT = 27;
// speed
final int BACKGROUND_SPEED = 2;
final int ENEMY_SPEED = 5;
final int FIGHTER_SPEED = 5;
final int BULLET_SPEED = 5;
final int BULLET_TRACE_SPEED = 1;
// level_enemy_num
final int ENEMY_NUM_1 = 5;
final int ENEMY_NUM_2 = 5;
final int ENEMY_NUM_3 = 8;
final int MAX_ENEMY_NUM = 8;
final int BOOM_IMAGE_NONE = -1;
final int MAX_BOOM_IMAGE_NUM = 5;
final int MAX_BULLET_NUM = 5;
int[] arrayBoomX = new int[MAX_ENEMY_NUM];
int[] arrayBoomY = new int[MAX_ENEMY_NUM];
int[] arrayBoomShow = new int[MAX_ENEMY_NUM];
int[] arrayBulletX = new int[MAX_BULLET_NUM];
int[] arrayBulletY = new int[MAX_BULLET_NUM];
boolean[] arrayBulletEnable = new boolean[MAX_BULLET_NUM];
// variables
int gameState;
int enemyTeamType = -1;
boolean changeEnemyTeamType = false;
int slantDirection;
int currentMaxEnemyNum = ENEMY_NUM_1;
// positions
int bgR;
int bg1RX;
int bg2RX;
int fighterX;
int fighterY;
int hpPoint;
int treasureX;
int treasureY;
// keypressed
boolean isLeftPressed;
boolean isRightPressed;
boolean isUpPressed;
boolean isDownPressed;
PImage enemy;
int enemyCount = 8;
int type;
int[] enemyX = new int[enemyCount];
int[] enemyY = new int[enemyCount];
int score;
PFont scoreFont;
// images
PImage gameStart1;
PImage gameStart2;
PImage bg1;
PImage bg2;
PImage fighter;
PImage hp;
PImage treasure;
PImage gameLose1;
PImage gameLose2;
PImage bullet;
PImage[] boom = new PImage[5];
void setup () {
size(640, 480) ;
bgR = 0;
bg1RX = 0; // bg1 left side
bg2RX = -width; // bg2 left side
// load images
enemy = loadImage("img/enemy.png");
gameStart1 = loadImage("img/start2.png");
gameStart2 = loadImage("img/start1.png");
bg1 = loadImage("img/bg1.png");
bg2 = loadImage("img/bg2.png");
fighter = loadImage("img/fighter.png");
hp = loadImage("img/hp.png");
treasure = loadImage("img/treasure.png");
gameLose1 = loadImage("img/end2.png");
gameLose2 = loadImage("img/end1.png");
for (int i = 0; i < MAX_BOOM_IMAGE_NUM; i++) {
boom[i] = loadImage("img/flame" + (i+1) + ".png");
}
for (int i = 0; i < MAX_ENEMY_NUM; i++) {
arrayBoomShow[i] = BOOM_IMAGE_NONE; // not show
}
bullet = loadImage("img/shoot.png");
// default
gameState = GAME_START;
isLeftPressed = false;
isRightPressed = false;
isUpPressed = false;
isDownPressed = false;
score = 0;
scoreFont = createFont("Arial", 32);
type = 0;
addEnemy(type);
// hp
hpPoint = HP_POINT_DEFAULT;
// fighter
fighterX = DEFAULT_FIGHTER_X;
fighterY = DEFAULT_FIGHTER_Y;
// treasure
treasureX = floor(random(width-TREASURE_SIZE));
treasureY = floor(random(height-TREASURE_SIZE));
frameRate(60);
}
void draw()
{
background(0);
switch (gameState) {
case GAME_START:
drawGameStart();
break;
case GAME_RUN:
drawBackground();
drawTreasure();
drawBoom();
drawFighter();
drawEnemy();
drawBullet();
collisionDetect();
drawScore();
drawHp();
break;
case GAME_LOSE:
drawGameLose();
break;
}
}
void drawGameStart() {
// Mouse in the button rectangle
if (BUTTON_X < mouseX && mouseX < BUTTON_X + BUTTON_WIDTH &&
BUTTON_Y < mouseY && mouseY < BUTTON_Y + BUTTON_HEIGHT) {
image(gameStart2, 0, 0);
// Click
if (mousePressed) {
gameState = GAME_RUN;
}
} else {
image(gameStart1, 0, 0);
}
}
void drawGameLose() {
// Mouse in the button rectangle
if (BUTTON_X < mouseX && mouseX < BUTTON_X + BUTTON_WIDTH &&
BUTTON_Y < mouseY && mouseY < BUTTON_Y + BUTTON_HEIGHT) {
image(gameLose2, 0, 0);
// Click
if (mousePressed) {
gameState = GAME_RUN;
// reset hpPoint & score
hpPoint = HP_POINT_DEFAULT;
score = 0;
// reset fighter
fighterX = DEFAULT_FIGHTER_X;
fighterY = DEFAULT_FIGHTER_Y;
enemyTeamType = -1;
changeEnemyTeamType = true;
// Clear Boom
for (int i = 0; i < MAX_ENEMY_NUM; i++) {
arrayBoomShow[i] = BOOM_IMAGE_NONE;
}
// Clear Bullets
for (int i = 0; i < MAX_BULLET_NUM; i++){
arrayBulletEnable[i] = false;
}
// Reset enemy
type = 0;
addEnemy(type);
}
} else {
image(gameLose1, 0, 0);
}
}
void drawBackground() {
image(bg1, bg1RX, 0);
image(bg2, bg2RX, 0);
bg1RX += BACKGROUND_SPEED;
if (bg1RX >= width)
bg1RX = -width;
bg2RX += BACKGROUND_SPEED;
if (bg2RX >= width)
bg2RX = -width;
}
void drawTreasure() {
image(treasure, treasureX, treasureY);
}
void drawFighter() {
// Fighter Position
if (isUpPressed) {
fighterY -= FIGHTER_SPEED;
}
if (isDownPressed) {
fighterY += FIGHTER_SPEED;
}
if (isLeftPressed) {
fighterX -= FIGHTER_SPEED;
}
if (isRightPressed) {
fighterX += FIGHTER_SPEED;
}
// Fighter - Screen Edge Boundery Detection
if (fighterX <= 0) {
fighterX = 0;
} else if (fighterX > width - FIGHTER_SIZE) {
fighterX = width - FIGHTER_SIZE;
}
if (fighterY <= 0) {
fighterY = 0;
} else if (fighterY > height - FIGHTER_SIZE) {
fighterY = height - FIGHTER_SIZE;
}
// draw fighter
image(fighter, fighterX, fighterY);
}
void drawBullet(){
// draw bullet
for (int i = 0; i < MAX_BULLET_NUM; i++) {
if (arrayBulletEnable[i] == false) {
continue;
}
image(bullet, arrayBulletX[i], arrayBulletY[i]);
arrayBulletX[i]-=BULLET_SPEED;
// out of screen, disable bullet
if (arrayBulletX[i] < -BULLET_WIDTH)
arrayBulletEnable[i] = false;
int enemyNo = closestEnemy(arrayBulletX[i], arrayBulletY[i]);
if (enemyNo > -1) {
// find closest enemy
if (enemyY[enemyNo] < arrayBulletY[i]) {
// above bullet
arrayBulletY[i]-=BULLET_TRACE_SPEED;
} else {
// below bullet
arrayBulletY[i]+=BULLET_TRACE_SPEED;
}
}
}
}
void drawHp(){
fill(#FF0000);
rect(HP_RED_X, HP_RED_Y, hpPoint*HP_WIDTH/HP_POINT_MAX, HP_HEIGHT, 0, 3, 0, 0);
image(hp, HP_X, HP_Y);
}
void drawEnemy(){
boolean isAllOutScreen = true;
for (int i = 0; i < enemyCount; ++i) {
if (enemyX[i] != -1 || enemyY[i] != -1) {
image(enemy, enemyX[i], enemyY[i]);
enemyX[i]+=5;
if (enemyX[i] <= width + ENEMY_SIZE) // + SIZE for longer gap between types
isAllOutScreen = false;
}
}
// change enemy type
if (isAllOutScreen == true) {
type++;
if (type > 2)
type = 0;
addEnemy(type);
}
}
void drawBoom() {
int switchNextFlame = (frameCount % (60/6) == 0) ? 1 : 0;
for (int i = 0; i < MAX_ENEMY_NUM; i++) {
if (arrayBoomShow[i] != BOOM_IMAGE_NONE) {
image(boom[arrayBoomShow[i]], arrayBoomX[i], arrayBoomY[i]);
arrayBoomShow[i]+=switchNextFlame;
if (arrayBoomShow[i] >= MAX_BOOM_IMAGE_NUM)
arrayBoomShow[i] = BOOM_IMAGE_NONE;
}
}
}
void drawScore(){
fill(255);
textFont(scoreFont);
text("Score: " + score, SCORE_X, SCORE_Y);
}
// 0 - straight, 1-slope, 2-diamond
void addEnemy(int type)
{
for (int i = 0; i < enemyCount; ++i) {
enemyX[i] = -1;
enemyY[i] = -1;
}
switch (type) {
case 0:
addStraightEnemy();
break;
case 1:
addSlopeEnemy();
break;
case 2:
addDiamondEnemy();
break;
}
}
void addStraightEnemy()
{
float t = random(height - enemy.height);
int h = int(t);
for (int i = 0; i < 5; ++i) {
enemyX[i] = (i+1)*-80;
enemyY[i] = h;
}
}
void addSlopeEnemy()
{
float t = random(height - enemy.height * 5);
int h = int(t);
for (int i = 0; i < 5; ++i) {
enemyX[i] = (i+1)*-80;
enemyY[i] = h + i * 40;
}
}
void addDiamondEnemy()
{
float t = random( enemy.height * 3 ,height - enemy.height * 3);
int h = int(t);
int x_axis = 1;
for (int i = 0; i < 8; ++i) {
if (i == 0 || i == 7) {
enemyX[i] = x_axis*-80;
enemyY[i] = h;
x_axis++;
}
else if (i == 1 || i == 5){
enemyX[i] = x_axis*-80;
enemyY[i] = h + 1 * 40;
enemyX[i+1] = x_axis*-80;
enemyY[i+1] = h - 1 * 40;
i++;
x_axis++;
}
else {
enemyX[i] = x_axis*-80;
enemyY[i] = h + 2 * 40;
enemyX[i+1] = x_axis*-80;
enemyY[i+1] = h - 2 * 40;
i++;
x_axis++;
}
}
}
void scoreChange(int value){
score += value;
}
boolean isHit(int ax, int ay, int aw, int ah, int bx, int by, int bw, int bh) {
if (ax < bx + bw &&
ax + aw > bx &&
ay < by + bh &&
ah + ay > by) {
// collision detected!
return true;
}
return false;
}
void collisionDetect() {
boolean hit = false;
// Enemy hit Detection
int detectSource = SOURCE_NONE;
int detectHit = HIT_NONE;
for (int enemyNo = 0; enemyNo < MAX_ENEMY_NUM; enemyNo++) {
if (enemyX[enemyNo] == -1 && enemyY[enemyNo] == -1) {
continue;
}
// fighter hit enemy detect
hit = isHit(enemyX[enemyNo], enemyY[enemyNo], ENEMY_SIZE, ENEMY_SIZE,
fighterX, fighterY, FIGHTER_SIZE, FIGHTER_SIZE);
if (hit == true) {
detectSource = SOURCE_FIGHTER;
detectHit = HIT_ENEMY;
} else {
// bullet hit detect
for (int j = 0; j < MAX_BULLET_NUM; j++) {
if (arrayBulletEnable[j] == false)
continue;
hit = isHit(enemyX[enemyNo], enemyY[enemyNo], ENEMY_SIZE, ENEMY_SIZE,
arrayBulletX[j], arrayBulletY[j], BULLET_WIDTH, BULLET_HEIGHT);
if (hit == true) {
detectSource = SOURCE_BULLET;
detectHit = HIT_ENEMY;
arrayBulletEnable[j] = false;
break;
}
}
}
// hit enemy => disable enemy
if (detectHit == HIT_ENEMY) {
// Get enemy position as boom position
for (int j = 0; j < MAX_ENEMY_NUM; j++) {
// Find space to show boom
if (arrayBoomShow[j] == BOOM_IMAGE_NONE) {
arrayBoomX[j] = enemyX[enemyNo];
arrayBoomY[j] = enemyY[enemyNo];
arrayBoomShow[j] = 0;
break;
}
}
// clear this enemy
enemyX[enemyNo] = -1;
enemyY[enemyNo] = -1;
break;
}
}
if (hit == false) {
// Treasure hit Detection
hit = isHit (treasureX, treasureY, TREASURE_SIZE, TREASURE_SIZE,
fighterX, fighterY, FIGHTER_SIZE, FIGHTER_SIZE);
if (hit == true) {
detectHit = HIT_TREASURE;
}
}
// Check fighter hit enemy
if (detectHit == HIT_ENEMY){
if (detectSource == SOURCE_FIGHTER) {
hpPoint -= HP_POINT_HIT;
if (hpPoint <= 0) {
hpPoint = 0;
gameState = GAME_LOSE;
}
} else if (detectSource == SOURCE_BULLET) {
scoreChange(ENEMY_SCORE);
}
} else if (detectHit == HIT_TREASURE) {
hpPoint += HP_POINT_ENERGY;
if (hpPoint >= HP_POINT_MAX) {
hpPoint = HP_POINT_MAX;
}
// reset treasure position
treasureX = floor(random(width-TREASURE_SIZE));
treasureY = floor(random(height-TREASURE_SIZE));
}
}
// return enemyNo, if no enemy, return -1
int closestEnemy(int x, int y){
int enemyNo = -1;
int enemyDistance = 999999;
for (int index=0; index < enemyCount; index++) {
if (enemyX[index] == -1 && enemyY[index] == -1) {
continue;
}
// enemy not on the left side of bullet
if (enemyX[index] >= x) {
continue;
}
int distance = ((x- enemyX[index])*abs(y-enemyY[index]))/2;
// find nearest enemy
if (distance < enemyDistance)
{
enemyDistance = distance;
enemyNo = index;
}
}
return enemyNo;
}
void keyPressed() {
if (key == CODED) {
switch(keyCode)
{
case UP:
isUpPressed = true;
break;
case DOWN:
isDownPressed = true;
break;
case LEFT:
isLeftPressed = true;
break;
case RIGHT:
isRightPressed = true;
break;
}
} else if (key == ' ') {
// space to shoot bullet
for (int i = 0; i < MAX_BULLET_NUM; i++) {
if (arrayBulletEnable[i] == false) {
arrayBulletEnable[i] = true;
arrayBulletX[i] = fighterX;
arrayBulletY[i] = fighterY + (FIGHTER_SIZE / 2) - (BULLET_HEIGHT/2);
break;
}
}
}
}
void keyReleased() {
if (key == CODED) {
switch(keyCode)
{
case UP:
isUpPressed = false;
break;
case DOWN:
isDownPressed = false;
break;
case LEFT:
isLeftPressed = false;
break;
case RIGHT:
isRightPressed = false;
break;
}
}
}