textout(handle,40,18,wColors+2,1,"按a开始游戏");
textout(handle,40,20,wColors+2,1,"按c退出");
}
}
chardir=_getch();
if(dir=='r')
{system("cls");
break;
}
}
elseif(choice=='c')//退出游戏
exit(0);
else
continue;
}
}
for(intrx=2;rx<=62;)//显示边框30*20紫色
{
textout(handle,rx,1,wColors,1,"■");
textout(handle,rx,21,wColors,1,"■");
rx+=2;
}
for(intry=1;ry<=21;ry++)
{
textout(handle,2,ry,wColors,1,"■");
textout(handle,62,ry,wColors,1,"■");
}
snake.length=2;//蛇的初始长度为2
snake.s_x[0]=22;//蛇头的初始坐标
snake.s_y[0]=7;
snake.s_x[1]=20;
snake.s_y[1]=8;
fruit.f_x=14;//水果的初始坐标
fruit.f_y=6;
pfruit.p_x[0]=12;//毒果的初始坐标
pfruit.p_y[0]=15;
intscore;//分数
snake.direct[0]=80;//蛇运动的初始方向
inta(0);//判断毒果产生的参数
textout(handle,12,15,wColors+4,1,"★");
textout(handle,66,5,wColors+2,1,"SCORE:
");
textout(handle,65,7,wColors+3,1,"●是水果");
textout(handle,65,9,wColors+4,1,"★是毒果");
textout(handle,65,19,wColors+3,1,"制作人:
王波");
textout(handle,65,11,wColors+2,1,"按任意键暂停");
while
(1)//蛇身运动和增长
{
if(snake.direct[0]==75)
{
textout(handle,--(--snake.s_x[0]),snake.s_y[0],wColors+3,1,"◆");
Sleep(speed);
PRODUCT_FRUIT(snake.s_x,snake.s_y,&snake.length,&fruit.f_x,&fruit.f_y,wColors+1,handle);//产生水果
MOVE(snake.s_x,snake.s_y,snake.length,wColors+2,handle);
}
elseif(snake.direct[0]==77)
{
textout(handle,++(++snake.s_x[0]),snake.s_y[0],wColors+3,1,"◆");
Sleep(speed);
PRODUCT_FRUIT(snake.s_x,snake.s_y,&snake.length,&fruit.f_x,&fruit.f_y,wColors+1,handle);
MOVE(snake.s_x,snake.s_y,snake.length,wColors+2,handle);
}
elseif(snake.direct[0]==72)
{
textout(handle,snake.s_x[0],--snake.s_y[0],wColors+3,1,"◆");
Sleep(speed);
PRODUCT_FRUIT(snake.s_x,snake.s_y,&snake.length,&fruit.f_x,&fruit.f_y,wColors+1,handle);
MOVE(snake.s_x,snake.s_y,snake.length,wColors+2,handle);
}
elseif(snake.direct[0]==80)
{
textout(handle,snake.s_x[0],++snake.s_y[0],wColors+3,1,"◆");
Sleep(speed);
PRODUCT_FRUIT(snake.s_x,snake.s_y,&snake.length,&fruit.f_x,&fruit.f_y,wColors+1,handle);
MOVE(snake.s_x,snake.s_y,snake.length,wColors+2,handle);
}
else
continue;
while(_kbhit())
{
DIR=_getch();DIR=_getch();
switch(DIR)
{
case75:
if(snake.direct[0]!
=77)
snake.direct[0]=75;
break;
case77:
if(snake.direct[0]!
=75)
snake.direct[0]=77;
break;
case72:
if(snake.direct[0]!
=80)
snake.direct[0]=72;
break;
case80:
if(snake.direct[0]!
=72)
snake.direct[0]=80;
break;
default:
break;
}
}
inttotal=snake.length%5;//产生毒果
inttemp=snake.length/5;
if(total==0&&temp==a+1)
{
PRODUCT_POISON_FRUIT(snake.s_x,snake.s_y,&snake.length,&fruit.f_x,&fruit.f_y,pfruit.p_x,pfruit.p_y,wColors,handle);
a++;
}
score=(snake.length-2)*10;
charbuf[10];
itoa(score,buf,10);
textout(handle,72,5,wColors+2,1,buf);
if(DEAD(snake.s_x,snake.s_y,snake.length,pfruit.p_x,pfruit.p_y,handle)==1)//判断蛇死亡的函数
{
system("cls");
textout(handle,27,7,wColors+1,1,"GAMEOVER");
textout(handle,27,10,wColors+1,1,"SCORE:
");
textout(handle,27,13,wColors+2,1,"制作人:
王波");
textout(handle,34,10,wColors+1,1,buf);
for(intx=2;x<=62;)//显示边框30*20紫色
{
textout(handle,x,1,wColors+3,1,"■");
textout(handle,x,21,wColors+3,1,"■");
Sleep(20);
x+=2;
}
for(inty=1;y<=21;y++)
{
textout(handle,2,y,wColors+3,1,"■");
textout(handle,62,y,wColors+3,1,"■");
Sleep(20);
}
break;
}
}
}
#include
#include
#include
#include
#include"colorConsole.h"
voidRE_FRUIT(int*foodx,int*foody,int*length,intsnakex[],intsnakey[]);
voidPRODUCT_FRUIT(intsnakex[],intsnakey[],int*length,int*foodx,int*foody,WORDwColors[],HANDLEhandle);
voidMOVE(intsnakex[],intsnakey[],intlen,WORDwColors[],HANDLEhandle);
intDEAD(intsnakex[],intsnakey[],intlen,intpfoodx[],intpfoody[],HANDLEhandle);
voidRE_POISON_FRUIT(intpfoodx[],intpfoody[],int*foodx,int*foody,int*length,intsnakex[],intsnakey[]);
voidPRODUCT_POISON_FRUIT(intsnakex[],intsnakey[],int*length,int*foodx,int*foody,intpfoodx[],intpfoody[],WORDwColors[],HANDLEhandle);
#include
#include
#include
#include
#include
#include"colorConsole.h"
voidMOVE(intsnakex[],intsnakey[],intlen,WORDwColors[],HANDLEhandle)
{
for(inti=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,"");
}
}
intn;
voidRE_FRUIT(int*foodx,int*foody,int*length,intsnakex[],intsnakey[])//布置水果的位置
{
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;
}
}
}
}
voidPRODUCT_FRUIT(intsnakex[],intsnakey[],int*length,int*foodx,int*foody,WORDwColors[],HANDLEhandle)//布置水果位置
{
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,"●");
}
}
intnum=1;
inti;
intq;
voidRE_POISON_FRUIT(intpfoodx[],intpfoody[],int*foodx,int*foody,int*length,intsnakex[],intsnakey[])//布置毒果的位置
{
while
(1)
{
srand((unsigned)time(NULL)*(unsigned)time(NULL));
intpoison_x=2*(rand()%28+2);
intpoison_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;
}
}
}
}
}
intp_n=1;
voidPRODUCT_POISON_FRUIT(intsnakex[],intsnakey[],int*length,int*foodx,int*foody,intpfoodx[],intpfoody[],WORDwColors[],HANDLEhandle)//布置毒果位置
{
RE_POISON_FRUIT(pfoodx,pfoody,foodx,foody,length,snakex,snakey);
textout(handle,pfoodx[p_n],pfoody[p_n],wColors+4,1,"★");
p_n++;
}
intDEAD(intsnakex[],intsnakey[],intlen,intpfoodx[],intpfoody[],HANDLEhandle)
{
inttemp;
for(inti=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(intb=0;b<=num;b++)
{
if((snakex[0]==pfoodx[b])&&(snakey[0]==pfoody[b]))
{
temp=1;
break;
}
}
if(temp==1)
return1;
else
return0;
}
#include
#include
;
HANDLEinitiate();
BOOLtextout(HANDLEhOutput,intx,inty,WORDwColors[],intnColors,LPTSTRlpszString);
#include"colorConsole.h"
HANDLEinitiate()
{
HANDLEhOutput;
hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
returnhOutput;
}
BOOLtextout(HANDLEhOutput,intx,inty,WORDwColors[],intnColors,LPTSTRlpszString)
{