android贪吃蛇opencv,贪吃蛇小游戏—C++、Opencv编写实现

贪吃蛇游戏,C++、Opencv实现

设计思路:

1.显示初始画面,蛇头box初始位置为中心,食物box位置随机

2.按随机方向移动蛇头,按a、s、d、w键控制移动方向,分别为向左,向下,向右,向上

3.蛇头位置与食物box位置重合,则把食物box加入到蛇身向量arraryBox里,并设置食物box为第一个元素

4.蛇身各个box移动规律是,每次移动后一个box的位置变为前一个box的位置

5.蛇头移动超越边界,游戏结束

开始界面:

848f357b3580b0a926cbed9fd78aa337.png

过程:

d3a6116a1ba72110d85075d2c2c8659c.png

游戏结束:

9c74a34d3fb2d414d4cba5e0f3e769df.png

编码:

控制方向模块:

//*************************************************************************************

//通过键盘a s d w四个键控制上下左右移动方向;

//int waittime; 等待时间,通过这个可以设置游戏的难易程度

//bool &horizMove //允许水平移动标志

//bool &verMove //允许垂直移动标志

//int &moveDirection 具体移动方向,0:向左 1:向右 2:向上 3:向下

//*************************************************************************************

void MoveDirection(int waittime,bool &horizMove,bool &verMove,int &moveDirection)

{

char pointKey=waitKey(waittime);

switch (pointKey)

{

case 'a':

if(horizMove)

{

moveDirection=0;

horizMove=false;

verMove=true;

}

break;

case 'd':

if(horizMove)

{

moveDirection=1;

horizMove=false;

verMove=true;

}

break;

case 's':

if(verMove)

{

moveDirection=3;

horizMove=true;

verMove=false;

}

break;

case 'w':

if(verMove)

{

moveDirection=2;

horizMove=true;

verMove=false;

}

break;

default:

break;

}

}游戏结束判定模块:

//************************************************************************************

//越界判断游戏是否结束

//传入参数: Box序列

//************************************************************************************

bool GameOver(const vector arraryBox)

{

if(arraryBox[0].boxPoint.x<0)

{

putText(backGroundImage,"Game Over!",Point(50,200),1,3,Scalar(0,0,255),2,8,false);

imshow(windowname,backGroundImage);

return false;

}

if((arraryBox[0].boxPoint.x)>=Width)

{

putText(backGroundImage,"Game Over!",Point(50,200),1,3,Scalar(0,0,255),2,8,false);

imshow(windowname,backGroundImage);

return false;

}

if(arraryBox[0].boxPoint.y<0)

{

putText(backGroundImage,"Game Over!",Point(50,200),1,3,Scalar(0,0,255),2,8,false);

imshow(windowname,backGroundImage);

return false;

}

if((arraryBox[0].boxPoint.y)>=Hight)

{

putText(backGroundImage,"Game Over!",Point(50,200),1,3,Scalar(0,0,255),2,8,false);

imshow(windowname,backGroundImage);

return false;

}

return true;

}

蛇身序列第一个(蛇头)Box根据移动方向移动模块:

//************************************************************************

//根据移动方向改变第一个box的增长方向

//const int moveDirection: 移动方向,0:向左 1:向右 2:向上 3:向下

//vector arraryBox :Box 序列

//************************************************************************

void MoveAccordingDirecton(const int moveDirection, vector &arraryBox,Box & boxTarget)

{

switch (moveDirection)

{

case 0:

arraryBox[0].boxPoint.x-=width;

if(arraryBox[0].boxPoint==boxTarget.boxPoint)

{

arraryBox.insert(arraryBox.begin(),boxTarget);

meetTargetBox=true;

}

break;

case 1:

arraryBox[0].boxPoint.x+=width;

if(arraryBox[0].boxPoint==boxTarget.boxPoint)

{

arraryBox.insert(arraryBox.begin(),boxTarget);

meetTargetBox=true;

}

break;

case 2:

arraryBox[0].boxPoint.y-=hight;

if(arraryBox[0].boxPoint==boxTarget.boxPoint)

{

arraryBox.insert(arraryBox.begin(),boxTarget);

meetTargetBox=true;

}

break;

case 3:

arraryBox[0].boxPoint.y+=hight;

if(arraryBox[0].boxPoint==boxTarget.boxPoint)

{

arraryBox.insert(arraryBox.begin(),boxTarget);

meetTargetBox=true;

}

break;

default:

break;

}

}蛇身每一个Box移动模块:

