服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++代码实现贪吃蛇小游戏

C++代码实现贪吃蛇小游戏

2021-10-07 15:22木笔# C/C++

这篇文章主要为大家详细介绍了C++贪吃蛇小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下

1.游戏描述

贪吃蛇可谓是从小玩到大的经典趣味小游戏,蛇每吃到一次食物,身体就会长一节,如果撞到墙或者撞到自身,游戏结束。

2.代码实现

1.首先需要思考的问题是如何指定位置输出字符?这时候就有一个非常强大的函数叫 gotoxy() ,现在库函数里边已经没有了,只能我们自己实现,代码中注释很完整,自行阅读即可。

2.实现了指哪画哪的目标之后,就可以开始游戏内容制作了。首先便是圈地,即画地图,一个简简单单的循环就能安排的明明白白。

3.伟大的圈地运动就结束了,接下来我们就实现画一条蛇,我们选择使用deque双端队列,这个操作更为方便,画好蛇之后就给画一个食物出来,食物的位置坐标使用随机数来实现,简单吧~

4.让蛇动起来。我们默认让蛇往上走,即‘w'方向,之后便是按键响应,这个只要懂点语法,小白都能实现,就不多说了。

5.贪吃蛇的大体框架就这样搭好了,是不是soeasy~

3.装饰环节

只是会跑当然不能满足我们日益增长的精神需求,那就加点料呗,下面的代码中只加入了计分、等级,其他的都没有加,为的是新手能快速上手,你也可以再往里边加吃到食物时“滴~”响一声等元素,这都不是问题。

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
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
#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <deque>
#include <ctime>
#pragma warning(disable:4996)
using namespace std;
HANDLE hOut;
COORD pos;
 
//1.实现gotoxy函数
void gotoxy(short x, short y)
{
 hOut = GetStdHandle(STD_OUTPUT_HANDLE); //获取句柄
 pos = { x, y };
 SetConsoleCursorPosition(hOut, pos);  //移动光标到指定位置
}
void HideCursor() //隐藏光标
{
 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
 CONSOLE_CURSOR_INFO CursorInfo;
 GetConsoleCursorInfo(handle, &CursorInfo);//获取控制台光标信息
 CursorInfo.bVisible = false;    //隐藏控制台光标
 SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态
}
 
//2.蛇的结构体
struct Snake
{
 char body;
 short position_x, position_y;    //蛇的坐标
};
 
