#include<bits/stdc++.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#include <time.h>
#include <dos.h>
#define Speed 4
#define High 29
#define Width 116
#define F_long 8
#define Setwall 5 //设置障碍数
using namespace std;
int canvas[High][Width]; //显示游戏画布
//0是空格,-1是边框#,1是蛇头@
// >1是蛇身*
int speed;
int iskill;
int up_x,up_y; //升级点
int wall_x,wall_y; //墙
int Long;
int move; //数字表示方向
void gotoxy(int x, int y) //光标移动到x,y位置
{
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);// 获取标准输出设备句柄
SetConsoleCursorPosition(hOut, pos); //两个参数分别是指定哪个窗体,具体位置
}
void HideCursor() // 用于隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0}; // 第二个值为0表示隐藏光标
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);//函数和结构体都在windows.h中定义。
}
void setup()
{
up_x=rand()%High;
up_y=rand()%Width;
}
void setwall()
{
wall_x=rand()%High;
wall_y=rand()%Width;
for(int i=0;i<High;i++)
{
for(int x=0;x<Width;x++)
{
if(wall_x==i&&wall_x==x)
{
if(canvas[i][x]!=0)
{
setwall();
i=0;x=0;
break;
}
}
}
}
canvas[wall_x][wall_y]=-1;
}
void checkup() //检查升级点
{
for(int i=0;i<High;i++)
{
for(int x=0;x<Width;x++)
{
if(up_x==i&&up_y==x)
{
if(canvas[i][x]!=0)
{
// canvas[up_x][up_y]=0;
setup();
i=0;x=0;
break;
}
}
}
}
canvas[up_x][up_y]=-2;
}
void Move_she()
{
// int old_x,old_y;
int out=0;
for(int i=1;i<High-1;i++)
{
for(int x=1;x<Width-1;x++)
{
if(canvas[i][x]>0)
canvas[i][x]++;
if(canvas[i][x]>
Long+1) canvas[i][x]=0;
}
}
for(int i=1;i<High-1;i++)
{
for(int x=1;x<Width-1;x++)
{
if(canvas[i][x]==2)
{
if(move==1)
{
if(canvas[i-1][x]==-2)
{
Long++;
canvas[i-1][x]=1;
checkup();
for(int p=0;p<Setwall;p++)
setwall();
break;
}
if(canvas[i-1][x]==0)
canvas[i-1][x]=1;
else iskill=1;
}
if(move==2)
{
if(canvas[i][x+1]==-2)
{
Long++;
canvas[i][x+1]=1;
checkup();
for(int p=0;p<Setwall;p++)
setwall();
break;
}
if(canvas[i][x+1]==0)
canvas[i][x+1]=1;
else iskill=1;
}
if(move==3)
{
if(canvas[i+1][x]==-2)
{
Long++;
canvas[i+1][x]=1;
checkup();
for(int p=0;p<Setwall;p++)
setwall();
break;
}
if(canvas[i+1][x]==0)
canvas[i+1][x]=1;
else iskill=1;
}
if(move==4)
{
if(canvas[i][x-1]==-2)
{
Long++;
canvas[i][x-1]=1;
checkup();
for(int p=0;p<Setwall;p++)
setwall();
break;
}
if(canvas[i][x-1]==0)
canvas[i][x-1]=1;
else iskill=1;
}
}
// if(out==1) break;
}
// if(out==1) break;
}
}
void startup()
{
srand(time(NULL));
HideCursor();
speed=0;
iskill=0;
Long=F_long;
memset(canvas,0,sizeof(canvas));
//初始化边框
for(int i=0;i<High;i++)
{
canvas[i][0]=-1;
canvas[i][Width-1]=-1;
}
for(int i=0;i<Width;i++)
{
canvas[0][i]=-1;
canvas[High-1][i]=-1;
}
//初始化小蛇
canvas[High/2][Width/2]=1;
for(int i=1;i<=F_long;i++)
canvas[High/2][Width/2-i]=1+i;
//设置升级点
setup();
checkup();
}
void show()
{
gotoxy(0,0);
for(int i=0;i<High;i++)
{
for(int j=0;j<Width;j++)
{
if(canvas[i][j]==-1)
cout<<'#';
if(canvas[i][j]==0)
cout<<' ';
if(canvas[i][j]==1)
cout<<'@';
if(canvas[i][j]>1)
cout<<'*';
if(canvas[i][j]==-2)
cout<<'$';
}
printf("\n");
}
cout<<"当前长度"<<Long;
if(iskill==1)
{
cout<<endl<<"呜呜,失败了\n"<<
"输入空格退出"<<endl;
char input;
while(1)
{
input=getch();
if(input==' ')
break;
}
// system("pause");
exit(0);
}
// printf("*");
}
void updateWithoutInput()
{
if(move!=0) Move_she();
}
void updateWithInput()
{
char input;
if(kbhit())
{
input=getch();
if(input=='a') move=4;
if(input=='w') move=1;
if(input=='d') move=2;
if(input=='s') move=3;
if(input==27) system("pause");
}
// Sleep(50);
}
int main()
{
startup(); //数据初始化
while(1) //游戏循环执行
{
show(); //游戏画面
updateWithoutInput(); //与输入无关更新
updateWithInput(); //与输入有关更新
}
return 0;
}