本文实例为大家分享了C++实现控制台贪吃蛇的具体代码,供大家参考,具体内容如下
初学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
|
#pragma once
#ifndef SNACK_H
#define SNACK_H
#define Frame_X 5//边框起点横坐标
#define Frame_Y 5//边框起点纵坐标
#define Frame_width 50//边框长度
#define Frame_height 25//边框高度
struct point //蛇个点坐标
{
int x[100]; //蛇的横坐标,其中x[0]表示蛇尾的横坐标,x[N-1]表示蛇头的横坐标
int y[100]; //蛇的竖坐标,其中y[0]表示蛇尾的竖坐标,y[N-1]表示蛇头的竖坐标
};
struct Food //食物坐标
{
int x;
int y;
};
class snack
{
public :
snack();
void init_snack(point *snake); //初始化蛇
void gotoxy( int x, int y); //光标移动
void begin_view(); //封面
void frame(); //打印边界与游戏说明
void menu(point *snake,Food *food); //打实时印信息
void print_snack(point *snake); //打印蛇
void move(point *snake); //移动蛇
void get_food(point *snake,Food *food); //随机产生食物
void eating(point *snake,Food *food); //吃食物
bool die(point *snake); //判断蛇是否死亡
void start_game(); //主函数
void end_game(); //结束界面
private :
int length; //蛇长度
int score; //得分
int account; //已吃的食物数量
int difficulty; //难度
int stage; //关卡
int life; //生命值
};
#endif
|
主要的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
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
360
361
362
363
364
365
366
367
368
369
|
#include"stdafx.h"
#include"snack.h"
#include<Windows.h>
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
/*************获取按键************/
#define key_number 11//虚拟键数
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
enum keyboard { up_, down_, left_, right_ ,w_,s_,a_,d_,y_,n_,esc_,other_};
int keyboard_list[] = {VK_UP,VK_DOWN,VK_LEFT,VK_RIGHT, 'W' , 'S' , 'A' , 'D' , 'Y' , 'N' ,VK_ESCAPE};
keyboard keyboardarray[] = { up_,down_,left_,right_,w_,s_,a_,d_,y_,n_,esc_,other_ };
keyboard get_the_key()
{
for ( int i = 0; i < key_number; i++)
{
if (KEYDOWN(keyboard_list[i]))
return keyboardarray[i];
}
return keyboardarray[key_number];
}
int a[2]; //记录蛇尾坐标
snack::snack()
{
length = 3;
score = 0;
account = 0;
difficulty = 1;
stage = 1;
life = 3;
}
void snack::init_snack(point *snake)
{
snake->x[0] = Frame_X + 2; //初始化蛇的横坐标
snake->y[0] = Frame_Y + Frame_height / 2; //初始化蛇的竖坐标
for ( int i = 1; i < length; i++) /* 依次得到蛇身、蛇头的坐标 */
{
snake->x[i] = snake->x[i - 1] + 2;
snake->y[i] = snake->y[i - 1];
}
}
void snack::gotoxy( int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hOut, pos);
}
void snack::begin_view()
{
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
cout << "\n\n\n\n" ;
cout << " **** * * * **** * *\n" ;
cout << " * * ** * * * * * *\n" ;
cout << " * * * * *** * * *\n" ;
cout << " *** * * * * * * **\n" ;
cout << " * * * * * * * * *\n" ;
cout << " * * * * ***** * * *\n" ;
cout << " * * * ** * * * * * *\n" ;
cout << " **** * * ** ** **** * *\n" ;
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
cout << "\n\n\n\n" ;
cout << " /^\\/^\\ " <<endl;
cout << " _|__| O|" <<endl;
cout << " \\/ /~ \_/ \\ " <<endl;
cout << " \\____|__________/ \\ " <<endl;
cout << " \\_______ \\ " <<endl;
cout << " `\\ \\ \\ " <<endl;
cout << " | | \\ " <<endl;
cout << " / / \\ " <<endl;
cout << " / / \\\\ " <<endl;
cout << " / / \\ \\ " <<endl;
cout << " / / \\ \\ " <<endl;
cout << " / / _----_ \\ \\ " <<endl;
cout << " / / _-~ ~-_ | |" <<endl;
cout << " ( ( _-~ _--_ ~-_ _/ |" <<endl;
cout << " \\ ~-____-~ _-~ ~-_ ~-_-~ /" <<endl;
cout << " ~-_ _-~ ~-_ _-~" <<endl;
cout << " ~--______-~ ~-___-~" <<endl;
}
void snack::frame()
{
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
//打印上边界
gotoxy(Frame_X, Frame_Y);
cout << "┏" ;
for ( int i = 0; i < Frame_width/2-2; i++)
cout << "━" ;
cout << "┓" ;
//打印下边界
gotoxy(Frame_X, Frame_Y + Frame_height);
cout << "┗" ;
for ( int i = 0; i < Frame_width/2-2; i++)
cout << "━" ;
cout << "┛" ;
//打印左右边界
for ( int i = 0; i < Frame_height-1; i++)
{
gotoxy(Frame_X, Frame_Y + 1 + i);
cout << "┃" ;
gotoxy(Frame_X+Frame_width-2, Frame_Y + 1 + i);
cout << "┃" ;
}
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoxy(Frame_X + Frame_width + 3, Frame_Y + 1);
cout << "按键说明:方向键或WSAD键控制" ;
gotoxy(Frame_X + Frame_width + 3, Frame_Y + 3);
cout << " 长按加速" ;
gotoxy(Frame_X + Frame_width + 3, Frame_Y + 5);
cout << " 按ESC退出游戏" ;
}
void snack::menu(point *snake,Food *food)
{
gotoxy(Frame_X + 3, Frame_Y - 2);
cout << "分数:" << score;
gotoxy(Frame_X + Frame_width -11, Frame_Y - 2);
cout << "生命值:" << life;
gotoxy(Frame_X + Frame_width - 30, Frame_Y - 2);
cout << "等级:" << stage;
/*gotoxy(Frame_X + Frame_width + 3, Frame_Y + 5);
cout << "横坐标:" << food->x;
gotoxy(Frame_X + Frame_width + 3, Frame_Y + 7);
cout << "纵坐标:" << food->y;
gotoxy(Frame_X + Frame_width + 3, Frame_Y + 9);
cout << "X: " << snake->x[length - 1];
gotoxy(Frame_X + Frame_width + 3, Frame_Y + 11);
cout << "Y: " << snake->y[length - 1];*/
}
void snack::print_snack(point *snake)
{
for ( int i = 0; i<length; i++)
{
gotoxy(snake->x[i], snake->y[i]);
if (i == 0)
cout << "○" ; //打印蛇尾
else if (i == length - 1)
cout << "¤" ; //打印蛇头
else
cout << "◎" ; //打印蛇身
}
}
void snack::move(point *snake)
{
gotoxy(snake->x[0], snake->y[0]);
cout << " " ; //清除蛇尾
for ( int i = 1; i < length ; i++) /* 后一节的坐标依次取代前一节的坐标 */
{
snake->x[i - 1] = snake->x[i];
snake->y[i - 1] = snake->y[i];
}
}
void snack::get_food(point *snake,Food *food)
{
int i, j;
srand ( static_cast <unsigned int >( time (0)));
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
/* 产生食物的条件:1.在游戏窗口内 2.不在蛇的身上 */
while (1)
{
j = 0;
food->x = Frame_X + 2 * ( rand () % (Frame_width / 2 - 2)) + 2; //得到食物的横坐标
food->y = Frame_Y + rand () % (Frame_height - 2) + 1; //得到食物的纵坐标
for (i = 0; i<length; i++)
{ /* 判断食物是否在蛇的身上,如果在蛇身上,则重新产生;否则,打印蛇身 */
if (food->x == snake->x[i] && food->y == snake->y[i])
{
j = 1;
break ;
}
}
if (i == length)
{
gotoxy(food->x, food->y);
cout << "⊙" ;
break ;
}
else
continue ;
}
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY); //设置蛇颜色
}
void snack::eating(point *snake,Food *food)
{
if (snake->x[length - 1] == food->x&&snake->y[length - 1] == food->y)
{
length++;
for ( int i = length - 1; i > 0; i--) /* 蛇后节坐标依次赋值给蛇前一节的坐标,依次得到蛇身及蛇头的坐标 */
{
snake->x[i] = snake->x[i - 1];
snake->y[i] = snake->y[i - 1];
}
snake->x[0] = a[0]; //得到蛇尾移动前的横坐标
snake->y[0] = a[1]; //得到蛇尾移动前的纵坐标
get_food(snake, food);
account++;
score+=difficulty*10; //分数增加
if (account % 5 == 0)
{
difficulty++;
stage++;
}
}
}
bool snack::die(point *snake)
{
for ( int i = 0; i < length-1; i++)
{
if (snake->x[length - 1] == snake->x[i] && snake->y[length - 1] == snake->y[i])
return true ;
}
if (snake->x[length - 1] == Frame_X || snake->y[length - 1] == Frame_Y || snake->x[length - 1] == Frame_X + Frame_width - 2 || snake->y[length - 1] == Frame_Y + Frame_height - 1)
return true ;
return false ;
}
void snack::start_game()
{
//光标隐藏
CONSOLE_CURSOR_INFO Cursorlnfo;
GetConsoleCursorInfo(hOut, &Cursorlnfo); //获取控制台光标信息
Cursorlnfo.bVisible = false ; //隐藏控制台光标信息
SetConsoleCursorInfo(hOut, &Cursorlnfo); //设置控制台光标信息
system ( "mode con cols=100 lines=40" ); //设置窗口大小
point s, *snake = &s; //定义蛇的结构体指针并指向蛇的结构体
Food f, *food = &f; //定义食物的结构体指针并指向食物的结构体
keyboard key_state, Key;
keyboard temp = right_;
int j = 0;
char ch=77;
begin_view();
gotoxy(12, 34);
cout << "按任意键继续......" ;
while (!_kbhit())
{ }
system ( "cls" );
frame();
init_snack(snake);
get_food(snake,food);
while (1)
{
j = 0;
menu(snake,food);
a[0] = snake->x[0];
a[1] = snake->y[0];
key_state = get_the_key();
if (key_state == temp) //长按加速
{
j = 1;
Sleep(20);
}
if (key_state!=other_)
temp = key_state;
switch (temp)
{
case up_:
case w_:
move(snake);
snake->y[length - 1] -= 1;
break ;
case down_:
case s_:
move(snake);
snake->y[length - 1] += 1;
break ;
case left_:
case a_:
move(snake);
snake->x[length - 1] -= 2;
break ;
case right_:
case d_:
move(snake);
snake->x[length - 1] += 2;
break ;
case esc_:
end_game();
break ;
}
eating(snake, food);
print_snack(snake);
if (die(snake))
{
life--;
if (life == 0)
end_game();
else
{
length = 3;
start_game();
}
}
if (j == 0)
{
if (difficulty < 10)
Sleep(200 - difficulty * 20);
else
Sleep(10);
}
else
Sleep(10);
}
}
void snack::end_game()
{
system ( "CLS" );
system ( "color 0D" );
cout << "\n *** * ** ** *********" ;
cout << "\n * * * ** ** * " ;
cout << "\n * *** * * * * * " ;
cout << "\n * * * * * * * ******** " ;
cout << "\n * *** * * * * * * " ;
cout << "\n * * ***** * * * * " ;
cout << "\n * * * * * * * * " ;
cout << "\n ** ** ** * * * *********" ;
cout << "\n\n\n\n\n\n" ;
cout << "\n *** ** ** ********** ****** " ;
cout << "\n * * * * * * * " ;
cout << "\n * * * * * * * " ;
cout << "\n * * * * ******** ****** " ;
cout << "\n * * * * * * * " ;
cout << "\n * * * * * * * " ;
cout << "\n * * *** * * ** " ;
cout << "\n *** * ********** * ** " ;
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY);
gotoxy(Frame_X + Frame_width / 2, 27);
cout << "总得分:" << score;
Sleep(200);
exit (0);
}
|
然后是main函数
1
2
3
4
5
6
7
8
9
|
#include "stdafx.h"
#include"snack.h"
int main()
{
snack a;
a.start_game();
return 0;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/Brucelee399/article/details/72575925