//3.游戏运行类
class Game
{
private:
 char image;
 enum mapSize { width = 60, height = 30 }; //游戏地图
 deque<Snake> snake;      //定义一个队列,装蛇的身体
 int score = 0;        //游戏分数
 char hit = 'w';       //按键输入
 bool eat_Food = false;      //是否吃到食物
 short food_x, food_y;      //食物坐标
 int speed = 400;       //蛇的速度
 bool snake_state = true;     //蛇的状态
 int level = 1;        //游戏关卡
public:
 Game();
 void draw_Frame()  //画边框
 {
 for (int i = 0; i < height; i++)
 {
 gotoxy(0, i);
 cout << "■";
 gotoxy(width, i);
 cout << "■";
 }
 for (int i = 0; i <= width; i += 2) //■ 这个符号占两个字符位置,所以是+2
 {
 gotoxy(i, 0);
 cout << "■";
 gotoxy(i, height);
 cout << "■";
 }
 }
 void init_snake()  //初始化蛇
 {
 snake.push_back({ '#', width / 2, static_cast<short>(height / 2) }); //添加蛇头
 for (int i = 0; i < 3; i++) //蛇的初始身体节数,可自定
 snake.push_back({ char('o'), width / 2, static_cast<short>((height / 2) + 1 + i) });
 snake.push_back({ ' ', width / 2, static_cast<short>((height / 2) + 4) }); //添加蛇尾,先放空,以便于后面添加节数时操作
 }
 void draw_Snake() //画蛇
 {
 for (int k = 0; k < snake.size(); k++)
 {
 gotoxy(snake[k].position_x, snake[k].position_y);
 cout << snake[k].body;
 }
 }
 void clear_Tail() //清除蛇尾,不建议使用清屏函数,避免屏闪
 {
 int k = snake.size() - 1;
 gotoxy(snake[k].position_x, snake[k].position_y);
 cout << " "; //蛇每移动一次(即一格),就把上一次画出来的蛇尾擦掉
 }
 void draw_Food() //画食物
 {
 while (1)
 {
 food_x = rand() % (width - 4) + 2; //食物要在地图中,不能再地图边框上,地图的符号在x方向占两个字符位置所以+2,而-4则是减去边框
 food_y = rand() % (height - 2) + 1; //与上同理
 if (wrong_Location() && food_x % 2 == 0)
 break;
 }
 gotoxy(food_x, food_y);
 cout << "O";
 }
 bool wrong_Location() //判断食物的坐标是否合理
 {
 for (auto i : snake) //c++11的基于范围的循环
 {
 if (food_x == i.position_x && food_y == i.position_y) //食物不能出现在蛇的身体上
 return 0;
 }
 return 1;
 }
 void judge_eatFood() //判断是否吃到食物
 {
 if (food_x == snake[0].position_x && food_y == snake[0].position_y)
 eat_Food = true;
 }
 void judge_state() //判断蛇是否撞墙或撞身体
 {
 if (snake.size() >= 2)
 {
 deque<Snake>::iterator iter = snake.begin() + 1; //实际就是把snake容器里第一个(即蛇头)存在iter里
 int x = (iter - 1)->position_x, y = (iter - 1)->position_y;
 for (; iter != snake.end(); ++iter)
 {
 if (iter->position_x == x && iter->position_y == y) //蛇头不能撞自身
  snake_state = false;
 }
 }
 if(snake[0].position_x == 1 ||
 snake[0].position_x == 59 ||
 snake[0].position_y == 0 ||
 snake[0].position_y == 30) //蛇头不能撞边框
 snake_state = false;
 }
 void key_Down() //按键响应
 {
 char key = hit;
 if (_kbhit()) //接受按键
 hit = _getch();
 for (int i = snake.size() - 1; i > 0; i--) //蛇的移动方法,每移动一次,后面一节的身体到了它的前一节身体上
 {
 snake[i].position_x = snake[i - 1].position_x;
 snake[i].position_y = snake[i - 1].position_y;
 }
 if ((hit == 'a' || hit == 'A') && hit != 'd')
 {
 hit = 'a'; snake[0].position_x--;
 }
 else if ((hit == 'd' || hit == 'D') && hit != 'a')
 {
 hit = 'd'; snake[0].position_x++;
 }
 else if ((hit == 'w' || hit == 'W') && hit != 's')
 {
 hit = 'w'; snake[0].position_y--;
 }
 else if ((hit == 's' || hit == 'S') && hit != 'w')
 {
 hit = 's'; snake[0].position_y++;
 }
 }
 void show()
 {
 gotoxy(65, 0);
 cout << "你的得分是:";
 gotoxy(71, 1);
 cout << score;
 gotoxy(69, 2);
 cout << "关卡:" << level;
 }
};
Game::Game()
{
 HideCursor();
 srand(static_cast<unsigned int>(time(NULL))); //随机数种子
 init_snake();
 draw_Food();
 Snake tail; //蛇尾
 while (1)
 {
 draw_Frame();
 tail = snake.back();
 if (eat_Food)
 {
 snake.back().body = 'o'; //把初始化蛇的空尾显示化为o,看到的效果就是加了一节
 snake.push_back(tail); //再添加一节空尾,便于下次操作
 gotoxy(food_x, food_y);
 cout << " "; //食物被吃后要在原来的位置画空,否则光标会退位,导致边框错位
 draw_Food();
 score++;
 if (score % 5 == 0)
 {
 speed *= 0.8;
 level++;
 }
 eat_Food = false;
 }
 if (level == 10)
 break;
 key_Down();
 draw_Snake();
 judge_state();
 if (!snake_state)
 break;
 judge_eatFood();
 Sleep(speed);
 clear_Tail();
 show();
 }
}
int main()
{
 system("mode con cols=100 lines=40"); //设置打开窗口大小
 system("color 7C"); //设置背景色和前景色
 system("title 贪吃蛇 v1.0"); 设置窗口标题
 Game game;
 gotoxy(0, 32);
 cout << "Game over!" << endl;
}

本期教程到这里就结束了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_46117139/article/details/106328238

延伸 · 阅读

精彩推荐