-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbomberman.cpp
More file actions
400 lines (365 loc) · 8.16 KB
/
bomberman.cpp
File metadata and controls
400 lines (365 loc) · 8.16 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
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
#define SUBMIT 4
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"},
{"1m0000000000000000110000000000000m00k1"},
{"101101011k1101011011011010111110101101"},
{"10000100010001000011000010001000100001"},
{"11110111010111011111111011101011101111"},
{"1m000100000001010000k0101m000000101000"},
{"111101011l1101011101111010110110101111"},
{"100000010001m0000000000000100010000001"},
{"111101010O010101111011101010k010101111"},
{"0001010111110101p000001010111110101000"},
{"11110100000001011111111010000000101111"},
{"10000101111101000011000010111110100001"},
{"1011m00001m000011011011m00001m00001101"},
{"10010111010111010011001011101011101001"},
{"11010001k00100010111101000100010001011"},
{"1000010001000100001100001000100k100001"},
{"10101111000111101011010111100011110101"},
{"10000000010000000011000000001000000001"},
{"11111111111111111111111111111111111111"},
};// 0 : 통로, 1 : 벽, k : 열쇠(아이템), p : 플레이어, O : 탈출구 ㅣ : 잠긴 문 r :우로 이동 몬스터
int keyControl(void);
void titleDraw(void);
int menuDraw(void);
void infoDraw(void);
void init();
void gotoxy(int, int);
void setColor(int, int);
void drawMap(int* pX, int* pY, char(*tMap)[40]);
int move(char(*tMap)[40], int* pX, int* pY, int _x, int _y, int* pKey);
void gLoop(void);
void drawUI(int pX, int pY, int pKey);
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;
}
}
}
}
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;
}
}
void gLoop(void)
{
int mKey;
int pX, pY;
int pKey = 0;
int playing = 1;
char tempMap[19][40];
memcpy(tempMap, map, sizeof(tempMap));
drawMap(&pX, &pY, tempMap);
while (playing)
{
drawUI(pX, pY, pKey);
mKey = keyControl();
switch (mKey)
{
case UP:
playing = move(tempMap, &pX, &pY, 0, -1, &pKey);
break;
case DOWN:
playing = move(tempMap, &pX, &pY, 0, 1, &pKey);
break;
case RIGHT:
playing = move(tempMap, &pX, &pY, 1, 0, &pKey);
break;
case LEFT:
playing = move(tempMap, &pX, &pY, -1, 0, &pKey);
break;
case SUBMIT:
playing = 0;
}
}
//playing = 1;
}
void drawMap(int* pX, int* pY, 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')
{
setColor(lightred, black);
printf("M");
}
}
printf("\n");
}
setColor(white, black);
}
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)
{
setColor(white, black);
gotoxy(40, 4);
printf("플레이어 위치:(%02d,%02d)", pX, pY);
setColor(yellow, black);
gotoxy(45, 8);
printf("열쇠: %d/6개", pKey);
setColor(white, black);
}
int move(char(*tMap)[40], int* pX, int* pY, int _x, int _y, int* pKey)
{
int playflag = 1;
char mapObject = tMap[*pY + _y][*pX + _x];
setColor(white, black);
if (mapObject == '0')
{
gotoxy(*pX, *pY);
printf(" ");
setColor(lightcyan, black);
gotoxy(*pX + _x, *pY + _y);
printf("@");
*pX += _x;
*pY += _y;
}
if (mapObject == 'm')
{
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);
}
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");
printf("\n\n\n\n\n");
printf(" * * *\n");
printf(" * * *\n");
printf(" ### ### ###\n");
printf(" ##### ##### #####\n");
printf(" ### ### ###\n\n\n");
printf(" - 탈출 실패 -\n\n\n");
printf(" ! 다시 한 번 도전하세요 !");
}