forked from renewing1007/assign4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assign4.pde
567 lines (503 loc) · 17.1 KB
/
assign4.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
/* please implement your assign4 code in this file. */
// 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 = 50;
final int HP_POINT_MAX = 100;
final int HP_POINT_HIT = 20;
final int HP_POINT_ENERGY = 10;
// 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;
// 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;
// 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[] arrayEnemyX = new int[MAX_ENEMY_NUM];
int[] arrayEnemyY = new int[MAX_ENEMY_NUM];
boolean[] arrayEnemyEnable = new boolean[MAX_ENEMY_NUM];
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 bg1RX;
int bg2RX;
int fighterX;
int fighterY;
int hpPoint;
int treasureX;
int treasureY;
// keypressed
boolean isLeftPressed;
boolean isRightPressed;
boolean isUpPressed;
boolean isDownPressed;
// images
PImage gameStart1;
PImage gameStart2;
PImage bg1;
PImage bg2;
PImage imageEnemy;
PImage fighter;
PImage hp;
PImage treasure;
PImage gameLose1;
PImage gameLose2;
PImage bullet;
PImage[] boom = new PImage[5];
void setup () {
size(640, 480) ; // must use this size.
// your code
bg1RX = width; // bg1 right side position
bg2RX = width * 2; // bg2 right side position
// load images
gameStart1 = loadImage("img/start2.png");
gameStart2 = loadImage("img/start1.png");
bg1 = loadImage("img/bg1.png");
bg2 = loadImage("img/bg2.png");
imageEnemy = loadImage("img/enemy.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;
// Begin from type 0
enemyTeamType = 0;
// get random start position
// hp
hpPoint = HP_POINT_DEFAULT;
// enemy
changeEnemyTeamType = true;
// fighter
//fighterX = floor(random(width-FIGHTER_SIZE));
//fighterY = floor(random(height-FIGHTER_SIZE));
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() {
// your code
switch (gameState)
{
case GAME_START:
// 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);
}
break;
case GAME_RUN:
// draw background
bg1RX = (bg1RX + BACKGROUND_SPEED);
if (bg1RX >= width * 2)
bg1RX = 0;
bg2RX = (bg2RX + BACKGROUND_SPEED);
if (bg2RX >= width * 2)
bg2RX = 0;
image(bg1, bg1RX-width, 0);
image(bg2, bg2RX-width, 0);
// draw treasure
image(treasure, treasureX, treasureY);
// draw boom
//// 1frame = 100ms = 0.1s
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;
}
}
// 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);
// draw enemy team
if (changeEnemyTeamType == true) {
if (enemyTeamType == 1) {
int startY = 0;
// Dive a slant direction
if (random(-1, 1) >= 0) {
// slant /
slantDirection = 1;
startY = floor(random(height-ENEMY_SIZE*5));
} else {
// slant \
slantDirection = -1;
startY = floor(random(ENEMY_SIZE*4, height-ENEMY_SIZE));
}
// set start position
for (int enemyNo = 0; enemyNo < MAX_ENEMY_NUM; enemyNo++) {
if (enemyNo < ENEMY_NUM_2) {
arrayEnemyX[enemyNo] = 0 - enemyNo * (ENEMY_SIZE + ENEMY_GAP);
arrayEnemyY[enemyNo] = startY + slantDirection * enemyNo * ENEMY_SIZE;
arrayEnemyEnable[enemyNo] = true;
} else {
arrayEnemyEnable[enemyNo] = false;
}
}
} else if (enemyTeamType == 2) {
int startY = floor(random(2*(ENEMY_SIZE + ENEMY_GAP), height - 3*(ENEMY_SIZE + ENEMY_GAP)));
for (int enemyNo = 0; enemyNo < MAX_ENEMY_NUM; enemyNo++) {
if (enemyNo == 0) {
arrayEnemyX[enemyNo] = 0 - enemyNo * (ENEMY_SIZE + ENEMY_GAP);
arrayEnemyY[enemyNo] = startY - enemyNo * (ENEMY_SIZE + ENEMY_GAP);
} else if (enemyNo <= 2) {
arrayEnemyX[enemyNo] = arrayEnemyX[enemyNo-1] - (ENEMY_SIZE + ENEMY_GAP);
arrayEnemyY[enemyNo] = arrayEnemyY[enemyNo-1] - (ENEMY_SIZE + ENEMY_GAP);
} else if (enemyNo <= 4) {
arrayEnemyX[enemyNo] = arrayEnemyX[enemyNo-1] - (ENEMY_SIZE + ENEMY_GAP);
arrayEnemyY[enemyNo] = arrayEnemyY[enemyNo-1] + (ENEMY_SIZE + ENEMY_GAP);
} else if (enemyNo <= 6) {
arrayEnemyX[enemyNo] = arrayEnemyX[enemyNo-1] + (ENEMY_SIZE + ENEMY_GAP);
arrayEnemyY[enemyNo] = arrayEnemyY[enemyNo-1] + (ENEMY_SIZE + ENEMY_GAP);
} else {
arrayEnemyX[enemyNo] = arrayEnemyX[enemyNo-1] + (ENEMY_SIZE + ENEMY_GAP);
arrayEnemyY[enemyNo] = arrayEnemyY[enemyNo-1] - (ENEMY_SIZE + ENEMY_GAP);
}
arrayEnemyEnable[enemyNo] = true;
}
} else { //if (enemyTeamType == 0) {
enemyTeamType = 0;
int startY = floor(random(height-ENEMY_SIZE));
for (int enemyNo = 0; enemyNo < MAX_ENEMY_NUM; enemyNo++)
{
if (enemyNo < ENEMY_NUM_1) {
arrayEnemyX[enemyNo] = 0-enemyNo*(ENEMY_SIZE+ENEMY_GAP);
arrayEnemyY[enemyNo] = startY;
arrayEnemyEnable[enemyNo] = true;
} else {
arrayEnemyEnable[enemyNo] = false;
}
}
}
changeEnemyTeamType = false;
}
// draw enemy images
// all enemies fly from left to right
for (int enemyNo = 0; enemyNo < MAX_ENEMY_NUM; enemyNo++) {
if (arrayEnemyEnable[enemyNo] == true)
{
image(imageEnemy, arrayEnemyX[enemyNo], arrayEnemyY[enemyNo]);
// set new position
arrayEnemyX[enemyNo]+=ENEMY_SPEED;
}
}
// 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;
}
// ENABLE_COLLISION
// Enemy hit Detection
int detectSource = SOURCE_NONE;
int detectHit = HIT_NONE;
for (int enemyNo = 0; enemyNo < MAX_ENEMY_NUM; enemyNo++) {
if (arrayEnemyEnable[enemyNo] == false) {
continue;
}
// fighter hit detect
if (arrayEnemyX[enemyNo] <= fighterX && fighterX <= arrayEnemyX[enemyNo] + ENEMY_SIZE &&
arrayEnemyY[enemyNo] <= fighterY && fighterY <= arrayEnemyY[enemyNo] + ENEMY_SIZE) {
// Fighter Left-Up corner is in enemy's rect
detectSource = SOURCE_FIGHTER;
detectHit = HIT_ENEMY;
} else if (arrayEnemyX[enemyNo] <= fighterX+FIGHTER_SIZE && fighterX+FIGHTER_SIZE <= arrayEnemyX[enemyNo] + ENEMY_SIZE &&
arrayEnemyY[enemyNo] <= fighterY && fighterY <= arrayEnemyY[enemyNo] + ENEMY_SIZE) {
// Fighter Right-Up corner is in enemy's rect
detectSource = SOURCE_FIGHTER;
detectHit = HIT_ENEMY;
} else if (arrayEnemyX[enemyNo] <= fighterX && fighterX <= arrayEnemyX[enemyNo] + ENEMY_SIZE &&
arrayEnemyY[enemyNo] <= fighterY+FIGHTER_SIZE && fighterY+FIGHTER_SIZE <= arrayEnemyY[enemyNo] + ENEMY_SIZE) {
// Fighter Left-Down corner is in enemy's rect
detectSource = SOURCE_FIGHTER;
detectHit = HIT_ENEMY;
} else if (arrayEnemyX[enemyNo] <= fighterX+FIGHTER_SIZE && fighterX+FIGHTER_SIZE <= arrayEnemyX[enemyNo] + ENEMY_SIZE &&
arrayEnemyY[enemyNo] <= fighterY+FIGHTER_SIZE && fighterY+FIGHTER_SIZE <= arrayEnemyY[enemyNo] + ENEMY_SIZE) {
// Fighter Right-Down corner is in enemy's rect
detectSource = SOURCE_FIGHTER;
detectHit = HIT_ENEMY;
}
// not hit fighter, then check bullet
if (detectHit == HIT_NONE)
{
// bullet hit detect
for (int j = 0; j < MAX_BULLET_NUM; j++) {
if (arrayBulletEnable[j] == false)
continue;
if (arrayEnemyX[enemyNo] <= arrayBulletX[j] && arrayBulletX[j] <= arrayEnemyX[enemyNo] + ENEMY_SIZE &&
arrayEnemyY[enemyNo] <= arrayBulletY[j] && arrayBulletY[j] <= arrayEnemyY[enemyNo] + ENEMY_SIZE) {
// Bullet Left-Up corner is in enemy's rect
detectSource = SOURCE_BULLET;
detectHit = HIT_ENEMY;
} else if (arrayEnemyX[enemyNo] <= arrayBulletX[j]+FIGHTER_SIZE && arrayBulletX[j]+FIGHTER_SIZE <= arrayEnemyX[enemyNo] + ENEMY_SIZE &&
arrayEnemyY[enemyNo] <= arrayBulletY[j] && arrayBulletY[j] <= arrayEnemyY[enemyNo] + ENEMY_SIZE) {
// Bullet Right-Up corner is in enemy's rect
detectSource = SOURCE_BULLET;
detectHit = HIT_ENEMY;
} else if (arrayEnemyX[enemyNo] <= arrayBulletX[j] && arrayBulletX[j] <= arrayEnemyX[enemyNo] + ENEMY_SIZE &&
arrayEnemyY[enemyNo] <= arrayBulletY[j]+FIGHTER_SIZE && arrayBulletY[j]+FIGHTER_SIZE <= arrayEnemyY[enemyNo] + ENEMY_SIZE) {
// Bullet Left-Down corner is in enemy's rect
detectSource = SOURCE_BULLET;
detectHit = HIT_ENEMY;
} else if (arrayEnemyX[enemyNo] <= arrayBulletX[j]+FIGHTER_SIZE && arrayBulletX[j]+FIGHTER_SIZE <= arrayEnemyX[enemyNo] + ENEMY_SIZE &&
arrayEnemyY[enemyNo] <= arrayBulletY[j]+FIGHTER_SIZE && arrayBulletY[j]+FIGHTER_SIZE <= arrayEnemyY[enemyNo] + ENEMY_SIZE) {
// Bullet Right-Down corner is in enemy's rect
detectSource = SOURCE_BULLET;
detectHit = HIT_ENEMY;
}
if (detectHit == HIT_ENEMY)
{
arrayBulletEnable[j] = false;
break;
}
}
}
// hit enemy => disable enemy
if (detectHit == HIT_ENEMY) {
arrayEnemyEnable[enemyNo] = false;
// 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] = arrayEnemyX[enemyNo];
arrayBoomY[j] = arrayEnemyY[enemyNo];
arrayBoomShow[j] = 0;
break;
}
}
break;
}
}
// Treasure hit Detection
if (treasureX <= fighterX && fighterX <= treasureX + ENEMY_SIZE &&
treasureY <= fighterY && fighterY <= treasureY + ENEMY_SIZE) {
// Fighter Left-Up corner is in treasure's rect
detectHit = HIT_TREASURE;
} else if (treasureX <= fighterX+FIGHTER_SIZE && fighterX+FIGHTER_SIZE <= treasureX + ENEMY_SIZE &&
treasureY <= fighterY && fighterY <= treasureY + ENEMY_SIZE) {
// Fighter Right-Up corner is in treasure's rect
detectHit = HIT_TREASURE;
} else if (treasureX <= fighterX && fighterX <= treasureX + ENEMY_SIZE &&
treasureY <= fighterY+FIGHTER_SIZE && fighterY+FIGHTER_SIZE <= treasureY + ENEMY_SIZE) {
// Fighter Left-Down corner is in treasure's rect
detectHit = HIT_TREASURE;
} else if (treasureX <= fighterX+FIGHTER_SIZE && fighterX+FIGHTER_SIZE <= treasureX + ENEMY_SIZE &&
treasureY <= fighterY+FIGHTER_SIZE && fighterY+FIGHTER_SIZE <= treasureY + ENEMY_SIZE) {
// Fighter Right-Down corner is in treasure's rect
detectHit = HIT_TREASURE;
}
// Check fighter hit enemy
if (detectHit == HIT_ENEMY && detectSource == SOURCE_FIGHTER) {
hpPoint -= HP_POINT_HIT;
if (hpPoint <= 0) {
hpPoint = 0;
gameState = GAME_LOSE;
}
} 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));
}
// check if changeEnemyTeamType
boolean allDisabled = true;
int enemyNo;
for (enemyNo = 0; enemyNo < MAX_ENEMY_NUM; enemyNo++) {
if (arrayEnemyEnable[enemyNo] == false)
continue;
// out of screen
if (arrayEnemyX[enemyNo] > 640 + ENEMY_SIZE) {
arrayEnemyEnable[enemyNo] = false;
} else {
allDisabled = false;
}
}
if (allDisabled == true) {
changeEnemyTeamType = true;
enemyTeamType++;
if (enemyTeamType > 2) {
enemyTeamType = 0;
}
}
// draw hpRed - show on the top
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);
break;
case GAME_LOSE:
// 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
hpPoint = HP_POINT_DEFAULT;
// reset fighter
//fighterX = floor(random(width-FIGHTER_SIZE));
//fighterY = floor(random(height-FIGHTER_SIZE));
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;
}
}
} else {
image(gameLose1, 0, 0);
}
break;
}
}
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;
}
}
}