贪吃蛇.docx

上传人:b****9 文档编号:23412090 上传时间:2023-05-16 格式:DOCX 页数:18 大小:17.67KB
下载 相关 举报
贪吃蛇.docx_第1页
第1页 / 共18页
贪吃蛇.docx_第2页
第2页 / 共18页
贪吃蛇.docx_第3页
第3页 / 共18页
贪吃蛇.docx_第4页
第4页 / 共18页
贪吃蛇.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

贪吃蛇.docx

《贪吃蛇.docx》由会员分享,可在线阅读,更多相关《贪吃蛇.docx(18页珍藏版)》请在冰豆网上搜索。

贪吃蛇.docx

贪吃蛇

#include

#include

#include

#include

#include

#include

#include"colorConsole.h"

#include"game.h"

intspeed;//蛇的移动速度

externconstintf_t=200;//等级一的移动速度

externconstints_t=120;//等级二的移动速度

charDIR;//蛇移动方向的暂存值

structSNAKE//创造蛇的结构

{

ints_x[250];

ints_y[250];

intlength;

chardirect[4];

}snake;

structFRUIT//创造水果的结构

{

intf_x;

intf_y;

}fruit;

structPFRUIT//创造毒果的结构

{

intp_x[50];

intp_y[50];

}pfruit;

voidmain()

{

HANDLEhandle=initiate();

WORDwColors[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(intx=2;x<=62;)//显示边框30*20紫色

{

textout(handle,x,1,wColors,1,"■");

textout(handle,x,21,wColors,1,"■");

x+=2;

}

for(inty=1;y<=21;y++)

{

textout(handle,2,y,wColors,1,"■");

textout(handle,62,y,wColors,1,"■");

}

textout(handle,22,6,wColors+3,1,"请选择游戏选项:

");//创造游戏选项

textout(handle,22,8,wColors+3,1,"a.开始游戏");

textout(handle,22,10,wColors+3,1,"b.帮助");

textout(handle,22,12,wColors+3,1,"c.退出游戏");

charchoice;

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;

}

elseif(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;

}

elseif(choice!

='1'&&choice!

='2')

continue;

}

break;

}

elseif(choice=='b')//未完成

{

system("cls");

chare;

ifstreamifile("help.txt");

if(ifile)

{

while(ifile.good())

{

ifile.get(e);

if(e=='\0')

break;

cerr<

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)

{

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1