找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 15309|回复: 12
打印 上一主题 下一主题
收起左侧

led点阵屏实现贪吃蛇游戏--硬件图流程图程序都有

  [复制链接]
跳转到指定楼层
楼主
本人在点阵屏上实现了贪吃蛇游戏,这篇文档里包括了硬件连接图、流程图、全部的程序和效果图。完全是自己做的,谢谢各位支持。当然还可以继续完善程序功能。

  • 实验效果图


实践项目
LED贪吃蛇游戏

实验内容:
此单片机项目实验的主要功能是在LED点阵屏上实现贪吃蛇游戏。首先在LCD1602液晶屏显示贪吃蛇游戏的注意事项(游戏开始,按键对蛇的控制K2上,K6下,K2+K6右);然后在LED点阵屏上展现出游戏主体,通过两个按键控制蛇的移动;每当蛇吃掉一个食物得分加一,如果蛇撞到自己或者墙游戏结束,点阵屏上将出现“囧”,最后蜂鸣器响起音乐表示祝贺,数码管显示所得分数。

  • 硬件图
  



二、流程图


三、主要程序

  1. /*************************************************
  2. 全局头文件  config.h
  3. **************************************************/

  4. #ifndef _CONFIG_H
  5. #define _CONFIG_H
  6. //--包含你要使用的头文件--//
  7. #include
  8. #include
  9. #include
  10. //--定义重要关键词--//
  11. #ifndef uchar
  12. #define uchar unsigned char
  13. #endif
  14. #ifndef uint
  15. #define uint unsigned int
  16. #endif
  17. /--液晶屏IO口定义--//
  18. #define LCD1602_DATAPINS P0   
  19. sbit LCD1602_E=P2^7;         
  20. sbit LCD1602_RW=P2^5;
  21. sbit LCD1602_RS=P2^6;
  22. //--定义数码管要使用的IO口--//
  23. #define GPIO_DIG P1  
  24. //--定义点阵屏要使用的 IO--//
  25. sbit MOSIO = P3^4;              
  26. sbit R_CLK = P3^5;
  27. sbit S_CLK = P3^6;
  28. //--定义按键要使用的 IO--//
  29. sbit g2=P3^0;                  //k2
  30. sbit g1=P3^1;                  //k6
  31. //--定义蜂鸣器要使用的 IO--//
  32. sbit Beep =  P3^7 ;
  33. extern               int score; //得分
  34. #endif
  35. /********************************
  36.   LED头文件   led.h
  37. ********************************/
  38. #ifndef _LED_H
  39. #define _LED_H
  40. extern              uchar code x[];              //led列选
  41. extern              uchar code y[];              //led行选
  42. extern               uchar code tab13[];          //显示囧
  43. //显示倒计时//
  44. extern               uchar code tab12[];         
  45. extern               uchar code tab11[];
  46. extern               uchar code tab10[];
  47. extern               uchar code tab9[];
  48. extern               uchar code tab8[];
  49. extern               uchar code tab0[];           //取模后选
  50. void st_led(void);                     //led初始化
  51. void HC595SendData(  uchar BT3, uchar BT2,uchar BT1,uchar BT0);     //led发送数据
  52. #endif
  53. /********************************
  54.   snake头文件  snake.h
  55. ********************************/
  56. #ifndef _SNAKE_H
  57. #define _SNAKE_H

  58. #define MAX_LENGTH 33   //最长蛇长

  59. void createFood();       //创造食物
  60. void snakegame();       //游戏主体(此时游戏开始)                 
  61. void expandSnake();     //延长蛇身
  62. void moveSnake();      // 移动蛇
  63. void gameOver();       //游戏结束
  64. uchar touchSelf();      //撞到自己
  65. uchar touchWall();                    //撞到墙
  66. int oppositeDirection(int t);             //方向选取
  67. int foodEat();                         // 吃食
  68. void init();                           //蛇的初始化
  69. void draw();                         //显示蛇长
  70. #endif
  71. /********************************
  72.   LCD头文件  lcd.h
  73. ********************************/
  74. #ifndef __LCD_H_
  75. #define __LCD_H_
  76. #define LCD1602_4PINS
  77. //—定义重要关键词—//
  78. #ifndef uchar
  79. #define uchar unsigned char
  80. #endif
  81. #ifndef uint
  82. #define uint unsigned int
  83. #endif
  84. //—函数声明—//
  85. void Lcd1602_Delay1ms(uint c); //在51单片机12MHZ时钟下的延时函数
  86. void LcdWriteCom(uchar com); //LCD1602写入8位命令子函数
  87. void LcdWriteData(uchar dat); //LCD1602写入8位数据子函数
  88. void LcdInit();//LCD1602写入8位数据子函数                                                                                    
  89. void lcd_show(void);//液晶屏显示注意事项
  90. void Delay10ms(unsigned int c);              //延时
  91. #endif
  92. /********************************
  93.   数码管头文件  tube.h
  94. ********************************/
  95. #ifndef __TUBE_H_
  96. #define __TUBE_H_
  97. //—定义全局变量—//
  98. //RAM,ROM
  99. unsigned char code DIG_CODE[16]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,
  100.                                  0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71};
  101. #endif
  102. /********************************
  103.   主函数    main.c
  104. ********************************/
  105. //—包含你要使用的头文件—//
  106. #include "config.h"
  107. #include "snake.h"
  108. #include "led.h"
  109. void main()
  110. {
  111.    TMOD=0x01;    //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
  112.    TH1=(65535-10000)/255;  //给定初值,这里使用定时器最大值从0开始计数一直到65535溢出
  113.    TL1=(65535-10000)%255;
  114.    ET1=1;  //定时器中断打开
  115.    TR1=1;  //定时器开关打开
  116.    EA=1;   //总中断打开
  117.    init();   //蛇初始化
  118.    lcd_show();   //液晶屏显示(K2 上,K6 下,K2+K6右)
  119.    st_led();  //点阵屏显示游戏
  120.    while(1)
  121.    {
  122.                  snakegame();  //游戏主体(游戏开始 )
  123.    }
  124.    gameOver();    游戏结束
  125. }
  126. /********************************
  127.   Snake部分函数    snake.c
  128. ********************************/
  129. #define  _SNAKE_C
  130. #include "config.h"
  131. #include "snake.h"
  132. #include "led.h"
  133. #include "tube.h"

  134. int lastx,lasty;
  135. int score=0;       //分数初始化
  136. uint t=0;                                  //方向选量
  137. uint left=0,top=0,right=15,bottom=15,jfood=0;

  138. struct Point
  139. {
  140.               uchar x, y;
  141. };
  142. struct Snake
  143. {
  144.               struct Point nodes[MAX_LENGTH];
  145.               uchar length;
  146.               uchar direction;
  147. } snake;
  148. struct Food
  149. {
  150.               struct Point position;
  151.               uchar exist;
  152. } food;

  153. void init()                       //蛇初始化
  154. {
  155.     snake.nodes[0].x=15;
  156.     snake.nodes[0].y=3;
  157.     snake.nodes[1].x=14;
  158.     snake.nodes[1].y=3;
  159.               snake.length=2;
  160.               snake.direction=2;
  161.     food.exist=0;
  162. }
  163. void snakegame()                    //游戏主体   
  164. {            
  165.               while(1)
  166.               {
  167.                             if (touchWall() || touchSelf())  //撞墙或自己游戏结束
  168.                                 gameOver();
  169.                             if (!food.exist)              //是否有食物
  170.                                createFood();           //创造食物
  171.                                food.exist=1;
  172.                   draw();                  
  173.                   lastx=snake.nodes[snake.length-1].x;
  174.                                lasty=snake.nodes[snake.length-1].y;
  175.                             if (!oppositeDirection(t))
  176.                             {
  177.                               snake.direction =t;
  178.                             }
  179.          moveSnake();       //移动蛇
  180.                             if (foodEat())         //是否吃食物
  181.                             {
  182.                                           food.exist = 0;
  183.                                           expandSnake();   蛇身扩展
  184.                             }
  185.               }
  186. }
  187. int foodEat()                             //吃食                  
  188. {
  189.     if(snake.nodes[0].x==food.position.x&&snake.nodes[0].y==food.position.y)
  190.        return 1;
  191.     else
  192.        return 0;
  193. }
  194. int oppositeDirection(int t)                             //方向选量            
  195. {
  196.    if(t==0&&snake.direction==2)
  197.    {
  198.     return 1;
  199.    }
  200.    else if(t==2&&snake.direction==0)
  201.           {
  202.                                 return 1;
  203.                               }
  204.    else if(t==1&&snake.direction==3)
  205.           {
  206.             return 1;
  207.           }
  208.    else if(t==3&&snake.direction==1)
  209.           {
  210.             return 1;
  211.           }
  212.    else
  213.           return 0;
  214. }

  215. void moveSnake()                 //移动蛇                                         
  216. {
  217.     int k;
  218.     lastx=snake.nodes[snake.length-1].x;
  219.     lasty=snake.nodes[snake.length-1].y;
  220.     for(k=snake.length-1;k>0;k--)
  221.     {
  222.                             snake.nodes[k].x=snake.nodes[k-1].x;
  223.                             snake.nodes[k].y=snake.nodes[k-1].y;
  224.     }
  225.               if(snake.direction==2)
  226.        {
  227.                                snake.nodes[0].y-=1;
  228.                  }
  229.     else if(snake.direction==0)
  230.        {
  231.                  snake.nodes[0].y+=1;
  232.                  }
  233.     else if(snake.direction==3)
  234.        {
  235.                   snake.nodes[0].x-=1;
  236.                  }
  237.     else if(snake.direction==1)
  238.        {
  239.                   snake.nodes[0].x+=1;
  240.                  }
  241.     else;
  242. }
  243. void T0_1() interrupt 3              using 2                             
  244. {
  245.       TH1=(65535-10000)/255;
  246.                 TL1=(65535-10000)%255;
  247.                 if(g1==0&&g2==0)  {t=1;}//left
  248.                 if(g1==1&&g2==0)  {t=0;}//up
  249.                 if(g1==0&&g2==1)  {t=2;}//down
  250.                 if(g1==1&&g2==1)  {t=3;}//right
  251. }
  252. uchar touchSelf()                             //撞自己
  253. {
  254.     uchar i;
  255.     for (i=3;i<snake.length-1;i++)
  256.     {
  257.        if(snake.nodes[0].x==snake.nodes[i].x&&snake.nodes[0].y==snake.nodes[i].y)
  258.        return 1;
  259.     }
  260.     return 0;
  261. }
  262. uchar touchWall()                 //撞墙
  263. {
  264.     uchar x1=snake.nodes[0].x;
  265.     uchar y1=snake.nodes[0].y;
  266.     if(x1right||y1bottom)
  267.        return 1;
  268.     else
  269.        return 0;
  270. }
  271. void gameOver()               //游戏结束                                          
  272. {            
  273.    int k,n;
  274.    while(1)
  275.               {
  276.                             for(n = 0; n < 50; n++)
  277.                             {
  278.                                           for(k = 0; k < 16; k++)                                                                                                               
  279.                                           {            
  280.                                                         HC595SendData(~tab13[2*k +1],~tab13[2*k],tab0[2*k],tab0[2*k + 1]);              
  281.                                           }
  282.                             }
  283.               GPIO_DIG = ~DIG_CODE[score];  //数码管积分
  284.               beep_show();                //蜂鸣器响
  285.      }
  286. }
  287. void expandSnake()              //蛇身扩展                                                      
  288. {
  289.     snake.nodes[snake.length].x=lastx;
  290.     snake.nodes[snake.length].y=lasty;
  291.     snake.length++;
  292.               score++;
  293. }
  294. void createFood()    //创造食物   
  295. {
  296.               int i;
  297. label:
  298.               jfood+=3;            
  299.               if(jfood>=500) jfood=0;
  300.               food.position.x=((int)rand()%16);
  301.     food.position.y=((int)rand()%16);
  302.     for(i=0;i<=snake.length-1;i++)
  303.               {
  304.               if(snake.nodes[i].x==food.position.x&&snake.nodes[i].y==food.position.y)
  305.               goto label;
  306.               }
  307. }
  308. void draw()       //显示蛇长
  309. {
  310.        uint j,i,m,n;
  311.                  for(m=0;m<16;m++)
  312.                                  {
  313.            for(i=0;i<snake.length;i++)
  314.               {
  315.                                              for(j=0;j<snake.length;j++)
  316.                          if(m==i||m==j)               
  317.                           HC595SendData(  y[2*(snake.nodes[j].x)-2],y[2*(snake.nodes[j].x)-1],
  318.                                           x[2*(snake.nodes[i].y)-2],x[2*(snake.nodes[i].y)-1]);
  319.                                    }
  320.                             for(j=0;j<16;j++)
  321.                                {
  322.                                 if(food.position.x==j||food.position.y==j)                                            
  323.                                  HC595SendData(  y[2*(food.position.x)-2],y[2*(food.position.x)-1],
  324.                                        x[2*(food.position.y)-2],x[2*(food.position.y)-1]);
  325.            }
  326.                             for(n=25;n>0;n--)
  327.                                {
  328.                                HC595SendData(0xff,0xff,0,0);            
  329.                                }
  330.                               }
  331. }
  332. /********************************
  333.   LED部分函数    led.c
  334. ********************************/
  335. #define  _LED_C
  336. #include "config.h"
  337. #include "led.h"
  338. #include "snake.h"
  339. //点阵显示数组
  340. uchar code y[]={       //选列
  341. 0xff,0xfe, 0xff,0xfd,0xff,0xfb,0xff,0xf7, 0xff,0xef,0xff,0xdf,0xff,0xbf,0xff,0x7f,
  342. 0xfe,0xff, 0xfd,0xff,0xfb,0xff,0xf7,0xff,0xef,0xff,0xdf,0xff,0xbf,0xff,0x7f,0xff            
  343. };
  344. uchar code x[]={                    //选行
  345. 0x00,0x01, 0x00,0x02,              0x00,0x04,0x00,0x08, 0x00,0x10,0x00,0x20,0x00,0x40, 0x00,0x80,
  346. 0x01,0x00, 0x02,0x00,              0x04,0x00 , 0x08,0x00, 0x10,0x00, 0x20,0x00,0x40,0x00, 0x80,0x00,           
  347. };
  348. uchar code tab0[] =  //取模后选
  349. {0x00, 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08,0x00, 0x10, 0x00, 0x20, 0x00, 0x40, 0x00, 0x80,
  350. 0x01, 0x00, 0x02, 0x00, 0x04, 0x00, 0x08, 0x00,0x10, 0x00, 0x20, 0x00, 0x40, 0x00, 0x80, 0x00};
  351. uchar code tab8[] =
  352. {0,0,0,0,0,0,24,60,36,66,66,66,66,32,66,24,66,32,66,64,66,64,66,66,36,34,24,28,0,0,0,0};
  353. uchar code tab9[] =
  354. {0,0,0,0,0,0,24,60,36,66,66,66,66,66,66,32,66,32,66,16,66,8,66,4,36,66,24,126,0,0,0,0};
  355. uchar code tab10[] =
  356. {0,0,0,0,0,0,24,8,36,14,66,8,66,8,66,8,66,8,66,8,66,8,66,8,36,8,24,62,0,0,0,0};
  357. uchar code tab11[] =
  358. {0,0,0,0,0,0,24,24,36,36,66,66,66,66,66,66,66,66,66,66,66,66,66,66,36,36,24,24,0,0,0,0};
  359. uchar code tab12[] =   //显示倒计时
  360. {0,0,0,0,0,0,60,28,34,34,34,65,1,65,1,65,1,65,113,65,33,65,34,65,34,34,28,28,0,0,0,0};
  361. uchar code tab13[] =   //显示“囧”
  362. {0,0,0,0,254,63,6,48,102,51,54,54,62,60,30,60,6,48,246,55,54,54,54,54,54,54,54,54,254,63,0,0};
  363. void HC595SendData(  uchar BT3, uchar BT2,uchar BT1,uchar BT0)   // 595锁存器发送数据
  364. {
  365.               uchar i;
  366.      //发送第一个字节
  367.               for(i=0;i<8;i++)
  368.               {
  369.                             MOSIO = BT3 >> 7 ;                //从高位到低位
  370.                             BT3 <<= 1;

  371.                             S_CLK = 0;
  372.                             S_CLK = 1;                           
  373.               }
  374.      //发送第一个字节
  375.               for(i=0;i<8;i++)
  376.               {
  377.                             MOSIO = BT2 >>7;                //从高位到低位
  378.                             BT2 <<= 1;

  379.                             S_CLK = 0;
  380.                             S_CLK = 1;            
  381.               }
  382.       //发送第一个字节
  383.     for(i=0;i<8;i++)
  384.               {
  385.                             MOSIO = BT1 >> 7;                //从高位到低位
  386.                             BT1 <<= 1;

  387.                             S_CLK = 0;
  388.                             S_CLK = 1;            
  389.               }
  390.      //发送第一个字节
  391.         for(i=0;i<8;i++)
  392.               {
  393.                             MOSIO = BT0 >> 7;                            //从高位到低位

  394.                             BT0 <<= 1;
  395.                             S_CLK = 0;
  396.                             S_CLK = 1;
  397.               }
  398.   //输出
  399.     R_CLK = 0; //set dataline low
  400.               R_CLK = 1; //片选
  401.               R_CLK = 0; //set dataline low
  402. }                                                                           
  403. void st_led(void)   //点阵屏显示游戏
  404. {
  405.               int k, i, ms;
  406.               i = 60;                            //显示时间                                                                                                                                                                       
  407.               {
  408.                             HC595SendData(0xff,0xff,0,0);
  409.                             for(ms = i; ms > 0; ms--)
  410.                             {
  411.                                           for(k = 0; k < 16; k++)                                                                                                
  412.                                           {            
  413.                                                         HC595SendData(~tab8[2*k +1],~tab8[2*k],tab0[2*k],tab0[2*k + 1]);              
  414.                                           }
  415.                             }
  416.                             HC595SendData(0xff,0xff,0,0);   //清屏                                                                                                               
  417.                             for(ms = i; ms > 0; ms--)
  418.                             {
  419.                                           for(k = 0; k < 16; k++)                                                                                   
  420.                                           {            
  421.                                                         HC595SendData(~tab9[2*k +1],~tab9[2*k],tab0[2*k],tab0[2*k + 1]);              
  422.                                           }
  423.                             }            
  424.                             HC595SendData(0xff,0xff,0,0);              //清屏                                                                                                               
  425.                             for(ms = i; ms > 0; ms--)
  426.                             {
  427.                                           for(k = 0; k < 16; k++)                                                                                                               
  428.                                           {            
  429.                                                         HC595SendData(~tab10[2*k +1],~tab10[2*k],tab0[2*k],tab0[2*k + 1]);              
  430.                                           }
  431.                             }
  432.                             HC595SendData(0xff,0xff,0,0);              //清屏                                                                                                                  
  433.                             for(ms = i; ms > 0; ms--)
  434.                             {
  435.                                           for(k = 0; k < 16; k++)                                                                                                
  436.                                           {            
  437.                                                         HC595SendData(~tab11[2*k +1],~tab11[2*k],tab0[2*k],tab0[2*k + 1]);              
  438.                                           }
  439.                             }            
  440.                             HC595SendData(0xff,0xff,0,0);              //清屏                                                                                                                       
  441.                   for(ms = 120; ms > 0; ms--)
  442.                             {
  443.                                           for(k = 0; k < 16; k++)                                                                                                                                                         
  444.                                           {            
  445.                                                         HC595SendData(~tab12[2*k +1],~tab12[2*k],tab0[2*k],tab0[2*k + 1]);
  446.                                           }                                                      
  447.                             }                                                                                                                                   
  448.               }
  449. }
  450. /********************************
  451.   LCD部分函数    lcd.c
  452. ********************************/
  453. #define  _LCD_C
  454. #include"lcd.h"
  455. #include "config.h"
  456. /*****************************************************************
  457. * 函 数 名         : Lcd1602_Delay1ms
  458. * 函数功能                               : 延时函数,延时1ms
  459. * 输    入         : c
  460. ****************************************************************/
  461. void Lcd1602_Delay1ms(uint c) //误差 0us
  462. {
  463.     uchar a,b;
  464.               for (; c>0; c--)
  465.               {
  466.                             for (b=199;b>0;b--)
  467.                             {
  468.                                             for(a=1;a>0;a--);
  469.                             }     
  470.               }               
  471. }
  472. /****************************************************************
  473. * 函 数 名         : LcdWriteCom
  474. * 函数功能                               : 向LCD写入一个字节的命令

  475. *****************************************************************/
  476. #ifndef               LCD1602_4PINS //当没有定义这个LCD1602_4PINS时            
  477. void LcdWriteCom(uchar com)              //写入命令
  478. {
  479.               LCD1602_E = 0;  //使能清零  
  480.               LCD1602_RS = 0;              //选择发送命令  
  481.               LCD1602_RW = 0;              //选择写入  
  482.             
  483.               LCD1602_DATAPINS = com;  //放入命令  
  484.               Lcd1602_Delay1ms(1);                //等待数据稳定                           

  485.               LCD1602_E = 1;                 //写入时序      
  486.               Lcd1602_Delay1ms(5); //保持时间               
  487.               LCD1602_E = 0;
  488. }
  489. #else
  490. void LcdWriteCom(uchar com)                //写入命令
  491. {
  492.               LCD1602_E = 0;                //使能清零
  493.               LCD1602_RS = 0;                //选择写入命令
  494.               LCD1602_RW = 0;                //选择写入

  495.               LCD1602_DATAPINS = com;              //由于4位的接线是接到P0口的高四位,所以传送高四位不用改
  496.               Lcd1602_Delay1ms(1);

  497.               LCD1602_E = 1;  //写入时序            
  498.               Lcd1602_Delay1ms(5);
  499.               LCD1602_E = 0;

  500.               LCD1602_DATAPINS = com << 4;   //写入低四位
  501.               Lcd1602_Delay1ms(1);

  502.               LCD1602_E = 1;              //写入时序
  503.               Lcd1602_Delay1ms(5);
  504.               LCD1602_E = 0;
  505. }
  506. #endif
  507. /****************************************************************
  508. * 函 数 名       : LcdWriteData
  509. * 函数功能                            : 向LCD写入一个字节的数据
  510. * 输    入       : dat
  511. *****************************************************************/                              
  512. #ifndef               LCD1602_4PINS                                             
  513. void LcdWriteData(uchar dat)              //写入数据                           
  514. {
  515.               LCD1602_E = 0;              //使能清零
  516.               LCD1602_RS = 1;              //选择输入数据
  517.               LCD1602_RW = 0;              //选择写入

  518.               LCD1602_DATAPINS = dat;  //写入数据
  519.               Lcd1602_Delay1ms(1);

  520.               LCD1602_E = 1;  //写入时序
  521.               Lcd1602_Delay1ms(5);   //保持时间
  522.               LCD1602_E = 0;
  523. }
  524. #else
  525. void LcdWriteData(uchar dat)                            //写入数据            
  526. {
  527.               LCD1602_E = 0;                //使能清零
  528.               LCD1602_RS = 1;                //选择写入数据
  529.               LCD1602_RW = 0;              //选择写入

  530.               LCD1602_DATAPINS = dat;//由于4位的接线是接到P0口的高四位,所以传送高四位不用改            
  531.               Lcd1602_Delay1ms(1);

  532.               LCD1602_E = 1;                //写入时序
  533.               Lcd1602_Delay1ms(5);
  534.               LCD1602_E = 0;

  535.               LCD1602_DATAPINS = dat << 4;  //写入低四位
  536.               Lcd1602_Delay1ms(1);

  537.               LCD1602_E = 1;              //写入时序
  538.               Lcd1602_Delay1ms(5);
  539.               LCD1602_E = 0;
  540. }
  541. #endif
  542. /*********************************************************************
  543. * 函 数 名       : LcdInit()
  544. * 函数功能                            : 初始化LCD屏
  545. ********************************************************************/                              
  546. #ifndef                            LCD1602_4PINS
  547. void LcdInit()                                          //LCD初始化子程序                                          
  548. {
  549.               LcdWriteCom(0x38);   //开显示
  550.               LcdWriteCom(0x0c);   //开显示不显示光标
  551.               LcdWriteCom(0x06);   //写一个指针加1
  552.               LcdWriteCom(0x01);   //清屏
  553.               LcdWriteCom(0x80);   //设置数据指针起点
  554. }
  555. #else
  556. void LcdInit()                                          //LCD初始化子程序                                          
  557. {
  558.               LcdWriteCom(0x32);              //将8位总线转为4位总线
  559.               LcdWriteCom(0x28);              //在四位线下的初始化
  560.               LcdWriteCom(0x0c);   //开显示不显示光标
  561.               LcdWriteCom(0x06);   //写一个指针加1
  562.               LcdWriteCom(0x01);   //清屏
  563.               LcdWriteCom(0x80);   //设置数据指针起点
  564. }
  565. #endif
  566. /********************************
  567.   LCD部分函数    lcd_show.c
  568. ********************************/
  569. #define  _LCD_C
  570. #include"lcd.h"
  571. #include "config.h"

  572. unsigned char PuZh[30] = "    snake game   snake game ";
  573. unsigned char CnCh[30] = "    k2 up k6 down k2+k6 right";

  574. void Delay10ms(unsigned int c)  //延时
  575. {
  576.     unsigned char a, b;
  577.                   for (;c>0;c--)
  578.               {
  579.                             for (b=38;b>0;b--)
  580.                             {
  581.                                           for (a=130;a>0;a--);
  582.                             }         
  583.               }      
  584. }
  585. void lcd_show(void)
  586. {
  587.               unsigned char i;
  588.               LcdInit();  //初始化
  589.               //--写第一行--//
  590.               for(i=0; i<30; i++)
  591.               {
  592.                             LcdWriteData(PuZh[i]);            
  593.               }
  594.               LcdWriteCom(0xC0);
  595.               for(i=0; i<30; i++)
  596.               {
  597.                             LcdWriteData(CnCh[i]);
  598.               }
  599.               LcdWriteCom(0x07);
  600.               {                                         
  601.                             LcdWriteCom(0xC0);
  602.                             for(i=0; i<30; i++)
  603.                             {
  604.                                           LcdWriteData(CnCh[i]);
  605.                                           Delay10ms(75);
  606.                             }
  607.               }                                                      
  608. }
  609. /********************************
  610.   beep部分函数    beep.c
  611. ********************************/
  612. #include "config.h"  
  613. unsigned char n=0;       //n为节拍常数变量   
  614. unsigned char code music_tab[] ={  
  615. 0x18, 0x30, 0x1C , 0x10,   //格式为: 频率常数, 节拍常数, 频率常数, 节拍常数,     
  616. 0x20, 0x40, 0x1C , 0x10,  
  617. 0x18, 0x10, 0x20 , 0x10,  
  618. 0x1C, 0x10, 0x18 , 0x40,  
  619. 0x1C, 0x20, 0x20 , 0x20,  
  620. 0x1C, 0x20, 0x18 , 0x20,  
  621. 0x20, 0x80, 0xFF , 0x20,  
  622. 0x30, 0x1C, 0x10 , 0x18,  
  623. 0x20, 0x15, 0x20 , 0x1C,  
  624. 0x20, 0x20, 0x20 , 0x26,  
  625. 0x40, 0x20, 0x20 , 0x2B,  
  626. 0x20, 0x26, 0x20 , 0x20,  
  627. 0x20, 0x30, 0x80 , 0xFF,  
  628. 0x20, 0x20, 0x1C , 0x10,  
  629. 0x18, 0x10, 0x20 , 0x20,  
  630. 0x26, 0x20, 0x2B , 0x20,  
  631. 0x30, 0x20, 0x2B , 0x40,  
  632. 0x20, 0x20, 0x1C , 0x10,  
  633. 0x18, 0x10, 0x20 , 0x20,  
  634. 0x26, 0x20, 0x2B , 0x20,  
  635. 0x30, 0x20, 0x2B , 0x40,  
  636. 0x20, 0x30, 0x1C , 0x10,  
  637. 0x18, 0x20, 0x15 , 0x20,  
  638. 0x1C, 0x20, 0x20 , 0x20,  
  639. 0x26, 0x40, 0x20 , 0x20,  
  640. 0x2B, 0x20, 0x26 , 0x20,  
  641. 0x20, 0x20, 0x30 , 0x80,  
  642. 0x20, 0x30, 0x1C , 0x10,  
  643. 0x20, 0x10, 0x1C , 0x10,  
  644. 0x20, 0x20, 0x26 , 0x20,  
  645. 0x2B, 0x20, 0x30 , 0x20,  
  646. 0x2B, 0x40, 0x20 , 0x15,  
  647. 0x1F, 0x05, 0x20 , 0x10,  
  648. 0x1C, 0x10, 0x20 , 0x20,  
  649. 0x26, 0x20, 0x2B , 0x20,  
  650. 0x30, 0x20, 0x2B , 0x40,  
  651. 0x20, 0x30, 0x1C , 0x10,  
  652. 0x18, 0x20, 0x15 , 0x20,  
  653. 0x1C, 0x20, 0x20 , 0x20,  
  654. 0x26, 0x40, 0x20 , 0x20,  
  655. 0x2B, 0x20, 0x26 , 0x20,  
  656. 0x20, 0x20, 0x30 , 0x30,  
  657. 0x20, 0x30, 0x1C , 0x10,  
  658. 0x18, 0x40, 0x1C , 0x20,  
  659. 0x20, 0x20, 0x26 , 0x40,  
  660. 0x13, 0x60, 0x18 , 0x20,  
  661. 0x15, 0x40, 0x13 , 0x40,  
  662. 0x18, 0x80, 0x00  
  663. };   
  664. void int0()  interrupt 1 using 1   //采用中断控制节拍  
  665. {  TH0=0xd8;  
  666.    TL0=0xef;  
  667.    n--;  
  668. }     
  669. void delay (unsigned char m)       //控制频率延时
  670. {  
  671. unsigned i=3*m;  
  672. while(--i);  
  673. }  
  674. void delayms(unsigned char a)      //毫秒延时子程序
  675. {  
  676.   while(--a);                    //采用while(--a) 不要采用while(a--)
  677. }  

  678. void beep_show()  
  679. { unsigned char p,m;     //m为频率常数变量
  680.   unsigned char i=0;  
  681.   TMOD&=0x0f;  
  682.   TMOD|=0x01;  
  683.   TH0=0xd8;TL0=0xef;  
  684.   IE=0x82;  
  685. play:  
  686.    while(1)  
  687.     {  
  688.     a: p=music_tab[i];  
  689.    if(p==0x00)    { i=0, delayms(1000); goto play;   //如果碰到结束符,延时1秒,回到开始再来一遍
  690.        else if(p==0xff)  { i=i+1;delayms(100),TR0=0; goto a;}   //若碰到休止符,延时100ms,继续取下一音符  
  691.         else     {m=music_tab[i++], n=music_tab[i++];} //取频率常数 和 节拍常数     
  692.         TR0=1;      //开定时器                                          
  693.          while(n!=0) Beep=~Beep,delay(m);   //等待节拍完成, 通过输出音频                          
  694.        TR0=0;             //关定时器                                   
  695.     }  
  696. }
复制代码

本制作的完整文档下载:
点阵屏实现贪吃蛇游戏.docx (3.06 MB, 下载次数: 119)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏4 分享淘帖 顶1 踩
回复

使用道具 举报

沙发
ID:148621 发表于 2016-11-20 16:11 | 只看该作者
好厉害!!!!!大神跪拜~!
回复

使用道具 举报

板凳
ID:148621 发表于 2016-11-29 22:04 | 只看该作者
楼主,请问595怎么用的呀.有点没看懂呢QAQ
回复

使用道具 举报

地板
ID:89286 发表于 2017-3-26 23:26 | 只看该作者
thanks for sharing. amazing...
回复

使用道具 举报

5#
ID:184098 发表于 2017-3-28 14:37 | 只看该作者
一直搞不明白怎么做到的!大神!膜拜!
回复

使用道具 举报

6#
ID:184207 发表于 2017-3-28 18:57 | 只看该作者
加工客观比较高看过没
回复

使用道具 举报

7#
ID:335292 发表于 2018-5-21 22:31 | 只看该作者
编译出来,怎么会这么多错误啊
回复

使用道具 举报

8#
ID:343482 发表于 2018-6-3 22:27 | 只看该作者
x1right为什么找不到的呢?是什么意思啊?
回复

使用道具 举报

9#
ID:343482 发表于 2018-6-3 22:28 | 只看该作者
x1right是什么意思啊?为什么编译出来不认?
回复

使用道具 举报

10#
ID:311166 发表于 2018-6-11 11:14 | 只看该作者
厉害了
回复

使用道具 举报

11#
ID:90677 发表于 2018-8-2 11:08 | 只看该作者
楼主666,慢慢看慢慢学习
回复

使用道具 举报

12#
ID:382308 发表于 2018-8-2 20:57 | 只看该作者
大神太强了
回复

使用道具 举报

13#
ID:1065964 发表于 2023-3-10 10:36 | 只看该作者
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表