-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathassign4.pde
395 lines (343 loc) · 7.13 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
/* please implement your assign1 code in this file. */
PImage fighterImg;
PImage treasureImg;
PImage hpImg;
PImage enemyImg;
PImage bg1Img;
PImage bg2Img;
PImage bg3Img;
PImage bg4Img;
PImage end1Img;
PImage end2Img;
PImage start1Img;
PImage start2Img;
final int GAME_START=0;
final int GAME_RUN=1;
final int GAME_OVER=2;
int numFrames=5;
int gamestate = GAME_START;
int currentFrame;
boolean upPressed=false;
boolean downPressed=false;
boolean leftPressed=false;
boolean rightPressed=false;
final int straight=3;
final int slash=4;
final int diamond=5;
int state=3;
int treasure_x,treasure_y,hp_x,x2,y2,x3,y3;
float fighterx1,fightery1;
int blockX;
int blockY;
//float spacingx,spacingy,spacingy1;
float [] enemy_y;
float [] enemy_x;
float [] enemy_X;
float [] enemy_Y;
PImage [] flameImg = new PImage[numFrames];
int nbrEnemy = 5;
int nbrEnemy1=8;
float enemyY;
float enemyX;
float enemyx;
float enemyy;
int disappear;
int disappear1;
int notHere;
int notHere1;
boolean detect;
void setup () {
size(640,480) ; // must use this size.
// your code
//load image
fighterImg=loadImage("img/fighter.png");
treasureImg=loadImage("img/treasure.png");
hpImg=loadImage("img/hp.png");
enemyImg=loadImage("img/enemy.png");
bg1Img=loadImage("img/bg1.png");//bg1
bg2Img=loadImage("img/bg2.png");//bg2
bg3Img=loadImage("img/bg1.png");//bg1
bg4Img=loadImage("img/bg2.png");//bg2
end1Img=loadImage("img/end1.png");
end2Img=loadImage("img/end2.png");
start1Img=loadImage("img/start1.png");
start2Img=loadImage("img/start2.png");
treasure_x=floor(random(600));
treasure_y=floor(random(34,440));
//treasure_x=176;
//treasure_y=100;
enemy_x=new float[nbrEnemy];
enemy_y=new float[nbrEnemy];
enemy_X=new float[nbrEnemy1];
enemy_Y=new float[nbrEnemy1];
blockX=enemyImg.width+5;
blockY=enemyImg.height;
hp_x=38;
enemyX=0;
enemyY=0;
enemyx=-(4*blockX+enemyImg.width);
enemyy=random(2*blockY,height-blockY);
for(int i=0; i<nbrEnemy; i++){
enemy_x[0]=enemyx;
enemy_y[0]=enemyy;
}
for(int i=0; i<nbrEnemy1; i++){
enemy_X[0]=enemyX;
enemy_Y[0]=enemyY;
}
//enemy_y=315;
//enemy_y=floor(random(135,315));
currentFrame = 0;
for (int i=0; i<numFrames; i++){
flameImg[i]=loadImage("img/flame"+(i+1)+".png");
}
//frameRate(10);
x2=-640;//cordinate of bg1
y2=0;//cordinate of bg2
x3=-1920;
y3=-1280;
fighterx1=width-50;
fightery1=height/2;
//fighterx1=176;
//fightery1=200;
}
void draw() {
// your code
switch(gamestate){
case GAME_START:
background(0);
image(start2Img,0,0);
if(mouseY>375 && mouseY<420){
if(mouseX>200 && mouseX<450){
background(0);
image(start1Img,0,0);
if(mousePressed){
gamestate=1;
}
}else{
background(0);
image(start1Img,0,0);
}
}
break;
case 1:
background(0);
image(bg1Img,x2,0);
x2++;//bg1 move to right
image(bg2Img,y2,0);
y2++;//bg2 move to right
image(bg1Img,x3,0);
x3++;//bg1 move to right
image(bg2Img,y3,0);
y3++;//bg2 move to right
//background cycle
if(x2==0 && y2==640){
x3=-1280;
y3=-640;}
if(x3==0 && y3==640){
x2=-1280;
y2=-640;}
//fighter
image(fighterImg,fighterx1,fightery1);
//keypressed
if(rightPressed){
fighterx1+=5;
}if(leftPressed){
fighterx1-=5;
}if(upPressed){
fightery1-=5;
}if(downPressed){
fightery1+=5;
}
//boundary detection
if(fighterx1>width-50){
fighterx1=width-50;
}
if(fighterx1<=0){
fighterx1=0;
}
if(fightery1>height-50){
fightery1=height-50;
}
if(fightery1<=0){
fightery1=0;}
//more fun
/*
if(fightery1>enemy_y){
enemy_y+=2;}
if(fightery1<enemy_y)
{enemy_y-=2;}
if(fightery1==enemy_y){
fightery1=enemy_y;
enemy_x+=2;
}
*/
//if the fighter gets the treasure
//if the fighter gets the treasure
if(treasure_x-40 <= fighterx1 && fighterx1 <= treasure_x+40 ){
if(treasure_y-50 <= fightery1 && fightery1 <= treasure_y+40){
hp_x+=19;
if(hp_x>190){
hp_x=190;
}
treasure_x=floor(random(600));
treasure_y=floor(random(34,440));
}
}
//hp
fill(#FF0000);
rect(13,5,hp_x,15);
image(hpImg,0,0);
//treasure
image(treasureImg,treasure_x,treasure_y);
//enemy
switch(state){
case straight:
enemyx+=3;
//enemy staright
for(int i=0; i<5;i++){
//println(enemy_x[i]);
if(fighterx1+fighterImg.width>enemy_x[i] && fighterx1 < enemy_x[i]+enemyImg.width&&
enemy_y[i] <fightery1+fighterImg.height && fightery1 < enemy_y[i]+enemyImg.height){
detect=true;
disappear=i;
hp_x-=38;
}
if(detect==true){
enemy_y[disappear]=-100;
enemy_x[i]+=3;
}else{
enemy_x[i]=enemyx+i*blockX;
enemy_y[i]=enemyy;
}
image(enemyImg,enemy_x[i],enemy_y[i]);
//if the fighter is attacked by the enemy
if(enemyx>width){
detect=false;
enemyx=-(4*blockX+enemyImg.width);
enemyy=random(5*blockY,height-blockY);
state=4;
}
}
break;
case 4:
//image(enemyImg,enemy_x,enemy_y);
enemyx+=3;
for(int i=0; i<5;i++){
//println(enemy_x[i]);
if(fighterx1+fighterImg.width>enemy_x[i] && fighterx1 < enemy_x[i]+enemyImg.width&&
enemy_y[i] <fightery1+fighterImg.height && fightery1 < enemy_y[i]+enemyImg.height){
hp_x-=38;
detect=true;
disappear=i;
}if(detect==true){
enemy_x[i]+=3;
enemy_y[disappear]=-100;
}
else{
enemy_x[i]=enemyx+i*blockX;
enemy_y[i]=enemyy-i*blockY;
}
image(enemyImg,enemy_x[i],enemy_y[i]);
if(enemyx>width){
detect=false;
enemyX=-(2*blockX+enemyImg.width);
enemyY=random(blockY,height-5*blockY);
state=5;
}
}
break;
case 5:
enemyX+=3;
for(int i=0; i<8;i++){
if(fighterx1+fighterImg.width>enemy_X[i] && fighterx1 < enemy_X[i]+enemyImg.width&&
enemy_Y[i] <fightery1+fighterImg.height && fightery1 < enemy_Y[i]+enemyImg.height){
hp_x-=38;
detect=true;
disappear=i;
}if(detect==true){
enemy_X[i]+=3;
enemy_Y[disappear]=-100;
}else{
enemy_Y[i]=enemyY+i*blockY;
enemy_X[i]=enemyX-(2-abs(i-2))*blockY;
if(i>=5){
enemy_X[i]=enemyX+(2-abs(i-6))*blockX;
enemy_Y[i]=enemyY+(i-4)*blockY;
}
}
image(enemyImg,enemy_X[i],enemy_Y[i]);
if(enemyX-(enemyImg.width+2*blockX)>width){
enemyx=-(4*blockX+enemyImg.width);
enemyy=random(blockY,height-blockY);
detect=false;
state=3;
}
}
break;
}
if(hp_x<=0){
gamestate=2;}
break;
case 2:
background(0);
image(end2Img,0,0);
if(mouseY>300 && mouseY<350){
if(mouseX>200 && mouseX<445){
background(0);
image(end1Img,0,0);
if(mousePressed){
hp_x=38;
fighterx1=width-50;
fightery1=height/2;
gamestate=1;
detect=false;
enemyx=-(4*blockX+enemyImg.width);
enemyy=random(2*blockY,height-blockY);
state=3;
}
}
else{
background(0);
image(end1Img,0,0);
}
}
break;
}
}
void keyPressed(){
if(key== CODED){
switch(keyCode){
case UP:
upPressed=true;
break;
case DOWN:
downPressed=true;
break;
case RIGHT:
rightPressed=true;
break;
case LEFT:
leftPressed=true;
break;
}
}
}
void keyReleased(){
if(key== CODED){
switch(keyCode){
case UP:
upPressed=false;
break;
case DOWN:
downPressed=false;
break;
case RIGHT:
rightPressed=false;
break;
case LEFT:
leftPressed=false;
break;
}
}
}