51單片機實現(xiàn)貪食蛇的子程序
/*---------------------------------------------------------
函數(shù)功能:蛇吃到食物處理
調(diào)用形式:Snake_Feed();
參數(shù):
返回值:遲到食物返回1 沒吃到返回0->判斷是否死亡
備注:
---------------------------------------------------------*/
void Snake_Feed(void)
{
uint8 i;
if(snake[snake_size].X==food.X&&snake[snake_size].Y==food.Y)
{
//while(food.S==0)
Creat_Food();//重新產(chǎn)生食物
//snake_foodflag=1;
snake_size++;//蛇身加1
snake_score+=BASIC_SCORE;//游戲得分加上加分基數(shù)
snake_speed-=BASIC_SPEED;//游戲速度加上加速基數(shù)
if(snake_speed<=0)
snake_speed=1;
if(snake_size==SNAKE_SIZE)//此刻蛇長等于最長時候,通關條件
{
for(i=0;i<=SNAKE_SIZE;i++)
{
SetStatusXY(snake[i].X,snake[i].Y,0);//蛇消隱
}
//清屏幕緩存并顯示得分
ClearBuf();
{
Display_5x8(0,0,S_);
Display_5x8(6,0,C_);
Display_5x8(12,0,O_);
Display_5x8(18,0,R_);
Display_5x8(24,0,E_);
}
//下32x8像素顯示分數(shù)(最長52節(jié),吃50個食物通關,50*10=500分最高)
{
Display_5x8(31-6,8,snake_score%10);
Display_5x8(31-12,8,snake_score%100/10);
Display_5x8(31-18,8,snake_score/100);
}
while(1)
{
for(i=0;i<5;i++)
Display();
DelayMs(255);
if(snake_status==0)
break;
}
}
//return 1;
}
//else return 0;
}
/*---------------------------------------------------------
函數(shù)功能:處理蛇的死亡
調(diào)用形式:Snake_Die();
參數(shù):
返回值:
備注:
---------------------------------------------------------*/
void Snake_Die(void)
{
uint16 i;
for(i=1;i
if(snake[0].X==snake[i].X)
{
if(snake[i].Y==snake[0].Y)//死亡
{
ClearBuf();
Display_5x8(0,0,G_);
Display_5x8(6,0,A_);
Display_5x8(12,0,M_);
Display_5x8(18,0,E_);
Display_5x8(0,8,O_);
Display_5x8(6,8,V_);
Display_5x8(12,8,E_);
Display_5x8(18,8,R_);
for(i=0;i<800;i++)
Display();
ClearBuf();
Display_5x8(31-6,8,snake_score%10);
Display_5x8(31-12,8,snake_score%100/10);
Display_5x8(31-18,8,snake_score/100);
Display_5x8(0,0,S_);
Display_5x8(6,0,C_);
Display_5x8(12,0,O_);
Display_5x8(18,0,R_);
Display_5x8(24,0,E_);
while(1)
{
if(!snake_status)
break;
for(i=0;i<100;i++)
Display();
DelayMs(255);
}
break;//連跳
}
}
}
}
/*---------------------------------------------------------
函數(shù)功能:游戲進行時
調(diào)用形式:Snake_Ing();
參數(shù):
返回值:
備注:
---------------------------------------------------------*/
void Snake_Ing(void)
{
uint8 i;
if(!snake_status)
Snake_Init();
Snake_Step(snake_direct);
Snake_Feed();
Snake_Die();
for(i=0;i
}
#endif
評論