본문으로 바로가기
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
 
/* 헤더파일 */
 
#include <stdio.h> // scanf()등 여러 함수
#include <stdlib.h> // malloc()등 여러 함수
#include <windows.h> // 윈도우 API
#include <conio.h> // getch()등 여러 함수
#include <time.h> // 랜덤함수
#include <mmsystem.h> // 노래실행
#include <string.h>
 
/* 매크로 ( 전처리기 ) */
 
#pragma comment(lib, "winmm.lib"// 노래실행용 전처리기
 
#define PATH "C:\\Users\\Sunrin_JT\\Desktop\\VS_똥피하기\\VS_Ddong_Avoid\\Debug\\VS_Ddong_Avoid.exe"
#define MUSIC "C:\\Users\\Sunrin_JT\\Desktop\\VS_똥피하기\\ddong_avoid_bgm.wav"
 
#define UP 10
#define DOWN 11
#define LEFT 12
#define RIGHT 13
#define ENTER 15
 
#define CHAR ('@')
#define SPACE (' ')
#define DDONG ('#')
 
#define SOUND_G 1567.982
 
/* 전역변수 */
 
char map[1500][50]; // 게임공간 정의
int check_start_or_exit = 1// 메뉴에서 시작 or 종료를 선택할때 사용    // 전역변수
int floor_x = 30, floor_y = 1// 바닥의 좌표
int dongfall_x = 1, dongfall_y = 1// 장애물 생성좌표
int point = 0;
int count_print_ddong = 0;
int break_cnt = 0;
int level = 1;
int ddong_speed = 6// 똥 내려오는 속도
int ddong_space = 20// 랜덤으로 사라지는 똥 수 = 46 - ddong_space
int ddong_line = 5// 똥이 내려오는 간격
int best_point = 0// 최고점수
 
/* 사용한 함수 ( 사용자 정의 함수 ) */
 
void set_cursor(); // 커서 깜빡임 여부 설정
void set_textcolor(); // 출력될 글자의 색깔 설정
void gotoxy();
void print_title(); // 시작하면 출력될 게임의 제목 등등...
void print_menu_start(); // 메뉴 출력 >> start
void print_menu_exit(); // 메뉴 출력 >> exit
int get_direction(); // 방향키 입력 값 반환
void print_menu_start_or_exit(); // 시작 메뉴에서 시작 / 종료 처리
void print_game_start(); // 게임 스타트 할때 안내메시지
void make_line(); // 인자 수만큼 개행
void print_game_floor(); // 바닥과 바닥 아래쪽에 있는것들 출력
void make_game_map(); // 게임 맵을 전부 공백으로 세팅
void set_main_char(); // 메인캐릭터 @의 위치를 설정해줌
void print_game_map(); // 게임 맵을 프린트함
int move_main_char(); // 캐릭터를 좌우로 한칸씩 움직일수있게 해줌
int print_ddong(); // 장애물을 만났을때 종료되거나 등등 프로그램의 핵심 함수부분
void make_txt_save_point(); // 텍스트 파일을 만든 후에 파일에 이름과 점수를 저장함
 
int main()
{
 
    point = 0;
    srand(time(NULL));
    system("cls");
    set_cursor(0);
 
    system("mode con cols=93 lines=35 | title Avoid"); // 콘솔의 크기설정, 제목설정
    print_title();
    print_menu_start();
 
    int get_direction_cnt=0;
    int save_direction;
 
    while (1// 메뉴창에서 위아래로 이동하면서 선택분기를 나눔
    {
        save_direction = get_direction(get_direction_cnt);
 
        if (check_start_or_exit == 1 && save_direction == ENTER)
        {
            break;
        }
        if (check_start_or_exit == 2 && save_direction == ENTER)
        {
            system("cls");
            exit(1);
        }
        if (save_direction == UP)
        {
            check_start_or_exit = 1;
            system("cls");
            print_title();
            print_menu_start();
        }
        if (save_direction == DOWN)
        {
            check_start_or_exit = 2;
            system("cls");
            print_title();
            print_menu_exit();
        }
    }
 
    print_game_start();
    make_game_map();
    set_main_char();
    print_game_map();
    print_game_floor();
    system("mode con cols=46 lines=35 | title Avoid");
    PlaySound(TEXT(MUSIC), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP );
 
    while (1)
    {
        move_main_char();
        print_ddong();
    }
 
    return 0;
}
 
void set_cursor(int num)
{
    CONSOLE_CURSOR_INFO curInfo;
    GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &curInfo);
    curInfo.bVisible = num;
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &curInfo);
}
 
