经典游戏贪吃蛇代码(c++ 编写)

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

/*Í·Îļþ*/#includevector#includequeueusingnamespacestd;#ifndefSNAKE_H#defineSNAKE_HclassCmp{friendclassCsnake;intrSign;//ºá×ø±êintlSign;//Êú×ø±êpublic://friendboolisDead(constCmp&cmp);Cmp(intr,intl){setPoint(r,l);}Cmp(){}voidsetPoint(intr,intl){rSign=r;lSign=l;}Cmpoperator-(constCmp&m)const{returnCmp(rSign-m.rSign,lSign-m.lSign);}Cmpoperator+(constCmp&m)const{returnCmp(rSign+m.rSign,lSign+m.lSign);}};constintmaxSize=5;//³õʼÉßÉí³¤¶ÈclassCsnake{CmpfirstSign;//ÉßÍ·×ø±êCmpsecondSign;//Éß¾±×ø±êCmplastSign;//Éßβ×ø±êCmpnextSign;//Ô¤±¸ÉßÍ·introw;//ÁÐÊýintline;//ÐÐÊýintcount;//ÉßÉí³¤¶ÈvectorvectorcharsnakeMap;//Õû¸öÓÎÏ·½çÃæqueueCmpsnakeBody;//ÉßÉípublic:intGetDirections()const;chargetSymbol(constCmp&c)const//»ñÈ¡Ö¸¶¨×ø±êµãÉϵÄ×Ö·û{returnsnakeMap[c.lSign][c.rSign];}Csnake(intn)//³õʼ»¯ÓÎÏ·½çÃæ´óС{if(n20)line=20+2;elseif(n30)line=30+2;elseline=n+2;row=line*3+2;}boolisDead(constCmp&cmp){return(getSymbol(cmp)=='c'||cmp.rSign==row-1||cmp.rSign==0||cmp.lSign==line-1||cmp.lSign==0);}voidInitInstance();//³õʼ»¯ÓÎÏ·½çÃæboolUpdataGame();//¸üÐÂÓÎÏ·½çÃævoidShowGame();//ÏÔʾÓÎÏ·½çÃæ};#endif//SNAKE_H====================================================================/*ÀàµÄʵÏÖ¼°Ó¦ÓÃ*/#includewindows.h#includectime#includeiostream#includesnake.husingnamespacestd;//²âÊԳɹ¦voidCsnake::InitInstance(){snakeMap.resize(line);//snakeMap[Êú×ø±ê][ºá×ø±ê]for(inti=0;iline;i++){snakeMap[i].resize(row);for(intj=0;jrow;j++){snakeMap[i][j]='';}}for(intm=1;mmaxSize+1;m++){//³õʼÉßÉísnakeMap[line/2][m]='c';//½«ÉßÉí×ø±êѹÈë¶ÓÁÐsnakeBody.push(Cmp(m,(line/2)));//snakeBody[ºá×ø±ê][Êú×ø±ê]}//Á´±íͷβfirstSign=snakeBody.back();secondSign.setPoint(maxSize-1,line/2);}//²âÊԳɹ¦intCsnake::GetDirections()const{if(GetKeyState(VK_UP)0)return1;//1±íʾ°´ÏÂÉϼüif(GetKeyState(VK_DOWN)0)return2;//2±íʾ°´ÏÂϼüif(GetKeyState(VK_LEFT)0)return3;//3±íʾ°´ÏÂ×ó¼üif(GetKeyState(VK_RIGHT)0)return4;//4±íʾ°´ÏÂÓÒ¼üreturn0;}boolCsnake::UpdataGame(){//-----------------------------------------------//³õʼ»¯µÃ·Ö0staticintscore=0;//»ñÈ¡Óû§°´¼üÐÅÏ¢intchoice;choice=GetDirections();coutTotalscore:scoreendl;//Ëæ»ú²úÉúʳÎïËùÔÚ×ø±êintr,l;//¿ªÊ¼³õʼÒѾ­³Ôʳ£¬²úÉúÒ»¸öʳÎïstaticbooleatFood=true;//Èç¹û³ÔÁËÒ»¸ö£¬²ÅÔÙ³öÏÖµÚ2¸öʳÎïif(eatFood){do{//×ø±ê·¶Î§ÏÞÖÆÔÚ£¨1£¬1£©µ½£¨line-2,row-2£©¶Ôµã¾ØÐÍÖ®¼äsrand(time(0));r=(rand()%(row-2))+1;//ºá×ø±êl=(rand()%(line-2))+1;//Êú×ø±ê//Èç¹ûËæ»ú²úÉúµÄ×ø±ê²»ÊÇÉßÉí£¬Ôò¿ÉÐÐ//·ñÔòÖØвúÉú×ø±êif(snakeMap[l][r]!='c'){snakeMap[l][r]='*';}}while(snakeMap[l][r]=='c');}switch(choice){case1://ÏòÉÏ//Èç¹ûÉßÍ·ºÍÉç¾±µÄºá×ø±ê²»Ïàͬ£¬Ö´ÐÐÏÂÃæ²Ù×÷if(firstSign.rSign!=secondSign.rSign)nextSign.setPoint(firstSign.rSign,firstSign.lSign-1);//·ñÔò£¬ÈçÏÂÔÚÔ­±¾·½ÏòÉϼÌÐøÒƶ¯elsenextSign=firstSign+(firstSign-secondSign);break;case2://ÏòÏÂif(firstSign.rSign!=secondSign.rSign)nextSign.setPoint(firstSign.rSign,firstSign.lSign+1);elsenextSign=firstSign+(firstSign-secondSign);break;case3://Ïò×óif(firstSign.lSign!=secondSign.lSign)nextSign.setPoint(firstSign.rSign-1,firstSign.lSign);elsenextSign=firstSign+(firstSign-secondSign);break;case4://ÏòÓÒif(firstSign.lSign!=secondSign.lSign)nextSign.setPoint(firstSign.rSign+1,firstSign.lSign);elsenextSign=firstSign+(firstSign-secondSign);break;default:nextSign=firstSign+(firstSign-secondSign);}//----------------------------------------------------------if(getSymbol(nextSign)!='*'&&!isDead(nextSign))//Èç¹ûûÓÐÅöµ½Ê³Îï(ÇÒûÓÐËÀÍöµÄÇé¿öÏÂ)£¬É¾³ýÉßβ£¬Ñ¹ÈëеÄÉßÍ·{//ɾ³ýÉßβlastSign=snakeBody.front();snakeMap[lastSign.lSign][lastSign.rSign]='';snakeBody.pop();//¸üÐÂÉßÍ·secondSign=firstSign;//ѹÈëÉßÍ·snakeBody.push(nextSign);firstSign=snakeBody.back();snakeMap[firstSign.lSign][firstSign.rSign]='c';//ûÓгÔʳeatFood=false;returntrue;}//-----³Ôʳ-----elseif(getSymbol(nextSign)=='*'&&!isDead(nextSign)){secondSign=firstSign;snakeMap[nextSign.lSign][nextSign.rSign]='c';//ֻѹÈëÉßÍ·snakeBody.push(nextSign);firstSign=snakeBody.back();eatFood=true;//¼Ó·Öscore+=20;returntrue;}//-----ËÀÍö-----else{coutDeadendl;coutYourlasttotalscoreisscoreendl;returnfalse;}}voidCsnake::ShowGame(){for(inti=0;iline;i++){for(intj=0;jrow;j++)coutsnakeMap[i][j];coutendl;}Sleep(1);system(cls);}======================================================================/*Ö÷º¯Êý²¿·Ö*/#includeiostream#includesnake.h#includewindows.husingnamespacestd;intmain(){Csnakes(20);s.InitInstance();//s.ShowGame();intnoDead;do{s.ShowGame();noDead=s.UpdataGame();}while(noDead);system(pause);return0;}

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功