-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
189 lines (177 loc) · 4.94 KB
/
game.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
#include "game.h"
#include "snake.h"
#include "food.h"
#include "record.h"
#include "scoreboard.h"
#include "settings.h"
#include "fatal.h"
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include <ncurses.h>
// 全局变量(好像信号处理函数不能传参,暂时用着全局变量吧)
Snake snake;
Food food;
int key;
Scoreboard scoreboard;
Record current_record;
Interface interface;
void setup(void)
{
initscr();
crmode();
clear();
ChooseInterfaceSize();
scoreboard = CreateScoreboard(RECORD_MAX_NUM);
// 玩家登录
Login();
noecho();
srand(time(NULL));
start();
}
void start(void)
{
// 读取记录
ReadScoreboard(scoreboard);
// 显示记录
int number = ShowScoreboardWithCurrentRecord(scoreboard, current_record, RECORD_1ST_Y, RECORD_1ST_X(interface.right_boundary));
mvaddstr(RECORD_1ST_Y + SHOWED_MAX_NUM + 2, RECORD_1ST_X(interface.right_boundary), "You:");
ShowRecord(current_record, number, RECORD_1ST_Y + SHOWED_MAX_NUM + 3, RECORD_1ST_X(interface.right_boundary));
// 画墙
DrawBoundary();
// 构建、画蛇
snake = CreateSnake(DEFAULT_LENGTH, DEFAULT_BODY);
PrintSnake(snake);
// 放食物
food = PutFood(snake);
// 刷新
refresh();
// 设置信号
signal(SIGALRM, DetactAndMove);
// 设置以定时发送信号
set_ticker(DEFAULT_SPEED);
}
void Login(void)
{
char name[NAME_MAX_LENGTH + 1];
mvaddstr(interface.lower_boundary / 3, interface.right_boundary / 2 - 3, "Your name: ");
refresh();
getnstr(name, NAME_MAX_LENGTH + 1);
mvaddnstr(interface.lower_boundary / 3, interface.right_boundary / 2 - 3, " ", NAME_MAX_LENGTH + 11);
refresh();
current_record = CreateRecord(name);
}
void ChooseInterfaceSize(void)
{
int y = 0, x = 0;
getmaxyx(stdscr, y, x);
if (y < 30 || x < 30)
{
nocrmode();
endwin();
FatalError("Screen too small!");
}
/* This ratio seems ok,
* it leaves enough space for scoreboard
*/
interface.lower_boundary = y * INTERFACE_SCR_RATIO_Y;
interface.right_boundary = x * INTERFACE_SCR_RATIO_X;
}
void DrawBoundary(void)
{
int i;
// 上下边
for (i = 0; i < interface.right_boundary; i++)
{
mvaddch(0, i, WALL);
mvaddch(interface.lower_boundary - 1, i, WALL);
}
// 左右边
for (i = 0; i < interface.lower_boundary; i++)
{
mvaddch(i, 0, WALL);
mvaddch(i, interface.right_boundary - 1, WALL);
}
refresh();
}
void DetactAndMove(int signum)
{
signal(SIGALRM, SIG_IGN); // 避免重入
MoveSnake(snake);
// show
refresh();
if (HitBoundary(snake) || HitBody(snake))
{
set_ticker(0);
// save name-score data
SaveData();
// remove food
mvaddch(food->y, food->x, BLANK);
// game over massage
mvaddstr(interface.lower_boundary / 3, interface.right_boundary / 2 - 3, "Game Over! ");
refresh();
sleep(2);
mvaddstr(interface.lower_boundary / 3, interface.right_boundary / 2 - 3, "Restart? ");
refresh();
do
key = getchar();
while (key != 'r' && key != 'q');
if (key == 'r')
{
mvaddstr(interface.lower_boundary / 3, interface.right_boundary / 2 - 3, " ");
Restart();
}
else
{
wrapup();
exit(0);
}
}
else if (HitFood(snake, food))
{
// add the body to tail
Add(snake->list, snake->list->tail->y + snake->y_dir, snake->list->tail->x + snake->x_dir);
// remove and delete food
free(food);
// reput food
food = PutFood(snake);
// Score increase
if (current_record->score + 10 < SCORE_MAX)
current_record->score += 10;
// Sort and show
int number = ShowScoreboardWithCurrentRecord(scoreboard, current_record, RECORD_1ST_Y, RECORD_1ST_X(interface.right_boundary));
ShowRecord(current_record, number, RECORD_1ST_Y + SHOWED_MAX_NUM + 3, RECORD_1ST_X(interface.right_boundary));
refresh();
}
signal(SIGALRM, DetactAndMove);
}
void SaveData(void)
{
int found = FindRecord(scoreboard->records, scoreboard->size, current_record->name);
WriteScoreboard(scoreboard, current_record, found);
}
void Restart(void)
{
current_record->score = 0;
mvaddnstr(RECORD_1ST_Y + scoreboard->size + 3, RECORD_1ST_X(interface.right_boundary), " ", NUM_MAX_LENGTH + NAME_MAX_LENGTH + SCORE_MAX_LENGTH + 4);
// erase and delete snake
EraseSnake(snake);
DisposeSnake(snake);
// delete food
free(food);
// set again
start();
}
void wrapup(void)
{
set_ticker(0);
nocrmode();
echo();
endwin();
DisposeSnake(snake);
DestroyScoreboard(scoreboard);
free(current_record);
// delete fodd
free(food);
}