-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmenu.c
More file actions
196 lines (177 loc) · 4.16 KB
/
menu.c
File metadata and controls
196 lines (177 loc) · 4.16 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
#include "snake.h"
void print_bound() {
endwin();
initscr();
int j;
int i = 1;
move(0, 0);
j = COLS;
while (j) {//위쪽 경계1
addstr("*");
j--;
}
j = COLS;
move(1, 0);
while (j) {//아래쪽 경계2
addstr("*");
j--;
}
while (i<LINES) {//왼 쪽, 오른쪽 경계
move(i, 0);
addstr("*");
move(i, 1);
addstr("*");
move(i, COLS - 2);
addstr("*");
move(i, COLS - 1);
addstr("*");
i++;
}
j = COLS;
move(LINES - 2, 0);
while (j) {//아래쪽 경계1
addstr("*");
j--;
}
j = COLS;
move(LINES - 1, 0);//아래쪽 경계2
while (j) {
addstr("*");
j--;
}
}
void print_menu() {
char c;
head.x_dir = 1;
head.y_dir = 0;
head.ttm = 5;
head.ttg = 5;
head.score = 0;
initscr();
print_bound();//boundary 출력 함수 호출
move((LINES - 1) / 3 - 1, (COLS - 1) / 2.7);
addstr("+---------------------------+");
move((LINES - 1) / 3, (COLS - 1) / 2.7 + 10);
standout();
addstr("SNAKE GAME");
standend();
move((LINES - 1) / 3 + 1, (COLS - 1) / 2.7);
addstr("+---------------------------+");
move((LINES - 1) / 2, (COLS - 1) / 2.7);
addstr("<PRESS 'S(s)' TO START>");
move((LINES - 1) / 2 + 2, (COLS - 1) / 2.7);
addstr("s,S:START");
move((LINES - 1) / 2 + 3, (COLS - 1) / 2.7);
addstr("q,Q:FINISH");
refresh();
c = getchar();
while(1){
if(c=='s'||c=='S'){print_rule();break;}
else if(c=='q'||c=='Q'){endwin();exit(0);break;}
else c=getchar();//s,S,q,Q가 아닌 값이 오면나올 때까지 다른 값을 받도록 함
}
}
void print_replay()
{
char c;
char str[10];
head.x_dir = 1;
head.y_dir = 0;
head.ttm = 5;
head.ttg = 5;
initscr();
print_bound();
move((LINES - 1)/3-1,(COLS - 1)/2.7);
addstr("+--------------------------+");
move((LINES-1)/3,(COLS-1)/2.7+3);
standout();
addstr("Do You Want To Replay?");
standend();
move((LINES-1)/3+1,(COLS -1)/2.7);
addstr("+--------------------------+");
move((LINES-1)/3+2,(COLS-1)/2-1);
standout();
addstr("Score:");
sprintf(str, "%d", head.score);
addstr(str);
standend();
move((LINES-1)/2,(COLS-1)/2.7);
addstr("s,S: START");
move((LINES-1)/2+3,(COLS-1)/2.7);
addstr("q,Q:FINISH");
refresh();
head.score = 0;
c=getchar();
while(1){
if(c=='s'||c=='S'){print_rule();break;}
else if(c=='q'||c=='Q'){endwin();exit(0);break;}
else c = getchar();
}
}
void print_rule()
{
char c;
clear();
initscr();
print_bound();
move((LINES - 1) / 3 - 1, (COLS - 1) / 2.7);
addstr("+---------------------------+");
move((LINES - 1) / 3, (COLS - 1) / 2.7 + 7);
standout();
addstr("SNAKE GAME RULE!");
standend();
move((LINES-1)/3+1, (COLS-1)/2.7);
addstr("+---------------------------+");
move((LINES - 1) / 2, (COLS - 1) / 2.7);
addstr("<PRESS ANY KEY TO START>");
move((LINES - 1) / 2 + 2, (COLS - 1) / 2.7);
addstr("->,<-:move");
move((LINES - 1) / 2 + 3, (COLS - 1) / 2.7);
addstr("q,Q :quit");
refresh();
c = getchar();
start_game();
}
void start_game()
{
int i,j;
char str[10];//score를 string으로 바꿔서 잠시 저장할 변수
struct snake *temp; // malloc을 위한 변수
temp = (struct snake*)malloc(sizeof(struct snake));
p_snake = temp;//snake의 머리 위치 정하기
p_snake->x_pos = 5;
p_snake->y_pos = 5;
temp = (struct snake*)malloc(sizeof(struct snake));
temp->x_pos = 5;//snake의 꼬리 위치 정하기
temp->y_pos = 5;
p_snake->blink = temp;
p_snake->flink = NULL;
temp->flink = p_snake;
temp->blink = NULL;
last_tail = temp;
temp = (struct snake*)malloc(sizeof(struct snake));
temp->x_pos = 5;
temp->y_pos = 5;
temp->flink = last_tail;
temp->blink = NULL;
last_tail->blink = temp;
last_tail = temp;
i=COLS;
j=LINES;
initscr();
clear();
print_bound();
move(0,COLS-10);
standout();
addstr("score:");
sprintf(str, "%d", head.score);
addstr(str);//print score
standend();
make_token();
mvaddch(p_snake->y_pos, p_snake->x_pos, HEAD);//snake의 머리 출력
mvaddch(temp->y_pos, temp->x_pos, BODY);//snake의 몸 출력
refresh();//화면에 출력
terminal_set(0);
signal(SIGALRM, move_snake);
set_ticker(10);
}