forked from steward379/assign5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassign5.pde
391 lines (360 loc) · 8.41 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
//gamestate
PImage startOne, startTwo, endOne, endTwo;
int gamestate;
final int GAME_START = 1, GAME_RUN = 2, GAME_END = 3;
//background
PImage backgroundOne, backgroundTwo;
int x;
//hp
PImage hpImg;
float hp;
//treasure
PImage treasure;
int treasureX, treasureY;
//fighter
PImage fighter;
int fighterX, fighterY;
boolean upPressed = false, downPressed = false, leftPressed = false, rightPressed = false;
//enemy
PImage enemy;
int restartX;
int enemyCount = 8, enemyType=0;
int[] enemyX = new int[enemyCount];
int[] enemyY = new int[enemyCount];
//bullet
PImage bullet;
int bulletSpeed=5;
int bCounter=-1;
int b=1;
int[] bulletX = new int[5];
int[] bulletY = new int [5];
boolean [] bulletShooting = new boolean[5];
boolean spacePressed = false;
//score
int score=0;
PFont Verdana;
void setup () {
size(640, 480) ;
//fighter
fighter = loadImage("img/fighter.png");
//hp
hpImg = loadImage("img/hp.png");
hp=196*0.2;
//treasure random
treasure = loadImage("img/treasure.png");
treasureX=floor(random(20,520));
treasureY=floor(random(50,430));
//enemy
enemy = loadImage("img/enemy.png");
enemyX[0]=10;
enemyY[0]=floor(random(50,400));
//background
backgroundOne=loadImage("img/bg1.png");
backgroundTwo=loadImage("img/bg2.png");
x=0;
//gamestate
startOne = loadImage("img/start2.png");
startTwo = loadImage("img/start1.png");
endOne = loadImage("img/end2.png");
endTwo = loadImage("img/end1.png");
gamestate = GAME_START;
//fighter
fighterX=570;
fighterY=240;
//bullet
bullet=loadImage("img/shoot.png");
for (int i=0; i<5;i++){
bulletX[i]=-100;
bulletY[i]=-100;
bulletShooting[i]=false;
}
Verdana=createFont("Verdana",30);
addEnemy(0);
}
void draw()
{
background(0);
switch(gamestate){
case GAME_START:
image(startOne,0,0);
if(mouseX>210 && mouseX<450 && mouseY>370 && mouseY<420){
image(startTwo,0,0);
if(mousePressed){
gamestate=2;
}
}
break;
case GAME_RUN:
//background moving
image(backgroundTwo,x,0);
image(backgroundOne,x-640,0);
image(backgroundTwo,x-1280,0);
x++;
x=x%1280;
// fighter moving
image(fighter, fighterX, fighterY);
if(upPressed){
fighterY -= 8;
}
if(downPressed){
fighterY += 8;
}
if(leftPressed){
fighterX -= 8;
}
if(rightPressed){
fighterX += 8;
}
// fighter boundary ditection
if(fighterX>width-fighter.width){
fighterX=width-fighter.width;
}
if(fighterX<0){
fighterX=0;
}
if(fighterY>height-fighter.height){
fighterY=height-fighter.height;
}
if(fighterY<0){
fighterY=0;
}
//bullet
for(int i=0; i<5; i++){
if(bulletShooting[i]==true){
image(bullet, bulletX[i],bulletY[i]);
bulletX[i]-=bulletSpeed;
if(enemyY[closestEnemy(fighterX, fighterY)]<fighterY){
bulletY[i]--;
}else{
bulletY[i]++;
}
if(bulletX[i]<0){
bulletX[i]=-10000;
bulletShooting[i]=false;
}
}
}
if (spacePressed == true){
int totalBullet=0;
for(int i=0; i<5; i++){
if(bulletShooting[i]==true){
totalBullet++;
}
}
if(b==1 && bulletShooting[(b-1)%5]==false){
bulletX[(b-1)%5]=fighterX;
bulletY[(b-1)%5]=fighterY+(fighter.height)/2 ;
bulletShooting[(b-1)%5] = true;
}
if(totalBullet < 5 && fighterX - bulletX[(b-1)%5] >= 65){
bulletX[b%5] = fighterX;
bulletY[b%5] = fighterY+(fighter.height)/2;
bulletShooting[b%5] = true;
b++;
}
}
//bullet hit enemy
for(int i =0;i< 8;i++){
if(enemyX[i]!=-1 || enemyY[i]!=-1){
for(int j=0; j<5; j++){
if(isHit(enemyX[i], enemyY[i], enemy.width, enemy.height, bulletX[j], bulletY[j], bullet.width, bullet.height)){
bulletY[j]=-10000;
enemyY[i]=-1000;
scoreChange(20);
}
}
}
}
//score
fill(255);
textFont(Verdana,30);
text("Score: "+score,10,height-10);
//eating treasure
image(treasure,treasureX,treasureY);
if(isHit(fighterX, fighterY, fighter.width, fighter.height, treasureX, treasureY, treasure.width, treasure.height)){
treasureX=floor(random(20,520));
treasureY=floor(random(50,430));
hp += 196*0.1;
if(hp>196){
hp=196;
}
}
//hp
stroke(255,0,0,230);
fill(255,0,0,230);
rect(20,12,hp,18);
image(hpImg,10,10);
//crash
for(int i =0;i< 8;i++){
if(isHit(fighterX, fighterY, fighter.width, fighter.height, enemyX[i], enemyY[i], enemy.width, enemy.height) ){
hp -=196*0.2;
enemyY[i]=-100;
if(hp<1){
gamestate=3;
}
}
}
//enemy moving
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[0]-80*5 > width){
enemyType++;
enemyType%=3;
addEnemy(enemyType);
}
break;
case GAME_END:
image(endOne,0,0);
if(mouseX>210 && mouseX<450 && mouseY>300 && mouseY<350){
image(endTwo,0,0);
if(mousePressed){
for(int i=0; i<5; i++){
bulletShooting[i]=false;
}
score=0;
gamestate=2;
addEnemy(0);
fighterX=570;
fighterY=240;
treasureX=floor(random(20,520));
treasureY=floor(random(50,430));
hp=196*0.2;
}
}
}
}
// 0 - straight, 1-slope, 2-dimond
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(float value){
score+=value;
}
boolean isHit(int ax, int ay, int aw, int ah, int bx, int by, int bw, int bh){
if(ay+ah>by && ay<by+bh && ax<bx+bw && ax+aw>bx){
return true;
}
return false;
}
int closestEnemy(int x, int y){
float minDist=800;
float [] distances = new float [8];
for(int i=0; i<8; i++){
if(enemyX[i]!=-1 || enemyY[i]!=-1){
distances[i]=dist(enemyX[i],enemyY[i],x,y);
}
if(distances[i]<minDist){
minDist=distances[i];
}
if(distances[i]==minDist){
return i;
}
}
return -1;
}
void keyPressed(){
if (key == CODED){
switch(keyCode){
case UP:
upPressed = true;
break;
case DOWN:
downPressed = true;
break;
case LEFT:
leftPressed = true;
break;
case RIGHT:
rightPressed = true;
}
}
if (key == ' '){
spacePressed = true;
}
}
void keyReleased(){
if (key == CODED){
switch(keyCode){
case UP:
upPressed = false;
break;
case DOWN:
downPressed = false;
break;
case LEFT:
leftPressed = false;
break;
case RIGHT:
rightPressed = false;
}
}
if (key == ' '){
spacePressed = false;
}
}