forked from RadianSmile/assign3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assign3.pde
400 lines (351 loc) · 11 KB
/
assign3.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
/* please implement your assign2 code in this file. */
// constant variables
final int GAME_START = 0;
final int GAME_RUN = 1;
final int GAME_LOSE = 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;
// 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;
// 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;
// speed
final int BACKGROUND_SPEED = 2;
final int ENEMY_SPEED = 5;
final int FIGHTER_SPEED = 5;
// level_enemy_num
final int MAX_ENEMY_TEAM_HORIZONTAL_NUM = 5;
final int MAX_ENEMY_TEAM_VERTICAL_NUM = 5;
// variables
int gameState;
int enemyTeamType;
int slantDirection;
float enemySpeedY;
// positions
int bg1RX;
int bg2RX;
int enemyX;
int enemyY;
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 enemy;
PImage fighter;
PImage hp;
PImage treasure;
PImage gameLose1;
PImage gameLose2;
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");
enemy = 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");
// 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;
enemySpeedY = 0;
// enemy
enemyX = 0; // from left side
enemyY = floor(random(height-ENEMY_SIZE));
// fighter
fighterX = floor(random(width-FIGHTER_SIZE));
fighterY = floor(random(height-FIGHTER_SIZE));
// treasure
treasureX = floor(random(width-TREASURE_SIZE));
treasureY = floor(random(height-TREASURE_SIZE));
enemyY = floor(random(ENEMY_SIZE*2,height-ENEMY_SIZE));
}
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);
// Fighter Position
if (isUpPressed){
fighterY -= FIGHTER_SPEED;
}
if (isDownPressed){
fighterY += FIGHTER_SPEED;
}
if (isLeftPressed){
fighterX -= FIGHTER_SPEED;
}
if (isRightPressed){
fighterX += FIGHTER_SPEED;
}
// draw enemy team
if (enemyTeamType == 0) {
for (int enemyNo = 0; enemyNo < MAX_ENEMY_TEAM_HORIZONTAL_NUM; enemyNo++) {
int enemyPosX = enemyX - enemyNo * (ENEMY_SIZE + ENEMY_GAP);
image(enemy, enemyPosX, enemyY);
}
}
else if (enemyTeamType == 1) {
for (int enemyNo = 0; enemyNo < MAX_ENEMY_TEAM_HORIZONTAL_NUM; enemyNo++) {
int enemyPosX = enemyX - enemyNo * (ENEMY_SIZE + ENEMY_GAP);
int enemyPosY = enemyY + slantDirection * enemyNo * ENEMY_GAP;
image(enemy, enemyPosX, enemyPosY);
}
}
else if (enemyTeamType == 2) {
for (int x = 0; x < MAX_ENEMY_TEAM_HORIZONTAL_NUM; x++) {
int enemyPosX = enemyX - x * (ENEMY_SIZE + ENEMY_GAP);
int upperY = abs(((int)(MAX_ENEMY_TEAM_HORIZONTAL_NUM/2) - x));
int lowerY = (MAX_ENEMY_TEAM_VERTICAL_NUM - 1 - abs((int)(MAX_ENEMY_TEAM_HORIZONTAL_NUM/2) - x));
image(enemy, enemyPosX, enemyY+upperY*ENEMY_SIZE);
if (upperY != lowerY) {
image(enemy, enemyPosX, enemyY+lowerY*ENEMY_SIZE);
}
}
}
// enemy start position
if (enemyX <= enemyX - ENEMY_SIZE) {
enemyX = enemyX - ENEMY_SIZE;
} else if (enemyX >= width + MAX_ENEMY_TEAM_HORIZONTAL_NUM * (ENEMY_SIZE + ENEMY_GAP)) {
enemyX = 0;
enemyTeamType++;
// reset position
if (enemyTeamType == 0 || enemyTeamType > 2) {
enemyTeamType = 0;
enemyY = floor(random(height-ENEMY_SIZE));
} else if (enemyTeamType == 1) {
if (random(-1, 1) >= 0) {
// slant /
slantDirection = 1;
enemyY = floor(random(height-ENEMY_SIZE*3));
}
else {
// slant \
slantDirection = -1;
enemyY = floor(random(ENEMY_SIZE*2,height-ENEMY_SIZE));
}
} else {
enemyY = floor(random(height-ENEMY_SIZE*(MAX_ENEMY_TEAM_VERTICAL_NUM)));
}
}
// enemy boundary detection
if (enemyY <= enemyY - ENEMY_SIZE) {
enemyY = enemyY - ENEMY_SIZE;
} else if (enemyY >= height) {
enemyY = height;
}
// enemy move
enemyX+=ENEMY_SPEED;
enemyY+=enemySpeedY;
// 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);
// ENABLE_COLLISION
// Enemy hit Detection
int detectHit = HIT_NONE;
//if (enemyX <= fighterX && fighterX <= enemyX + ENEMY_SIZE &&
// enemyY <= fighterY && fighterY <= enemyY + ENEMY_SIZE) {
// // Fighter Left-Up corner is in enemy's rect
// detectHit = HIT_ENEMY;
//} else if (enemyX <= fighterX+FIGHTER_SIZE && fighterX+FIGHTER_SIZE <= enemyX + ENEMY_SIZE &&
// enemyY <= fighterY && fighterY <= enemyY + ENEMY_SIZE) {
// // Fighter Right-Up corner is in enemy's rect
// detectHit = HIT_ENEMY;
//} else if (enemyX <= fighterX && fighterX <= enemyX + ENEMY_SIZE &&
// enemyY <= fighterY+FIGHTER_SIZE && fighterY+FIGHTER_SIZE <= enemyY + ENEMY_SIZE) {
// // Fighter Left-Down corner is in enemy's rect
// detectHit = HIT_ENEMY;
//} else if (enemyX <= fighterX+FIGHTER_SIZE && fighterX+FIGHTER_SIZE <= enemyX + ENEMY_SIZE &&
// enemyY <= fighterY+FIGHTER_SIZE && fighterY+FIGHTER_SIZE <= enemyY + ENEMY_SIZE) {
// // Fighter Right-Down corner is in enemy's rect
// detectHit = HIT_ENEMY;
//}
// 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 hit result
if (detectHit == HIT_ENEMY) {
hpPoint -= HP_POINT_HIT;
if (hpPoint <= 0) {
hpPoint = 0;
gameState = GAME_LOSE;
}
// reset enemy
enemySpeedY = 0;
enemyX = 0-ENEMY_SIZE; // left start
enemyY = floor(random(height-ENEMY_SIZE));
} 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));
}
// // enemy move toward fighter
// if (fighterY + FIGHTER_SIZE/2 > enemyY + ENEMY_SIZE/2) {
// // fighter is below enemy
// enemySpeedY+=0.2;
// } else if (fighterY + FIGHTER_SIZE/2 < enemyY + ENEMY_SIZE/2) {
// // fighter is above enemy
// enemySpeedY-=0.2;
// }
//}
// 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) {
// reset hpPoint
hpPoint = HP_POINT_DEFAULT;
gameState = GAME_RUN;
enemySpeedY = 0;
}
}
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;
}
}
}
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;
}
}
}