#include
#include
#include
#include
#include
#include
#include"colorConsole.h"
#include"game.h"
int speed; // 蛇的移动速度
extern const int f_t=200; //等级一的移动速度
extern const int s_t=120; //等级二的移动速度
char DIR; //蛇移动方向的暂存值
struct SNAKE //创造蛇的结构
{
int s_x[250];
int s_y[250];
int length;
char direct[4];
}snake;
struct FRUIT //创造水果的结构
{
int f_x;
int f_y;
}fruit;
struct PFRUIT //创造毒果的结构
{
int p_x[50];
int p_y[50];
}pfruit;
void main()
{
HANDLE handle=initiate();
WORD wColors[5];
wColors[0]=FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY; //紫色
wColors[1]=FOREGROUND_RED|FOREGROUND_INTENSITY; //红色
wColors[2]=FOREGROUND_BLUE|FOREGROUND_INTENSITY; //蓝色
wColors[3]=FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY; //黄色
wColors[4]=FOREGROUND_GREEN; //绿色
for (int x=2;x<=62;) //显示边框 30*20 紫色
{
textout(handle,x,1,wColors,1,"■");
textout(handle,x,21,wColors,1,"■");
x+=2;
}
for (int y=1;y<=21;y++)
{
textout(handle,2,y,wColors,1,"■");
textout(handle,62,y,wColors,1,"■");
}
textout(handle,22,6,wColors+2,1,"请选择游戏选项:"); //创造游戏选项
textout(handle,22,8,wColors+2,1,"a.开始游戏");
textout(handle,22,10,wColors+2,1,"b.帮助");
textout(handle,22,12,wColors+2,1,"c.退出游戏");
char choice;
while(1)
{
if(_kbhit())
{
choice=_getch();
if(choice=='a') //开始游戏选择等级
{
system("cls");
textout(handle,22,6,wColors+2,1,"请选择游戏等级:");
textout(handle,32,8,wColors+2,1,"1.等级一");
textout(handle,32,10,wColors+2,1,"2.等级二");
while(1)
{
choice=_getch();
if(choice=='1')
{
textout(handle,22,6,wColors+2,1,"开始游戏,等级一");
textout(handle,32,8,wColors+2,1," ");
textout(handle,32,10,wColors+2,1," ");
speed=f_t;
Sleep(2000);
textout(handle,22,6,wColors+2,1," ");
break;
}
else if(choice=='2')
{
textout(handle,22,6,wColors+2,1,"开始游戏,等级二");
textout(handle,32,8,wColors+2,1," ");
textout(handle,32,10,wColors+2,1," ");
speed=s_t;
Sleep(2000);
textout(handle,22,6,wColors+2,1," ");
break;
}
else if(choice!='1'&&choice!='2')
continue;
}
break;
}
else if(choice=='b') //未完成
{
system("cls");
char e;
ifstream ifile("help.txt");
if(ifile)
{
while(ifile.good())
{
ifile.get(e);
if(e=='\0')
break;
cerr<函数#
{
system("cls");
textout(handle,27,7,wColors+1,1," GAME OVER");
textout(handle,27,10,wColors+1,1," SCORE:");
textout(handle,27,13,wColors+2,1,"制作人:王波");
textout(handle,34,10,wColors+1,1,buf);
for (int x=2;x<=62;) //显示边框 30*20 紫色
{
textout(handle,x,1,wColors+3,1,"■");
textout(handle,x,21,wColors+3,1,"■");
Sleep(20);
x+=2;
}
for (int y=1;y<=21;y++)
{
textout(handle,2,y,wColors+3,1,"■");
textout(handle,62,y,wColors+3,1,"■");
Sleep(20);
}
break;
}
}
}
//game.h
#include
#include
#include
#include
#include"colorConsole.h"
void RE_FRUIT(int *foodx,int *foody,int *length,int snakex[],int snakey[]);
void PRODUCT_FRUIT(int snakex[],int snakey[],int *length,int *foodx, int *foody,WORD wColors[],HANDLE handle);
void MOVE(int snakex[],int snakey[],int len,WORD wColors[],HANDLE handle);
int DEAD(int snakex[],int snakey[],int len,int pfoodx[],int pfoody[],HANDLE handle);
void RE_POISON_FRUIT(int pfoodx[],int pfoody[],int *foodx,int *foody,int *length,int snakex[],int snakey[]);
void PRODUCT_POISON_FRUIT(int snakex[],int snakey[],int *length,int *foodx,int *foody,int pfoodx[],int pfoody[],WORD wColors[],HANDLE handle) ;
//game.cpp
#include
#include
#include
#include
#include
#include"colorConsole.h"
void MOVE(int snakex[],int snakey[],int len,WORD wColors[],HANDLE handle)
{
for(int i=len;i>0;i--)
{
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
textout(handle,snakex[i],snakey[i],wColors,1,"●");
textout(handle,snakex[len],snakey[len],wColors,1," ");
}
}
int n;
void RE_FRUIT(int *foodx,int *foody,int *length,int snakex[],int snakey[]) //布置水果的位置
{
while(1)
{
srand((unsigned)time(NULL));
*foodx=2*(rand()%28+2);
*foody=rand()%18+2;
if(*foodx>=4&&*foody>=2) //检测水果位置是否正确
{
for( n=0;n<*length;n++)
{
if((snakex[n]==*foodx)&&(snakey[n]==*foody))
break;
}
if ( n==*length ) // 上面for循环完全执行没有break, 则退出while循环
{
break;
}
}
}
}
void PRODUCT_FRUIT(int snakex[],int snakey[],int *length,int *foodx,int *foody,WORD wColors[],HANDLE handle) //布置水果位置
{
textout(handle,*foodx,*foody,wColors+2,1,"●");
if(snakex[0]==*foodx&&snakey[0]==*foody)
{
*length=*length+1;
RE_FRUIT(foodx,foody,length,snakex,snakey);
textout(handle,*foodx,*foody,wColors+2,1,"●");
}
}
int num=1;
int i;
int q;
void RE_POISON_FRUIT(int pfoodx[],int pfoody[],int *foodx,int *foody,int *length,int snakex[],int snakey[]) //布置毒果的位置
{
while(1)
{
srand((unsigned)time(NULL)*(unsigned)time(NULL));
int
poison_x=2*(rand()%28+2);
int poison_y=rand()%20;
if(poison_x>=4&&poison_y>=2) //检测毒果位置是否正确
{
for(q=0;q<*length;q++)
{
if((snakex[q]==poison_x)&&(snakey[q]==poison_y))
break;
}
if(q==*length)
{
for(i=0;i<30;i++)
{
if(pfoodx[i]==*foodx&&pfoody[i]==*foody)
break;
}
if(i==30)
{
pfoodx[num]=poison_x;
pfoody[num]=poison_y;
num++;
break;
}
}
}
}
}
int p_n=1;
void PRODUCT_POISON_FRUIT(int snakex[],int snakey[],int *length,int *foodx,int *foody,int pfoodx[],int pfoody[],WORD wColors[],HANDLE handle) //布置毒果位置
{
RE_POISON_FRUIT(pfoodx,pfoody,foodx,foody,length,snakex,snakey);
textout(handle,pfoodx[p_n],pfoody[p_n],wColors+4,1,"★");
p_n++;
}
int DEAD(int snakex[],int snakey[],int len,int pfoodx[],int pfoody[],HANDLE handle)
{
int temp;
for(int i=2;i<=len;i++)
{
if((snakex[0]<4||snakex[0]>60||snakey[0]<2||snakey[0]>20)||(snakex[0]==snakex[i]&&snakey[0]==snakey[i]))
{
temp=1;
break;
}
}
for( int b=0;b<=num;b++)
{
if((snakex[0]==pfoodx[b])&&(snakey[0]==pfoody[b]))
{
temp=1;
break;
}
}
if(temp==1)
return 1;
else
return 0;
}