推箱子-- >一箱多关

前一段做了半个月的推箱子,因为老师要求做的有其他项目,暂时停一段。

 这个推箱子做到 了一个可以有很多关的程度,但是还是只有一个人推一个箱子到一个目的地,这样就不会有很难的地图,这个问题在我有空的时候会进一步改进的。一下是代码,保存一下,防止以后找不到。

//人是1;道路是2;墙是0;箱子是3;目的地是4
//
//
#include
#define uint unsigned int 
#define uchar unsigned char
sbit rw=P2^0;
sbit rs=P2^2;
sbit en=P2^3;
sbit cs1=P2^4;
sbit cs2=P2^5;
sbit bflag=P3^7;
uchar n=0,renx,reny,i,flag=1,ff=0,f,aa,bb,cc,ax,bx,cx,j,k,m;
sbit up=P1^0;
sbit down=P1^2;
sbit le=P1^1;
sbit rig=P1^3;
sbit res=P1^4;
uchar  temp[8][8];
//uchar ttemp[n][8][8];
uchar table[];
uchar code ttemp[][8][8]={
 0,0,0,0,0,0,0,0,
 2,2,2,2,2,2,2,0,
 0,0,0,0,0,0,0,0,
 0,0,0,1,0,4,0,0,
 0,2,2,3,2,2,0,0,
 0,2,0,2,0,2,0,0,
 0,2,2,2,2,2,0,0,
 0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,
 0,0,0,1,0,4,0,0,
 0,2,2,3,2,2,0,0,
 0,2,0,2,0,2,0,0,
 0,2,2,2,2,2,0,0,
 0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,
 0,2,2,1,0,0,4,0,
 0,2,3,2,0,0,2,0,
 0,0,0,2,0,0,2,0,
 0,2,2,2,2,2,2,0,
 0,0,2,2,0,2,2,0,
 0,0,2,2,0,0,0,0,
 0,0,0,0,0,0,0,0,

}; // 设置地图
uchar code ren[]={
0xff,0xff,0xe3,0xc5,0xb1,0xc5,0xe3,0xff
};   //人
uchar code mudi[]={
0xff,0xff,0xe7,0x81,0x81,0xe7,0xff,0xff
};    //目的地
uchar code xiangzi[]={
0xff,0xff,0xe7,0xdb,0xdb,0xe7,0xff,0xff
};    //箱子
uchar code niu[]={
0xef,0xe7,0xe9,0xeb,0x00,0xeb,0xef,0xff
};//牛
uchar code chan[]={
0x00,0x08,0x8b,0xce,0x78,0x08,0x00,0xf9,0xae,0xaa,0xf8,0xac,0xab,0xf8,0x00,0x00,
0x00,0x01,0x00,0x3f,0x00,0x02,0x02,0x02,0x02,0x02,0x3f,0x02,0x02,0x02,0x02,0x00
};//禅
void delayms(uint x)
{
 uchar i;
 while(x--)
 {
 for(i=0;i<10;i++);
 }
}
/*void map()
{
 uchar i,j;
 for(i=0;i<8;i++)
  for(j=0;j<8;j++)
  {
  
  }
}
*/
void right()
{
 cs1=1;
 cs2=0;
}
void left()
{
 cs1=0;
 cs2=1;
}
void write_data(uchar dat)
{
 rs=1; //写数据
 rw=0;
 en=0;
 P3=dat;
 en=1;
 delayms(5);
 en=0;
}
void write_cmd(uchar cmd)
{
 rs=0; //写指令
 rw=0;
 en=0;
 P3=cmd;
 delayms(5);
 en=1;
 delayms(5);
 en=0;
}
void Linefirst(uchar c)
{
       uchar i;
       i=c;
       c=i|0x40;
       write_cmd(c);
       delayms(5);
}
void pagetwo(uchar c)
{
 uchar i;
 i=c;
 c=i|0xb9;
 write_cmd(c);
}
void Zfirst(uchar c)
{
 uchar i;
 i=c;
 c=i|0xc0;
 write_cmd(c);
 delayms(5);
}
void Pagefirst(uchar c)
{
       uchar i;
      i=c;
      c=i|0xb8;
//      Busy_12864();
      write_cmd(c);
      delayms(5);
}
//设置显示位置
void lcd_pos(uchar x,uchar y,uchar z)
{
 Linefirst(0x40+y*8);
 Pagefirst(0xb8+x);
 Zfirst(z);
 
}
 
