-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
116 lines (105 loc) · 2.9 KB
/
main.cpp
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
#include <iostream>
#include "canvas.h"
#include "person.h"
#include "terminal.h"
#include "config.h"
using namespace std;
void sig_action(int sig);
Canvas canvas;
int main(void) {
//关闭游戏控制模式
Terminal::startControlMode();
// 捕获定时器信号
if (signal(SIGALRM, sig_action) == SIG_ERR) {
perror("signal:SIGALRM");
exit(-1);
}
// 捕获窗口变化
if (signal(SIGWINCH, sig_action) == SIG_ERR) {
perror("signal:SIGWINCH");
exit(-1);
}
Person person("A");
person.setColor(PERSON_BG_COLOR);
person.setBodyColor(PERSON_FT_COLOR);
canvas.addPerson(person);
canvas.setTitle("迷宫游戏");
canvas.init();
int ret;
do {
switch(Terminal::getKey(1)) {
case UP:
ret = person.moveUp();
break;
case DOWN:
ret = person.moveDown();
break;
case LEFT:
ret = person.moveLeft();
break;
case RIGHT:
ret = person.moveRight();
break;
case ESC:
Terminal::stopControlMode();
return 0;
default :
ret = -1;
break;
}
if (MOVE_OUT == ret) {
//成功走出迷宫
canvas.success();
if (ESC == Terminal::getKey(2)) {
Terminal::stopControlMode();
return 0;
}
if (!canvas.nextLevel()) {
canvas.showInfo("You Vectory! \\^o^/\n\nPress Enter to restart!");
if (ESC == Terminal::getKey(2)) {
Terminal::stopControlMode();
return 0;
}
canvas.init();
}
} else if (MOVE_YES == ret) {
canvas.setStepCount(canvas.getStepCount() + 1);
}
} while(1);
//关闭游戏控制模式
Terminal::stopControlMode();
return 0;
}
void sig_action(int sig) {
switch (sig) {
case SIGALRM:
//定时器信号,更新倒计时
if (canvas.getCountDown() > 0) {
canvas.addCountDown(-1);
alarm(1);
} else {
canvas.failed();
if (ESC == Terminal::getKey(2)) {
Terminal::stopControlMode();
exit(0);
}
canvas.resetLevel();
}
break;
case SIGWINCH:
//窗口大小变化信号,重绘画面
int winsX, winsY, canvasW, canvasH;
Terminal::getWs(winsX, winsY);
canvasW = canvas.getWidth();
canvasH = canvas.getHeight();
if (winsX < canvasW || winsY < canvasH) {
cout << "Exit: Window size not satisfy(" << canvasW << "x" << canvasH << ")" << endl;
exit(1);
}
canvas.setAnch_x((winsX - canvasW) / 2);
canvas.setAnch_y((winsY - canvasH) / 2);
canvas.print();
alarm(1);
break;
}
}