#include <iostream>#include<windows.h>#include<time.h>#include<conio.h>#defineHEIGHT 20#defineWIDTH 20using namespacestd;voidSetmap (CharMaps[height][width]) { inti,j; for(i=0; i<=width-1; i++) {maps[0][i]='-'; Maps[height-1][i]='-'; } for(j=1; j<=height-2; j + +) {maps[j][0]='|'; Maps[j][width-1]='|'; } for(i=1; i<=height-2; i++) { for(j=1; j<=width-2; j + +) Maps[i][j]=' '; }}voidShowmap (CharMaps[height][width]) { inti,j; for(i=0; i) {cout<<"\ t"; for(j=0; j<width;j++) cout<<maps[i][j]<<' ';//cout<<Endl; }}voidcountdown () {inti,j; Longstart; intTimeover=1; for(i=2; i>=0; i--) {Start=clock (); while(Clock ()-start<= +); System ("CLS"); cout<<"The game is going to start!"<<i<<"s"<<Endl; }}voidInitialization (intpos[2][ -]){ for(intI=0;i<4; i++) {pos[0][i]=1; pos[1][i]=i+1; }}voidSetfood (CharMaps[height][width],int*x,int*y) {}intMain () {countdown ();//Countdown CharMaps[height][width]; Setmap (maps);//set up a map inttcspos[2][ -]; Initialization (TCSPOS);//Initialize the location of the snake inti,j; intHead=3, tail=0; for(i=1; i<=3; i++) maps[1][i]='*'; maps[1][4]='#';//Initialize the body of a snake intx1,y1; Srand (Time (0)); Do{x1=rand ()% -+1; Y1=rand ()% -+1; } while(maps[x1][y1]!=' '); MAPS[X1][Y1]='*'; System ("CLS"); Showmap (maps);//Draw map + snake CharDirection = the;//Right intx, y; while(1) { intTimeover=1; Longstart=clock (); while(Timeover= (Clock ()-start<= the)) &&!Kbhit ()); if(timeover) {getch ();d irection=Getch (); } Switch(direction) { Case the: x=tcspos[0][head]-1; y=tcspos[1][head]; Break;//on Case the: x=tcspos[0][head]+1; y=tcspos[1][head]; Break;//under Case the: x=tcspos[0][head];y=tcspos[1][head]-1; Break;//left Case the: x=tcspos[0][head];y=tcspos[1][head]+1;;//Right } if(! (direction== the|| direction== the|| direction== the|| direction== the))//key non-directional key{cout <<"\tgame over!"<< Endl;return 0;} if(maps[x][y]!=' '&&! (x==x1&&y==y1)) {cout<<"\tgame over!"<<endl;return 0; } if(x==x1&&y==y1) {Maps[x][y]='#'; maps[tcspos[0][head]][tcspos[1][head]]='*'; Head=head+1; tcspos[0][head]=x; tcspos[1][head]=y; Do //painting Rice{x1=rand ()% -+1; Y1=rand ()% -+1; } while(maps[x1][y1]!=' '); MAPS[X1][Y1]='*'; System ("CLS"); Showmap (maps); } Else{maps[tcspos[0][tail]][tcspos[1][tail]]=' '; Tail= (tail+1)% -; maps[tcspos[0][head]][tcspos[1][head]]='*'; Head= (head+1)% -; tcspos[0][head]=x; tcspos[1][head]=y; maps[tcspos[0][head]][tcspos[1][head]]='#'; System ("CLS"); Showmap (maps); } } return 0;}
Beginners Learn C + +: To be the most simple snake game