将基础贪吃蛇代码进一步简化改进,可以自定义棋盘大小,可以自定义棋盘与控制台边界的距离,可以自定义墙面,蛇身,蛇头,食物的颜色。
#include <iostream>
#include <windows.h>
#include <cstring>
#include <time.h>
#include <conio.h>
#include <cstdio>
using namespace std;
const long Length=18; //The length of Map
const long Width=15; //The Width of Map
const long BLength=3; //The Length of Blank
const long BWidth=20; //The Width of Blank
bool Error(char x,char y)
{
switch(x-y)
{
case 3:case -3:case 4:case -4:case 0: return true;
}
return false;
}
//avoid abnormal self-eating and accelerating
void Pos(long x,long y)
{
HANDLE direct=GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos={x,y};
SetConsoleCursorPosition(direct,pos);
}
//Change the indicator's position
void Color(long clr)
{
HANDLE direct=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(direct,clr);
}
//Change the indicator's color
void Show(long judge)
{
HANDLE direct=GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(direct,&cci);
cci.bVisible=judge;
SetConsoleCursorInfo(direct,&cci);
}
//Hide the indicator
int main()
{
char Key,Keys;
long xh,yh,xt,yt,xm,ym;
long start,timeover,level;
long X,Y;
char map[Width][Length];
memset(map,' ',sizeof(map));
for(long i=0;i<Width;i++)
{
if((i==0)||(i==Width-1))
for(long j=0;j<Length;j++)
map[i][j]='b';
else
map[i][0]=map[i][Length-1]='b';
}
//Initialize the Map
xh=7,yh=9;
xt=7,yt=8;
map[xt][yt]='d';
//Initialize the Map
srand(time(0));
do{
xm=rand()%(Width-1)+1;
ym=rand()%(Length-1)+1;
}while(map[xm][ym]!=' '||(xm!=xh&&ym!=yh));
map[xm][ym]='m';
//Creat the food
Pos(22,8);
cout<<"Please input the level (1-8): ";
cin>>level;
//Input the level
Show(0);
X=BWidth;Y=BLength;
Pos(X,Y);
for(long i=0;i<Width;i++)
{
for(long j=0;j<Length;j++)
{
switch(map[i][j])
{
case 'b':
Color(0x02);
cout<<"■";
break;
case 'w':
case 'a':
case 's':
case 'd':
Color(0x0F);
cout<<"■";
break;
case 'm':
Color(0x03);
cout<<"■";
break;
case ' ':
Color(0x0F);
cout<<" ";
break;
}
}
Y++;
Pos(X,Y);
}
X=BWidth+yh*2;Y=BLength+xh;
Pos(X,Y);
Color(0x01);
cout<<"■";
//Output the Map
//'b' refers to the wall
//'w' 'a' 's' 'd' refers to the body of snake
//'m' refers to the food
Key='d';
while(1)
{
timeover=1;
start=clock();
while((timeover=(clock()-start<=(900-level*100)))&&!kbhit());
{
Keys=Key;
if(timeover)
{
Key=getch();
if(Error(Key,Keys))
{
Key=Keys;
continue;
}
}
//avoid abnormal self-eating and accelerating
X=BWidth+yh*2;Y=BLength+xh;
Pos(X,Y);
Color(0x0F);
cout<<"■";
switch(Key)
{
case 'W':case 'w':map[xh][yh]='w';xh--;break;
case 'S':case 's':map[xh][yh]='s';xh++;break;
case 'A':case 'a':map[xh][yh]='a';yh--;break;
case 'D':case 'd':map[xh][yh]='d';yh++;break;
}
if(map[xh][yh]!=' '&&map[xh][yh]!='m')
{
Pos(30,18);
cout<<"GameOver";
getch();
return 0;
}
//Game over
X=BWidth+yh*2;Y=BLength+xh;
Pos(X,Y);
Color(0x01);
cout<<"■";
//Move the head
if(map[xh][yh]=='m')
{
srand(time(0));
do{
xm=rand()%(Width-1)+1;
ym=rand()%(Length-1)+1;
}while(map[xm][ym]!=' '||(xm!=xh&&ym!=yh));
map[xm][ym]='m';
X=BWidth+ym*2;Y=BLength+xm;
Pos(X,Y);
Color(0x03);
cout<<"■";
//Creat the food
}
else
{
X=BWidth+yt*2;Y=BLength+xt;
Pos(X,Y);
Color(0x0F);
cout<<" ";
switch(map[xt][yt])
{
case 'w':map[xt][yt]=' ';xt--;break;
case 's':map[xt][yt]=' ';xt++;break;
case 'a':map[xt][yt]=' ';yt--;break;
case 'd':map[xt][yt]=' ';yt++;break;
}
}
//Move the tail
}
}
}