본문으로 바로가기
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
 
/* 헤더파일 */
 
#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 BACK_GROUND_MUSIC "back_ground_music.wav"  // 노래 경로 - ★
#define BEST_SCORE_MUSIC "best_score_music.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// print_ddong 함수에서 사용함 ( 장애물이 한줄한줄 내려올때마다 ++ )
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(); // system("cls") 대용으로 사용, 화면 청소용도!
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(); // 텍스트 파일을 만든 후에 파일에 이름과 점수를 저장함
void file_open_error(); // 파일열기를 실패했을때 에러를 처리함
 
int main()
{
    FILE* find_best_point=NULL// 최고점수를 불러오기위해서 파일구조체 선언
    int txt_best_point=0;
    find_best_point = fopen("only_best_point.txt""r");
    fscanf(find_best_point, "%d"&txt_best_point);
 
    best_point = txt_best_point; // 최고점수를 메모장에 있는 값으로 불러옴
 
    fclose(find_best_point);
 
    point = 0// 포인트를 0으로 초기화
    srand(time(NULL)); // rand 함수로 랜덤값을 사용하기위함
    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"); // 콘솔창 크기설정, 제목을 Avoid로 바꿈!
    PlaySound(TEXT(BACK_GROUND_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)) // GetAsyncKeyState는 현재키를 알아오는 함수
        {
            map[floor_x][floor_y] = SPACE; 
            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)
            {
                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, l = 0, 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 && point !=    0// 하이스코어 찾기!
                    {
                        best_point = point;
                        PlaySound(TEXT(BEST_SCORE_MUSIC), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
                        for (k = 1;k <= best_point;k++)
                        {
                            gotoxy(1914); // 19 16
                            printf("%4d점\n", k);
                            Sleep(5);
                        }
 
                        char message[20= "최고점수달성"// ★
                        
                        printf("\n\t      ");
                        while (message[l]!='\0')
                        {
                            printf("%c%c ", message[l], message[l+1]);
                            Sleep(750);
                            l = l + 2;
                        }
 
                        Sleep(1000);
                        make_line(1);
                        PlaySound(NULL00);
                        system("PAUSE");
                    }
 
                    while (1)
                    {
                        make_line(10);
                        printf("1 입력 : 재시작 !\n\n");
                        printf("2 입력 : 종료!\n\n");
                        printf("당신의 선택은? : ");
                        fflush(stdin);
 
                        Beep(SOUND_G, 100);
 
                        if (GetAsyncKeyState(0x31)) // ★
                        {
                            system(PATH); // 프로그램을 재시작함
                        }
                        else if (GetAsyncKeyState(0x32)) // ★ 왜 종료하고나서 설정하는지 설명!
                        {
                            make_txt_save_point();
                            printf("\n저장이 완료되었습니다. 프로그램이 종료됩니다.");
                            Sleep(1000);
                            exit(0);
                        }
                        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// 250점당 레벨 1씩 증가!
                {
                    level++;
                    if (level % 3 == 0 && ddong_speed > 3// 스피드가 너무 빨라질수있으므로 사전에 막음
                    {
                        ddong_speed--// 속도 감소
                    }
                    if (level > 4// 난이도가 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; // 출력 후 내려간부분을 공백으로 바꿈
        }
 
        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 select = 0;
    time_t t = time(NULL);
    struct tm tm = *localtime(&t);
 
    FILE* point_save = NULL;
    FILE* best_point_save = NULL;
    FILE* only_best_point = NULL;
 
    point_save = fopen("check_point.txt""a"); // 날짜, 이름, 점수를 저장함
    void file_open_error(save);
    best_point_save = fopen("best_point_save.txt""w+"); // 이름과 최고점수를 저장함
    void file_open_error(best_point_save);
    only_best_point = fopen("only_best_point.txt""w"); // 최고점수 " 만 " 저장함
    void file_open_error(only_best_point);
 
    char* name = (char*)malloc(sizeof(char* 30); // ★
    char* name_and_point = (char*)malloc(sizeof(char* 50);
    char* date = (char*)malloc(sizeof(char* 60);
 
    char* best_point_txt = (char*)malloc(sizeof(char* 60);
 
    system("cls");
    make_line(13);
    set_cursor(1); // 커서 재생성
    printf("\n   이름을 입력하시면 정보가 같이 저장됩니다.\n");
    printf("   이름을 입력하세요. : ");    scanf("%30s", 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);
 
    sprintf(best_point_txt, "현재 최고점수는 \" %s \" 사용자가 달성한 %d 점 입니다!\n", name,best_point);
 
    fprintf(point_save, name_and_point);        fprintf(point_save, date);
    fprintf(best_point_save, best_point_txt);
    fprintf(only_best_point, "%d", best_point);
    
    fclose(point_save);
    fclose(best_point_save);
    fclose(only_best_point);
    
    free(name);
    free(name_and_point);
    free(date);
    free(best_point_txt);
}
 
void file_open_error(char* file_st_name) // ★
{
    if (file_st_name == NULL)
    {
        system("cls");
        printf("FILE OPEN ERROR !!\n");
        system("PAUSE");
        exit(0);
    }
}
cs

최종발표 잘하자..

중간점검이 끝났다. 선배님들의 조언으로 여러가지를 수정했다.

구현할껀 다 구현했다. 완성도가 확실히 높아졌다.

이제 최종발표 전까지는 무조건 완성도에만 신경써서 완성도만 높이면 될듯 하다.