您好,欢迎访问三七文档
#includereg51.h#includestdlib.h#defineKEYP2#defineMAX_LENGTH33#defineRIGEH1#defineDOWN2#defineLEFT3#defineUP4//--¶¨ÒåÈ«¾Ö±äÁ¿--//bitlive=0;bitstart=0;bitfoodexist=0;unsignedcharBT3,BT2,BT1,BT0;unsignedcharSnakeBodyLength;unsignedchardirect;unsignedcharfirstx,firsty,lastx,lasty;unsignedcharfoodx,foody;unsignedcharspeed;//--¶¨ÒåÊý×é--//unsignedcharcodetable0[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00};unsignedcharcodetable1[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00};unsignedcharcodetable2[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff};unsignedcharcodetable3[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff};unsignedcharSnakex[MAX_LENGTH];unsignedcharSnakey[MAX_LENGTH];//--¶¨ÒåSPIҪʹÓõÄIO--//sbitMOSIO=P3^4;sbitR_CLK=P3^5;sbitS_CLK=P3^6;//--È«¾Öº¯ÊýÉùÃ÷--//voidHC595SendData(unsignedcharBT3,unsignedcharBT2,unsignedcharBT1,unsignedcharBT0);//µãÕóÊäÈ뺯ÊývoidSnakemove();//ÉßÒƶ¯voidDrawSnake();//ÏÔʾÉßvoidDrawFood();//ÏÔʾʳÎïvoidCreatFood();//²úÉúʳÎïvoidKeyscanf();//ɨÃè¼üÅÌvoidSnakeDeathCheck();//ËÀÍö¼ì²âvoidSnakeGrow();//ÉßÉú³¤voidGameInit();//ÓÎÏ·³õʼ»¯voidGameOver();//ÓÎÏ·½áÊøvoidDelay10ms(unsignedintc);//ÑÓ³Ùº¯ÊývoidKeyscanf(){unsignedcharkeyValue=0,i;if(KEY!=0xff){Delay10ms(1);if(KEY!=0xff){keyValue=KEY;i=0;while((i50)&&(KEY!=0xff)){Delay10ms(1);i++;}}}switch(keyValue){case(0xbf):direct=4;break;case(0xf7):direct=3;break;case(0xfb):direct=2;break;case(0xfd):direct=1;break;case(0xef):start=1;break;}}voidGameOver(){HC595SendData(0xff,0xff,0,0);}voidSnakeDeathCheck(){unsignedchari;for(i=1;iSnakeBodyLength;i++){if((Snakex[0]==Snakex[i])&&(Snakey[0]==Snakey[i])){live=0;}}}voidSnakeGrow(){if((Snakex[0]==foodx)&&(Snakey[0]==foody)){SnakeBodyLength++;foodexist=0;Snakex[SnakeBodyLength-1]=lastx;Snakey[SnakeBodyLength-1]=lasty;}}voidGameInit(){live=1;SnakeBodyLength=4;direct=1;Snakex[0]=9;Snakey[0]=9;Snakex[1]=8;Snakey[1]=9;Snakex[2]=7;Snakey[2]=9;Snakex[3]=6;Snakey[3]=9;DrawSnake();if(foodexist==0){CreatFood();}DrawFood();}voidDrawSnake(){chari;for(i=SnakeBodyLength-1;i=0;i--){switch(Snakey[i]){case(1):BT0=table0[0],BT1=table1[8];break;case(2):BT0=table0[1],BT1=table1[8];break;case(3):BT0=table0[2],BT1=table1[8];break;case(4):BT0=table0[3],BT1=table1[8];break;case(5):BT0=table0[4],BT1=table1[8];break;case(6):BT0=table0[5],BT1=table1[8];break;case(7):BT0=table0[6],BT1=table1[8];break;case(8):BT0=table0[7],BT1=table1[8];break;case(9):BT0=table0[8],BT1=table1[0];break;case(10):BT0=table0[8],BT1=table1[1];break;case(11):BT0=table0[8],BT1=table1[2];break;case(12):BT0=table0[8],BT1=table1[3];break;case(13):BT0=table0[8],BT1=table1[4];break;case(14):BT0=table0[8],BT1=table1[5];break;case(15):BT0=table0[8],BT1=table1[6];break;case(16):BT0=table0[8],BT1=table1[7];break;}switch(Snakex[i]){case(1):BT2=table2[0],BT3=table3[8];break;case(2):BT2=table2[1],BT3=table3[8];break;case(3):BT2=table2[2],BT3=table3[8];break;case(4):BT2=table2[3],BT3=table3[8];break;case(5):BT2=table2[4],BT3=table3[8];break;case(6):BT2=table2[5],BT3=table3[8];break;case(7):BT2=table2[6],BT3=table3[8];break;case(8):BT2=table2[7],BT3=table3[8];break;case(9):BT2=table2[8],BT3=table3[0];break;case(10):BT2=table2[8],BT3=table3[1];break;case(11):BT2=table2[8],BT3=table3[2];break;case(12):BT2=table2[8],BT3=table3[3];break;case(13):BT2=table2[8],BT3=table3[4];break;case(14):BT2=table2[8],BT3=table3[5];break;case(15):BT2=table2[8],BT3=table3[6];break;case(16):BT2=table2[8],BT3=table3[7];break;}HC595SendData(BT3,BT2,BT1,BT0);}}voidCreatFood(){unsignedinti,j;i=rand()%16+1;j=rand()%16+1;foodx=i;foody=j;foodexist=1;}voidDrawFood(){//if(foodexist==1)//{switch(foody){case(1):BT0=table0[0],BT1=table1[8];break;case(2):BT0=table0[1],BT1=table1[8];break;case(3):BT0=table0[2],BT1=table1[8];break;case(4):BT0=table0[3],BT1=table1[8];break;case(5):BT0=table0[4],BT1=table1[8];break;case(6):BT0=table0[5],BT1=table1[8];break;case(7):BT0=table0[6],BT1=table1[8];break;case(8):BT0=table0[7],BT1=table1[8];break;case(9):BT0=table0[8],BT1=table1[0];break;case(10):BT0=table0[8],BT1=table1[1];break;case(11):BT0=table0[8],BT1=table1[2];break;case(12):BT0=table0[8],BT1=table1[3];break;case(13):BT0=table0[8],BT1=table1[4];break;case(14):BT0=table0[8],BT1=table1[5];break;case(15):BT0=table0[8],BT1=table1[6];break;case(16):BT0=table0[8],BT1=table1[7];break;}switch(foodx){case(1):BT2=table2[0],BT3=table3[8];break;case(2):BT2=table2[1],BT3=table3[8];break;case(3):BT2=table2[2],BT3=table3[8];break;case(4):BT2=table2[3],BT3=table3[8];break;case(5):BT2=table2[4],BT3=table3[8];break;case(6):BT2=table2[5],BT3=table3[8];break;case(7):BT2=table2[6],BT3=table3[8];break;case(8):BT2=table2[7],BT3=table3[8];break;case(9):BT2=table2[8],BT3=table3[0];break;case(10):BT2=table2[8],BT3=table3[1];break;case(11):BT2=table2[8],BT3=table3[2];break;case(12):BT2=table2[8],BT3=table3[3];break;case(13):BT2=table2[8],BT3=table3[4];break;case(14):BT2=table2[8],BT3=table3[5];break;case(15):BT2=table2[8],BT3=table3[6];break;case(16):BT2=table2[8],BT3=table3[7];break;}HC595SendData(BT3,BT2,BT1,BT0);//}}v
本文标题:51单片机贪吃蛇
链接地址:https://www.777doc.com/doc-3687622 .html