//==============================================================================================
#include<iostream>
#include<ctime>
#include<windows.h>
#include<conio.h>
//----------------------------------------------------------------------------------------------
using namespace std;
//---------------------------------------------------------------------------------------------
enum Direction
{
UP,
DOWN,
LEFT,
RIGHT
};
typedef struct Snake_Position //保存每一节蛇躯的位置信息,同时保存下一节蛇躯的位置
{
int x;
int y;
struct Snake_Position *next;
}Snake_P;
typedef struct Snake_Attribution
{
int length;
Direction Dir;
}Snake_A;
typedef struct Food_Position //保存食物的位置信息
{
int x;
int y;
}Food;
//----------------------------------------------------------------------------------------------
Snake_P *head;
Snake_A *Attribution;
Food *food;
int flag=1;
int speed=500; //速度
int score=0; //分数
//----------------------------------------------------------------------------------------------
void Hide_Cursor();
void Interface();
void Set_Cursor_Position(int x,int y); //设置光标位置
void Init_Snake(); //初始化蛇躯
void Init_Food();//初始化食物
void KeyBoard();
void Snake_Move();//蛇的行动控制
void show();
//----------------------------------------------------------------------------------------------
int main()
{
Interface();
while(flag)
{
KeyBoard();
Snake_Move();
show();
Sleep(speed);
}
if(flag==0)
{
Set_Cursor_Position(25,20);
cout<<" ";
Set_Cursor_Position(25,21);
cout<<" GAME OVER!!!"<<endl;
system("pause>nul");
}
return 0;
}//=============================================================================================
void Interface()
{
system("color 4E");
Hide_Cursor();
cout<<"|*|===============================|*|\n";
cout<<"|*| |*|\n";
cout<<"|*| |*|\n";
cout<<"|*| |*|\n";
cout<<"|*| |*|\n";
cout<<"|*| |*|\n";
cout<<"|*| |*|\n";
cout<<"|*| |*|\n";
cout<<"|*| |*|\n";
cout<<"|*| |*|\n";
cout<<"|*| |*|\n";
cout<<"|*| |*|\n";
cout<<"|*| |*|\n";
cout<<"|*| |*|\n";
cout<<"|*| |*|\n";
cout<<"|*| |*|\n";
cout<<"|*|===============================|*|\n";
Set_Cursor_Position(10,5);
cout<<"按任意键开始游戏!";
system("pause>nul");
Set_Cursor_Position(10,5);
cout<<" ";
Init_Snake();
Init_Food();
}//---------------------------------------------------------------------------------------------
void Hide_Cursor() //光标隐藏
{
HANDLE hCnd=GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO Cursor_Info={1,0};
SetConsoleCursorInfo(hCnd,&Cursor_Info);
}//---------------------------------------------------------------------------------------------
void Set_Cursor_Position(int x,int y) //设置光标位置
{
HANDLE hOut=GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X=x;
pos.Y=y;
SetConsoleCursorPosition(hOut,pos);
}//---------------------------------------------------------------------------------------------
void Init_Snake() //初始化蛇躯
{
Snake_P *p,*p1,*p2,*p3;
char ch=2;
Attribution=new Snake_A;
Attribution->length=3;
Attribution->Dir=RIGHT;
p1=new Snake_P;
p2=new Snake_P;
p3=new Snake_P;
head=p1;
p1->x=8;
p1->y=5;
p1->next=p2;
p2->x=7;
p2->y=5;
p2->next=p3;
p3->x=6;
p3->y=5;
p3->next=NULL;
p=head;
while(p)
{
Set_Cursor_Position(p->x,p->y);
cout<<ch;
p=p->next;
}
}//---------------------------------------------------------------------------------------------
void Init_Food() //初始化食物
{
char ch=3;
food=new Food;
Snake_P *p;
p=head;
srand((unsigned)time(NULL));
food->x=rand()%31+3;
food->y=rand()%15+1;
while(p!=NULL)
{
if( p->x==food->x && p->y==food->y)
{
food->x=rand()%31+3;
food->y=rand()%15+1;
}
else
p=p->next;
}
Set_Cursor_Position(food->x,food->y);
cout<<ch;
}//---------------------------------------------------------------------------------------------
void KeyBoard() //蛇的行动控制
{
char ch;
if(_kbhit())
{
ch=getch(); //getch()在conio.h中定义
switch(ch)
{
case 'W':
case 'w':
if(Attribution->Dir==DOWN)
break;
else Attribution->Dir=UP; break;
case 'S':
case 's':
if(Attribution->Dir==UP)
break;
else Attribution->Dir=DOWN; break;
case 'A':
case 'a':
if(Attribution->Dir==RIGHT)
break;
else Attribution->Dir=LEFT; break;
case 'D':
case 'd':
if(Attribution->Dir==LEFT)
break;
else Attribution->Dir=RIGHT; break;
case ' ':
Set_Cursor_Position(25,20);
cout<<"游戏暂停,按任意键恢复!";
Set_Cursor_Position(26,21);
cout<<" ";
system("pause>nul");
Set_Cursor_Position(25,20);
cout<<" ";break;
default:break;
}
}
}//---------------------------------------------------------------------------------------------
void Snake_Move()
{
Snake_P *t,*p,*p1,*p2,*p3;
char ch=2;
p3=head->next;
while(p3)
{
if(head->x<3 || head->x>33 || head->y<1 || head->y>15)
{
flag=0;
return;
}
if(head->x==p3->x && head->y==p3->y)
{
flag=0;
return;
}
else p3=p3->next;
}
if(Attribution->Dir==UP)
{
t=new Snake_P;
t->x=head->x;
t->y=head->y-1;
t->next=head;
head=t;
p1=p2=p=head;
if(head->x==food->x && head->y==food->y)
{
Set_Cursor_Position(food->x,food->y);
cout<<" ";
if(speed>100)
speed=speed-20;
score++;
while(p)
{
Set_Cursor_Position(p->x,p->y);
cout<<ch;
p=p->next;
}
Init_Food();
}
else
{
while(p2->next!=NULL)
{
p1=p2;
p2=p2->next;
}
p1->next=NULL;
Set_Cursor_Position(p2->x,p2->y);
cout<<" ";
delete p2;
while(p)
{
Set_Cursor_Position(p->x,p->y);
cout<<ch;
p=p->next;
}
}
}
if(Attribution->Dir==DOWN)
{
t=new Snake_P;
t->x=head->x;
t->y=head->y+1;
t->next=head;
head=t;
p1=p2=p=head;
if(head->x==food->x && head->y==food->y)
{
Set_Cursor_Position(food->x,food->y);
cout<<" ";
if(speed>100)
speed=speed-20;
score++;
while(p)
{
Set_Cursor_Position(p->x,p->y);
cout<<ch;
p=p->next;
}
Init_Food();
}
else
{
while(p2->next!=NULL)
{
p1=p2;
p2=p2->next;
}
p1->next=NULL;
Set_Cursor_Position(p2->x,p2->y);
cout<<" ";
delete p2;
while(p)
{
Set_Cursor_Position(p->x,p->y);
cout<<ch;
p=p->next;
}
}
}
if(Attribution->Dir==LEFT)
{
t=new Snake_P;
t->x=head->x-1;
t->y=head->y;
t->next=head;
head=t;
p1=p2=p=head;
if(head->x==food->x && head->y==food->y)
{
Set_Cursor_Position(food->x,food->y);
cout<<" ";
if(speed>100)
speed=speed-20;
score++;
while(p)
{
Set_Cursor_Position(p->x,p->y);
cout<<ch;
p=p->next;
}
Init_Food();
}
else
{
while(p2->next!=NULL)
{
p1=p2;
p2=p2->next;
}
p1->next=NULL;
Set_Cursor_Position(p2->x,p2->y);
cout<<" ";
delete p2;
while(p)
{
Set_Cursor_Position(p->x,p->y);
cout<<ch;
p=p->next;
}
}
}
if(Attribution->Dir==RIGHT)
{
用Visual C++6.0开发的贪吃蛇游戏(含注释)
需积分: 47 176 浏览量
2015-06-07
22:46:35
上传
评论 3
收藏 3.38MB ZIP 举报
qq_28561471
- 粉丝: 0
- 资源: 1