//******************************************************************************

//定义蛇身box移动

//每次移动,后一个box移动到前一个box位置

//******************************************************************************

void SnakeMove(vector&arraryBox1,vector&arraryBox,Mat &imageBackground)

{

Box bb;

arraryBox1.push_back(bb);

arraryBox1[0]=arraryBox[0];

for(int i=0;i

{

if(i!=0)

{

arraryBox1[1]=arraryBox[i];

arraryBox[i]=arraryBox1[0];

arraryBox1[0]=arraryBox1[1];

}

for(int i=0;i

{

Mat ROICenterPoint=imageBackground(Rect(arraryBox[i].boxPoint.x,arraryBox[i].boxPoint.y,width,hight));

addWeighted(ROICenterPoint,0,arraryBox[i].boxMat,1,0,ROICenterPoint);

}

}

imshow(windowname,imageBackground);

}主程序:

//*************************************************

//贪吃蛇游戏,C++、Opencv实现

//设计思路:

//1.显示初始画面,蛇头box初始位置为中心,食物box位置随机

//2.按随机方向移动蛇头,按a、s、d、w键控制移动方向,分别为向左,向下,向右,向上

//3.蛇头位置与食物box位置重合,则把食物box加入到蛇身向量arraryBox里,并设置食物box为第一个元素

//4.蛇身各个box移动规律是,每次移动后一个box的位置变为前一个box的位置

//5.蛇头移动超越边界,游戏结束

//*************************************************

#include

#include

#include

#include

#include

using namespace std;

using namespace cv;

//定义每个box大小和窗口大小

int width=14;

int hight=14;

int Width=41*width;

int Hight=41*hight;

//定义box类,包含大小和位置信息

class Box

{

public:

Mat boxMat;

Point boxPoint;

Box();

};

Box:: Box()

{

Mat image01(width,hight,CV_8UC3,Scalar(255,255,255));

boxMat=image01;

boxPoint=Point(((Width/width)/2+1)*width,((Hight/hight)/2+1)*hight);

}

vector arraryBox;

vector arraryBox1;

Mat image;

int moveDirection; //移动方向,

bool horizMove=true; //水平移动

bool verMove=true; //垂直移动

bool meetTargetBox=false; //是否击中目标box

Box boxTarget;

Mat backGroundImage;

string windowname="Gluttonous Snake ----by 牧野";

//*************************************************************************************

//通过键盘a s d w四个键控制上下左右移动方向;

//int waittime; 等待时间,通过这个可以设置游戏的难易程度

//bool &horizMove //允许水平移动标志

//bool &verMove //允许垂直移动标志

//int &moveDirection 具体移动方向,0:向左 1:向右 2:向上 3:向下

//*************************************************************************************

void MoveDirection(int waittime,bool &horizMove,bool &verMove,int &moveDirection);

//************************************************************************************

//越界判断游戏是否结束

//传入参数: Box序列

//************************************************************************************

bool GameOver(const vector arraryBox);

//******************************************************************************

//定义蛇身box移动

//每次移动,后一个box移动到前一个box位置

//******************************************************************************

void SnakeMove(vector&arraryBox1,vector&arraryBox,Mat &imageBackground);

//************************************************************************

//根据移动方向改变box

//const int moveDirection: 移动方向,0:向左 1:向右 2:向上 3:向下

//vector arraryBox :Box 序列

//************************************************************************

void MoveAccordingDirecton(const int moveDirection, vector &arraryBox,Box & boxTarget);

int main(int argc,char* argv[])

{

Box box01;

arraryBox.push_back(box01);

srand(time(0));

boxTarget.boxPoint.x=(rand()%(Width/width))*width;

boxTarget.boxPoint.y=(rand()%(Hight/hight))*hight;

moveDirection=rand()%4; //初始化移动方向 0:向左 1:向右 2 向上 3 乡下

while(true)

{

//判断是否越界,越界则游戏结束

if(!GameOver(arraryBox))

{

break;

}

//初始化显示界面

Mat imageBackground(Width,Hight,CV_8UC3,Scalar(0,0,0));

Mat ROIBackground=imageBackground(Rect(boxTarget.boxPoint.x,boxTarget.boxPoint.y,width,hight));

addWeighted(ROIBackground,0,boxTarget.boxMat,1,0,ROIBackground);

Mat ROICenterPoint=imageBackground(Rect(arraryBox[0].boxPoint.x,arraryBox[0].boxPoint.y,width,hight));

addWeighted(ROICenterPoint,0,arraryBox[0].boxMat,1,0,ROICenterPoint);

if(arraryBox.size()>1)

{

Mat imageBackground01(Width,Hight,CV_8UC3,Scalar(0,0,0));

imageBackground=imageBackground01;

Mat ROIBackground=imageBackground(Rect(boxTarget.boxPoint.x,boxTarget.boxPoint.y,width,hight));

addWeighted(ROIBackground,0,boxTarget.boxMat,1,0,ROIBackground);

}

if(meetTargetBox)

{

boxTarget.boxPoint.x=(rand()%(Width/width))*width;

boxTarget.boxPoint.y=(rand()%(Hight/hight))*hight;

Mat ROIBackground=imageBackground(Rect(boxTarget.boxPoint.x,boxTarget.boxPoint.y,width,hight));

addWeighted(ROIBackground,0,boxTarget.boxMat,1,0,ROIBackground);

meetTargetBox=false;

}

//通过键盘a s d w四个键控制上下左右移动方向;

MoveDirection(10000,horizMove,verMove,moveDirection);

//定义蛇身box移动

SnakeMove(arraryBox1,arraryBox,imageBackground);

//根据移动方向改变第一个box的增长方向

MoveAccordingDirecton(moveDirection, arraryBox,boxTarget);

backGroundImage=imageBackground;

imshow(windowname,imageBackground);

}

waitKey();

}

原理很简单,部分代码有注释,就不多说了,欢迎探讨。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本系统的研发具有重大的意义,在安全性方面,用户使用浏览器访问网站时,采用注册和密码等相关的保护措施,提高系统的可靠性,维护用户的个人信息和财产的安全。在方便性方面,促进了校园失物招领网站的信息化建设,极大的方便了相关的工作人员对校园失物招领网站信息进行管理。 本系统主要通过使用Java语言编码设计系统功能,MySQL数据库管理数据,AJAX技术设计简洁的、友好的网址页面,然后在IDEA开发平台中,编写相关的Java代码文件,接着通过连接语言完成与数据库的搭建工作,再通过平台提供的Tomcat插件完成信息的交互,最后在浏览器中打开系统网址便可使用本系统。本系统的使用角色可以被分为用户和管理员,用户具有注册、查看信息、留言信息等功能,管理员具有修改用户信息,发布寻物启事等功能。 管理员可以选择任一浏览器打开网址,输入信息无误后,以管理员的身份行使相关的管理权限。管理员可以通过选择失物招领管理,管理相关的失物招领信息记录,比如进行查看失物招领信息标题,修改失物招领信息来源等操作。管理员可以通过选择公告管理,管理相关的公告信息记录,比如进行查看公告详情,删除错误的公告信息,发布公告等操作。管理员可以通过选择公告类型管理,管理相关的公告类型信息,比如查看所有公告类型,删除无用公告类型,修改公告类型,添加公告类型等操作。寻物启事管理页面,此页面提供给管理员的功能有:新增寻物启事,修改寻物启事,删除寻物启事。物品类型管理页面,此页面提供给管理员的功能有:新增物品类型,修改物品类型,删除物品类型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值