-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame.cpp
More file actions
652 lines (616 loc) · 13.8 KB
/
game.cpp
File metadata and controls
652 lines (616 loc) · 13.8 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
643
644
645
646
647
648
649
650
651
652
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <time.h>
// #define
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
#define SUBMIT 4 // 선택(스페이스바)
#define WALL '1'
#define DOORWALL '#'
#define PLAYER '2'
#define WATER '3'
#define FOOD '4'
#define DOOR '*'
#define MONSTER '5'
#define EAST 0
#define WAST 1
#define SOUTH 2
#define NORTH 3
#define MAPNUMBER 4
#define DOOROPEN 5
#define MAPSIZE 10
// setting
enum {
black,
blue,
green,
cyan,
red,
purple,
brown,
lightgray,
darkgray,
lightblue,
lightgreen,
lightcyan,
lightred,
lightpurple,
yellow,
white
};
char maps[MAPSIZE][32][32];
char defaultmap[32][32] = {
// 0: 빈공간, 1: 벽, *; 문, #: 벽(문o), @: 벽(문x), +:표시선
// 게임 내 공간 [2][2]~[29][29], 정중앙 [15][15]
{"0000000000000#***#0000000000000"},
{"0111111111111#@@@#1111111111110"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"##000000000000000000000000000##"},
{"*@000000000000000000000000000@*"},
{"*@000000000000000000000000000@*"},
{"*@000000000000000000000000000@*"},
{"##000000000000000000000000000##"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0100000000000000000000000000010"},
{"0111111111111#@@@#1111111111110"},
{"0000000000000#***#0000000000000"},
};
int infomap[MAPSIZE][6];
int reset[MAPSIZE][6];
int pX = 0;
int pY = 0;
// funcions list
void init();
void gotoxy(int, int);
void setColor(int, int);
void titleDraw();
int menuDraw();
int keyControl();
void infoDraw();
void randomObjectMaking(char, char*);
void MONSTER_create_check(char* , int list_MON[20][3],int,int*);
void creatmap();
void drawMap(int);
int mobile(char);
int move(int, int, int, int);
int list_MON[20][3] = {{0}} ;//몬스터를 체크할 리스트 생성 해f자 최대 20마리 생성 할것 같으니 자리를 남게 설정할예정.
void print_MONLIST(int list_MON[20][3]);
// main function
int main()
{
srand(time(NULL));
int menuCode = -1;
init();
while (1)
{
list_MON[20][3] = 0 ;
titleDraw();
menuCode = menuDraw();
if (menuCode == 0)
{
creatmap();
Sleep(500);
system("cls");
for (int x = 0; x < MAPSIZE; x++)
{
printf("\n%d\n", x);
printf("EAST= %d, ", infomap[x][EAST]);
printf("WAST= %d, ", infomap[x][WAST]);
printf("SOUTH= %d, ", infomap[x][SOUTH]);
printf("NORTH= %d, ", infomap[x][NORTH]);
}
_getch();
drawMap(0);
int mKey;
int playing = 1;
int mappos = 0;
while (playing)
{
mKey = keyControl();
switch (mKey)
{
case UP:
mappos = move(PLAYER, mappos, 0, -1);
break;
case DOWN:
mappos = move(PLAYER, mappos, 0, 1);
break;
case RIGHT:
mappos = move(PLAYER, mappos, 1, 0);
break;
case LEFT:
mappos = move(PLAYER, mappos, -1, 0);
break;
case SUBMIT:
playing = 0;
}
}
}
else if (menuCode == 1)
{
infoDraw();
}
else if (menuCode == 2)
{
break;
}
menuCode = -1;
system("cls");
}
gotoxy(12, 16);
printf("게임이 종료되었습니다!!!");
return _getch();
}
// function seting
void init()
{
system("mode con cols=104 lines=40 | title survival game");
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO ConsoleCursor;
ConsoleCursor.bVisible = 0;
ConsoleCursor.dwSize = 1;
SetConsoleCursorInfo(consoleHandle, &ConsoleCursor);
for (int x = 0; x < MAPSIZE; x++)
{
for (int y = 0; y < 6; y++)
{
reset[x][y] = -1;
}
}
}
void gotoxy(int x, int y)
{
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(consoleHandle, pos);
}
void setColor(int forground, int background)
{
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); // 콘솔 핸들가져오기
int code = forground + background * 16;
SetConsoleTextAttribute(consoleHandle, code);
}
void titleDraw()
{
printf("\n\n\n\n"); // 맨위에 4칸 개행
printf(" 00OOOOOOOOOOO000000000000000000O000000000000000O000 \n");
printf(" 0 O OOOOOOOOOOO O OOOOOOOOO O 0 \n");
printf(" 0 O O O O O O 0 \n");
printf(" 0OOOOOOOOOOOOO O O O O O O 0 \n");
printf(" 0 O O O O O O OOOO \n");
printf(" 0OOOOO0 OOOOO O O O O O O 0 \n");
printf(" 0 0 O O O O O O O O 0 \n");
printf(" 0OOOOOO O O O O O OOOOOOOOO O 0 \n");
printf(" 0O O O O O 0 \n");
printf(" 0OOOOOO0OOOOO000000000000000000O000000000000000O000 \n");
}
int menuDraw()
{
int x = 24;
int y = 12;
gotoxy(x - 2, y); // -2 한 이유는 > 를 출력해야하기 때문에
printf("> 게임시작");
gotoxy(x, y + 1);
printf("게임정보");
gotoxy(x, y + 2);
printf(" 종료 ");
while (1) { // 무한반복
int n = keyControl(); // 키보드 이벤트를 키값으로 받아오기
switch (n) {
case UP: { // 입력된 키의 값이 UP인경우 (w)
if (y > 12) { // y는 12~14까지만 이동
gotoxy(x - 2, y); // x-2 하는 이유는 ">"를 두칸 이전에 출력하기위해
printf(" "); // 원래 위치를 지우고
gotoxy(x - 2, --y); // 새로 이동한 위치로 이동하여
printf(">"); // 다시 그리기
}
break;
}
case DOWN: { // 입력된 키의 값이 DOWN인경우 (s)
if (y < 14) { // 최대 14
gotoxy(x - 2, y);
printf(" ");
gotoxy(x - 2, ++y);
printf(">");
}
break;
}
case SUBMIT: {
return y - 12; // 스페이스바(선택)되었을 경우 y-12
// y 시작 위치가 12였으므로 y-12 를 하면 0, 1, 2 세 숫자를 받을 수 있다.
}
}
}
}
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 infoDraw()
{
}
void creatmap()
{
int mob_num = 0;
srand(rand());
int index = 0, moveMap = 0;
memcpy(infomap, reset, sizeof(reset));
memcpy(maps[index], defaultmap, sizeof(defaultmap));
// 초기 맵 설정
pX = 20;
pY = 20;
maps[0][20][20] = PLAYER;
infomap[0][MAPNUMBER] = 0;
for (index = 1; index < MAPSIZE; index++)
{
int mapNumber, find = 1;
memcpy(maps[index], defaultmap, sizeof(defaultmap));
infomap[index][MAPNUMBER] = index;
mapNumber = rand() % index;
while (find)
{
moveMap = rand() % 4;
if (infomap[mapNumber][moveMap] != -1)
{
mapNumber = infomap[mapNumber][moveMap];
continue;
}
else
{
find = 0;
infomap[mapNumber][moveMap] = index;
//infomap[mapNumber][1] += (1 << 3 * moveMap);
if (moveMap == EAST)
{
infomap[index][WAST] = mapNumber;
//infomap[index][1] += (1 << 3 * WAST);
}
if (moveMap == WAST)
{
infomap[index][EAST] = mapNumber;
//infomap[index][1] += (1 << 3 * EAST);
}
if (moveMap == SOUTH)
{
infomap[index][NORTH] = mapNumber;
//infomap[index][1] += (1 << 3 * NORTH);
}
if (moveMap == NORTH)
{
infomap[index][SOUTH] = mapNumber;
//infomap[index][1] += (1 << 3 * SOUTH);
}
}
}
int i, fooditem = 0, wateritem = 0, monster = 0;
fooditem = rand() % 5, wateritem = rand() % 5, monster = rand() % 3;
for (i = 0; i < fooditem; i++)
{
randomObjectMaking(FOOD, (char*)maps[index]);
}
for (i = 0; i < wateritem; i++)
{
randomObjectMaking(WATER, (char*)maps[index]);
}
for (i = 0; i < monster; i++)
{
MONSTER_create_check((char*)maps[index],list_MON, index, &mob_num);
}
}
//char* changePoint = (char*)&maps + index * 32 * 32;
print_MONLIST(list_MON);
}
void MONSTER_create_check(char* map, int list_MON[20][3], int mappos, int* mob_num)
{
int posx, posy;
while (1)
{
srand(rand());
posx = 2 + rand() % 28;
posy = 2 + rand() % 28;
char* p = map + posx * 32;
if (p[posy] == '0')
{
list_MON[*mob_num][0] = mappos;
list_MON[*mob_num][1] = posx;
list_MON[*mob_num][2] = posy;
p[posy] = MONSTER;
*mob_num+=1;
break;
}
}
}
void print_MONLIST(int list_MON[20][3])
{
int a, b;
for (a = 0; a < 20; a++)
{
for (b = 0; b < 3; b++)
{
printf("%d ", list_MON[a][b]);
}
printf("\n");
}
Sleep(10000);
}
void randomObjectMaking(char object, char* map)
{
int posx, posy;
while (1)
{
srand(rand());
posx = 2 + rand() % 28;
posy = 2 + rand() % 28;
char* p = map + posx * 32;
if (p[posy] == '0')
{
p[posy] = object;
break;
}
}
}
void drawMap(int pos)
{
system("cls");
int h, w;
char temp;
for (h = 0; h < 32; h++)
{
for (w = 0; w < 32; w++)
{
gotoxy(2 * h, w);
temp = maps[pos][h][w];
if (temp == '0' || temp == NULL)
{
setColor(black, black);
printf(" ");
}
else if (temp == '1')
{
setColor(white, white);
printf(" ");
}
else if (temp == PLAYER)
{
//플레이어 X,Y 위치 저장
setColor(cyan, black);
printf("@@");
}
else if (temp == WATER)
{
setColor(black, cyan);
printf("WW");
}
else if (temp == FOOD)
{
setColor(black, lightgreen);
printf("FF");
}
else if (temp == MONSTER)
{
setColor(white, red);
printf("MM");
}
else
{
setColor(white, white);
printf(" ");
}
}
setColor(black, black);
}
setColor(red, red);
gotoxy(46, 34);
printf(" ");
setColor(green, green);
if (infomap[pos][EAST] != -1)
{
gotoxy(48, 34);
printf(" ");
}
if (infomap[pos][WAST] != -1)
{
gotoxy(44, 34);
printf(" ");
}
if (infomap[pos][SOUTH] != -1)
{
gotoxy(46, 36);
printf(" ");
}
if (infomap[pos][NORTH] != -1)
{
gotoxy(46, 32);
printf(" ");
}
setColor(white, black);
gotoxy(60, 0);
printf("pos=%2d ", pos);
gotoxy(62, 1);
printf("EAST=%d ", infomap[pos][EAST]);
gotoxy(62, 2);
printf("WAST=%d ", infomap[pos][WAST]);
gotoxy(62, 3);
printf("SOUTH=%d ", infomap[pos][SOUTH]);
gotoxy(62, 4);
printf("NORTH=%d ", infomap[pos][NORTH]);
}
int mobile(char object)
{
switch (object)
{
case WALL:
case DOORWALL:
case PLAYER:
case FOOD:
case WATER:
case MONSTER:
return 0;
case '*':
return 2;
default:
return 1;
}
}
int move(int moveObject, int mappos, int x, int y)
{
gotoxy(34, 34);
printf("%d, %d", pX, pY);
char object = maps[mappos][pX + x][pY + y];
int t = mobile(object);
if (t == 1)
{
//maps[mappos][pX + x][pY + y] == object;
maps[mappos][pX][pY] = '0';
setColor(white, black);
gotoxy(2 * pX, pY);
printf(" ");
setColor(cyan, black);
gotoxy(2 * (pX + x), pY + y);
printf("@@");
pX += x;
pY += y;
return mappos;
}
else if (t == 2)
{
int newmappos;
int moveNewMap = 0;
if (pY + y == 30) // SOUTH
{
newmappos = infomap[mappos][SOUTH];
if (newmappos != -1)
{
maps[mappos][pX][pY] = '0';
pX = 14, pY = 2;
moveNewMap = 1;
}
else
return mappos;
}
if (pY + y == 0) // NORTH
{
newmappos = infomap[mappos][NORTH];
if (newmappos != -1)
{
maps[mappos][pX][pY] = '0';
pX = 14, pY = 28;
moveNewMap = 1;
}
else
return mappos;
}
if (pX + x == 31) // EAST
{
newmappos = infomap[mappos][EAST];
if (newmappos != -1)
{
maps[mappos][pX][pY] = '0';
pX = 2, pY = 15;
moveNewMap = 1;
}
else
return mappos;
}
if (pX + x == 0) // WAST
{
newmappos = infomap[mappos][WAST];
if (newmappos != -1)
{
maps[mappos][pX][pY] = '0';
pX = 28, pY = 15;
moveNewMap = 1;
}
else
return mappos;
}
if (moveNewMap == 1)
{
maps[newmappos][pX][pY] = PLAYER;
drawMap(newmappos);
return newmappos;
}
}
return mappos;
}
/*void monster_move(int moveObject, int mappos)
{
gotoxy(15, 15);
int x = rand() % 3 - 3;
int y = rand() % 3 - 3;
char object = maps[mappos][mX + x][mY + y];
int t = mobile(object);
if (t == 3) //몬스터도 플레이어 느낌으로 봐야 쉽지 않을까.?
{
//maps[mappos][pX + x][pY + y] == object;
maps[mappos][mX][mY] = '0';
setColor(white, black);
gotoxy(2 * mX, mY);
printf(" ");
setColor(red, black);
gotoxy(2 * (mX + x), mY + y);
printf("MM");
mX += x;
mY += y;
}
}
void randomObjectMaking(char object, char* map)
{
int posx, posy;
while (1)
{
srand(rand());
posx = 2 + rand() % 28;
posy = 2 + rand() % 28;
char* p = map + posx * 32;
if (p[posy] == '0')
p[posy] = object;
break;
}
}
*/