C++如何实现简易贪吃蛇游戏
作者
这篇文章主要介绍C++如何实现简易贪吃蛇游戏,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
C++实现建议贪吃蛇(不会闪屏幕)
使用vs2013完成。记录踏上游戏开发的道路。
效果图
代码
//2021.7.24.1贪吃蛇.cpp:定义控制台应用程序的入口点。 // #include"stdafx.h" #include<iostream> #include<list> #include<numeric> #include<algorithm> #include<Windows.h> #include<WinUser.h> #include<cstdlib> #include<ctime> #include<vector> usingnamespacestd; #defineKEY_DOWN(vk_code)(GetAsyncKeyState(vk_code)&0x8000?1:0) #defineMAX_SNAKE_LEN20 #defineMAP_MAXIMUM_HEIGHT20 #defineMAP_MAXIMUM_WIDTH20 #defineMAX_NUMBER_FRUIT5 structsSnakeBody { voidsetPostion(intx,inty) { nSnakeBodyX=x; nSnakeBodyY=y; } voidsetPostion(sSnakeBody*temp) { nSnakeBodyX=temp->nSnakeBodyX; nSnakeBodyY=temp->nSnakeBodyY; } intnSnakeBodyX; intnSnakeBodyY; }; typedeflist<sSnakeBody>LISTSNAKEBODY; structsSnake { sSnake() { nSnakeHeadX=1; nSnakeHeadY=1; nSnakeDirection=1; speed=5; } boolisExit(sSnakeBodytemp) { if(nSnakeHeadX==temp.nSnakeBodyX&&nSnakeHeadY==temp.nSnakeBodyY) { returntrue; } returnfalse; } boolisExit(intx,inty) { if(nSnakeHeadX==x&&nSnakeHeadY==y) { returntrue; } returnfalse; } voidreduction(sSnakeBodytemp) { nSnakeHeadX=2*nSnakeHeadXBk-temp.nSnakeBodyX; nSnakeHeadY=2*nSnakeHeadYBk-temp.nSnakeBodyY; } intnSnakeHeadX; intnSnakeHeadY; intnSnakeHeadXBk; intnSnakeHeadYBk; intnSnakeDirection;//0表示上,1表示右,2表示下,3表示左,顺时针 intspeed;//指的是几个循环前进一次 LISTSNAKEBODYsnakeBodyList[MAX_SNAKE_LEN]; }; structsFruit { intFruitX; intFruitY; sFruit() { FruitX=-1; FruitY=-1; } sFruit(intx,inty) { FruitX=x; FruitY=y; } boolisExit(sSnakesnake) { if(FruitX==snake.nSnakeHeadX&&FruitY==snake.nSnakeHeadY) returntrue; elsereturnfalse; } }; vector<sFruit>gFruitVector; sSnakegSnake; //显示地图 voidshowArrMap(intarrMap[MAP_MAXIMUM_HEIGHT][MAP_MAXIMUM_WIDTH]) { system("cls"); for(inti=0;i<MAP_MAXIMUM_HEIGHT;i++) { for(intj=0;j<MAP_MAXIMUM_WIDTH;j++) { if(1==arrMap[i][j]||gSnake.isExit(i,j)) { cout<<"■"; } elseif(arrMap[i][j]==2) { cout<<"●"; } else { cout<<""; } } if(4==i) { cout<<"\t得分:"<<gSnake.snakeBodyList->size(); } cout<<endl; } } //将数据加载到地图上。返回值为加载后游戏是否失败 boolmapLoadData(intarrMap[MAP_MAXIMUM_HEIGHT][MAP_MAXIMUM_WIDTH]) { //加载水果的数据 for(unsignedinti=0;i<gFruitVector.size();i++) { arrMap[gFruitVector[i].FruitX][gFruitVector[i].FruitY]=2; } //加载蛇的身体数据 for(LISTSNAKEBODY::iteratoriter=gSnake.snakeBodyList->begin();iter!=gSnake.snakeBodyList->end();iter++) { arrMap[iter->nSnakeBodyX][iter->nSnakeBodyY]=1; } //吃到第一个水果的时候,一定不是游戏失败 if(gSnake.snakeBodyList->size()==1&&gSnake.isExit(gSnake.snakeBodyList->front())) { returnfalse; } if(arrMap[gSnake.nSnakeHeadX][gSnake.nSnakeHeadY]==1) returntrue; returnfalse; } //更新蛇的数据 voidupdateSnake(int&nProgramCounter) { if(nProgramCounter>gSnake.speed) { gSnake.nSnakeHeadXBk=gSnake.nSnakeHeadX; gSnake.nSnakeHeadYBk=gSnake.nSnakeHeadY; if(gSnake.nSnakeDirection==0) { gSnake.nSnakeHeadX--; } elseif(gSnake.nSnakeDirection==1) { gSnake.nSnakeHeadY++; } elseif(gSnake.nSnakeDirection==2) { gSnake.nSnakeHeadX++; } elseif(gSnake.nSnakeDirection==3) { gSnake.nSnakeHeadY--; } if(gSnake.snakeBodyList->size()!=0) { if(gSnake.isExit(gSnake.snakeBodyList->front())) { gSnake.reduction(gSnake.snakeBodyList->front()); } } //蛇的身体移动 if(gSnake.snakeBodyList->size()!=0) { sSnakeBodysnakeBody; snakeBody.setPostion(gSnake.nSnakeHeadXBk,gSnake.nSnakeHeadYBk); gSnake.snakeBodyList->push_front(snakeBody); gSnake.snakeBodyList->pop_back(); } nProgramCounter=0; } } //绑定键盘事件 voidbindKeyboardEvents() { if(KEY_DOWN(VK_UP)) { gSnake.nSnakeDirection=0; } elseif(KEY_DOWN(VK_RIGHT)) { gSnake.nSnakeDirection=1; } elseif(KEY_DOWN(VK_DOWN)) { gSnake.nSnakeDirection=2; } elseif(KEY_DOWN(VK_LEFT)) { gSnake.nSnakeDirection=3; } if(KEY_DOWN(VK_SHIFT)) { gSnake.speed=2; } else { gSnake.speed=5; } } //随机生成N个水果, voidrandomlyGeneratedFruit(intN,intarrMap[MAP_MAXIMUM_HEIGHT][MAP_MAXIMUM_WIDTH]) { intmaxi=MAP_MAXIMUM_HEIGHT; intmaxj=MAP_MAXIMUM_WIDTH; intAllCnt=0; vector<int>randMap; for(inti=0;i<maxi;i++) { for(intj=0;j<maxj;j++) { if(0==arrMap[i][j]) { randMap.push_back(i*maxj+j); AllCnt++; } } } if(AllCnt<N) { N=AllCnt; } for(inti=0;i<N;i++) { inttemp=(rand()%AllCnt--); randMap.erase(randMap.begin()+temp); sFruitfruitTemp=sFruit(temp/maxi+1,temp%(maxi-2)+1); gFruitVector.push_back(fruitTemp); } } //初始化地图 voidinitArrMap(intarrMap[MAP_MAXIMUM_HEIGHT][MAP_MAXIMUM_WIDTH]) { for(inti=0;i<MAP_MAXIMUM_HEIGHT;i++) { for(intj=0;j<MAP_MAXIMUM_WIDTH;j++) { if(i==0||j==0||i==MAP_MAXIMUM_HEIGHT-1||j==MAP_MAXIMUM_WIDTH-1) arrMap[i][j]=1; else arrMap[i][j]=0; } } } //初始化水果数据 voidinitFruit(intarrMap[MAP_MAXIMUM_HEIGHT][MAP_MAXIMUM_WIDTH]) { randomlyGeneratedFruit(MAX_NUMBER_FRUIT,arrMap); } //蛇头吃到水果事件 voideatFruitEvent(intarrMap[MAP_MAXIMUM_HEIGHT][MAP_MAXIMUM_WIDTH]) { for(unsignedinti=0;i<gFruitVector.size();i++) { if(gFruitVector[i].isExit(gSnake)) { sSnakeBodysnakeBodyTemp; if(gSnake.snakeBodyList->size()==0) { snakeBodyTemp.setPostion(gSnake.nSnakeHeadX,gSnake.nSnakeHeadY); } else { snakeBodyTemp.setPostion(&gSnake.snakeBodyList->back()); } gSnake.snakeBodyList->push_back(snakeBodyTemp); gFruitVector.erase(gFruitVector.begin()+i); randomlyGeneratedFruit(1,arrMap); } } } int_tmain(intargc,_TCHAR*argv[]) { HANDLEhOutput; COORDcoord={0,0}; hOutput=GetStdHandle(STD_OUTPUT_HANDLE); //创建新的缓冲区 HANDLEhOutBuf=CreateConsoleScreenBuffer( GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CONSOLE_TEXTMODE_BUFFER, NULL ); //设置新的缓冲区为活动显示缓冲 SetConsoleActiveScreenBuffer(hOutBuf); //隐藏两个缓冲区的光标 CONSOLE_CURSOR_INFOcci; cci.bVisible=0; cci.dwSize=1; SetConsoleCursorInfo(hOutput,&cci); SetConsoleCursorInfo(hOutBuf,&cci); //双缓冲处理显示 DWORDbytes=0; chardata[3200]; //地图,需要将数据映射到地图上,再去渲染地图。 intarrMap[MAP_MAXIMUM_HEIGHT][MAP_MAXIMUM_WIDTH]; srand(time(NULL)); //计数器。 intnProgramCounter=0; //初始化各个数据 initArrMap(arrMap); initFruit(arrMap); while(true) { nProgramCounter++; initArrMap(arrMap); bindKeyboardEvents(); updateSnake(nProgramCounter); eatFruitEvent(arrMap); boolbIsWin=mapLoadData(arrMap); showArrMap(arrMap); if(bIsWin) { cout<<endl<<"游戏失败!!!"; ReadConsoleOutputCharacterA(hOutput,data,3200,coord,&bytes); WriteConsoleOutputCharacterA(hOutBuf,data,3200,coord,&bytes); break; } ReadConsoleOutputCharacterA(hOutput,data,3200,coord,&bytes); WriteConsoleOutputCharacterA(hOutBuf,data,3200,coord,&bytes); } system("pause"); return0; }
以上是“C++如何实现简易贪吃蛇游戏”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注恰卡编程网行业资讯频道!
目录
推荐阅读
0 条评论
本站已关闭游客评论,请登录或者注册后再评论吧~