-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProject9.cpp
More file actions
642 lines (598 loc) · 13.4 KB
/
Project9.cpp
File metadata and controls
642 lines (598 loc) · 13.4 KB
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
#define SUBMIT 4
#define BUP 6 //추가
#define BDOWN 7
#define BLEFT 8
#define BRIGHT 9
enum {
black,
blue,
green,
cyan,
red,
purple,
brown,
lightgray,
darkgray,
lightblue,
lightgreen,
lightcyan,
lightred,
lightpurple,
yellow,
white
};
//char tempMap[19][40];
char map[19][40] = {
{"11111111111111111111111111111111111111"},
{"10000000000000000011a000000000000000k1"},
{"101101011k1101011011011010111110101101"},
{"10000100010001000011000010001000100001"},
{"11110111010111011111111011101011101111"},
{"000101m0000001010000k01010000000101000"},
{"111101011l1101011101111010110110101111"},
{"1t000001000100000000000000100010000001"},
{"111101010O010101111011101010k010101111"},
{"0001010111110101p000001010111110101000"},
{"11110100000001011111111010000000101111"},
{"10000101111101000011000010111110100001"},
{"1011c000010000011011011000001000001101"},
{"10010111010111010011001011101011101001"},
{"11010001k00100010111101000100010001011"},
{"10000100010001000011000b1000100k100001"},
{"10101111000111101011010111100011110101"},
{"10000000010000000011000000001000000001"},
{"11111111111111111111111111111111111111"},
};// 0 : 통로, 1 : 벽, k : 열쇠(아이템), p : 플레이어, O : 탈출구 ㅣ : 잠긴 문 t : 아이템
int keyControl(void);
void titleDraw(void);
int menuDraw(void);
int bomb(char(*tMap)[40], int* pX, int* pY, int _x, int _y, int* pKey, int* mlife, int* alife, int* blife, int* clife);
void infoDraw(void);
void init();
void gotoxy(int, int);
void setColor(int, int);
void drawMap(int* pX, int* pY, int* mX, int* mY, int* aX, int* aY, int* bX, int* bY, int* cX, int* cY, char(*tMap)[40]);
int move(char(*tMap)[40], int* pX, int* pY, int* mX, int* mY, int* aX, int* aY, int* bX, int* bY, int* cX, int* cY, int _x, int _y, int* pKey, int* heart, int* mlife, int* alife, int* blife, int* clife);
void gLoop(void);
void drawUI(int pX, int pY, int pKey, int heart);
void endDraw();
//int pKey = 0;
//int playing = 1;
int main(void)
{
int menuCode;
int playing = 1;
init();
while (1)
{
setColor(white, black);
titleDraw();
menuCode = menuDraw();
if (menuCode == 0)
{
gLoop();
}
else if (menuCode == 1)
{
infoDraw();
}
else if (menuCode == 2)
{
break;
}
menuCode = 5;
system("cls");
}
gotoxy(20, 18);
printf("<게임이 종료되었습니다>");
_getch();
return 0;
}
void init()
{
system("mode con cols=63 lines=20 | title Bomber Man");
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO ConsoleCursor;
ConsoleCursor.bVisible = 0;
ConsoleCursor.dwSize = 1;
SetConsoleCursorInfo(consoleHandle, &ConsoleCursor);
}
void titleDraw(void)
{
printf("\n");
printf(" * \n");
printf(" @@@@ * @ @ @@@@ @@@@ @@@@ \n");
printf(" @ @ @@@@ @@ @@ @ @ @ @ @ \n");
printf(" @@@@ @ @ @ @ @ @ @@@@ @@@@ @@@@ \n");
printf(" @ @ @ @ @ @ @ @ @ @ @ @ \n");
printf(" @@@@ @@@@ @ @ @@@@ @@@@ @ @ \n");
printf("\n");
printf(" @ @ @ @ @ \n");
printf(" @@ @@ @ @ @@ @ \n");
printf(" @ @ @ @ @@@@@ @ @ @ \n");
printf(" @ @ @ @ @ @ @@ \n");
}
int menuDraw(void)
{
int x = 27;
int y = 14;
gotoxy(x - 2, y);
printf("> 게임시작");
gotoxy(x, y + 1);
printf("게임설명");
gotoxy(x, y + 2);
printf(" 종료 ");
while (1) {
int n = keyControl();
switch (n) {
case UP: {
if (y > 14) {
gotoxy(x - 2, y);
printf(" ");
gotoxy(x - 2, --y);
printf(">");
}
break;
}
case DOWN: {
if (y < 16) {
gotoxy(x - 2, y);
printf(" ");
gotoxy(x - 2, ++y);
printf(">");
}
break;
}
case SUBMIT: {
return y - 14;
break;
}
}
}
}
void gotoxy(int x, int y)
{
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(consoleHandle, pos);
}
int keyControl()
{
char temp = _getch();
if (temp == 'w' || temp == 'W')
{
return UP;
}
else if (temp == 'a' || temp == 'A')
{
return LEFT;
}
else if (temp == 's' || temp == 'S')
{
return DOWN;
}
else if (temp == 'd' || temp == 'D')
{
return RIGHT;
}
else if (temp == ' ')
{
return SUBMIT;
}
else if (temp == 'i' || temp == 'I')
{
return BUP;
}
else if (temp == 'j' || temp == 'J')
{
return BLEFT;
}
else if (temp == 'k' || temp == 'K')
{
return BDOWN;
}
else if (temp == 'l' || temp == 'L')
{
return BRIGHT;
}
}
void gLoop(void)
{
int mKey;
int pX, pY, mX, mY, aX, aY, bX, bY, cX, cY;
int pKey = 0;
int playing = 1;
int heart = 2;
int mlife = 1;
int alife = 1;
int blife = 1;
int clife = 1;
char tempMap[19][40];
memcpy(tempMap, map, sizeof(tempMap));
drawMap(&pX, &pY, &mX, &mY, &aX, &aY, &bX, &bY, &cX, &cY, tempMap);
while (playing)
{
drawUI(pX, pY, pKey, heart);
mKey = keyControl();
switch (mKey)
{
case UP:
playing = move(tempMap, &pX, &pY, &mX, &mY, &aX, &aY, &bX, &bY, &cX, &cY, 0, -1, &pKey, &heart, &mlife, &alife, &blife, &clife);
break;
case DOWN:
playing = move(tempMap, &pX, &pY, &mX, &mY, &aX, &aY, &bX, &bY, &cX, &cY, 0, 1, &pKey, &heart, &mlife, &alife, &blife, &clife);
break;
case RIGHT:
playing = move(tempMap, &pX, &pY, &mX, &mY, &aX, &aY, &bX, &bY, &cX, &cY, 1, 0, &pKey, &heart, &mlife, &alife, &blife, &clife);
break;
case LEFT:
playing = move(tempMap, &pX, &pY, &mX, &mY, &aX, &aY, &bX, &bY, &cX, &cY, -1, 0, &pKey, &heart, &mlife, &alife, &blife, &clife);
break;
case SUBMIT:
playing = 0;
break;
case BUP:
playing = bomb(tempMap, &pX, &pY, 0, -1, &pKey, &mlife, &alife, &blife, &clife);
break;
case BDOWN:
playing = bomb(tempMap, &pX, &pY, 0, 1, &pKey, &mlife, &alife, &blife, &clife);
break;
case BRIGHT:
playing = bomb(tempMap, &pX, &pY, 1, 0, &pKey, &mlife, &alife, &blife, &clife);
break;
case BLEFT:
playing = bomb(tempMap, &pX, &pY, -1, 0, &pKey, &mlife, &alife, &blife, &clife);
break;
}
}
//playing = 1;
}
void drawMap(int* pX, int* pY, int* mX, int* mY, int* aX, int* aY, int* bX, int* bY, int* cX, int* cY, char(*tMap)[40])
{
char temp;
system("cls");
int h, w;
for (h = 0; h < 19; h++)
{
for (w = 0; w < 40; w++)
{
temp = tMap[h][w];
if (temp == '0')
{
setColor(black, black);
printf(" ");
}
else if (temp == '1')
{
setColor(white, white);
printf("#");
}
else if (temp == 'p')
{
*pX = w;
*pY = h;
setColor(lightcyan, black);
printf("@");
}
else if (temp == 'O')
{
setColor(lightgreen, black);
printf("O");
}
else if (temp == 'k')
{
setColor(yellow, black);
printf("*");
}
else if (temp == 'l')
{
setColor(white, white);
printf("#");
}
else if (temp == 'm')
{
*mX = w;
*mY = h;
setColor(lightred, black);
printf("M");
}
else if (temp == 't')
{
setColor(cyan, black);
printf("?");
}
else if (temp == 'a')
{
*aX = w;
*aY = h;
setColor(lightred, black);
printf("M");
}
else if (temp == 'b')
{
*bX = w;
*bY = h;
setColor(lightred, black);
printf("M");
}
else if (temp == 'c')
{
*cX = w;
*cY = h;
setColor(lightred, black);
printf("M");
}
}
printf("\n");
}
setColor(white, black);
}
int bomb(char(*tMap)[40], int* pX, int* pY, int _x, int _y, int* pKey, int* mlife, int* alife, int* blife, int* clife)
{
int playflag = 1;
char mapObject = tMap[*pY + _y][*pX + _x];
setColor(black, red);
if (mapObject == '0')
{
gotoxy(*pX + _x, *pY + _y);
printf("B");
tMap[*pY + _y][*pX + _x] = 'B';
}
if (mapObject == 'm')
{
setColor(purple, black);
gotoxy(*pX + _x, *pY + _y);
printf("X");
tMap[*pY + _y][*pX + _x] = '0';
*mlife = 0;
}
if (mapObject == 'a')
{
setColor(purple, black);
gotoxy(*pX + _x, *pY + _y);
printf("X");
tMap[*pY + _y][*pX + _x] = '0';
*alife = 0;
}
if (mapObject == 'b')
{
setColor(purple, black);
gotoxy(*pX + _x, *pY + _y);
printf("X");
tMap[*pY + _y][*pX + _x] = '0';
*blife = 0;
}
if (mapObject == 'c')
{
setColor(purple, black);
gotoxy(*pX + _x, *pY + _y);
printf("X");
tMap[*pY + _y][*pX + _x] = '0';
*clife = 0;
}
return playflag;
}
void setColor(int forground, int background)
{
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); // 콘솔 핸들가져오기
int code = forground + background * 16;
SetConsoleTextAttribute(consoleHandle, code);
}
void drawUI(int pX, int pY, int pKey, int heart)
{
setColor(white, black);
gotoxy(40, 4);
printf("플레이어 위치:(%02d,%02d)", pX, pY);
setColor(yellow, black);
gotoxy(45, 8);
printf("열쇠: %d/6개", pKey);
setColor(lightred, black);
gotoxy(45, 12);
printf("목숨: %d/2개", heart);
setColor(white, black);
}
int move(char(*tMap)[40], int* pX, int* pY, int* mX, int* mY, int* aX, int* aY, int* bX, int* bY, int* cX, int* cY, int _x, int _y, int* pKey, int* heart, int* mlife, int* alife, int* blife, int* clife)
{
int playflag = 1;
char mapObject = tMap[*pY + _y][*pX + _x];
char mapObject1 = tMap[*mY + _y][*mX + _x];
char mapObject2 = tMap[*aY + _y][*aX + _x];
char mapObject3 = tMap[*bY + _y][*bX + _x];
char mapObject4 = tMap[*cY + _y][*cX + _x];
setColor(white, black);
if (mapObject == '0')
{
tMap[*pY][*pX] = '0';
gotoxy(*pX, *pY);
printf(" ");
setColor(lightcyan, black);
gotoxy(*pX + _x, *pY + _y);
tMap[*pY + _y][*pX + _x] = 'p';
printf("@");
*pX += _x;
*pY += _y;
}
if (mapObject1 == '0' && *mlife == 1)
{
tMap[*mY][*mX] = '0';
gotoxy(*mX, *mY);
printf(" ");
setColor(lightred, black);
tMap[*mY + _y][*mX + _x] = 'm';
gotoxy(*mX + _x, *mY + _y);
printf("M");
*mX += _x;
*mY += _y;
}
if (mapObject1 == 'B' && *mlife == 1)
{
tMap[*mY][*mX] = '0';
gotoxy(*mX, *mY);
printf(" ");
tMap[*mY + _y][*mX + _x] = '0';
gotoxy(*mX + _x, *mY + _y);
printf("X");
*mlife = 0;
}
if (mapObject2 == '0' && *alife == 1)
{
tMap[*aY][*aX] = '0';
gotoxy(*aX, *aY);
printf(" ");
setColor(lightred, black);
tMap[*aY + _y][*aX + _x] = 'a';
gotoxy(*aX + _x, *aY + _y);
printf("M");
*aX += _x;
*aY += _y;
}
if (mapObject2 == 'B' && *alife == 1)
{
tMap[*aY][*aX] = '0';
gotoxy(*aX, *aY);
printf(" ");
tMap[*aY + _y][*aX + _x] = '0';
gotoxy(*aX + _x, *aY + _y);
printf("X");
*alife = 0;
}
if (mapObject3 == '0' && *blife == 1)
{
tMap[*bY][*bX] = '0';
gotoxy(*bX, *bY);
printf(" ");
setColor(lightred, black);
tMap[*bY + _y][*bX + _x] = 'b';
gotoxy(*bX + _x, *bY + _y);
printf("M");
*bX += _x;
*bY += _y;
}
if (mapObject3 == 'B' && *blife == 1)
{
tMap[*bY][*bX] = '0';
gotoxy(*bX, *bY);
printf(" ");
tMap[*bY + _y][*bX + _x] = '0';
gotoxy(*bX + _x, *bY + _y);
printf("X");
*blife = 0;
}
if (mapObject4 == '0' && *clife == 1)
{
tMap[*cY][*cX] = '0';
gotoxy(*cX, *cY);
printf(" ");
setColor(lightred, black);
tMap[*cY + _y][*cX + _x] = 'c';
gotoxy(*cX + _x, *cY + _y);
printf("M");
*cX += _x;
*cY += _y;
}
if (mapObject4 == 'B' && *clife == 1)
{
tMap[*cY][*cX] = '0';
gotoxy(*cX, *cY);
printf(" ");
tMap[*cY + _y][*cX + _x] = '0';
gotoxy(*cX + _x, *cY + _y);
printf("X");
*clife = 0;
}
if (mapObject == 'm' || mapObject == 'a' || mapObject == 'b' || mapObject == 'c' || mapObject1 == 'p' || mapObject2 == 'p' || mapObject3 == 'p' || mapObject4 == 'p' || mapObject == 'B')
{
*heart -= 1;
if (*heart == 0)
{
playflag = 0;
endDraw();
Sleep(5000);
}
}
else if (mapObject == 'k')
{
*pKey += 1;
tMap[*pY + _y][*pX + _x] = '0';
gotoxy(*pX + _x, *pY + _y);
printf(" ");
if (*pKey == 6)
{
tMap[6][9] = '0';
gotoxy(9, 6);
printf(" ");
setColor(lightgreen, black);
gotoxy(41, 10);
printf("탈출구가 열렸습니다");
}
}
else if (mapObject == 'O')
{
playflag = 0;
setColor(yellow, black);
gotoxy(39, 16);
printf("! 탈출을 축하드립니다 !");
Sleep(5000);
}
else if (mapObject == 't')
{
int num = 0;
srand(time(NULL));
num += rand() % 2;
if (num == 0)
{
*heart += 1;
}
else if (num == 1)
{
*heart -= 1;
}
tMap[*pY + _y][*pX + _x] = '0';
gotoxy(*pX + _x, *pY + _y);
printf(" ");
}
return playflag;
}
void infoDraw(void)
{
system("cls");
printf("\n\n");
printf(" [ 게임설명 ]\n\n");
printf(" 몬스터들을 피하거나 폭탄으로 물리쳐\n");
printf(" 열쇠를 얻어 탈출해라\n\n");
printf(" [ 조작법 ]\n\n");
printf(" 이동: W, A, S, D\n\n");
printf(" 폭탄설치: I, J, K, L\n\n");
printf(" 선택: 스페이스바\n\n\n");
printf(" 개발자: 6조(강재윤, 이유진, 이인구)\n\n");
printf(" 스페이스바를 누르면 메인화면으로 이동합니다.");
while (1) {
if (keyControl() == SUBMIT) {
break;
}
}
}
void endDraw()
{
system("cls");
setColor(white, black);
printf("\n\n\n\n\n");
printf(" * * *\n");
printf(" * * *\n");
printf(" ### ### ###\n");
printf(" ##### ##### #####\n");
printf(" ### ### ###\n\n\n");
printf(" - 탈출 실패 -\n\n\n");
printf(" ! 다시 한 번 도전하세요 !");
}