//液晶的初始化
void lcd_init()
{
 write_cmd(0x30);
 delayms(5);
 write_cmd(0x0c);
 delayms(5);
 write_cmd(0x01);
 delayms(5);
 
}
//人的初始化
void ren_init()
{ 
 lcd_pos(renx,reny,0xff); //第二行,第四列的方块
  for(k=0;k<8;k++)
  {
   write_data(ren[k]);
  }
}
//箱子的初始化
void xiangzi_init()
{
 ax=0x68;
 bx=0xbc;
 cx=0xff;//箱子的初始位置
 lcd_pos(ax,bx,cx); //第二行,第四列的方块
  for(i=0;i<8;i++)
  {
   write_data(xiangzi[i]);
  }
}
//目的地初始化
void mudi_init()
{
 lcd_pos(0x48,0xbc,0xff);
 for(i=0;i<8;i++)
 {
  write_data(mudi[i]);
 }
}
void disp(dis)
{
   //ff,f均为标志位
  lcd_pos(renx,reny,0xff);//擦除
   for(k=0;k<8;k++) write_data(0xff);
    switch(dis)    //判断按键的上下左右
  {
   case 0:    renx--;break;
   case 1:    renx++;break;
   case 2:      reny--;break;
   case 3:    reny++;break;
   case 4:     break;
  }
  
  
    if(ttemp[n][renx][reny]==2||ttemp[n][renx][reny]==1)
  {
  if(ttemp[n][renx][reny]!=0&&ttemp[n][renx][reny]!=3&&ttemp[n][renx][reny]!=4){ 
  while(up==0&&f==1){
   lcd_pos(renx,reny,0xff);
   for(k=0;k<8;k++) write_data(ren[k]);
  }
  while(down==0&&f==1){
   lcd_pos(renx,reny,0xff);
   for(k=0;k<8;k++) write_data(ren[k]);
  }
  while(le==0&&f==1){
   lcd_pos(renx,reny,0xff);
   for(k=0;k<8;k++) write_data(ren[k]);
  }
  while(rig==0&&f==1){
   lcd_pos(renx,reny,0xff);
   for(k=0;k<8;k++) write_data(ren[k]);
  }
  if(ttemp[n][renx][reny]==0) //防止穿墙
  {
   renx++;
   flag=0;
  }
  }
  }
}
void key()
{
 ren_init();
  
   if(up==0)
   {   
    if(ttemp[n][renx-1][reny]!=0){  //判断是否能走
   if(renx!=(ax+1)){  //判断不遇见箱子
U:   lcd_pos(renx,reny,0xff);
   for(k=0;k<8;k++) write_data(0xff);
     renx--;
   while(up==0)
   {
    lcd_pos(renx,reny,0xff);
    for(k=0;k<8;k++) write_data(ren[k]);
   }
   
   }
   
  
    else if(ttemp[n][ax-1][bx]!=0&&reny==bx){  //判断是否遇见箱子和箱子是否能走 
  
   lcd_pos(ax,bx,0xff);   //擦除箱子
   for(k=0;k<8;k++) write_data(0xff);
   ax--;
   
   lcd_pos(renx,reny,0xff); //擦除小人
   for(k=0;k<8;k++) write_data(0xff);
   renx--;
   while(up==0)
   {
    lcd_pos(renx,reny,0xff);
    for(k=0;k<8;k++) write_data(ren[k]);
    lcd_pos(ax,bx,0xff);
    for(k=0;k<8;k++) write_data(xiangzi[k]);
   }
  }
  else if(ttemp[n][ax-1][bx]==0&&reny==bx&&renx==(ax+1))
  {
   
  }
  else goto U;
   }
   }
   if(down==0)
   { 
    if(ttemp[n][renx+1][reny]!=0){  //判断是否能走
   
  if(renx!=(ax-1)){   //判断不能遇见箱子
D:   lcd_pos(renx,reny,0xff);
   for(k=0;k<8;k++) write_data(0xff);
     renx++;
   while(down==0)
   {
    lcd_pos(renx,reny,0xff);
    for(k=0;k<8;k++) write_data(ren[k]);
   }
   
   }
    else if(ttemp[n][ax+1][bx]!=0&&reny==bx){  //判断是否遇见箱子和箱子是否能走 
  
   lcd_pos(ax,bx,0xff);   //擦除箱子
   for(k=0;k<8;k++) write_data(0xff);
   ax++;
   
   lcd_pos(renx,reny,0xff); //擦除小人
   for(k=0;k<8;k++) write_data(0xff);
   renx++;
   while(down==0)
   {
    lcd_pos(renx,reny,0xff);
    for(k=0;k<8;k++) write_data(ren[k]);
    lcd_pos(ax,bx,0xff);
    for(k=0;k<8;k++) write_data(xiangzi[k]);
   }
  }
  else if(ttemp[n][ax+1][bx]==0&&reny==bx&&renx==(ax-1))
  {
   
  }
  else 
  {
   goto D;
  }
   }
  
  }
 
  if(le==0)
   {
  if(ttemp[n][renx][reny-1]!=0){    //判断是否能走
  if(reny!=(bx+1)){  //判断不能遇见箱子
L:   lcd_pos(renx,reny,0xff);
   for(k=0;k<8;k++) write_data(0xff);
     reny--;
   while(le==0)
   {
    lcd_pos(renx,reny,0xff);
    for(k=0;k<8;k++) write_data(ren[k]);
   }   
   }
   
  
   else if(ttemp[n][ax][bx-1]!=0&&renx==ax){  //判断是否遇见箱子和箱子是否能走 &&reny==(bx+1)
  
   lcd_pos(ax,bx,0xff);   //擦除箱子
   for(k=0;k<8;k++) write_data(0xff);
   bx--;
   
   lcd_pos(renx,reny,0xff); //擦除小人
   for(k=0;k<8;k++) write_data(0xff);
   reny--;
   while(le==0)
   {
    lcd_pos(renx,reny,0xff);
    for(k=0;k<8;k++) write_data(ren[k]);
    lcd_pos(ax,bx,0xff);
    for(k=0;k<8;k++) write_data(xiangzi[k]);
   }
  }
  else if(ttemp[n][ax][bx-1]==0&&renx==ax&&reny==(bx+1))
  {
   
  }
  else 
  {
  goto L;
  
  }

  }
   }
   if(rig==0)
   { 
   if(ttemp[n][renx][reny+1]!=0){    //判断是否能走
  if(reny!=(bx-1)){  //判断不能遇见箱子
R:   lcd_pos(renx,reny,0xff);
   for(k=0;k<8;k++) write_data(0xff);
     reny++;
   while(rig==0)
   {
    lcd_pos(renx,reny,0xff);
    for(k=0;k<8;k++) write_data(ren[k]);
   }   
   }
   
  
   else if(ttemp[n][ax][bx+1]!=0&&renx==ax){  //判断是否遇见箱子和箱子是否能走 &&reny==(bx+1)
  
   lcd_pos(ax,bx,0xff);   //擦除箱子
   for(k=0;k<8;k++) write_data(0xff);
   bx++;
   
   lcd_pos(renx,reny,0xff); //擦除小人
   for(k=0;k<8;k++) write_data(0xff);
   reny++;
   while(rig==0)
   {
    lcd_pos(renx,reny,0xff);
    for(k=0;k<8;k++) write_data(ren[k]);
    lcd_pos(ax,bx,0xff);
    for(k=0;k<8;k++) write_data(xiangzi[k]);
   }
  }
  else if(ttemp[n][ax][bx+1]==0&&renx==ax&&reny==(bx-1))
  {
   
  }
  else 
  {
  goto R;
  
  }

  }
   }
}
void youxi_init()
{
 right();
  for(i=0;i<8;i++)
  {
   for(j=0;j<8;j++)
   {
    temp[i][j]=ttemp[n][i][j];
    switch(temp[i][j])
    {
     case 0:lcd_pos(i,j,0xff);
       for(k=0;k<8;k++)
       write_data(0);
       break;
     case 1:lcd_pos(i,j,0xff);
       renx=i;reny=j; //人的初始位置
       for(k=0;k<8;k++)
       write_data(ren[k]);
       break;
        case 2:lcd_pos(i,j,0xff);
       for(k=0;k<8;k++)
       write_data(0xff);
       break;
     case 3:lcd_pos(i,j,0xff);//箱子的位置
       ax=i;bx=j;
       for(k=0;k<8;k++)
       write_data(xiangzi[k]);
       break;
     case 4:lcd_pos(i,j,0xff);
       for(k=0;k<8;k++)
       write_data(mudi[k]);
       break;
    }
   }
  }
}
void win()
{
  left(); 
  //while(1)
  //{
  lcd_pos(0x68,0xbc,0xff);
  for(i=0;i<8;i++)   //写一个牛字
  { 
  write_data(niu[i]);  
  }
  //while(res==0)break;
  //}
 
/* lcd_pos(0x58,0xbc,0xff);
  for(i=0;i<16;i++)
  { 
  write_data(chan[i]);  
  }
 lcd_pos(0x58,0xbd,0xff);
 for(i=16;i<32;i++)
 {
  write_data(chan[i]);
 }*/  
}
//第一关的人初始外置:(0x48,0xb9,0xff);
//箱子的初始位置:(0x68,0xbc,0xff);
//目的地位置:(0x48,0xbc,0xff)
//主函数
void main()
{
l: right();
 lcd_init();
 youxi_init();
 ren_init();
 while(1)
 {   
  key();
  if(ttemp[n][ax][bx]==4)
  {
   win();
   n++;
   if(n==9)
   {
    goto l1;
   }
   goto l;
  }
 }
l1:n--;
}


你可能感兴趣的:(单片机代码)