-
Notifications
You must be signed in to change notification settings - Fork 2
/
rhythmgame.c
330 lines (285 loc) · 6.97 KB
/
rhythmgame.c
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
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <sys/time.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <curses.h>
/* 타이머 */
#define CCHAR 0
#ifdef CTIME
#undef CTIME
#endif
#define CTIME 1
/* 게임 시작, 종료 설정*/
#define GAME_START 0
#define GAME_END 1
static struct result
{
char name[30];
long nScore;
int year;
int month;
int day;
int hour;
int min;
}temp_result;
int nScore = 0;
int best_nScore = 0;
char strScore[20] = " ";
int nCombo = 0;
int game = GAME_END;
char result[50] = " ";
void Map();
void ReadyMap();
void ScoreMap();
void note();
int getkey(void);
int print_result();
void execute(char *[]);
void main()
{
int menu = 1, ch;
char *arglist[3] = { "paplay", "Good_Times.wav", 0 };
time_t ptime;
struct tm *t;
FILE *fp = NULL;
while (menu) {
char buf[100];
initscr();
clear();
Map();
ReadyMap();
refresh();
menu = getkey();
if (menu == 49) {
initscr();
clear();
Map();
ScoreMap();
refresh();
execute(arglist);
for (int i = 0; i < 38; i++) {
note();
refresh();
if (i == 37) {
// 기록 파일로 저장
move(31, 0);
sprintf(buf, "your score is : %d\n", nScore);
addstr(buf);
refresh();
move(32, 0);
addstr("what's your name? ");
refresh();
scanf("%s", temp_result.name);
temp_result.nScore = nScore;
if (temp_result.nScore >= best_nScore)
best_nScore = temp_result.nScore;
/*시간 저장 부분*/
ptime = time(NULL); // 현재 시각을 초 단위로 얻기
t = localtime(&ptime); // 초 단위의 시간을 분리하여 구조체에 넣기
temp_result.year = t->tm_year + 1900;
temp_result.month = t->tm_mon + 1;
temp_result.day = t->tm_mday;
temp_result.hour = t->tm_hour;
temp_result.min = t->tm_min;
fp = fopen("result", "ab");//추가할때는 ab
fseek(fp, 1, SEEK_END); //파일속에서 위치를 찾는 함수(파일명,이동할바이트,SEEK_SET(시작점),CUR(읽고있는),END(끝))
fwrite(&temp_result, sizeof(struct result), 1, fp);
fclose(fp);
nScore = 0;
nCombo = 0;
}
}
}
else if (menu == 50) {
print_result();
}
else if (menu == 51) {
exit(0);
}
}
endwin();
}
void execute(char *arglist[])
{
int pid, exitstatus = -1;
pid = fork();
switch (pid) {
case -1: exit(1);
case 0: execvp(arglist[0], arglist);
default: return;
}
return;
}
// 스테이지 기본 틀
void Map()
{
move(0, 0);
addstr("*****************************************");
for (int i = 1; i < 29; i++) {
move(i, 0);
addstr("*\t\t\t\t\t*");
}
move(29, 0);
addstr("*****************************************");
}
// 시작화면
void ReadyMap() {
move(10, 14);
addstr("Rhythm Game");
move(13, 16);
addstr("1. Start");
move(14, 16);
addstr("2. Result");
move(15, 16);
addstr("3. Exit");
move(26, 2);
addstr(" ****** ****** ****** ****** ");
move(27, 2);
addstr(" A S D F ");
move(30, 1);
}
// 우측 점수 출력틀
void ScoreMap() {
int ch;
// 점수 목록
move(10, 44);
addstr(strScore);
move(22, 44);
addstr("Return to the Menu : Q(q)");
// 점수
char nScoree[20];
sprintf(nScoree, "score : %d point", nScore);
move(4, 44);
addstr(nScoree);
move(25, 44);
addstr("<<< Hit Section(Great)");
move(26, 44);
addstr("<<< Hit Section(Perfect)");
move(27, 44);
addstr("<<< Hit Section(Great)");
//콤보
char strCombo[20];
sprintf(strCombo, "%d combo", nCombo);
move(13, 44);
addstr(strCombo);
}
/*문자를 바로 입력 받을 수 있는 함수*/
int getkey(void)
{
char ch;
int error;
static struct termios Otty, Ntty;
fflush(stdout);
tcgetattr(0, &Otty);
Ntty = Otty;
Ntty.c_iflag = 0;
Ntty.c_oflag = 0;
Ntty.c_lflag &= ~ICANON;
#if 1
Ntty.c_lflag &= ~ECHO;
#else
Ntty.c_lflag |= ECHO;
#endif
Ntty.c_cc[VMIN] = CCHAR;
Ntty.c_cc[VTIME] = CTIME;
#if 1
#define FLAG TCSAFLUSH
#else
#define FLAG TCSANOW
#endif
if (0 == (error = tcsetattr(0, FLAG, &Ntty)))
{
error = read(0, &ch, 1);
error += tcsetattr(0, FLAG, &Otty);
}
return (error == 1 ? (int)ch : -1);
}
void note() {
int row;
int i, j;
int ch;
row = (rand() % 4) * 6 + 4;
if (row == 10) row = 13;
else if (row == 16) row = 22;
else if (row == 22) row = 31;
for (i = 1; i<28;i++) {
move(i, row);
addstr("******");
refresh();
usleep(1000 * 10);
move(i, row);
addstr(" ");
move(26, 0);
addstr("*****************************************");
ch = getkey();
if (ch == 'a' || ch == 'A') {
if (row == 4 && i == 26) { nScore += 500; nCombo += 1; sprintf(strScore, "%s", "Perfect!"); ScoreMap(); }
else if (row == 4 && i >= 25 && i <= 27) { nScore += 300; nCombo += 1; sprintf(strScore, "%s", "Great! "); ScoreMap(); }
}
else if (ch == 's' || ch == 'S') {
if (row == 13 && i == 26) { nScore += 500; nCombo += 1; sprintf(strScore, "%s", "Perfect!"); ScoreMap(); }
else if (row == 13 && i >= 25 && i <= 27) { nScore += 300; nCombo += 1; sprintf(strScore, "%s", "Great! "); ScoreMap(); }
}
else if (ch == 'd' || ch == 'D') {
if (row == 22 && i == 26) { nScore += 500; nCombo += 1; sprintf(strScore, "%s", "Perfect!"); ScoreMap(); }
else if (row == 22 && i >= 25 && i <= 27) { nScore += 300; nCombo += 1; sprintf(strScore, "%s", "Great! "); ScoreMap(); }
}
else if (ch == 'f' || ch == 'F') {
if (row == 31 && i == 26) { nScore += 500; nCombo += 1; sprintf(strScore, "%s", "Perfect!"); ScoreMap(); }
else if (row == 31 && i >= 25 && i <= 27) { nScore += 300; nCombo += 1; sprintf(strScore, "%s", "Great! "); ScoreMap(); }
}
else if (ch == 'q' || ch == 'Q') {
main();
}
}
}
/* 메뉴에서 기록출력시 호출되어 기록을 출력하는 함수*/
int print_result(void)
{
FILE *fp = NULL;
char ch = 1, i = 10;
fp = fopen("result", "rb");
if (fp == NULL)
return 0;
system("clear");
move(1, 19);
addstr("Rhythm Game");
move(2, 22);
addstr("Result");
move(4, 13);
addstr("Return to the Menu : Q(q)");
move(7, 8);
addstr("Name\tScore\t Date\tTime");
move(8, 4);
addstr(" ==========================================");
while (1)
{
fread(&temp_result, sizeof(struct result), 1, fp);
if (!feof(fp))
{
move(i, 8);
char result[100];
sprintf(result, "%s\t%ld\t%d. %d. %d.\t%d : %d\n", temp_result.name, temp_result.nScore, temp_result.year, temp_result.month, temp_result.day, temp_result.hour, temp_result.min);
addstr(result);
refresh();
for (int j = 0; j <100; j++) result[i] = '\0';
i++;
}
else break;
}
fclose(fp);
while (1)
{
move(5, 8);
ch = getkey();
if (ch == 81 || ch == 113) break;
}
return 0;
}