#include #include #include #include #include #define X 23//地图的x轴 #define Y 75//地图的y轴 #define UP 0 #define DOWN 1 #define LEFT 2 #define RIGHT 3 #define WAIT_TIME 180//等待蛇刷新的时间,可以说是速度 修改可变速 int map_0[X][Y];//地图 int Snake[X*Y][2]; // 蛇 int Slength; //蛇的长度 int direction; int score=0; bool pdEatFood=false; void csh(); void huaMap(); void huaSnake(); void gotoxy(int x,int y); void move(); void intokey(); int check(int x,int y); void putfood(); bool gameover(); void dy_fs(); int main() { csh(); huaMap(); putfood(); Sleep(5000); while(1) { huaSnake(); Sleep(WAIT_TIME); intokey(); move(); dy_fs(); if(gameover()) { system("cls"); //清除屏幕内容 printf("Game Over\n"); system("pause"); getchar(); break; } if(map_0[Snake[0][0]][Snake[0][1]]==-1) { map_0[Snake[0][0]][Snake[0][1]]=0; pdEatFood=true; putfood(); score+=1; } } return 0; } void csh() { //初始化 srand((unsigned)time(NULL)); //设置种子为现在的时间 Slength=4; gotoxy(0,0); CONSOLE_CURSOR_INFO cursor_info = {1, 0}; //清除光标 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); int x,y; Snake[0][0]=X/2; Snake[0][1]=Y/2; for(x=0; x0; i--) { //从尾巴开始,每一个点的位置等于它前面一个点的位置 Snake[i][0]=Snake[i-1][0]; Snake[i][1]=Snake[i-1][1]; } switch(direction) { case UP: Snake[0][0]--; break; case DOWN: Snake[0][0]++; break; case LEFT: Snake[0][1]--; break; case RIGHT: Snake[0][1]++; break; } if(pdEatFood) { Slength++; pdEatFood=false; } } void intokey() { if(kbhit()!=0) { //kbhit()函数 检查当前是否有键盘输入,若有则返回一个非0值,否则返回0 char in; while(!kbhit()==0) //如果玩家输入了多个按键,以最后一个按键为准 in=getch(); switch(in) { case 'w': case 'W': if(direction!=DOWN) //防止缩头 direction=UP; break; case 's': case 'S': if(direction!=UP) direction=DOWN; break; case 'a': case 'A': if(direction!=RIGHT) direction=LEFT; break; case 'd': case 'D': if(direction!=LEFT) direction=RIGHT; break; case 'p': case 'P': gotoxy(X,0); system("pause"); gotoxy(X,0); printf(" "); // 消去下面的按任意键继续 break; } } } int check(int ii,int jj) { // 检查是否能投放食物 if(map_0[ii][jj]==1) return 0; if(ii==0 || jj==0 || ii==X-1 || jj==Y-1) return 0; int i; for(i=0; i