标签:
#include<iostream> #include<windows.h> #include<time.h> #include<conio.h> #define HEIGHT 20 #define WIDTH 20 using namespace std; void setMap(char maps[HEIGHT][WIDTH]) { int i,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]=‘ ‘; } } void showMap(char maps[HEIGHT][WIDTH]) { int i,j; for(i=0;i<HEIGHT;i++){ cout << "\t"; for(j=0;j<WIDTH;j++) cout<<maps[i][j]<<‘ ‘; // cout<<endl; } } void countdown() { int i,j; long start; int timeover=1; for(i=2;i>=0;i--) { start=clock(); while(clock()-start<=1000); system("cls"); cout<<"The game is going to START! "<<i<<"s"<<endl; } } void initialization(int pos[2][100]) { for(int i=0;i<4;i++) { pos[0][i]=1; pos[1][i]=i+1; } } void setFood(char maps[HEIGHT][WIDTH],int *x,int *y) { } int main() { countdown();//倒计时 char maps[HEIGHT][WIDTH]; setMap(maps);//设置地图 int tcsPos[2][100]; initialization(tcsPos);//初始化蛇的位置 int i,j; int head=3,tail=0; for(i=1;i<=3;i++) maps[1][i]=‘*‘; maps[1][4]=‘#‘; //初始化蛇身 int x1,y1; srand(time(0)); do { x1=rand()%20+1; y1=rand()%20+1; }while(maps[x1][y1]!=‘ ‘); maps[x1][y1]=‘*‘; system("cls"); showMap(maps);//画地图+蛇 char direction = 77;//右 int x,y; while(1) { int timeover=1; long start=clock(); while((timeover=(clock()-start<=900))&&!kbhit()); if(timeover){ getch();direction=getch(); } switch(direction){ case 72:x=tcsPos[0][head]-1;y=tcsPos[1][head];break;//上 case 80:x=tcsPos[0][head]+1;y=tcsPos[1][head];break;//下 case 75:x=tcsPos[0][head];y=tcsPos[1][head]-1;break;//左 case 77:x=tcsPos[0][head];y=tcsPos[1][head]+1;;//右 } if(!(direction==72||direction==80||direction==75 ||direction==77)) // 按键非方向键 {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 //画米粒 { x1=rand()%20+1; y1=rand()%20+1; }while(maps[x1][y1]!=‘ ‘); maps[x1][y1]=‘*‘; system("cls"); showMap(maps); } else { maps[tcsPos[0][tail]][tcsPos[1][tail]]=‘ ‘; tail=(tail+1)%100; maps[tcsPos[0][head]][tcsPos[1][head]]=‘*‘; head=(head+1)%100; tcsPos[0][head]=x; tcsPos[1][head]=y; maps[tcsPos[0][head]][tcsPos[1][head]]=‘#‘; system("cls"); showMap(maps); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/minemine/p/4296035.html