#include<iostream>
#include<fstream>
#include<windows.h>
#include<conio.h>
#include<vector>
#include"gotoxy.h"
using namespace std;
int V; //游戏速度
int X=0,Y=0; //蛇的坐标
int N=3; //蛇的初始长度
int fx,fy; //食物坐标
int MAX=0; //记录最高分
class Game_map
{
public:
Game_map();
private:
int x,y; //画地图的坐标
};
class sbody
{
public:
sbody() {body=3;}
int x,y; //蛇身的坐标
char body; //蛇身
};
class control
{
public:
control() {};
void order();
void gameover();
void logo();
private:
int button;
};
void control::logo()
{
int x=3,y=5;
while(x<=45)
{
gotoxy(x,y);
cout<<" * * * * * * * * * *"<<endl;
gotoxy(x,y+1);
cout<<" * *"<<endl;
gotoxy(x,y+2);
cout<<" * *"<<endl;
gotoxy(x,y+3);
cout<<" * *"<<endl;
gotoxy(x,y+4);
cout<<" * *"<<endl;
gotoxy(x,y+5);
cout<<" * * * * * * * * * *"<<endl;
gotoxy(x,y+6);
cout<<" * * "<<endl;
gotoxy(x,y+7);
cout<<" * * "<<endl;
gotoxy(x,y+8);
cout<<" * * "<<endl;
gotoxy(x,y+9);
cout<<" * * "<<endl;
gotoxy(x,y+10);
cout<<" * * "<<endl;
gotoxy( x,y+11);
cout<<" * * "<<endl;
gotoxy(x,y+12);
cout<<" * * * * "<<endl;
x++;
Sleep(20);
}
x=3,y=5;
while(x<=20)
{
gotoxy(x,y);
cout<<" * * "<<endl;
gotoxy(x,y+1);
cout<<" * * "<<endl;
gotoxy(x,y+2);
cout<<" * * "<<endl;
gotoxy(x,y+3);
cout<<" * * "<<endl;
gotoxy(x,y+4);
cout<<" * * "<<endl;
gotoxy(x,y+5);
cout<<" * * "<<endl;
gotoxy(x,y+6);
cout<<" * * "<<endl;
gotoxy(x,y+7);
cout<<" * * "<<endl;
gotoxy(x,y+8);
cout<<" * * "<<endl;
gotoxy(x,y+9);
cout<<" * * "<<endl;
gotoxy(x,y+10);
cout<<" * * "<<endl;
gotoxy(x,y+11);
cout<<" * ** * * * * * * * *"<<endl;
gotoxy(x,y+12);
cout<<" * ** * * * * * * * *"<<endl;
x++;
Sleep(10);
}
x=3,y=3;
while(1)
{
gotoxy (x++,y);
Sleep(10);
cout<<"*";
if(x==75)
{
cout<<endl;
break;
}
}
while(1)
{
gotoxy(x,y++);
Sleep(10);
cout<<"*";
if(y==22)
{
cout<<endl;
break;
}
}
while(1)
{
gotoxy(x--,y);
Sleep(10);
cout<<"*";
if(x==3)
{
cout<<endl;
break;
}
}
while(1)
{
gotoxy(x,y--);
Sleep(10);
cout<<"*";
if(y==3)
break;
}
gotoxy(33,20);
system("pause");
}
vector<sbody> Sbody(N);
void control::gameover()
{
system("cls");
gotoxy(5,5);
cout<<" ================================================="<<endl;
cout<<" || ||"<<endl;
cout<<" || @ @ @@@@@ @ @ @ # @@@@ @ ||"<<endl;
cout<<" || @ @ @ @ @ @ @ @ @ ||"<<endl;
cout<<" || @ @ @ @ @ @@@@ @ @@@@ @@@@ ||"<<endl;
cout<<" || @ @ @ @ @ @ @ @ @ @ @ ||"<<endl;
cout<<" || @ @@@@@ @@@@@ @@@@ @ @@@@ @@@@ ||"<<endl;
cout<<" ================================================="<<endl;
getch();
if(N>MAX)
{
MAX=N;
}
if(V==60)
{
ofstream ou("最高分2.txt",ios::out);
ou<<MAX;
ou.close();
}
else if(V==30)
{
ofstream ou("最高分3.txt",ios::out);
ou<<MAX;
ou.close();
}
else
{
ofstream ou("最高分.txt",ios::out);
ou<<MAX;
ou.close();
}
exit(0);
}
void control::order()
{
int px=4,py=5,p2x=5,p2y=6,x,y;
char point=26;
x=0,y=5;
while(1)
{
gotoxy(x++,y);
cout<<"设置颜色";
if(x>5)
{
break;
}
Sleep(200);
}
x=4;y=3;
while(1)
{
gotoxy(x++,y);
cout<<"选择游戏难度";
Sleep(50);
if(x>=50)
{
gotoxy(--x,y);
cout<<" ";
break;
}
}
x++;y=7;
while(1)
{
gotoxy(x--,y);
cout<<"选择游戏难度";
Sleep(50);
if(x<5)
break;
}
x=50;y=9;
while(1)
{
gotoxy(x--,y);
cout<<"游戏开始";
Sleep(50);
if(x==5)
break;
}
gotoxy(4,5);
cout<<point;
FLAG:
gotoxy(5,5);
cout<<"设置颜色";
gotoxy(5,7);
cout<<"选择游戏难度";
gotoxy(5,9);
cout<<"游戏开始";
gotoxy(5,11);
cout<<"不想玩了";
while(1)
{
F1:
button=getch();
switch(button)
{
case 72: //上
if(py!=5)
{
system("cls");
gotoxy(px,py-=2);
cout<<point;
goto FLAG;
}
else
{
system("cls");
gotoxy(px,py);
cout<<point;
goto FLAG;
}
if(kbhit()!=0) //判断按键的函数,如果kbhit()函数的返回值不等0则有键按下
{
goto F1;
}break;
case 80: //下
if(py!=11)
{
system("cls");
gotoxy(px,py+=2);
cout<<point;
goto FLAG;
}
else
{
system("cls");
gotoxy(px,py);
cout<<point;
goto FLAG;
}
if(kbhit()!=0)
{
goto F1;
}break;
case 13: //回车键
system("cls");
if(py==5)
{
int cor;
gotoxy(5,6);
cout<<point;
FFAG:
gotoxy(6,6);
cout<<"选择这里是紫色";
gotoxy(6,8);
cout<<"选择这里是绿色";
gotoxy(6,10);
cout<<"选择这里是蓝色";
gotoxy(6,12);
cout<<"选择这里是黑色";
while(1)
{
F2:
cor=getch();
switch(cor)
{
case 72: //上
if(p2y!=6)
{
system("cls");
gotoxy(p2x,p2y-=2);
cout<<point;
goto FFAG;
}
else
{
system("cls");
gotoxy(p2x,p2y);
cout<<point;
goto FFAG;
}
if(kbhit()!=0)
{
goto F2;
}
break;
case 80: //下
if(p2y!=12)
{
system("cls");
gotoxy(p2x,p2y+=2);
cout<<point;
goto FFAG;
}
else
{
system("cls");
gotoxy(p2x,p2y);
cout<<point;
goto FFAG;
}
if(kbhit()!=0)
{
goto F2;
} break;
case 13:
system("cls");
if(p2y==6)
{
system("color D");
goto FLAG;
}
if(p2y==8)
{
system("color 2");
goto FLAG;
}
if(p2y==10)
{
system("color 9");
goto FLAG;
}
if(p2y==12)
{
system("color f0");
goto FLAG;
} break;
}
}
}
else if(py==7)
{
int gread,p3x=5,p3y=6;
gotoxy(5,6);
cout<<point;
FLAG3:
gotoxy(6,6);
cout<<"没玩过";
gotoxy(6,8);
cout<<"略懂~略懂~";
gotoxy(6,10);
cout<<"大师,你懂吗?";
while(1)
{
F3:
gread=getch();
switch(gread)
{
case 72: //上
if(p3y!=6)
{
system("cls");
gotoxy(p3x,p3y-=2);
cout<<point;
goto FLAG3;
}
else
{
system("cls");
gotoxy(p3x,p3y);
cout<<point;
goto FLAG3;
}
if(kbhit()!=0)
{
goto F3;
}
case 80: //下
if(p3y!=10)
{
system("cls");
gotoxy(p3x,p3y+=2);
cout<<point;
goto FLAG3;
}
else
{
system("cls");
gotoxy(p3x,p3y);
cout<<point;
goto FLAG3;
}
if(kbhit()!=0)
{
goto F3;
}
//回车键
case 13:
system("cls");
if(p3y==6)
{
V=100;
goto FLAG;
}
if(p3y==8)
{
V=60;
goto FLAG;
}
if(p3y==10)
{
V=30;
goto FLAG;
}
}
}
}
else if(py==11)
{
gotoxy(32,10);
cout<<"谢谢体验本游戏"<<endl;
exit(0);
}
else if(py==9)
{
return;
} break;
}
}
}
void food();
void week()
{
control ctr;
if(V==0)
{
V=100;
}
gotoxy(8,4); //在蛇头初始坐标输出蛇
cout<<Sbody[1].body<<Sbody[0].body;
int key,i;
while(1)
{
flag:
key=getch();
switch(key)
{
case 75: //左
while(1)
{
Sleep(V);
for(i=N-1;i>0;i--)
{ //先把前面的坐标给后面的,然后面的跟进
Sbody[i].x=Sbody[i-1].x; //然后进行对头部++或者--的操作,是蛇头移动,循环后,后面的
Sbody[i].y=Sbody[i-1].y; //身子也会动,从而构成了蛇在走的动画。
}
Sbody[0].x--;
for(i=3;i<N;i++)
{
if((Sbody[0].x==Sbody[i].x)&&(Sbody[0].y==Sbody[i].y))
{
ctr.gameover();
}
}
if(Sbody[0].x>=60||Sbody[0].x<=1||Sbody[0].y>=23||Sbody[0].y<=0)
{
ctr.gameover();
}
for(i=0;i<N;i++)
{
gotoxy(Sbody[i].x,Sbody[i].y);
cout<<Sbody[i].body;
}
if(Sbody[0].x==fx&&Sbody[0].y==fy)
{
N++;
sbody j;
Sbody.push_back(j);
Sbody[N-1].body=' ';
Sbody[N-2].body=3;
food();
}
if(kbhit()!=0)
{
goto flag;
}
}
break;
case 77: //右
while(1)
{
Sleep(V);
for(i=N-1;i>0;i--)
{
Sbody[i].x=Sbody[i-1].x;
Sbody[i].y=Sbody[i-1].y;
}
Sbody[0].x++;
for(i=3;i<N;i++)
{
if((Sbody[0].x==Sbody[i].x)&&(Sbody[0].y==Sbody[i].y))
{
ctr.gameover();
}
}
if(Sbody[0].x>=60||Sbody[0].x<=1||Sbody[0].y>=23||Sbody[0].y<=0)
{
ctr.gameover();
}
for(i=0;i<N;i++)
{
gotoxy(Sbody[i].x,Sbody[i].y);
cout<<Sbody[i].body;
}
if(Sbody[0].x==fx&&Sbody[0].y==fy)
{
N++;
sbody j;
Sbody.push_back(j);
Sbody[N-1].body=' ';
Sbody[N-2].body=3;
food();
}
if(kbhit()!=0)
{
goto flag;
}
}
break;
case 72: //上
while(1)
{
Sleep(V);
for(i=N-1;i>0;i--)
{
Sbody[i].x=Sbody[i-1].x;
Sbody[i].y=Sbody[i-1].y;
}
Sbody[0].y--;
for(i=3;i<N;i++)
{
if((Sbody[0].x==Sbody[i].x)&&(Sbody[0].y==Sbody[i].y))
{
ctr.gameover();
}
}
if(Sbody[0].x>=60||Sbody[0].x<=1||Sbody[0].y>=23||Sbody[0].y<=0)
{
ctr.gameover();
}
for(i=0;i<N;i++)
{
gotoxy(Sbody[i].x,Sbody[i].y);
cout<<Sbody[i].body;
}
if(Sbody[0].x==fx&&Sbody[0].y==fy)
{
N++;
sbody j;
Sbody.push_back(j);
Sbody[N-1].body=' ';
Sbody[N-2].body=3;
food();
}
if(kbhit()!=0)
{
goto flag;
}
}
break;
case 80: //下
while(1)
{
Sleep(V);
for(i=N-1;i>0;i--)
{
Sbody[i].x=Sbody[i-1].x;
Sbody[i].y=Sbody[i-1].y;
}
Sbody[0].y++;
for(i=3;i<N;i++)
{
if((Sbody[0].x==Sbody[i].x)&&(Sbody[0].y==Sbody[i].y))
{
ctr.gameover();
}
}
if(Sbody[0].x>=60||Sbody[0].x<=1||Sbody[0].y>=23||Sbody[0].y<=0)
{
ctr.gameover();
}
for(i=0;i<N;i++)
{
gotoxy(Sbody[i].x,Sbody[i].y);
cout<<Sbody[i].body;
}
if(Sbody[0].x==fx&&Sbody[0].y==fy)
{
N++;
sbody j;
Sbody.push_back(j);
Sbody[N-1].body=' ';
Sbody[N-2].body=3;
food();
}
if(kbhit()!=0)
{
goto flag;
}
}
break;
}
}
}
Game_map::Game_map()
{
x=1;
y=0;
while(1)
{
gotoxy (x++,y);
Sleep(10);
cout<<"*";
if(x==60)
{
cout<<endl;
break;
}
}
while(1)
{
gotoxy(x,y++);
Sleep(10);
cout<<"*";
if(y==23)
{
cout<<endl;
break;
}
}
while(1)
{
gotoxy(x--,y);
Sleep(10);
cout<<"*";
if(x==1)
{
cout<<endl;
break;
}
}
while(1)
{
gotoxy(x,y--);
Sleep(10);
cout<<"*";
if(y==0)
break;
}
}
void food()
{
int i=0;
char Food=3;
while(1)
{
flag2:
fx=rand()%57+2;
fy=rand()%20+2;
for(i=0;i<N;i++)
{
if(fx==Sbody[i].x&&fy==Sbody[i].y)
{
goto flag2;
}
}
gotoxy(fx,fy);
cout<<Food;
gotoxy(65,3);
cout<<"按空格键游戏暂停";
gotoxy(61,5);
cout<<"~~~~~~~~~~~~~~~~~~~~~";
gotoxy(65,7);
if(MAX-3<0)
{MAX=0;}
else
MAX=MAX-3;
cout<<"历史最高纪录是"<<MAX;
gotoxy(65,9);
cout<<"现在的得分是:"<<(N-3);
gotoxy(61,11);
cout<<"~~~~~~~~~~~~~~~~~~~~~";
break;
}
}
int main()
{
system("mode con cols=82 lines=25");
control con;
con.logo();
system("cls");
con.order();
Sbody[0].body=1; //设置舌头的图案
Sbody[N-1].body=' '; //蛇尾
Sbody[0].x=8; //舌头的初始坐标
Sbody[0].y=4;
if(V==60)
{
ifstream in("最高分2.txt",ios::in);
in>>MAX;
in.close();
}
else if(V==30)
{
ifstream in("最高分3.txt",ios::in);
in>>MAX;
in.close();
}
else
{
ifstream in("最高分.txt",ios::in);
in>>MAX;
in.close();
}
Game_map map;
food();
week();
return 0;
}
贪吃蛇的备份