欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 5466|回復: 0
打印 上一主題 下一主題
收起左側

51單片機貪吃蛇程序

[復制鏈接]
跳轉到指定樓層
樓主


單片機源程序如下:
  1. #include<reg51.h>
  2. #include<intrins.h>
  3. #include<stdlib.h>
  4. typedef unsigned int u16;
  5. typedef unsigned char u8;
  6. sbit SER=P3^4;
  7. sbit RCLK=P3^5;
  8. sbit SRCLK=P3^6;
  9. sbit up=P1^7;                          //上
  10. sbit rt=P1^6;                          //右
  11. sbit down=P1^5;                      //下
  12. sbit lt=P1^4;                          //左

  13. u8        code Hc595_Coordx[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};
  14. u8        code Hc595_Coordy[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
  15. u8         foodx;
  16. u8   foody;
  17. u8   snake_x[20]={0};
  18. u8   snake_y[20]={0};
  19. u8 length;
  20. u8 direction;
  21. void initSnake();
  22. void delay(u16 i);
  23. void Hc595SendByte(u8 dat);
  24. void move();
  25. void Keypros();
  26. void creatFood();
  27. void snake_Grow(void);
  28. void drawsnake();

  29. void initSnake()
  30. {
  31.           snake_x[0]=0;
  32.         snake_y[0]=0;
  33.         length=1;
  34.         direction=2;
  35. }
  36. void delay(u16 i)
  37. {
  38.   while(i--);
  39. }
  40. void Hc595SendByte(u8 dat)
  41. {
  42.         u8 a;
  43.         SRCLK=0;
  44.         RCLK=0;
  45.         for(a=0;a<8;a++)
  46.         {
  47.                 SER=dat>>7;
  48.                 dat<<=1;

  49.                 SRCLK=1;
  50.                 _nop_();
  51.                 _nop_();
  52.                 SRCLK=0;        
  53.         }

  54.         RCLK=1;
  55.         _nop_();
  56.         _nop_();
  57.         RCLK=0;
  58. }

  59. void move()
  60. {
  61.      
  62.     u8 len = length- 1;
  63.     for(len; len > 0; len--)
  64.     {
  65.         snake_x[len] = snake_x[len - 1];
  66.         snake_y[len] = snake_y[len - 1];        //一直到蛇頭
  67.     }
  68.     switch(direction)
  69.     {
  70.     case 1:
  71.         snake_y[0]--;
  72.         break;
  73.     case 2:
  74.         snake_y[0]++;                                                 //再利用蛇頭轉變方向
  75.         break;
  76.     case 3:
  77.         snake_x[0]--;
  78.         break;
  79.     case 4:
  80.         snake_x[0]++;
  81.         break;
  82.     default:
  83.         break;
  84.     }

  85. }

  86. //貪吃蛇的上下左右行動控制
  87. void Keypros()
  88. {
  89.         if(up==0)
  90.         {
  91.                 delay(1000);  //消抖處理
  92.                 if(up==0)
  93.                 {
  94.                   if(direction==3|direction==4)
  95.                   direction=1;             //在地址1內寫入數據num
  96.                 }
  97.                 while(!up);
  98.         }
  99.         if(down==0)
  100.         {
  101.                 delay(1000);  //消抖處理
  102.                 if(down==0)
  103.                 {        if(direction==3|direction==4)
  104.                         direction=2;         
  105.                 }
  106.                 while(!down);
  107.         }
  108.         if(lt==0)
  109.         {
  110.                 delay(100);  //消抖處理
  111.                 if(lt==0)
  112.                 {
  113.                     if(direction==1|direction==2)
  114.                         direction=3;
  115.                 }
  116.                 while(!lt);
  117.         }
  118.         if(rt==0)
  119.         {
  120.                 delay(1000);  //消抖處理
  121.                 if(rt==0)
  122.                 {
  123.                    if(direction==1|direction==2)
  124.                    direction=4;
  125.                 }
  126.                 while(!rt);
  127.         }               
  128. }
  129. void creatFood()         //隨機創造食物
  130. {
  131.     u8 i;
  132.     foodx=rand()%8;
  133.     foody=rand()%8;
  134.     for(i=0; i<length; i++)
  135.     {
  136.         if((foodx==snake_x[i])&&(foody==snake_y[i]))        //如果食物與蛇的身體重疊,要重新創建
  137.                 {
  138.                     creatFood();
  139.                 }      
  140.     }
  141. }
  142. void snake_Grow(void)
  143. {
  144.     if(snake_x[0] == foodx && snake_y[0] == foody)
  145.     {
  146.         creatFood();
  147.         snake_x[length] = snake_x[length - 1];
  148.         snake_y[length] = snake_y[length - 1];
  149.         length++;
  150.     }
  151. }

  152. void drawsnake()   //點亮蛇和食物
  153. {
  154.      u8        i;
  155.      for(i=0;i<length;i++)
  156.         {
  157.                   Hc595SendByte(Hc595_Coordy[snake_y[i]]);
  158.                 P0=Hc595_Coordx[snake_x[i]];
  159.         delay(20);
  160.                 P0=0xff;

  161.             Hc595SendByte(Hc595_Coordy[foody]);   
  162.             P0=Hc595_Coordx[foodx];
  163.         delay(20);
  164.             P0=0xff;               
  165.         }
  166. }

  167. void JudgeDeath(void)
  168. {
  169.     u8 i;

  170.     // 判斷蛇撞墻死亡
  171.     if((snake_x[0]>7)||(snake_y[0]>7))
  172.     {
  173.          Hc595SendByte(0x00);
  174.         while(1);
  175.     }

  176.     // 判斷蛇撞到自己身體死亡
  177.     for(i=4; i<length; i++)
  178.     {
  179.         if((snake_x[0]==snake_x[i])&&(snake_y[0]==snake_y[i]))
  180.         {
  181.             P0=0xFF;
  182.             while(1);
  183.         }
  184.     }
  185. }


  186. void Timer0Init()
  187. {
  188.         TMOD|=0X01;//選擇為定時器0模式,工作方式1,僅用TR0打開啟動。

  189.         TH0=0XD8;        //給定時器賦初值,定時10ms
  190.         TL0=0XF0;        
  191.         ET0=1;//打開定時器0中斷允許
  192.         EA=1;//打開總中斷
  193.         TR0=1;//打開定時器                        
  194. }

  195. void main()
  196. {   
  197.    
  198.         Timer0Init();
  199.         initSnake();        
  200.         while(1)
  201.         {
  202.          drawsnake();
  203.          JudgeDeath();
  204.          snake_Grow();
  205.          Keypros();
  206.         }
  207. }

  208. void Timer0() interrupt 1
  209. {
  210.         static u16 i;
  211.         TH0=0XD8;        //給定時器賦初值,定時10ms
  212.         TL0=0XF0;
  213.         i++;
  214.         if(i==50)
  215.         {
  216.          i=0;
  217.      move();
  218.         
  219.         }        
  220. }

復制代碼

以上所有資料51hei提供下載:
tcs.docx (16.3 KB, 下載次數: 35)


評分

參與人數 1黑幣 +50 收起 理由
admin + 50

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表