欢迎来到天天文库
浏览记录
ID:33834986
大小:45.00 KB
页数:6页
时间:2019-03-01
《控制台贪吃蛇代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#include#include#include#include#includeusingnamespacestd;//definetheheighthorwidthofthewall/backgrand#defineM23//thespeedrateofsnake#defineSPEED_RATE10//positionclass(x,y)classpos{public:pos(i
2、nta=0,intb=0):x(a),y(b){}pos(constpos&b){x=b.x,y=b.y;}public:booloperator==(posa)const{returnx==a.x&&y==a.y;}booloperator!=(posa)const{returnx!=a.x
3、
4、y!=a.y;}pos&operator=(posb){x=b.x;y=b.y;return*this;}public:intx;inty;};//fuctionformovingcursortoposition(
5、x,y)voidgotoXY(poss){//asymboloccupies2intpositionss.x*=2;//changetypeposetotypeCOORDCOORDc;c.X=s.x;c.Y=s.y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);}//snakeclasssnake{public:voidPopRear(){snakeBody.pop_front();}voidPushHead(posnewpos){s
6、nakeBody.push_back(newpos);}posGetRearPos()const{returnsnakeBody.back();}posGetFrontPos()const{returnsnakeBody.front();}boolFindPos(poss)const{//thefirstelementisjustusedforcleaningthemarkofsnake,soleaveitlist::const_iteratorlt=snakeBody.begin();lt++;
7、if(find(lt,snakeBody.end(),s)!=snakeBody.end())returntrue;returnfalse;}voidDisplaySnake()const{list::const_iteratorite;//snakerearforcleaningthemarkite=snakeBody.begin();gotoXY(*ite);cout<<"";//beginwithnextslotofsnakeite++;//snakebodyexcepttheheadfor
8、(;ite!=snakeBody.end();ite++){gotoXY(*ite);cout<<"○";}//snakeheadite--;gotoXY(*ite);cout<<"¤";}private:listsnakeBody;};//initialisethewallanddisplayitvoidinitWall(){intwall[M][M]={{0}};for(inti=1;i9、0;k10、srand(time(NULL));p.x=rand()%(M-2)+1;p.y=rand()%(M-2)+1;while(s.FindPos(p)11、12、p==s.GetFrontPos()){p.x=rand()%(M-2)+1;p.y=rand()%(M-2)+1;}gotoXY(p);cout<<"☆";}//SnakeheadonwalloronitselfboolSnakeHeadOnWallOrOnse
9、0;k10、srand(time(NULL));p.x=rand()%(M-2)+1;p.y=rand()%(M-2)+1;while(s.FindPos(p)11、12、p==s.GetFrontPos()){p.x=rand()%(M-2)+1;p.y=rand()%(M-2)+1;}gotoXY(p);cout<<"☆";}//SnakeheadonwalloronitselfboolSnakeHeadOnWallOrOnse
10、srand(time(NULL));p.x=rand()%(M-2)+1;p.y=rand()%(M-2)+1;while(s.FindPos(p)
11、
12、p==s.GetFrontPos()){p.x=rand()%(M-2)+1;p.y=rand()%(M-2)+1;}gotoXY(p);cout<<"☆";}//SnakeheadonwalloronitselfboolSnakeHeadOnWallOrOnse
此文档下载收益归作者所有