贪吃蛇c语言代码-贪吃蛇c语言代码500行( 三 )


if(timeover)
{
#if (DEBUG==1)
direction = getch();//调试代码
#else
if((direction =getch())==-32)
direction = getch();
#endif
}
#if (DEBUG==1)//调试代码
start=clock();
while(clock()-start=2000);
gotoxy(24,4);
cout "\t按键ASCII代码"(int)direction" "endl;
#endif
if(!(direction==72||direction==80||direction==75 ||direction==77))
{
otherkey=0;// 按键非方向键 , otherkey设置为0
}
else
{
otherkey=1;// 按键为方向键 , otherkey设置为1
}
if(direction==72 directiontemp==80)//忽略反方向按键
{
direction=32;
otherkey=0;
//start = clock();
//while(clock()-start=starttemp);
}
else if(direction==80 directiontemp==72)
{
direction=32;//设置按键为非方向键
otherkey=0;// 按键为非方向键 , otherkey设置为0
// start = clock();
//while(clock()-start=starttemp);//补偿等待时间
}
else if(direction==75 directiontemp==77)
{
direction=32;
otherkey=0;
//start = clock();
//while(clock()-start=starttemp);
}
else if(direction==77 directiontemp==75)
{
direction=32;
otherkey=0;
//start = clock();
//while(clock()-start=starttemp);
}
switch(direction)//判断方向键
{
case 72: x= tcsZuobiao[0][head]-1; y= tcsZuobiao[1][head];break; // 向上
case 80: x= tcsZuobiao[0][head]+1; y= tcsZuobiao[1][head];break; // 向下
case 75: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]-1;break; // 向左
case 77: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]+1;break; // 向右
default: break;
}
if(x==0 || x==21 ||y==0 || y==21) // 蛇头碰到墙壁 , 结束本局游戏
{
gotoxy(22,12);
cout "\t游戏已结束!" endl;
if(score=(length+(grade-1)*8)*10)//判断是否破记录
{
gotoxy(10,7);
color(12);
cout "闯关失败 加油耶!" endl;
fclose(fpini);//关闭ini文件
}
else
{
gotoxy(10,7);
color(12);
cout "恭喜您打破记录" endl;
score=(length+(grade-1)*8)*10;
writeini(fpini, score, argv);//写入ini文件的最高纪录
}
gotoxy(23,12);
cout "按回车键重新开始 , 按ESC退出游戏" endl;//显示的提示
break;//退出该局游戏
}
if(tcsQipan[x][y]!=0!(x==x1y==y1)tcsQipan[x][y]!=3) // 蛇头碰到蛇身 , 结束本局游戏
{
gotoxy(22,12);
cout "\t游戏已结束!" endl;
if(score=(length+(grade-1)*8)*10)//判断是否破记录
{
gotoxy(10,7);
color(12);
cout "闯关失败 加油耶!" endl;
fclose(fpini);//关闭ini文件
}
else
{
gotoxy(10,7);
color(12);
cout "恭喜您打破记录" endl;
score=(length+(grade-1)*8)*10;
writeini(fpini, score, argv);//写入ini文件的最高纪录
}
gotoxy(23,12);
cout "按回车键重新开始 , 按ESC退出游戏" endl;//显示的提示
break;//退出该局游戏
}
/*
游戏运行时的核心算法开始
*/
if(x==x1 y==y1) // 吃果子 , 长度加1
{
length ++;
if(length=8)//长度大于等于8重新计算长度 , 等级加1
{
length -= 8;//重新计算长度
grade ++;//等级加1
if(gamespeed50)//控制最快速度为50
gamespeed = 550 - grade * 50; // 改变自动前进时间间隔
}
tcsQipan[x][y]= 3;//贪吃蛇棋盘相应坐标现在蛇头标志改为蛇头标志3
tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = 4;//贪吃蛇棋盘相应坐标原来蛇头标志改为蛇身标志4


特别声明:本站内容均来自网友提供或互联网,仅供参考,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。