原创 基于AT89S52的贪吃蛇游戏程序

2009-7-1 21:00 2824 4 4 分类: MCU/ 嵌入式

这是我经过测试通过的程序


#include"reg52.h"
#define uchar unsigned char
#define uint unsigned int
#define N 45
uchar count="0",time=0,ct=0,flag=0,speed=100,gg=0;
sbit up="P2"^0;       //down
sbit down="P3"^6;
sbit right="P2"^1;
sbit left="P3"^7;
sbit chose="P3"^4;
sbit st="P3"^3;
sbit P32=P3^2;
uchar start="0";  
uchar code xx[10]={11,13,9,7,6,15,2,1,7,9};
uchar code yy[10]={10,2,8,13,7,5,2,7,6,14};
uchar code x0[]={1,1,1,1,1,2,2,3,3,4,4,5,5,5,5,5};  //16
uchar code y0[]={5,6,7,8,9,4,10,4,10,4,10,5,6,7,8,9};
uchar code x1[]={2,2,3,3,3,3,3,3,3,4};  //10
uchar code y1[]={4,9,4,5,6,7,8,9,10,4};
uchar code x2[]={2,2,2,3,3,3,4,5,4,4,5,5,5};//13
uchar code y2[]={4,5,9,4,6,10,4,8,7,10,4,9,10};
uchar code x3[]={2,2,2,3,3,3,4,4,4,5,5,5,5};  //13
uchar code y3[]={4,7,10,4,7,10,4,7,10,5,6,8,9};
uchar code x4[]={1,1,2,2,3,3,4,4,4,4,4,5,4};//13a
uchar code y4[]= {6,7,6,8,6,9,4,5,6,7,10,6,8};
uchar code x5[]={2,2,2,2,2,3,3,4,4,4,5,5,3}; //13
uchar code y5[]={4,7,8,9,10,7,9,4,7,9,5,6,4};
uchar code x6[]={2,2,2,2,2,2,3,3,4,4,4,5,5,3,5};//15
uchar code y6[]={7,8,5,6,9,5,4,7,4,7,10,5,6,10,10};
uchar code x7[]={2,4,4,4,4,3,5,4,5};//9 
uchar code y7[]={9,4,5,6,7,9,8,9,9}; 
uchar code x8[]={1,1,2,1,2,2,3,3,3,4,5,4,4,5,5,1,5};//17
uchar code y8[]={5,9,4,6,7,10,4,7,10,4,6,7,10,5,9,8,8};
uchar code x9[]={1,1,2,2,3,3,4,4,4,5,5,5,5,5,3,2}; //17
uchar code y9[]={8,9,7,10,7,10,7,10,4,7,9,5,6,8,4,4};
uchar code xy[]={16,10,13,13,13,13,15,9,17,17};
uchar remx;
uchar remy;
struct Snake
{
   uchar  x[N];
   uchar y[N];
   uchar node;/*蛇的节数*/
   uchar direction;/*蛇移动方向*/
   uchar life;/* 蛇的生命,0活着,1死亡*/
}snake;
struct Food
{
   uchar x;/*食物的横坐标*/
   uchar y;/*食物的纵坐标*/
   uchar yes;/*判断是否要出现食物的变量*/
}food;/*食物的结构体*/
void Init(void)
{
 P32=1;
 EA=1;//开总中断
 ET0=1;//开定时器0中断
 IT0=1;//开定时器1中断
 EX0=1;//外部中断1中断允许
 TMOD=0x11;//设置定时器0为工作方式1
 TH0=0xec;
 TL0=0x78;
 P2=0x03;
 right=1;
 left=1;
 TR0=1;//定时器0 开始运行
 //TCON|=0x01;//外部中断0下降沿出发
 food.x=11;
 food.y=7;
 food.yes=0;
 snake.x[0]=11;
 snake.y[0]=5;
 snake.x[1]=11;
 snake.y[1]=4;
 snake.node=2;
 snake.direction=4;
 snake.life=0; 
}
void delay(unsigned char t)
{
    while(--t);
}
int mux(uchar temp)
{
 if(temp==7)return 128;
 if(temp==6)return 64;
 if(temp==5)return 32;
 if(temp==4)return 16;
 if(temp==3)return 8;
 if(temp==2)return 4;
 if(temp==1)return 2;
 if(temp==0)return 1;
 return 0;
}
void autorun(void)//贪吃蛇的日常运转
{
 uint i;
 remx=snake.x[snake.node-1];
 remy=snake.y[snake.node-1];
 for(i=snake.node-1;i>0;i--)/*移动算法*/
  {
     snake.x=snake.x[i-1];
     snake.y=snake.y[i-1];
  }
  switch(snake.direction)//蛇头前进
  {
    case 1:snake.x[0]+=1;break;
       case 2:snake.x[0]-=1;break;
       case 3:snake.y[0]-=1;break;
       case 4:snake.y[0]+=1;break;
  }
  for(i=3;i<snake.node;i++)//检测是否追尾
  {
    if(snake.x==snake.x[0]&&snake.y==snake.y[0])
    {
     snake.life=1;
    break;
    }
  }     
  if(snake.x[0]<0||snake.x[0]>15||snake.y[0]<0||snake.y[0]>15)//检测是否撞墙
  {
   snake.life=1;
  }
  //if(snake.life==1)//检测蛇是死是活
  // break;
  if(snake.x[0]==food.x&&snake.y[0]==food.y)//是否吃到食物
  {
   snake.node+=1;
   snake.x[snake.node-1]=remx;
   snake.y[snake.node-1]=remy;
   food.yes=1;
  }
  while(food.yes==1)//食物没有了,重新创建一个食物
  {
   //randomize();
   //food.x=rand()%16+1;
   //food.y=rand()%16+1;
   food.x=xx[ct];
   food.y=yy[ct];
   ct++;
   if(ct==9)
   {
   ct=0;
   }
   food.yes=0;
   for(i=0;i<snake.node;i++)
   {
    if(food.x==snake.x&&food.y==snake.y)
    {
     food.yes=1;
    }
   }
   
  }
}
void turnkey()// 键盘扫描
{
  if(!left)
  {
   delay(10);
    if(!left)
    {
     snake.direction=2;
    }
  }
  if(!right)
  {
   delay(10);
   if(!right)
   {
    snake.direction=1;
   }
  }
  if(!up)
  {
   delay(10);
   if(!up)
   {
    snake.direction=3;
   }
  }
  if(!down)
  {
   delay(10);
   if(!down)
   { 
     snake.direction=4;
   }
  }



}
void show(uchar x,uchar y) //显示一个点
{
 int i;
 P0=0xff;
 P1=0xff;
 P2=4*y;
 if(x<8)
 {
 i=mux(x);
 P0=~i;
 }
 else
 {
 i=mux(x-8);
 P1=~i;
 }
 P2|=0x03;
 
}
void display()//显示函数
{ uint i;
 show(food.x,food.y);
 for(i=0;i<snake.node;i++)
 {
  show(snake.x,snake.y);
 }
 
 
}
void scoredisplay(uchar s)
{
   uchar x,y,z;
   uint i;
   x="s/100";
  y="s"%100/10;
  z="s"%10;
  for(i=0;i<xy[x];i++)
 {
   switch(x)
   {
   case 0:show(x0,y0);break;
   case 1:show(x1,y1);break;
   case 2:show(x2,y2);break;
   case 3:show(x3,y3);break;
   case 4:show(x4,y4);break;
   case 5:show(x5,y5);break;
   case 6:show(x6,y6);break;
   case 7:show(x7,y7);break;
   case 8:show(x8,y8);break;
   case 9:show(x9,y9);break;
   }
  
 }
 for(i=0;i<xy[y];i++)
 {
   switch(y)
   {
   case 0:show(x0+5,y0);break;
   case 1:show(x1+5,y1);break;
   case 2:show(x2+5,y2);break;
   case 3:show(x3+5,y3);break;
   case 4:show(x4+5,y4);break;
   case 5:show(x5+5,y5);break;
   case 6:show(x6+5,y6);break;
   case 7:show(x7+5,y7);break;
   case 8:show(x8+5,y8);break;
   case 9:show(x9+5,y9);break;
   }
  
 }
 for(i=0;i<xy[z];i++)
 {
   switch(z)
   {
   case 0:show(x0+10,y0);break;
   case 1:show(x1+10,y1);break;
   case 2:show(x2+10,y2);break;
   case 3:show(x3+10,y3);break;
   case 4:show(x4+10,y4);break;
   case 5:show(x5+10,y5);break;
   case 6:show(x6+10,y6);break;
   case 7:show(x7+10,y7);break;
   case 8:show(x8+10,y8);break;
   case 9:show(x9+10,y9);break;
   }
  
 }


}
void main()
{
 uchar i;
 Init();
 while(1)
 { 
  if(start==1) 
  { 
   if(snake.life==0)
   {
   if(time==1)
   {
     autorun();
    time=0;
   }
   else
   {
    for(i=0;i<20;i++)
    {
    display();
    }
   
     turnkey();
 
   }
   }
   else
    scoredisplay(snake.node);
  }
  else if(start==2)
  {
   scoredisplay(snake.node); 
  }
 }



 
}
void time0() interrupt 1
{
 TH0=0xec;
 TL0=0x78;
 count++;
 if(count==100)
 {
  time=1;
   
 } 
}
void int0() interrupt 0
{
 EA=0;
 start=1;
 while(1)
 {
  if(!chose)
  {
   if(!chose)
   {
    flag++;
   }
  }
  switch(flag%3)
  {}
  gg=flag%3;
  scoredisplay(gg);
  if(!st)
  {
   if(!st)
   {
   // goto:end1;
   }
  }
 }
 //end1:EA=1;


}         

文章评论0条评论)

登录后参与讨论
我要评论
0
4
关闭 站长推荐上一条 /2 下一条