void make_line(int num_line)
{
    int i;
    for (int i = 1; i <= num_line; i++)
    {
        printf("\n");
    }
}
 
void set_textcolor(int color_number)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_number);
}
 
void gotoxy(int x, int y)
{
    COORD pos = { x,y };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
 
void print_title()
{
    printf("\t                                                                             \n");
    printf("\t                                                                             \n");
    set_textcolor(9);
    printf("\t ===========================================================================  \n");
    set_textcolor(7);
    printf("\t ***************************************************************************  \n");
    set_textcolor(2);
    printf("\t                                                                             \n");
    printf("\t                                                                             \n");
    printf("\t           #      ##         ##     ######      ##########   ########          \n");
    printf("\t          ###     ##         ##   ##      ##        ##        ##    ###        \n");
    printf("\t         ## ##     ##       ##   ##        ##       ##        ##      ##       \n");
    printf("\t        ##   ##     ##     ##    ##        ##       ##        ##      ##       \n");
    printf("\t       #########     ##   ##     ##        ##       ##        ##      ##       \n");
    printf("\t      ##       ##     ## ##       ##      ##        ##        ##    ###        \n");
    printf("\t     ##         ##     ###          ######      ##########   ########          \n");
    printf("\t                                                                             \n");
    printf("\t                                                                             \n");
    set_textcolor(7);
    printf("\t ***************************************************************************  \n");
    set_textcolor(9);
    printf("\t ===========================================================================  \n");
    printf("\t                                                                             \n");
    set_textcolor(7);
}
 
void print_menu_start()
{
    printf("\n\n\n");
    set_textcolor(6);
    printf("\t\t\t\t--------------------------- \n\n\n");
    set_textcolor(7);
    printf("\t\t\t\t     >>  -  Start!!         \n\n\n");
    printf("\t\t\t\t         -  Exit!!          \n\n\n");
    set_textcolor(6);
    printf("\t\t\t\t--------------------------- \n\n");
    set_textcolor(7);
}
 
void print_menu_exit()
{
    printf("\n\n\n");
    set_textcolor(6);
    printf("\t\t\t\t--------------------------- \n\n\n");
    set_textcolor(7);
    printf("\t\t\t\t         -  Start!!         \n\n\n");
    printf("\t\t\t\t     >>  -  Exit!!          \n\n\n");
    set_textcolor(6);
    printf("\t\t\t\t--------------------------- \n\n");
    set_textcolor(7);
}
 
int get_direction()
{
    int key;
    key = _getch();
    switch (key)
    {
    case 72:
        Beep(SOUND_G, 100);
        check_start_or_exit = 1;
        return UP;
    case 77:
        return LEFT;
    case 80:
        Beep(SOUND_G, 100);
        check_start_or_exit = 2;
        return DOWN;
    case 75:
        return RIGHT;
    }
    if (key == 13)
        return ENTER;
}
 
void print_menu_start_or_exit(int direction)
{
    if (direction == UP)
    {
        system("cls");
        print_title();
        print_menu_start();
    }
    else if (direction == DOWN)
    {
        system("cls");
        print_title();
        print_menu_exit();
    }
}
 
void print_game_start()
{
    system("cls");
 
    int i;
    for (i = 5; i >= 1; i--)
    {
        make_line(16);
        set_textcolor(4);
        printf("\t\t\t\t %d 초 후", i);
        set_textcolor(7);
        printf(" 에 게임이 시작됩니다! ");
        Beep(SOUND_G, 100);
        Sleep(1000);
        system("cls");
    }
}
 
 
void make_game_map()
{
    int i = 1, j = 1;
 
    for (i = 1; i <= 30; i++)
    {
        for (j = 1; j <= 46; j++)
        {
            map[i][j] = SPACE;
        }
        printf("\n");
    }
}
 
void print_game_floor()
{
    for (int i = 1; i <= 46;i++)
    {
        set_textcolor(9);
        printf("=");
        set_textcolor(7);
    }
 
    printf("POINT : %3d", point);
    printf("\t\t    최고 POINT : %5d\n",best_point);
 
    printf("방향키로 똥을 피하세요!!\t");
    set_textcolor(6);
 
    printf("    LEVEL :");
    set_textcolor(2);
    printf("%3d\n", level);
    set_textcolor(7);
 
    printf("SPACE를 누르면 프로그램이 종료됩니다.\t");
}
 
void set_main_char()
{
    map[floor_x][floor_y] = CHAR;
}
 
void print_game_map()
{
    gotoxy(00); //system("cls");
    int i = 1, j = 1;
 
    for (i = 1; i <= 30; i++)
    {
        for (j = 1; j <= 46; j++)
        {
            printf("%c", map[i][j]);
        }
        printf("\n");
    }
}
 
int move_main_char()
{
    int i;
    for (i = 1;i <= ddong_speed;i++// 출력 속도 적을수록 똥이 빨라짐, 기본 ddong_speed는 6
    {
        if (GetAsyncKeyState(VK_RIGHT))
        {
            map[floor_x][floor_y] = SPACE;
            if (floor_y + 1 == 47)
            {
            }
            else if (floor_y + 1 != 47)
            {
                floor_y = floor_y + 1;
            }
        }
        if (GetAsyncKeyState(VK_LEFT))
        {
            map[floor_x][floor_y] = SPACE;
            if (floor_y - 1 == 0)
            {
            }
            else if (floor_y - 1 != 0)
            {
                floor_y = floor_y - 1;
            }
        }
        if (GetAsyncKeyState(VK_SPACE))
        {
            system("cls");
            make_line(15);
            printf("\t  프로그램을 종료하셨습니다.\n");
            printf("\t   곧 프로그램이 종료됩니다.");
            Sleep(750);
            exit(1);
        }
        map[floor_x][floor_y] = CHAR;
        print_game_map();
        print_game_floor();
    }
}
 
int print_ddong()
{
    int i, j, k, cnt = 0, select;
    int line_space = count_print_ddong;
 
    while (1)
    {
        for (i = 1;i <= 46;i++)
        {
            if (map[line_space + 1][i] == CHAR)
            {
                if (map[line_space][i] == DDONG) // 게임오버
                {
                    PlaySound(NULL00);
                    system("cls");
                    make_line(16);
                    printf("       게임오버!!! 곧 게임이 종료됩니다.");
                    Sleep(1000);
 
                    system("cls");
                    make_line(14);
 
                    if (point >= best_point) // 하이스코어 찾기!
                    {
                        best_point = point;
                        for (k = 1;k <= best_point;k++)
                        {
                            gotoxy(1916);
                            printf("%4d점", k);
                            Sleep(1);
                        }
                        system("cls");
                        make_line(16);
                        printf("\t      최 고 점 수 달 성 !\n");
 
                        Sleep(3000);
                        system("PAUSE");
                    }
 
                    while (1)
                    {
                        make_line(10);
                        printf("1 입력 : 재시작 !\n");
                        printf("2 입력 : 종료!\n");
                        printf("당신의 선택은? : ");
                        fflush(stdin);
                        scanf("%d"&select);
                        Beep(SOUND_G, 100);
 
                        if (select == 1)
                        {
                            system(PATH);
                        }
                        else if (select == 2)
                        {
                            make_txt_save_point();
                            printf("\n저장이 완료되었습니다. 프로그램이 종료됩니다.");
                            Sleep(1000);
                            exit(1);
                        }
                        else
                        {
                            system("cls");
                            printf("잘못 입력하셨습니다.\n다시 입력해주세요.\n");
                            system("PAUSE");
                            Beep(SOUND_G, 100);
                            system("cls");
                        }
                    }
                }
 
                point += 50//레벨
                
                if (point >= best_point)
                {
                    best_point = point;
                }
 
                if (point % 250 == 0)
                {
                    level++;
                    if (level % 3 == 0 && ddong_speed > 3)
                    {
                        ddong_speed--// 속도 감소
                    }
                    if (level > 4)
                    {
                        ddong_space += 4;
                    }
                    else if (level <= 4)
                    {
                        ddong_space += 2// 출력 공백 감소
                    }
                }
            }
 
            if (line_space == 0)
            {
                for (j = 1;j <= 46;j++)
                {
                    map[line_space][j] = DDONG;
                }
            }
            map[1 + line_space][i] = map[line_space][i]; // y좌표를 1~46까지 증가시켜가면서 출력함
            map[line_space][i] = SPACE; // 출력 후 내려간부분을 공백으로 바꿈
        }
 
        if (break_cnt == 1)
            break;
 
        for (i = 1;i <= 46;i++)
        {
            if (map[1 + line_space][i] == DDONG)
            {
                cnt++;
            }
        }
 
        if (cnt >= ddong_space) // 45 - ddong_space 만큼 공백이 랜덤으로 생성됨
        {
            map[1 + line_space][rand() % 46 + 1= SPACE;
        }
        line_space -= ddong_line; // 출력 간격
        if (line_space < 0)
        {
            break;
        }
    }
    count_print_ddong++// 줄이 한줄씩 내려가야하므로 ++를 함
}
 
void make_txt_save_point()
{
    int start;
    int select = 0;
    time_t t = time(NULL);
    struct tm tm = *localtime(&t);
 
    FILE* save = NULL;
    save = fopen("check_point.txt""a");
 
    char* name = (char*)malloc(sizeof(char* 100);
    char* name_and_point = (char*)malloc(sizeof(char* 100);
    char* date = (char*)malloc(sizeof(char* 100);
 
    system("cls");
    make_line(13);
    set_cursor(1); // 커서 재생성
    printf("\n   이름을 입력하시면 정보가 같이 저장됩니다.\n");
    printf("   이름을 입력하세요. : ");    scanf("%s", name);
 
    sprintf(name_and_point, "%s 사용자의 점수는 %d 점 입니다!\n", name, point);
    sprintf(date, "날짜 : %d-%d-%d %d:%d:%d\n\n\n", tm.tm_year + 1900, tm.tm_mon + 1,
        tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
 
    fprintf(save, name_and_point);
    fprintf(save, date);
 
    fclose(save);
 
    free(name_and_point);
    free(date);
}
cs

하이스코어 시스템을 개발하고있다...

대충 어떤식으로 해야할지 구상이 되긴 하는데 쉽게 구현이 안된다.

main() 이런식으로 값을 넘겨서 전역변수의 값을 살려서 bestpoint와 point의값을 비교해보고싶었으나, 뭔가 충돌이 나는지 자꾸 point의 값이 비정상적으로 상승하였다..

그래서 일단은 system("파일경로") 의 방식으로 프로그램을 재시작하도록 바꿨다.

그리고 원래 프로그램에서는 죽을때마다 이름을 입력하였으나, 이제 종료할때만 입력하도록 바뀌었다.

또한 IDE를 Codeblocks에서 VS로 바꿨다! 노래가 재생된다!

확실히 게임은 노래가있어야한다.


이제 확실하게 해야할부분이 간추려졌다.

1. 코드 쓸모없는부분등 잡다한 오류 고치기

2. 하이스코어 시스템 확실하게 구현해보기!

- 지금 생각하는거로는 파일입출력을 이용해서 bestpoint를 저장해둔후 이값을 사용해서 최고점수를 유지시키고 기록하는것이다.


흑흑 오류고치고싶다.

시험빨리끝났으면좋겠다 :(











+4월 30일 추가

MUSIC 이랑 경로설정부분에서 선배의 꿀팁을 얻었다.

"C:\\Users\\Sunrin_JT\\Desktop\\VS_똥피하기\\ddong_avoid_bgm.wav" 이부분을..

"Music\\ddong_avoid_bgm.wav" 이렇게하고..

폴더를 하나 생성하면 끝! 일듯 하다!