본문으로 바로가기
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
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <time.h>
 
#define UP 10
#define DOWN 11
#define LEFT 12
#define RIGHT 13
#define ENTER 15
 
int check_start_or_exit=1// 메뉴에서 시작 or 종료를 선택할때 사용
char map[40][100]; // 35 46(93)
int x=27,y=1;
 
void remove_cursor(); // 커서 깜빡임 지우기
void set_consol_size(); // 콘솔창 크기 설정
void set_textcolor(); // 출력될 글자의 색깔 설정
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 main()
{
    remove_cursor();
    int get_direction_cnt;
    int save_direction;
 
    set_consol_size();
    print_title();
    print_menu_start();
 
    while(1)
    {
        save_direction=get_direction(get_direction_cnt);
 
        if(check_start_or_exit==1 && save_direction==ENTER)
        {
           // print_game_start();
            break;
        }
        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();
 
    move_main_char();
    printf("\n아무거나 입력하면 종료됩니다. ");
    getch();
}
 
void remove_cursor()
{
 CONSOLE_CURSOR_INFO curInfo;
 GetConsoleCursorInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &curInfo );
 curInfo.bVisible = 0// bVisible 멤버 변경
 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_consol_size()
{
    system("mode con cols=93 lines=35 | title Avoid");
}
 
void set_textcolor(int color_number)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color_number);
}
 
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");
    printf("\t\t\t\t--------------------------- \n\n\n");
    printf("\t\t\t\t      >> -  Start!!         \n\n\n");
    printf("\t\t\t\t         -  Exit!!          \n\n\n");
    printf("\t\t\t\t--------------------------- \n\n");
}
 
void print_menu_exit()
{
    printf("\n\n\n");
    printf("\t\t\t\t--------------------------- \n\n\n");
    printf("\t\t\t\t         -  Start!!         \n\n\n");
    printf("\t\t\t\t      >> -  Exit!!          \n\n\n");
    printf("\t\t\t\t--------------------------- \n\n");
}
 
int get_direction()
{
    int key;
    while(1)
    {
        key=getch();
        switch(key)
        {
        case 72:
            check_start_or_exit=1;
            return UP;
        case 77:
            return LEFT;
        case 80:
            check_start_or_exit=2;
            return DOWN;
        case 75:
            return RIGHT;
        case 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 print_game_floor()
{
    // make_line(29);
    for(int i=1; i<=93 ;i++)
    {
        printf("=");
    }
}
 
void make_game_map()
{
    int i=1, j=1;
 
    for(i=1; i<=27; i++)
    {
        for(j=1; j<=46; j++)
        {
            map[i][j]=' ';
        }
        printf("\n");
    }
}
 
void set_main_char()
{
    map[x][y]='@';
}
 
void print_game_map()
{
    system("cls");
    int i=1,j=1;
 
    for(i=1; i<28; i++)
    {
        for(j=1; j<=46; j++)
        {
            printf(" %c",map[i][j]);
        }
        printf("\n");
    }
}
 
int move_main_char()
{
    int save_move;
    while(1)
    {
        save_move=get_direction();
 
        if(save_move==LEFT)
        {
            map[x][y]=' ';
 
            if(y+1==47)
            {
                map[x][y]='@';
            }
            else if(y+1!=48)
            {
                y=y+1;
                map[x][y]='@';
            }
        }
        if(save_move==RIGHT)
        {
            map[x][y]=' ';
            if(y-1==0)
            {
                map[x][y]='@';
            }
            else if(y-1!=0)
            {
                y=y-1;
                map[x][y]='@';
            }
        }
 
        print_game_map();
        print_game_floor();
 
        if(save_move==ENTER)
        {
            break;
        }
    }
}
cs

열심히 개발중이다.

일단 키보드 상하키로 입력받으면 게임시작과 게임종료를 선택할수있고, 게임 시작을 선택하였을때에는 5초후에 게임이 시작되고, 게임종료를 누르면 바로 종료된다.

게임을 시작하고 나면 캐릭터를 바닥 위에서 좌우로 한칸씩 이동하게 구현했다.

나중에는 하늘에서 장애물이 떨어질것이고, 그 장애물을 열심히 피하도록 구현할것이다. 만약에 장애물을 피할경우에는 sleep을 이용해서 초당 점수가 증가하게 할것이고, 가면갈수록 장애물의 갯수가 더 많아지게, 또는 속도가 더 빨라지게 구현해보고싶다.

또한, 기록을 파일입출력으로 저장할수있게 하고싶다.

지금 내가 생각해야할 부분은, 좌우 하나씩 이동할때 걸리는 렉을 이중버퍼로 해결하고, 하늘에서 떨어지는 장애물을 어떤식으로 구현해야 할지를 생각해보아야겠다. 

재밌다.

시험공부는 프로젝트 끝나고부터 시작이다.

이런 프로젝트는 C를 배우고나서 처음인데 정말재밌다.