实例介绍
【实例简介】
控制你自己的蛇与AI控制的蛇争抢食物。
碰到自己的身体后不会死,碰到其他蛇的身体后会死。
【实例截图】
【核心代码】
class Snake {
public:
std::deque<SnakeSegment> segments; //deque双向链表
POINT direction; //方向
int length; //蛇身长度
bool AI=true; //是否是电脑控制的蛇
bool die = false; //蛇是否已经死亡
int diecount=0; //死亡次数
int score; //分数
char bodychar;
char headchar;
colorx color;
Snake(bool ai,char head,char body, colorx color1) :AI(ai), headchar(head), bodychar(body), color(color1),direction{0, -1}
{
init();
}
void init()
{
length = 4, segments.clear();
if (!AI)
{
for (int i = 0; i < length; i) {
segments.emplace_back(SnakeSegment(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2));
}
}
else
{
Rep:
int x1 = 3 rand() % (WINDOW_WIDTH - 6);
int y1 = 3 rand() % (WINDOW_HEIGHT - 6);
for (Snake * snake : snakes)
{
for (const auto& segment : snake->segments) {
if (segment.position.x == x1 && segment.position.x == y1)
{
goto Rep;
}
}
}
for (int i = 0; i < length; i) {
segments.emplace_back(SnakeSegment(x1, y1));
}
}
die = false;
}
bool collision()//判断是否越界
{
int x1 = segments.front().position.x, y1 = segments.front().position.y;
if (x1 < 1 || x1 >= WINDOW_WIDTH-1 || y1 < 1 || y1 >= WINDOW_HEIGHT-1)
{
die = true;
diecount ;
return true;
}
//是否与其他蛇发送碰撞
for (Snake* snake : snakes)
{
if (snake!=this)
{
for (const auto& segment : snake->segments) {
if (segment.position.x == this->segments.front().position.x && segment.position.y == this->segments.front().position.y)
{
die = true;
diecount ;
snake->score = 1;
return true;
}
}
}
}
if (segments.front().position.x == foodPosition.x && segments.front().position.y == foodPosition.y)
{
grow(); //吃到食物,身体增长一节
Servefood();//重新投放食物
}
return false;
}
void move() {
colortxt((int)segments.back().position.x*2, (int)segments.back().position.y, ' ', color, 0);
SnakeSegment headPosition = segments.front().position;
segments.emplace_front(headPosition direction); //从头部增加一个元素
segments.pop_back();
}
void print()
{
colortxt(segments.front().position.x * 2, segments.front().position.y, headchar, color, 1);
for (auto iter = segments.begin() 1; iter != segments.end(); iter )
{
colortxt(iter->position.x * 2, iter->position.y, bodychar, color, 1);
}
}
void Getdirection()
{
if (AI == false)
{
if (_kbhit())
{
char x = _getch();
switch (x)
{
case 27: //Esc
gameover = true;
break;
case 'w':
case 72:
{
if (direction.y == 0) { direction.x = 0, direction.y = -1; }
break;
}
case 's':
case 80:
{
if (direction.y == 0) { direction.x = 0, direction.y = 1; }
break;
}
case 'a':
case 75:
{
if (direction.x == 0) { direction.x = -1, direction.y = 0; }
break;
}
case 'd':
case 77:
{
if (direction.x == 0) { direction.x = 1, direction.y = 0; }
break;
}
default:
{
}
}
}
}
else
{
//AI部分
//判断是否越界 判断前方是否有障碍物 追赶食物 修改方向
auto changedirection = [](POINT &dir1,POINT p)
{
if (dir1.x == 0)
{
dir1.y = 0;
if (p.x < 4)
{
dir1.x = 1;
}
else if (p.x > WINDOW_WIDTH - 4)
{
dir1.x = -1;
}
else if (rand() % 2 > 0)
{
dir1.x = 1;
}
else
{
dir1.x = -1;
}
}
else if (dir1.y == 0)
{
dir1.x = 0;
if (p.y < 4)
{
dir1.y = 1;
}
else if (p.y > WINDOW_HEIGHT - 4)
{
dir1.y = -1;
}
if (rand() % 2 > 0)
{
dir1.y = 1;
}
else
{
dir1.y = -1;
}
}
};
auto Crossed = [&](SnakeSegment p)
{
if (p.position.x < 1 || p.position.x >= WINDOW_WIDTH - 1 || p.position.y < 1 || p.position.y >= WINDOW_HEIGHT - 1)
return true;
return false;
};
SnakeSegment headPosition = segments.front();
headPosition direction;
if (Crossed(headPosition)) { changedirection(direction, headPosition.position); return; }
for (Snake* snake : snakes) //躲避player
{
for (const auto& segment : snake->segments) {
if (segment.position.x == headPosition.position.x && segment.position.y == headPosition.position.y)
{
changedirection(direction, headPosition.position); return;
}
}
}
//追赶食物
POINT hxy = segments.front().position;
if (hxy.x == foodPosition.x && hxy.y < foodPosition.y)
{
if (direction.x !=0) { direction.x = 0; direction.y = 1;}
}
else if (hxy.x == foodPosition.x && hxy.y > foodPosition.y)
{
if (direction.x != 0) { direction.x = 0; direction.y =-1;}
}
else if (hxy.y == foodPosition.y && hxy.x < foodPosition.x)
{
if (direction.y != 0) { direction.y = 0; direction.x = 1;}
}
else if (hxy.y == foodPosition.y && hxy.x > foodPosition.x)
{
if (direction.y != 0) { direction.y = 0; direction.x =-1;}
}
else if (hxy.x < foodPosition.x && hxy.y < foodPosition.y)
{
if (direction.y < 0) { direction.y = 0; direction.x = 1; }
else if (direction.x < 0) { direction.x = 0; direction.y = 1;}
}
else if (hxy.x > foodPosition.x && hxy.y < foodPosition.y)
{
if (direction.y < 0) { direction.y = 0; direction.x = -1; }
else if (direction.x >0) { direction.x = 0; direction.y = 1; }
}
else if (hxy.x < foodPosition.x && hxy.y > foodPosition.y)
{
if (direction.y > 0) { direction.y = 0; direction.x = 1; }
else if (direction.x < 0) { direction.x = 0; direction.y =-1; }
}
else if (hxy.x > foodPosition.x && hxy.y > foodPosition.y)
{
if (direction.y > 0) { direction.y = 0; direction.x = -1; }
else if (direction.x > 0) { direction.x = 0; direction.y=-1; }
}
}
}
void grow() {
segments.emplace_back(segments.back().position);
length;
}
};
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论