-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassign4.pde
493 lines (413 loc) · 11 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
Ship ship;
PowerUp ruby;
Bullet[] bList;
Laser[] lList;
Alien[] aList;
//Game Status
final int GAME_START = 0;
final int GAME_PLAYING = 1;
final int GAME_PAUSE = 2;
final int GAME_WIN = 3;
final int GAME_LOSE = 4;
int status; //Game Status
int point; //Game Score
int expoInit; //Explode Init Size
int countBulletFrame; //Bullet Time Counter
int bulletNum; //Bullet Order Number
/*--------Put Variables Here---------*/
int countAlien;
int aliveAlien;
int laserNum;
void setup() {
status = GAME_START;
bList = new Bullet[30];
lList = new Laser[30];
aList = new Alien[100];
size(640, 480);
background(0, 0, 0);
rectMode(CENTER);
ship = new Ship(width/2, 460, 3);
ruby = new PowerUp(int(random(width)), -10);
reset();
}
void draw() {
background(50, 50, 50);
noStroke();
switch(status) {
case GAME_START:
/*---------Print Text-------------*/
printText();
/*--------------------------------*/
break;
case GAME_PLAYING:
background(50, 50, 50);
drawHorizon();
drawScore();
drawLife();
ship.display(); //Draw Ship on the Screen
drawAlien();
drawBullet();
drawLaser();
/*---------Call functions---------------*/
alienShoot(50);
checkWinLose();
checkAlienDead();/*finish this function*/
checkShipHit(); /*finish this function*/
countBulletFrame+=1;
break;
case GAME_PAUSE:
/*---------Print Text-------------*/
printText();
/*--------------------------------*/
break;
case GAME_WIN:
/*---------Print Text-------------*/
printText();
/*--------------------------------*/
winAnimate();
break;
case GAME_LOSE:
loseAnimate();
/*---------Print Text-------------*/
printText();
/*--------------------------------*/
break;
}
}
void drawHorizon() {
stroke(153);
line(0, 420, width, 420);
}
void drawScore() {
noStroke();
fill(95, 194, 226);
textAlign(CENTER, CENTER);
textSize(23);
text("SCORE:"+point, width/2, 16);
}
void keyPressed() {
if (status == GAME_PLAYING) {
ship.keyTyped();
cheatKeys();
shootBullet(30);
}
statusCtrl();
}
/*---------Make Alien Function-------------*/
void alienMaker(int alienNums,int numInRow) {
countAlien = alienNums;
for(int i = 0;i < alienNums;i++){
int col = int(i % numInRow);
int row = int(i / numInRow);
aList[i]= new Alien(50+(40*col), 50+(50*row));
}
}
void drawLife() {
fill(230, 74, 96);
text("LIFE:", 36, 455);
/*---------Draw Ship Life---------*/
for(int life = 0 ;life < ship.life ;life++){
ellipse(78+(25*life),459,15,15);
}
}
void drawBullet() {
for (int i=0; i<bList.length-1; i++) {
Bullet bullet = bList[i];
if (bullet!=null && !bullet.gone) { // Check Array isn't empty and bullet still exist
bullet.move(); //Move Bullet
bullet.display(); //Draw Bullet on the Screen
if (bullet.bY<0 || bullet.bX>width || bullet.bX<0) {
removeBullet(bullet); //Remove Bullet from the Screen
}
}
}
}
void drawLaser() {
for (int i=0; i<lList.length-1; i++) {
Laser laser = lList[i];
if (laser!=null && !laser.gone) { // Check Array isn't empty and Laser still exist
laser.move(); //Move Laser
laser.display(); //Draw Laser
if (laser.lY > 480) {
removeLaser(laser); //Remove Laser from the Screen
}
}
}
}
void drawAlien() {
for (int i=0; i<aList.length-1; i++) {
Alien alien = aList[i];
if (alien!=null && !alien.die) { // Check Array isn't empty and alien still exist
alien.move(); //Move Alien
alien.display(); //Draw Alien
/*---------Call Check Line Hit---------*/
/*--------------------------------------*/
}
}
}
/*--------Check Line Hit---------*/
void checkAlienBut(){
}
/*---------Ship Shoot-------------*/
void shootBullet(int frame) {
if ( key == ' ' && countBulletFrame>frame) {
if (!ship.upGrade) {
bList[bulletNum]= new Bullet(ship.posX, ship.posY, -3, 0);
if (bulletNum<bList.length-2) {
bulletNum+=1;
} else {
bulletNum = 0;
}
}
/*---------Ship Upgrade Shoot-------------*/
else {
bList[bulletNum]= new Bullet(ship.posX, ship.posY, -3, 0);
if (bulletNum<bList.length-2) {
bulletNum+=1;
} else {
bulletNum = 0;
}
}
countBulletFrame = 0;
}
}
/*---------Check Alien Hit-------------*/
void checkAlienDead() {
for (int i=0; i<bList.length-1; i++) {
Bullet bullet = bList[i];
for (int j=0; j<aList.length-1; j++) {
Alien alien = aList[j];
if (bullet != null && alien != null && !bullet.gone && !alien.die // Check Array isn't empty and bullet / alien still exist
/*------------Hit detect-------------*/
&& bList[i].bX >= int(aList[j].aX - (aList[j].aSize*3/2))
&& bList[i].bX <= int(aList[j].aX + (aList[j].aSize*3/2))
&& bList[i].bY >= aList[j].aY - aList[j].aSize
&& bList[i].bY <= aList[j].aY
) {
/*-------do something------*/
removeBullet(bList[i]);
removeAlien(aList[j]);
point += 10;
aliveAlien--;
}else if (bullet != null && alien != null && !bullet.gone && !alien.die
/*------------Hit detect-------------*/
&& bList[i].bX >= int(aList[j].aX - aList[j].aSize)
&& bList[i].bX <= int(aList[j].aX + aList[j].aSize)
&& bList[i].bY >= aList[j].aY
&& bList[i].bY <= aList[j].aY + aList[j].aSize
) {
/*-------do something------*/
removeBullet(bList[i]);
removeAlien(aList[j]);
point += 10;
aliveAlien--;
}
}
}
}
/*---------Alien Drop Laser-----------------*/
void alienShoot(int frame){
int i = int(frameCount / frame);
if (frameCount % frame == 0) {
int j = int(random(countAlien));
if(!aList[j].die){
lList[laserNum]= new Laser(aList[j].aX, aList[j].aY);
if(laserNum < lList.length-2){
laserNum++;
}else{
laserNum = 0;
}
}else if(aList[j].die){
alienShoot(50);
}
}
}
/*---------Check Laser Hit Ship-------------*/
void checkShipHit() {
for (int i=0; i<lList.length-1; i++) {
Laser laser = lList[i];
if (laser!= null && !laser.gone // Check Array isn't empty and laser still exist
/*------------Hit detect-------------*/
&& lList[i].lX >= int(ship.posX - ship.shipSize/2)
&& lList[i].lX <= int(ship.posX + ship.shipSize/2)
&& lList[i].lY >= int(ship.posY - ship.shipSize)
&& lList[i].lY <= int(ship.posY + ship.shipSize/2-6)
) {
/*-------do something------*/
ship.life--;
drawLife();
removeLaser(lList[i]);
}else if (laser!= null && !laser.gone
/*------------Hit detect-------------*/
&& lList[i].lX >= int( ship.posX - ship.shipSize)
&& lList[i].lX <= int( ship.posX + ship.shipSize)
&& lList[i].lY >= int( ship.posY + ship.shipSize/2-6)
&& lList[i].lY <= int( ship.posY + ship.shipSize)
) {
/*-------do something------*/
ship.life--;
drawLife();
removeLaser(lList[i]);
}
}
}
/*---------Check Win Lose------------------*/
void checkWinLose(){
switch(status){
case GAME_PLAYING:
if(ship.life == 0){
status = GAME_LOSE;
}else if(aliveAlien == 0){
status = GAME_WIN;
}
}
}
void winAnimate() {
int x = int(random(128))+70;
fill(x, x, 256);
ellipse(width/2, 200, 136, 136);
fill(50, 50, 50);
ellipse(width/2, 200, 120, 120);
fill(x, x, 256);
ellipse(width/2, 200, 101, 101);
fill(50, 50, 50);
ellipse(width/2, 200, 93, 93);
ship.posX = width/2;
ship.posY = 200;
ship.display();
}
void loseAnimate() {
fill(255, 213, 66);
ellipse(ship.posX, ship.posY, expoInit+200, expoInit+200);
fill(240, 124, 21);
ellipse(ship.posX, ship.posY, expoInit+150, expoInit+150);
fill(255, 213, 66);
ellipse(ship.posX, ship.posY, expoInit+100, expoInit+100);
fill(240, 124, 21);
ellipse(ship.posX, ship.posY, expoInit+50, expoInit+50);
fill(50, 50, 50);
ellipse(ship.posX, ship.posY, expoInit, expoInit);
expoInit+=5;
}
/*---------Check Ruby Hit Ship-------------*/
/*---------Check Level Up------------------*/
/*---------Print Text Function-------------*/
void printText(){
int startTitleSize = 60;
int titleSize = 40;
int subTitleSize = 20;
fill(95,194,226);
textAlign(CENTER);
switch(status){
case GAME_START:
textSize(startTitleSize);
text("GALIXIAN",width/2,240);
textSize(subTitleSize);
text("Press ENTER to Start",width/2,280);
break;
case GAME_PAUSE:
textSize(titleSize);
text("PAUSE",width/2,240);
textSize(subTitleSize);
text("Press ENTER to Start",width/2,280);
break;
case GAME_WIN:
textSize(titleSize);
text("WINNER",width/2,300);
textSize(subTitleSize);
text("Score:"+point,width/2,340);
break;
case GAME_LOSE:
textSize(titleSize);
text("BOOOM",width/2,240);
textSize(subTitleSize);
text("You are dead!!",width/2,280);
break;
}
}
void removeBullet(Bullet obj) {
obj.gone = true;
obj.bX = 2000;
obj.bY = 2000;
}
void removeLaser(Laser obj) {
obj.gone = true;
obj.lX = 2000;
obj.lY = 2000;
}
void removeAlien(Alien obj) {
obj.die = true;
obj.aX = 1000;
obj.aY = 1000;
}
/*---------Reset Game-------------*/
void reset() {
for (int i=0; i<bList.length-1; i++) {
bList[i] = null;
lList[i] = null;
}
for (int i=0; i<aList.length-1; i++) {
aList[i] = null;
}
point = 0;
expoInit = 0;
countBulletFrame = 30;
bulletNum = 0;
/*--------Init Variable Here---------*/
laserNum = 0;
ship.life = 3;
/*-----------Call Make Alien Function--------*/
alienMaker(53,12);
aliveAlien = countAlien;
ship.posX = width/2;
ship.posY = 460;
ship.upGrade = false;
ruby.show = false;
ruby.pX = int(random(width));
ruby.pY = -10;
}
/*-----------finish statusCtrl--------*/
void statusCtrl() {
if (key == ENTER) {
switch(status) {
case GAME_START:
status = GAME_PLAYING;
break;
/*-----------add things here--------*/
case GAME_PLAYING:
status = GAME_PAUSE;
break;
case GAME_PAUSE:
status = GAME_PLAYING;
break;
case GAME_WIN:
reset();
status = GAME_PLAYING;
break;
case GAME_LOSE:
reset();
status = GAME_PLAYING;
break;
}
}
}
void cheatKeys() {
if (key == 'R'||key == 'r') {
ruby.show = true;
ruby.pX = int(random(width));
ruby.pY = -10;
}
if (key == 'Q'||key == 'q') {
ship.upGrade = true;
}
if (key == 'W'||key == 'w') {
ship.upGrade = false;
}
if (key == 'S'||key == 's') {
for (int i = 0; i<aList.length-1; i++) {
if (aList[i]!=null) {
aList[i].aY+=50;
}
}
}
}