-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathg.cpp
More file actions
359 lines (317 loc) · 7.59 KB
/
g.cpp
File metadata and controls
359 lines (317 loc) · 7.59 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
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
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#include <unistd.h>
#include "c.h"
#include "g.h"
/*
* A text game for Linux
* by yuwei5[yuwei5@staff.sina.com.cn]
* 2014 SINA Hackathon
* TOP10:
* http://toyshop.sinaapp.com/game/top10.php
* */
Win *win_disp = NULL;
Win *win_input = NULL;
static int level = 1;
static int round = 0;
typedef struct _Object
{
int x;
int y;
int life;
char face;
int score;
int xstep;
int ystep;
} Object;
typedef std::vector<Object> Monsters;
Monsters monsters;
Object me;
typedef std::vector<Object> Fires;
Fires fires;
static int rnd(int from, int to)
{
return (rand() % (to - from + 1)) + from;
}
static void draw_me()
{
mvwaddch(win_disp->win, me.y, me.x, me.face);
}
// static void draw_refresh()
// {
// wrefresh(win_disp->win);
// }
static void draw()
{
unsigned i = 0;
if (NULL == win_disp) {
return;
}
wclear(win_disp->win);
char msg[16] = {0};
sprintf(msg, "L%d R%d Score:%d", level, round, me.score);
mvwaddstr(win_disp->win, 0, 1, msg);
mvwaddch(win_disp->win, me.y, me.x, me.face);
for (i=0; i<monsters.size(); i++) {
Object *m = &monsters[i];
mvwaddch(win_disp->win, m->y, m->x, m->face);
}
for (i=0; i<fires.size(); i++) {
Object *f = &fires[i];
mvwaddch(win_disp->win, f->y, f->x, f->face);
}
wrefresh(win_disp->win);
}
static void init_game(int level)
{
int i = 0;
monsters.clear();
for (i=0; i<level; i++) {
Object m;
m.x = rnd(1, 10);
m.y = rnd(1, 10);
m.life = 1;
m.face = '@';
m.score = 10;
monsters.push_back(Monsters::value_type(m));
}
}
static int fire_fly()
{
int hit = 0;
int ret = 0;
for (unsigned i=0; i<fires.size(); i++) {
ret++;
hit = 0;
Object *f = &fires[i];
mvwaddch(win_disp->win, f->y, f->x, ' '); // erase old face
f->x += f->xstep;
f->y += f->ystep;
if (f->x < 0 || f->x >= win_disp->locate.w
|| f->y < 0 || f->y >= win_disp->locate.h) {
fires.erase(fires.begin() + i);
break;
}
// hit test
for (unsigned j=0; j<monsters.size(); j++) {
Object *m = &monsters[j];
if (f->x == m->x && f->y == m->y) {
m->life--;
if (m->life <= 0) {
hit = 1;
monsters.erase(monsters.begin() + j);
me.score += m->score;
break;
}
}
}
if (hit) {
continue;
}
mvwaddch(win_disp->win, f->y, f->x, f->face);
draw_me();
wrefresh(win_disp->win);
usleep(1000*100);
}
return ret;
}
static int monsters_round()
{
int ret = 0;
int yoffset = 0;
int xoffset = 0;
for (unsigned i=0; i<monsters.size(); i++) {
ret++;
Object *m = &monsters[i];
mvwaddch(win_disp->win, m->y, m->x, ' ');
yoffset = me.y - m->y;
xoffset = me.x - m->x;
if (abs(yoffset) > abs(xoffset)) {
if (yoffset > 0) {
m->y++;
} else {
m->y--;
}
} else {
if (xoffset > 0) {
m->x++;
} else {
m->x--;
}
}
if (m->y == me.y && m->x == me.x) {
me.life--;
}
mvwaddch(win_disp->win, m->y, m->x, m->face);
wrefresh(win_disp->win);
usleep(1000*200);
}
return ret;
}
static void gameover()
{
int pos = 1;
wclear(win_disp->win);
draw();
mvwaddstr(win_disp->win, pos++, 1, "Game Over!");
mvwaddstr(win_disp->win, pos++, 1, "No. name score ");
mvwaddstr(win_disp->win, pos++, 1, "------------------");
mvwaddstr(win_disp->win, pos++, 1 ,"1. 0xFF 190");
mvwaddstr(win_disp->win, pos++, 1 ,"2. 0xFF 120");
mvwaddstr(win_disp->win, pos++, 1 ,"3. .... .....");
mvwaddstr(win_disp->win, pos++, 1 ,"4. .... .....");
mvwaddstr(win_disp->win, pos++, 1 ,"5. .... .....");
mvwaddstr(win_disp->win, pos++, 1 ,"6. .... .....");
wrefresh(win_disp->win);
readch();
return;
}
static void new_fire(int xstep, int ystep)
{
Object f;
f.x = me.x;
f.y = me.y;
f.face = '.';
f.xstep = xstep;
f.ystep = ystep;
fires.push_back(f);
}
static int do_round(char *key)
{
unsigned i = 0;
int ret = 0;
int len = strlen(key);
for (i=0; i<monsters.size(); i++) {
ret++;
Object *m = &monsters[i];
m->score += len*10;
}
for (i=0; i<len; i++) {
char cmd = key[i];
switch(cmd) {
case 'w':
if (me.y > 1) {
me.y--;
}
break;
case 'a':
if (me.x > 1) {
me.x--;
}
break;
case 's':
if (me.y < win_disp->locate.h) {
me.y++;
}
break;
case 'd':
if (me.x < win_disp->locate.w) {
me.x++;
}
break;
case 'h':
new_fire(-1, 0);
break;
case 'j':
new_fire(0, 1);
break;
case 'k':
new_fire(0, -1);
break;
case 'l':
new_fire(1, 0);
break;
default:
break;
}
ret = monsters_round();
draw();
fire_fly();
if (me.life <= 0) {
gameover();
return -1;
}
if (ret <= 0) {
while (fire_fly() > 0);
break;
}
usleep(1000*500);
}
return ret;
}
// return value: 0:running; not 0:quit;
static int wait_user_cmd(char *keybuf, size_t buflen)
{
int ch = 0;
int cursor = 0;
while(1) {
ch = readch();
keybuf[cursor++] = ch;
if (ch == '\n' || cursor >= buflen - 1) {
wclear(win_input->win);
wrefresh(win_input->win);
return cursor;
} else if (ch == KEY_BACKSPACE || ch == 127) {
keybuf[cursor] = '\0';
cursor--;
if (cursor < 0) {
cursor = 0;
}
}
if (NULL != win_input) {
wclear(win_input->win);
mvwaddstr(win_input->win, 1, 1, keybuf);
wrefresh(win_input->win);
}
};
return cursor;
}
void game_main_loop(Win *disp, Win *input)
{
int ret = 0;
static char keybuf[32] = {0};
win_disp = disp;
win_input = input;
me.x = win_disp->locate.w / 2;
me.y = win_disp->locate.h / 2;
me.life = 1;
me.face = 'X';
me.score = 0;
round = 0;
init_game(level);
wclear(win_disp->win);
draw();
do {
round++;
memset(keybuf, 0, sizeof(keybuf));
wait_user_cmd(keybuf, sizeof(keybuf));
ret = do_round(keybuf);
if (ret == -1) {
break;
}
if (ret == 0) {
round = 0;
init_game(++level);
continue;
}
if (keybuf[0] == 'q') {
break;
}
} while (strncmp(keybuf, "quit", 4) != 0);
}
#ifdef DEBUG_MAIN
int main()
{
char ch;
init_keyboard();
init_game(1);
do {
ch = readch();
printf("%c %d\n", ch, ch);
} while (ch != 'q');
close_keyboard();
return 0;
}
#endif