본문으로 바로가기
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
// 헤더파일 
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <time.h> // 랜덤함수
#include <mmsystem.h> // 노래실행
 
// 전처리기
#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;
 
// 사용자정의 함수
void remove_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(); // 캐릭터를 좌우로 한칸씩 움직일수있게 해줌
void print_ddong(); // 장애물을 만났을때 종료되거나 등등 장애물 관련 함수
void make_txt_save_point(); // 텍스트 파일을 만든 후에 파일에 이름과 점수를 저장함
int break_cnt = 0;
 
int main()
{
    system("cls");
    point=0;
    remove_cursor();
 
    system("mode con cols=93 lines=35 | title Avoid"); // 콘솔의 크기설정, 제목설정
    print_title();
    print_menu_start();
 
    int get_direction_cnt;
    int save_direction;
 
    while(1// 메뉴창에서 위아래로 이동하면서 선택분기를 나눔
    {
        save_direction=get_direction(get_direction_cnt);
 
        if(check_start_or_exit==1 && save_direction==ENTER)
        {
            print_game_start();
            break;
            void make_game_map();
 
        }
        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();
        }
    }
 
    int i=1, j=1;
 
    for(i=1; i<=27; i++)
    {
        for(j=1; j<=46; j++)
        {
            map[i][j]=SPACE;
        }
        printf("\n");
 
        if(check_start_or_exit==2 && save_direction==ENTER)
        {
            system("cls");
            exit(1);
        }
        print_menu_start_or_exit(save_direction);
    }
 
    make_game_map();
    set_main_char();
    print_game_map();
    print_game_floor();
 
    // sndPlaySound()
    system("mode con cols=46 lines=35 | title Avoid");
    while(1)
    {
        move_main_char();
        print_ddong();
    }
}
 
void remove_cursor()
{
 CONSOLE_CURSOR_INFO curInfo;
 GetConsoleCursorInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &curInfo );
 curInfo.bVisible = 0;
 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);
        printf("\t\t\t\t%d 초 후에 게임이 시작됩니다! ",i);
        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++)
    {
        printf("=");
    }
    printf("POINT : %3d\n",point);
    printf("방향키로 똥을 피하세요!!\n");
    printf("SPACE를 누르면 프로그램이 종료됩니다.");
}
 
void set_main_char()
{
    map[floor_x][floor_y]=CHAR;
}
 
void print_game_map()
{
    gotoxy(0,0); //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, j;
    for(i = 1;i <= 5;i++// 출력 속도
    {
        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(3000);
            exit(1);
        }
        map[floor_x][floor_y]=CHAR;
        print_game_map();
        print_game_floor();
    }
}
 
void print_ddong()
{
    srand(time(NULL));
    int i, j, cnt = 0;
    int tmp = count_print_ddong;
 
    while(1)
    {
        for(i=1;i<=46;i++)
        {
            if(map[tmp + 1][i]==CHAR)
            {
                if(map[tmp][i] == DDONG)
                {
                    system("cls");
                    make_line(16);
                    printf("      게임오버!!! 곧 게임이 종료됩니다.");
                    Sleep(1000);
 
                    system("cls");
                    make_line(14);
                    make_txt_save_point();
                }
 
                point++;
            }
            if(tmp == 0)
            {
                for(j=1;j<=46;j++)
                {
                    map[tmp][j]=DDONG;
                }
            }
            map[1+tmp][i]=map[tmp][i]; // y좌표를 1~46까지 증가시켜가면서 출력함
            map[tmp][i]=SPACE; // 출력 후 내려간부분을 공백으로 바꿈
        }
 
        if(break_cnt==1)
            break;
 
        for(i=1;i<=46;i++)
        {
            if(map[1+tmp][i] == DDONG)
            {
                cnt++;
            }
        }
        if(cnt >= 31// 45 - a 만큼 공백이 랜덤으로 생성됨
        {
            map[1+tmp][rand() % 46 + 1= SPACE;
        }
        tmp -= 5// 출력 간격
        if(tmp < 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","aw");
 
    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);
    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);
 
    fflush(stdin);
 
    while(1)
    {
        printf("\n\n1 입력 : 재시작 !\n");
        printf("2 입력 : 종료!\n\n");
        printf("당신의 선택은? : ");
 
        scanf("%d",&select);
        Beep(SOUND_G, 100);
 
        if(select==1)
        {
            main();
        }
        else if(select==2)
        {
            printf("\n저장이 완료되었습니다. 프로그램이 종료됩니다.");
            Sleep(1000);
            exit(1);
        }
        else
        {
            printf("잘못 입력하셨습니다.\n다시 입력해주세요.\n");
            printf("아무키나 입력하면 다시입력 할 수 있습니다.\n");
            getch();
            Beep(SOUND_G, 100);
            system("cls");
        }
    }
}
 
cs

이중버퍼링을 구현하고싶었는데, 그냥 gotoxy 함수를 추가한후에, 이를 활용하여 gotoxy(0,0) 방식으로 화면을 system("cls")와 비슷한 느낌으로 부드렵게 구현하였다.

그리고 메뉴화면에서 방향키로 메뉴를 선택할때, 소리가 출력되게 해서 게임에 생동감을 주었다.

게임중에서도 노래를 넣으려고 했지만, CodeBlocks에서는 활용이 안된다는 글도 본것같고, 라이브러리파일도 제대로 작동하지 않아서 일단 보류했다.

그리고 종료창에서 1번 2번을 입력받아서 재시작을 하거나, 종료하거나의 조건 분기를 만들었다.

일단 프로젝트가 지금까지는 성공적으로 진행된것같다. 5월3일 중간점검을 기다리고, 선배들의 조언을 들은 후에 뒤에서 수정해야겠다.

일단 보류!