|
基于51單片機(jī)做的貪吃蛇程序和仿真圖都有,是用的160128液晶屏做的,比較大氣。 希望對大家有幫助.下面是proteus仿真原理圖:
0.png (28.75 KB, 下載次數(shù): 95)
下載附件
2017-5-26 01:56 上傳
0.png (56.23 KB, 下載次數(shù): 134)
下載附件
2017-5-26 01:54 上傳
單片機(jī)源程序如下(含160128液晶屏的驅(qū)動(dòng)程序):
- /*---------------------------------------------------------------------------
- 文件功能:貪吃蛇游戲主程序
- 編寫者:笨小孩
- 版本:最終版本
- 作者郵箱:liaoguobao19860822@163.com
- 單片機(jī)類型:AT89C51
- 仿真軟件:Proteus7.2
- 代碼編譯軟件:Keil2
- ----------------------------------------------------------------------------*/
- #include "t6963c.h"
- #include "at89x51.h"
- #include "MoveDeal.h"
- #include "160128_driver.h"
- /*+-----------------------+
- | 注意:晶振為22.1184MHz |
- +-----------------------+*/
- /*---------------------------------------------------------------*/
- //按鍵端口宏定義
- #define KEY_UP_MOVE P1_0 //上移鍵端口
- #define KEY_LEFT_MOVE P1_1 //左移鍵端口
- #define KEY_RIGHT_MOVE P1_2 //右移鍵端口
- #define KEY_DOWN_MOVE P1_3 //下移鍵端口
- #define KEY_OK P1_4 //確定鍵端口
- #define KEY_SPEED_MOVE P3_2 //加速鍵端口
- #define KEY_CUSTOMS P3_3 //選關(guān)鍵端口
- #define KEY_RESTORAL P3_4 //復(fù)位鍵端口
- /*----------------------------------------------------------------------------*/
- //全局變量定義
- //游戲速度,初始值為GRADE_1(1秒移動(dòng)一次蛇頭)
- unsigned char m_Speed = GRADE_1;
- //更新顯示標(biāo)志,當(dāng)m_Flag為1時(shí)要刷新界面
- bit m_Flag = 0;
- //更新時(shí)間標(biāo)志,當(dāng)m_TimeFlag為1時(shí)要刷新時(shí)間
- bit m_TimeFlag = 0;
- //選關(guān)按鈕是否被按下標(biāo)志位
- bit m_IsChoose = 0;
- //時(shí)間變量
- unsigned int m_Time = 0;
- //蛇頭運(yùn)動(dòng)方向,初始值為MOVE_DOWN(默認(rèn)向下運(yùn)動(dòng))
- unsigned char m_MoveState = MOVE_DOWN;
- /*----------------------------------------------------------------------------*/
- //延時(shí)10ms子程序(為鍵盤消抖服務(wù))
- void Delay_10ms()
- {
- unsigned char a, b, c;
- for (a = 0; a < 10; a++)
- for (b = 0; b < 3; b++)
- for (c = 0; c < 220; c++);
- }
- /*----------------------------------------------------------------------------*/
- /*清除字符函數(shù)*/
- void ClearChar()
- {
- Set_xy(7,9);
- Write_8_Data(0,NOC_WR);//G
- Set_xy(7,10);
- Write_8_Data(0,NOC_WR);//A
- Set_xy(7,11);
- Write_8_Data(0,NOC_WR);//M
- Set_xy(7,12);
- Write_8_Data(0,NOC_WR);//E
- Set_xy(7,15);
- Write_8_Data(0,NOC_WR);//O
- Set_xy(7,16);
- Write_8_Data(0,NOC_WR);//V
- Set_xy(7,17);
- Write_8_Data(0,NOC_WR);//E
- Set_xy(7,18);
- Write_8_Data(0,NOC_WR);//R
- }
- /*----------------------------------------------------------------------------*/
- //游戲結(jié)束或復(fù)位時(shí)的while循環(huán)
- void Loop()
- {
- while (1) /*判斷確定建是否按下,屏蔽其他鍵的功能*/
- {
- if (!KEY_OK)
- {
- Delay_10ms();
- if(!KEY_OK)
- while(!KEY_OK); /*等待確定鍵被按下*/
- m_Time = 0; /*初始時(shí)間*/
- ClearChar(); /*清除字符*/
- RandFood(); /*隨機(jī)食物生成*/
- break;
- }
- }
- }
- /*-----------------------------主程序開始---------------------------------*/
- void main()
- {
- TMOD = 0x01; /*定時(shí)器設(shè)置(只用到了定時(shí)器1)*/
- InitializeSeries(); /*串口初始化*/
- TH0 = 0x4C;TL0 = 0x00; /*定時(shí)器0初始值設(shè)定(25ms中斷一次)*/
- EA = TR0 = 1; /*開啟定時(shí)器0定時(shí)*/
- Init_disply(0); /*初始化開機(jī)界面顯示*/
- SendString("Relive childhood popularity."); /*虛擬終端顯示*/
- SendString("Challenge the limits of intellectual.");
- SendString("GreedySnaker based on single-chip design.\r");
- SendString("Game instructions:");
- SendString("[0] key is Choice_Customs.");
- SendString("[1] key is Left_Move.");
- SendString("[2] key is Down_Move.");
- SendString("[3] key is Right_Move.");
- SendString("[5] key is On_Move.");
- SendString("[Enter] key is pause.");
- SendString("[SpaceBar] key is SpeedUp_Move.");
- while(1) /*判斷確定建是否按下,屏蔽其他鍵的功能*/
- {
- if(!KEY_OK)
- {
- Delay_10ms();
- if(!KEY_OK)
- while(!KEY_OK);
- break;
- }
- }
- Circle(63, 79, 60, 0); /*清除圓*/
- Clear_LCD(); /*清屏顯示*/
- Line(63, 0, 63, 127, 1); /*畫分隔線*/
- InitStruct(); /*初始化結(jié)構(gòu)體數(shù)組*/
- Init_disply(1); /*初始化游戲開始界面顯示*/
- RandFood(); /*隨機(jī)食物生成*/
- ET0=1; /*開啟定時(shí)器0中斷*/
- while(1) /*進(jìn)入主循環(huán),鍵盤掃描開始,響應(yīng)按鍵事件*/
- {
- if(!KEY_LEFT_MOVE) /*響應(yīng)向左運(yùn)動(dòng)事件*/
- {
- Delay_10ms();
- if(!KEY_LEFT_MOVE)
- {
- while(!KEY_LEFT_MOVE);
- if (m_MoveState != MOVE_RIGHT) /*如果蛇頭運(yùn)動(dòng)方向向右,直接跳過*/
- m_MoveState = MOVE_LEFT; /*刷新蛇頭運(yùn)動(dòng)方向(向左)*/
- }
- }
- if(!KEY_RIGHT_MOVE) /*響應(yīng)向右運(yùn)動(dòng)事件*/
- {
- Delay_10ms();
- if(!KEY_RIGHT_MOVE)
- {
- while(!KEY_RIGHT_MOVE);
- if (m_MoveState != MOVE_LEFT) /*如果蛇頭運(yùn)動(dòng)方向向左,直接跳過*/
- m_MoveState = MOVE_RIGHT; /*刷新蛇頭運(yùn)動(dòng)方向(向右)*/
- }
- }
- if(!KEY_UP_MOVE) /*響應(yīng)向上運(yùn)動(dòng)事件*/
- {
- Delay_10ms();
- if(!KEY_UP_MOVE)
- {
- while(!KEY_UP_MOVE);
- if (m_MoveState != MOVE_DOWN) /*如果蛇頭運(yùn)動(dòng)方向向下,直接跳過*/
- m_MoveState = MOVE_UP; /*刷新蛇頭運(yùn)動(dòng)方向(向上)*/
- }
- }
- if (!KEY_DOWN_MOVE) /*響應(yīng)向下運(yùn)動(dòng)事件*/
- {
- Delay_10ms();
- if (!KEY_DOWN_MOVE)
- {
- while (!KEY_DOWN_MOVE);
- if (m_MoveState != MOVE_UP) /*如果蛇頭運(yùn)動(dòng)方向向上,直接跳過*/
- m_MoveState = MOVE_DOWN; /*刷新蛇頭運(yùn)動(dòng)方向(向下)*/
- }
- }
- if(!KEY_CUSTOMS) /*響應(yīng)選關(guān)事件*/
- {
- Delay_10ms();
- if(!KEY_CUSTOMS)
- {
- while(!KEY_CUSTOMS);
- TR0 = 0;
- m_IsChoose = 1;
- Choice_Customs();
- }
- }
- /*******************為選關(guān)服務(wù)*********************/
- /**/ if(!KEY_OK && m_IsChoose) /**/
- /**/ { /**/
- /**/ Delay_10ms(); /**/
- /**/ if(!KEY_OK) /**/
- /**/ while(!KEY_OK); /**/
- /**/ m_IsChoose = 0; /**/
- /**/ TR0 = 1; /**/
- /**/ }//只有選關(guān)后確定鍵才有效 /**/
- /**************************************************/
- if(!KEY_RESTORAL) /*響應(yīng)復(fù)位事件*/
- {
- Delay_10ms();
- if(!KEY_RESTORAL)
- {
- while(!KEY_RESTORAL);
- Restoral(); /*游戲復(fù)位處理*/
- }
- }
- if(!KEY_SPEED_MOVE) /*響應(yīng)加速事件*/
- {
- Delay_10ms();
- if(!KEY_SPEED_MOVE)
- {
- TR0 = 0;
- while(!KEY_SPEED_MOVE)
- {
- MoveProcess(); /*一直調(diào)用移動(dòng)處理*/
- Delay_100ms(); /*延時(shí)100毫秒,也就是加速速度*/
- }
- TR0 = 1;
- }
- }
- if (m_Flag) /*響應(yīng)正常的移動(dòng)事件*/
- {
- m_Flag = 0;
- MoveProcess(); /*移動(dòng)處理*/
- }
- if (m_TimeFlag) /*刷新時(shí)間顯示*/
- {
- m_TimeFlag = 0;
- TimeDisplay();
- }
- Terminal(); /*響應(yīng)鍵盤消息*/
- }
- }
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
貪吃蛇.rar
(129.26 KB, 下載次數(shù): 65)
2017-5-25 20:25 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|