-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnakegame.cpp
More file actions
300 lines (266 loc) · 6.41 KB
/
snakegame.cpp
File metadata and controls
300 lines (266 loc) · 6.41 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
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <random>
#include <ctime>
#include <windows.h>
#include <chrono>
char direction = 'D';
int x,y,speed = 200 ,difficult = 0;
int point = 0;
const int width = 20;
const int height = 20;
const int left = 10;
const int top = 10;
const int right = 10 + width;
const int bottom = 10 + height;
char map[height][width]={};
auto last_move = std::chrono::system_clock::now();
int move = speed;//蛇的初速度,添加难度qwq
struct FoodPosition
{
int x,y;
};
void gotoxy(short x, short y)
{
COORD pos = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
struct SnakeBody
{
int x,y;
};
SnakeBody snakehead;
SnakeBody body;
//食物的坐标表示
std::vector<FoodPosition> food;
//蛇头的生成函数
void snakehead_generate()
{
snakehead.x=width/2;
snakehead.y=height/2;
}
//蛇身的坐标表示(考虑用队列表示)
std::deque<SnakeBody> snakebody;
void body_generate()//初始化蛇身(包括蛇头)
{
snakebody.clear();
snakebody.push_front(snakehead);//蛇头
for(int i = 1; i<=3 ;++ i)
{
body.x = snakehead.x - i ;
body.y = snakehead.y ;
snakebody.push_back(body);
}
}
//食物生成函数
void food_generate()
{
FoodPosition newfood;
static std::random_device rd;
static std::mt19937 generator(rd());
static std::uniform_int_distribution<int> distributionx(1,width-2);
static std::uniform_int_distribution<int> distributiony(1,height-2);
newfood.x = distributionx(generator);
newfood.y = distributiony(generator);
//添加防止食物和蛇身重合判定
for(auto it = snakebody.begin();it!= snakebody.end();++it)
{
if(newfood.x == it->x && newfood.y == it->y)
{
food_generate();
return;
}
}
food.push_back(newfood);
}
//地图边界,以及蛇的初始化
void map_update(int width,int height)
{
for(int i = 0;i<height;++i)
{
for(int j = 0; j < width;++j)
{
if(i==0|| i == height-1 || j==0 || j == width-1)
map[i][j]= '#';
else
map[i][j]= ' ';
}
}
bool ishead = true;
for (auto &it : snakebody)
{
if(ishead)
{
map[it.y][it.x]= 'O';
ishead = false;
}
else
{
map[it.y][it.x]='o';
}
}
//更新食物(食物位置放这似不妥)
for(auto &f :food)
{
map[f.y][f.x] = '*';
}
}
// 更新蛇的长度,同时把食物也更新掉
void update()
{
bool eatfood = false;
for(auto it = food.begin(); it!= food.end();++it)
{
if(snakehead.x == it->x && snakehead.y == it->y)//食到饭
{
eatfood = true;
food.erase(it);
food_generate();
point += 5;
break;
}
}
SnakeBody new_head;
new_head.x= snakehead.x;
new_head.y= snakehead.y;
snakebody.push_front(new_head);
if(!eatfood) // 如果没有食到饭
{
snakebody.pop_back();
}
}
void map_print()
{
gotoxy(0,0);
for(int i = 0; i < height; ++i)
{
for(int j = 0 ; j< width; ++j)
{
std::cout<<map[i][j]<<" ";
}
std::cout<<std::endl;
}
}
//形成游戏窗口
void window(int left, int top, int right, int bottom);
void mani_direction()
{
if(GetAsyncKeyState('W') & 0x8000 && direction != 'S')
direction = 'W';
if(GetAsyncKeyState('S') & 0x8000 && direction != 'W')
direction = 'S';
if(GetAsyncKeyState('A') & 0x8000 && direction != 'D')
direction = 'A';
if(GetAsyncKeyState('D') & 0x8000 && direction != 'A')
direction = 'D';
}
void move666()
{
if(direction == 'W')
snakehead.y--;
if(direction == 'S')
snakehead.y++;
if(direction == 'A')
snakehead.x--;
if(direction == 'D')
snakehead.x++;
}
//游戏结束
void gameover()
{
if(snakehead.x <= 0 || snakehead.x >= width-1 ||
snakehead.y <= 0 || snakehead.y >= height-1)
{
std::cout<<"Game Over"<<std::endl;
throw std::runtime_error("Game Over");
Sleep(3000);
}
else if(snakehead.x == snakebody.front().x &&
snakehead.y == snakebody.front().y)
{
std::cout<<"Game Over"<<std::endl;
throw std::runtime_error("Game Over");
}
// 检查蛇头是否与蛇身相撞
for(auto it = snakebody.begin() + 1; it != snakebody.end();++it)
{
if(snakehead.x == it->x && snakehead.y == it->y)
{
std::cout<<"Game Over"<<std::endl;
throw std::runtime_error("Game Over");
}
}
}
void Hide()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {0,0});
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
//游戏难度
void difficulty()
{
std::cout<< "Choose your difficulty(1~5) :";
std::cin>> difficult;
switch (difficult)
{
case 1 :
speed = 1000;
break;
case 2 :
speed = 800;
break;
case 3 :
speed = 600;
break;
case 4 :
speed = 400;
break;
case 5 :
speed = 200;
break;
default:
std::cout<<"Invalid input"<<std::endl;
difficulty();
break;
}
move = speed;
}
int main()
{
difficulty();
Hide();
snakehead_generate();
food_generate();
body_generate();
try
{
while(true)
{
mani_direction();
auto now = std::chrono::system_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - last_move);
if(duration.count() >= move)
{
move666();
gameover();
update();
map_update(width, height);
map_print();
last_move= now;
std::cout<<"Points:"<<point<<'\n';
}
Sleep(20);
}
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
Sleep(3000);
}
system("pause");
return 0;
}
//鲁棒性很强,暴赞!