博主在上一篇的代码中做了一些修改,使得贪吃蛇能够穿过边界并且将地图的长宽都换成了变量,这样既可自定义地图可以做出诺基亚上的战役模式。为了区分没有墙的边界与控制台背景,可以修改地图底色,虽然颜色搭配实在不忍直视。
#include <iostream>
#include <windows.h>
#include <cstring>
#include <time.h>
#include <conio.h>
using namespace std;
int error(char x,char y)
{
if(x<y)
if((y-x==3)||(y-x==4))
return 1;
if(x>y)
if((x-y==3)||(x-y==4))
return 1;
if(x==y)
return 1;
return 0;
}
//纠正强行自噬
int Pos(long x,long y)
{
HANDLE direct=GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos={x,y};
SetConsoleCursorPosition(direct,pos);
}
//改变光标位置
int Color(long clr)
{
HANDLE direct=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(direct,clr);
}
//改变输出颜色
int Show(long judge)
{
HANDLE direct=GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(direct,&cci);
cci.bVisible=judge;
SetConsoleCursorInfo(direct,&cci);
}
//是否显示光标
int main()
{
char map[99][99];
char Key,Keys;
long xh,yh,xt,yt,x0,y0;
long start,timeover,level;
long X,Y;
long hight=20,weight=20;
memset(map,' ',sizeof(map));
for(