-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
222 lines (213 loc) · 3.47 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
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
#include <windows.h>
#include <conio.h>
#include <time.h>
#include "include.h"
#include "config.h"
#ifdef DEBUG
int score = 2200; //得分
int level = 7; //等级(根据得分增加)
int sunlight = 5000; // 阳光数,初始50
int alive = 1; // 游戏是否存活
#else
int score = 0; //得分
int level = 1; //等级(根据得分增加)
int sunlight = 50; // 阳光数,初始50
int alive = 1; // 游戏是否存活
#endif // DEBUG
PlantList plist;
BulletList blist;
ZombieList zlist;
Shop shop;
static int sunlight_count = SUNLIGHT_ADD_TIME * 2 / 3; // 第一次所需时间较少
bool Game();
void levelOperate();
void sunlightAdd();
void reinit();
int main()
{
//while (1)
//{
// if (_kbhit())
// {
// int key = _getch();
// if (key == 161 || key == 163)
// continue;
// printf("%d ", key);
// }
// //printf("a");
//}
initWindow();
int play_game = 1;
while (play_game)
{
showMenu();
int ch = _getch();
switch (ch)
{
case UP: case DOWN: case LEFT: case RIGHT:
Menu::change_selector(ch);
break;
case ENTER:
play_game = Menu::choose();
break;
default:
break;
}
}
return 0;
}
bool Game()
{
system("cls");
int regame = 0;
ULONGLONG last_time = GetTickCount64();
showMap();
showMessage("欢迎来到植物大战僵尸无尽模式!");
while (alive)
{
while (GetTickCount64() - last_time < SLEEP_TIME);
last_time = GetTickCount64();
showInfo();
if (_kbhit())
{
int key = _getch();
if (key == 224)
key = _getch();
if (key == BACKSPACE)
{
showMessage("确认退出游戏?按ESC继续游戏,按ENTER确认退出!");
int exit_key = _getch();
while (exit_key != ESC && exit_key != ENTER) exit_key = _getch();
if (exit_key == ENTER)
break;
else
showMessage("继续游戏!");
}
shop.shopOperate(key);
}
plist.plantsOperate();
blist.bulletOperate();
zlist.zombieOperate();
shop.wait();
showShop();
levelOperate();
sunlightAdd();
}
if (!alive)
{
showMessage("Game Over! 僵尸吃掉了你的脑子!按 Esc 返回主界面,按 Enter 重新游戏!");
int ch = _getch();
int flag = 1;
while (flag)
{
flag = 0;
switch (ch)
{
case ESC:
regame = 0;
break;
case ENTER:
regame = 1;
break;
default:
flag = 1;
ch = _getch();
break;
}
}
}
else
{
regame = 0;
}
reinit();
return regame;
}
void levelOperate()
{
if (score < LEVEL_1_SCORE)
return;
else if (score < LEVEL_2_SCORE)
{
if (level == 1)
{
showMessage("等级提升到二级!");
level = 2;
}
}
else if (score < LEVEL_3_SCORE)
{
if (level == 2)
{
showMessage("等级提升到三级!");
level = 3;
}
}
else if (score < LEVEL_4_SCORE)
{
if (level == 3)
{
showMessage("等级提升到四级!");
level = 4;
}
}
else if (score < LEVEL_5_SCORE)
{
if (level == 4)
{
showMessage("等级提升到五级!");
level = 5;
}
}
else if (score < LEVEL_6_SCORE)
{
if (level == 5)
{
showMessage("等级提升到六级!");
level = 6;
}
}
else if (score < LEVEL_7_SCORE)
{
if (level == 6)
{
showMessage("等级提升到七级!");
level = 7;
}
}
}
void sunlightAdd()
{
if (sunlight_count == SUNLIGHT_ADD_TIME)
{
sunlight += SUNLIGHT_ADD_NUM;
sunlight_count = rand() % 1000 - 500; // 以 SUNLIGHT_ADD_TIME 为中心前后一定范围内随机时间
}
else
{
sunlight_count++;
}
}
void reinit()
{
shop.reinit(1);
plist.reinit();
blist.reinit();
zlist.reinit();
alive = 1;
#ifdef DEBUG
score = 2200;
level = 7;
sunlight = 5000;
#else
score = 0;
level = 1;
sunlight = 50;
#endif // DEBUG
system("cls");
}
/*
TODO:
几个列表的析构函数
添加僵尸
*/