自己第一個控制台的遊戲——貪吃蛇

來源:互聯網
上載者:User

標籤:c++   貪吃蛇   控制台   遊戲   

一直想自己寫個遊戲玩玩,好像很厲害的樣子,終於今天下定決心寫了個最經典的休閑的小遊戲——貪吃蛇,當然也有借鑒別人的程式,但是整個代碼都是和別人不一樣的,直接上代碼吧:

#include <conio.h>#include <iostream>#include <vector>#include <time.h>using namespace std;#define ROW 22#define COL 22struct Point{char ch;int x;int y;};void Refresh(vector<Point> vec1,vector<Point> vec2,Point pt,int grade,int speed);void move(vector<Point> &vec,int dir,int touch,bool &flag);Point produce_food(vector<Point> vec);bool IsGameOver(vector<Point> vec1,vector<Point> vec2);void main(){cout<<endl<<endl<<endl;cout<<"\t"<<"遊戲即將開始!";int start=clock();while(clock()-start<=1000);for(int i=3;i>=0;i--){start=clock();while(clock()-start<=1000);system("cls");cout<<endl<<endl;cout<<"\t"<<i<<endl;}Point pt;vector<Point> snake;int length=1,head=3,tail=0;for(int i=0;i<3;i++){pt.ch='*';pt.x=1;pt.y=i+1;snake.push_back(pt);}pt.ch='#';pt.x=1;pt.y=4;snake.push_back(pt);vector<Point> env;for(int i=0;i<ROW;i++){for(int j=0;j<COL;j++){if(i==0 || i==ROW-1){pt.ch='-';pt.x=i;pt.y=j;env.push_back(pt);}if(i!=0 && i!=ROW-1){if(j==0 || j==COL-1){pt.ch='|';pt.x=i;pt.y=j;env.push_back(pt);}else{pt.ch=' ';pt.x=i;pt.y=j;env.push_back(pt);}}}}int direction=77;int speed=500,grade=1;int timeover=1;int touch=0;Point food;srand(time(0));food=produce_food(env);bool flag=true;while(1){touch=0;start=clock();while((timeover=(clock()-start<=speed)) && !kbhit());if(timeover) {getch();direction=getch();}if(food.x==(snake.end()-1)->x && food.y==(snake.end()-1)->y) {touch=1;length++;food=produce_food(env);}if(length>=8) {length-=8;grade++;speed-=50;}move(snake,direction,touch,flag);if(IsGameOver(env,snake) && flag){Refresh(env,snake,food,grade,speed);}else{cout<<"game over"<<endl;exit(1);}}}void Refresh(vector<Point> vec1,vector<Point> vec2,Point pt ,int grade,int speed){system("cls");for(vector<Point>::iterator ite2=vec2.begin();ite2<vec2.end();ite2++){for(vector<Point>::iterator ite1=vec1.begin();ite1<vec1.end();ite1++){if(ite1->x==ite2->x && ite1->y==ite2->y) ite1->ch=ite2->ch;}}for(vector<Point>::iterator ite=vec1.begin();ite<vec1.end();ite++){if(ite->x==pt.x && ite->y==pt.y) ite->ch='$';cout<<ite->ch<<' ';if(ite->x==4 && ite->y==COL-1) cout<<"等級為:"<<grade;if(ite->x==6 && ite->y==COL-1) cout<<"時間間隔:"<<speed;if(ite->x==8 && ite->y==COL-1) cout<<"開發人員:dapeng dai";if(ite->y==COL-1) cout<<endl;}}void move(vector<Point> &vec,int dir,int touch,bool &flag){Point pt;vector<Point>::iterator ite=vec.end()-1;switch(dir){case 77:ite->ch='*';pt.x=(ite->x);pt.y=(ite->y)+1;pt.ch='#';for(vector<Point>::iterator it=vec.begin();it<vec.end();it++){if(it->x==pt.x && it->y==pt.y) flag=false;}vec.push_back(pt);if(!touch) vec.erase(vec.begin());break;case 75:ite->ch='*';pt.x=(ite->x);pt.y=(ite->y)-1;pt.ch='#';for(vector<Point>::iterator it=vec.begin();it<vec.end();it++){if(it->x==pt.x && it->y==pt.y) flag=false;}vec.push_back(pt);if(!touch) vec.erase(vec.begin());break;case 72:ite->ch='*';pt.x=(ite->x)-1;pt.y=(ite->y);pt.ch='#';for(vector<Point>::iterator it=vec.begin();it<vec.end();it++){if(it->x==pt.x && it->y==pt.y) flag=false;}vec.push_back(pt);if(!touch) vec.erase(vec.begin());break;case 80:ite->ch='*';pt.x=(ite->x)+1;pt.y=(ite->y);pt.ch='#';for(vector<Point>::iterator it=vec.begin();it<vec.end();it++){if(it->x==pt.x && it->y==pt.y) flag=false;}vec.push_back(pt);if(!touch) vec.erase(vec.begin());break;default:break;}}Point produce_food(vector<Point> vec){int x=0,y=0;do {x=rand()%(ROW-2)+1;y=rand()%(COL-2)+1;} while ((vec.begin()+(ROW*x+y))->ch!=' ');Point pt;pt.x=x;pt.y=y;pt.ch='$';return pt;}bool IsGameOver(vector<Point> vec1,vector<Point> vec2){if((vec2.end()-1)->x==0 ||(vec2.end()-1)->x==ROW-1 || (vec2.end()-1)->y==0 || (vec2.end()-1)->y==COL-1){return false;}return true;}
程式寫的有點長,但是可以完美運行了

希望大家多多指教。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.