/*------------------------------
贪吃蛇
------------------------------/
#include
#include
#include
#include
#include
#define uint unsigned int
#define _X 10
#define _Y 5
#define WIDTH 60
#define HEIGHT 16
#define U 0
#define R 1
#define D 2
#define L 3
#define SUC 0
#define EAT 1
#define BUMP 2
using namespace std;
/*--------------点的结构体 ----------------*/
struct PT
{
int x;
int y;
};
/*--------------结点的结构体 ----------------*/
struct Body
{
PT now;
Body last;
Body *next;
};
/--------------蛇的结构体 ----------------*/
struct Snake
{
int dir;
Body *head;
Body *tail;
uint bodylong;
}snake;
/*--------------全局变量 ----------------*/
HANDLE hStdout=GetStdHandle(STD_OUTPUT_HANDLE);
char obj='H',snk='O',food=1;
PT foodpt;
uint n,score,level=150;
unsigned char key;
char *msg;
char l;
/*--------------长延时----------------*/
void delay(uint time)
{
for(int k=0;k<time/10;k++)
{
Sleep(10);
if(kbhit()) key=getch();
}
}
/*--------------获取随机点坐标----------------*/
PT randpt()
{
PT pt;
pt.x=rand()%(WIDTH-1)+1+_X;
pt.y=rand()%(HEIGHT-1)+1+_Y;
return pt;
}
/*--------------隐藏光标----------------*/
void hidecursor()
{
CONSOLE_CURSOR_INFO cursor_info;
cursor_info.bVisible=false;
cursor_info.dwSize=20;
SetConsoleCursorInfo(hStdout,&cursor_info);
}
/*--------------开始gotoxy一系列的函数定义----------------*/
void gotoxy(int x,int y,char ch)
{
COORD c;
c.X = x - 1;
c.Y = y - 1;
SetConsoleCursorPosition (hStdout,c);
putchar(ch);
}
void gotoxy(int x,int y,char* ch)
{
COORD c;
c.X = x - 1;
c.Y = y - 1;
SetConsoleCursorPosition (hStdout,c);
puts(ch);
}
void gotoxy(PT pt,char ch)
{
COORD c;
c.X = pt.x - 1;
c.Y = pt.y - 1;
SetConsoleCursorPosition (hStdout,c);
putchar(ch);
}
void gotoxy(PT pt,char* ch)
{
COORD c;
c.X = pt.x - 1;
c.Y = pt.y - 1;
SetConsoleCursorPosition (hStdout,c);
puts(ch);
}
void gotoxy(PT pt)
{
COORD c;
c.X = pt.x - 1;
c.Y = pt.y - 1;
SetConsoleCursorPosition (hStdout,c);
}
void gotoxy(int x,int y)
{
COORD c;
c.X = x - 1;
c.Y = y - 1;
SetConsoleCursorPosition (hStdout,c);
}
/*--------------结束----------------*/
/*--------------清除一行的内容----------------*/
void clraline(int y)
{
gotoxy(1,y);
for(int k=0;k<70;k++)
{
putchar(' ');
}
}
/*--------------获取按键值给key 并返回键值----------------*/
char getkey()
{
key=getch();
return key;
}
/*--------------等待按键弹起----------------*/
void waitforkeyup()
{
while(kbhit()) Sleep(20);
}
/*--------------判断点是否在蛇身上----------------*/
bool isinsnk(PT pt)
{
Body *temp;
temp=snake.head;
while(temp!=NULL)
{
if(temp->now.x==pt.x&&temp->now.y==pt.y) return true;
temp=temp->next;
}
return false;
}
/*--------------初始化函数---------------*/
void init()
{
Body *temp,*temp2;
hidecursor();
system("cls");
gotoxy(_X+24,1,"By PL23K");
temp=snake.tail;
while(temp!=NULL)
{
temp2=temp;
temp=temp->last;
delete temp2;
}
srand((int)time(0));
score=0;
for(n=0;n<=WIDTH;n++)
{
gotoxy(_X+n,_Y,obj);
gotoxy(_X+n,_Y+HEIGHT,obj);
}
for(n=0;n<=HEIGHT;n++)
{
gotoxy(_X,_Y+n,obj);
gotoxy(_X+WIDTH,_Y+n,obj);
}
snake.dir=R;
temp=new Body;
snake.head=temp;
snake.head->now.x=WIDTH/2+_X;
snake.head->now.y=HEIGHT/2+_Y;
snake.head->last=NULL;
temp=NULL;
temp=new Body;
snake.head->next=temp;
snake.head->next->now.x=snake.head->now.x-1;
snake.head->next->now.y=snake.head->now.y;
snake.head->next->last=snake.head;
temp=NULL;
temp=new Body;
snake.head->next->next=temp;
snake.tail=snake.head->next->next;
snake.tail->last=snake.head->next;
snake.tail->next=NULL;
snake.tail->now.x=snake.tail->last->now.x-1;
snake.tail->now.y=snake.tail->last->now.y;
snake.bodylong=3;
temp=snake.head;
while(temp!=NULL)
{
gotoxy(temp->now.x,temp->now.y,snk);
temp=temp->next;
}
gotoxy(_X+13,_Y-2,"Please chooce level : 1 2 3 ");
l=getch();
while(l!='1'&&l!='2'&&l!='3')
{
Sleep(20);
l=getch();
}
switch(l)
{
case 1 : level=350;
case 2 : level=250;
case 3 : level=150;
}
waitforkeyup();
clraline(_Y-2);
gotoxy(_X+10,_Y+HEIGHT+1,"Press \'F\' to start----Press \'Q\' to quit");
foodpt=randpt();
while(isinsnk(foodpt)) {foodpt=randpt();}
gotoxy(foodpt,food);
}
/*--------------画个蛇的启动画面----------------*/
void drawsnk()
{
char c[10]=" ";
gotoxy(1,1);
cout<<c<<"\n";
cout<<c<<" /^\/^\"<<endl;
cout<<c<<" |_| O|"<<endl;
cout<<c<<" \/ /~ \_/ \"<<endl;
cout<<c<<" \____|__________/ \"<<endl;
cout<<c<<" \_______ \"<<endl;
cout<<c<<" `\ \ \"<<endl;
cout<<c<<" | | \"<<endl;
cout<<c<<" / / \"<<endl;
cout<<c<<" / / \\"<<endl;
cout<<c<<" / / \ \"<<endl;
cout<<c<<" / / \ \"<<endl;
cout<<c<<" / / ---- \ \"<<endl;
cout<<c<<" / / -~ ~- | |"<<endl;
cout<<c<<" ( ( -~ _-- ~-_ / |"<<endl;
cout<<c<<" \ ~-___-~ -~ ~- ~-_-~ /"<<endl;
cout<<c<<" ~-_ -~ ~- -~ "<<endl;
cout<<c<<" ~--_____-~ ~-___-~"<<endl;
gotoxy(27,22,"上: W 下:S 左:A 右:D");
while(!kbhit())
{
gotoxy(1,20);
cout<<c<<c<<c<<" Press any key to enter";
Sleep(200);
gotoxy(1,20);
cout<<c<<c<<c<<c<<c<<c<<c<<c;
Sleep(200);
}
}
/*--------------移动蛇----------------*/
int move()
{
PT next;
int statu;
Body *temp;
switch(snake.dir)
{
case U :
next.x=snake.head->now.x;
next.y=snake.head->now.y-1;
break;
case R :
next.x=snake.head->now.x+1;
next.y=snake.head->now.y;
break;
case L :
next.x=snake.head->now.x-1;
next.y=snake.head->now.y;
break;
case D :
next.x=snake.head->now.x;
next.y=snake.head->now.y+1;
break;
}
if(next.x==foodpt.x&&next.y==foodpt.y)
{
score+=10;
snake.bodylong++;
temp=new Body;
temp->last=NULL;
temp->next=snake.head;
snake.head->last=temp;
temp->now=next;
snake.head=temp;
gotoxy(next,snk);
statu=EAT;
}
else if(next.x>=WIDTH+_X||next.x<=_X||next.y>=HEIGHT+_Y||next.y<=_Y||isinsnk(next))
{
statu=BUMP;
}
else
{
snake.bodylong++;
temp=new Body;
temp->last=NULL;
temp->next=snake.head;
snake.head->last=temp;
temp->now=next;
snake.head=temp;
temp=snake.tail;
snake.tail=snake.tail->last;
snake.tail->next=NULL;
gotoxy(next,snk);
gotoxy(temp->now,' ');
delete temp;
statu=SUC;
}
return statu;
}
/*--------------主函数----------------*/
int main(int argc, char *argv[])
{
int statu;
snake.head=NULL;
snake.tail=NULL;
drawsnk();
while(1)
{
init();
statu=SUC;
getkey();
while(key!='f'&&key!='q')
{
Sleep(20);
getkey();
}
if(key=='f')
{
clraline(_Y+HEIGHT+1);
gotoxy(_X+10,_Y+HEIGHT+1,"Start! Press \'Q\' to quit,\'R\' to restart");
waitforkeyup();
while(key!='r'&&key!='q'&&statu!=BUMP)
{
waitforkeyup();
switch(key)
{
case 'w' : if(snake.dir!=D) snake.dir=U;break;
case 's' : if(snake.dir!=U) snake.dir=D;break;
case 'd' : if(snake.dir!=L) snake.dir=R;break;
case 'a' : if(snake.dir!=R) snake.dir=L;break;
}
gotoxy(_X+11,_Y-2);
cout<<"Your score is: "<<score<<" You are choose level"<<l;
statu=move();
if(statu==EAT)
{
foodpt=randpt();
while(isinsnk(foodpt)) {foodpt=randpt();}
gotoxy(foodpt,food);
}
delay(level);
}
}
else
{
break;
}
}
clraline(_Y-1);
gotoxy(_X+26,_Y-1,"GAME OVER");
waitforkeyup();
getch();
return EXIT_SUCCESS;
}
/*--------------OVER----------------*/