mdk keil写的 直接导入hex文件就可以运行
单片机源程序如下:
- /*********************************************************************************************
- File Name : snake.c
- Content : snake management module
- **********************************************************************************************/
- #include "snake.h"
- /**********************************************************************************************
- 不允许被外部调用的函数,受保护的对象
- **********************************************************************************************/
- static void Game_CreateFood(void);
- static void Game_AddSnake(void);
- static void Game_Over(void) ;
- static int Game_Collide(void);
- static void Game_SnakeMove(void);
- /**********************************************************************************************
- 所引用别的文件中的全局变量
- **********************************************************************************************/
- extern uint8 ucGameBegin_Flag;
- extern uint8 ucGamePause_Flag;
- extern uint8 ucGameOver_Flag;
- extern INT8U ucKey;
- extern INT8U ucLastKey;
- extern long long time ;
- /**********************************************************************************************
- 本文件中定义的全局变量,记录蛇的信息
- **********************************************************************************************/
- INT32U Snake[2][LEN]={{2,1,0},{3,3,3}};
- INT32U SHead = 3;
- INT32U SEnd = 0;
- INT32U Row_Food;
- INT32U Col_Food;
- INT32U uiScore = 0;
- uint16 uiLevel = 0;
- INT8U ucRow;
- INT8U ucCol;
- INT8U ucSnakeSpeed;
- /**********************************************************************************************
- 所取的一些游戏中需要的文字和图像的模
- **********************************************************************************************/
- INT8U ucGameOver[5][32] =
- {
- {0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
- 0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20
- },
- {0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
- 0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00
- },
- {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00
- },
- {0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
- 0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00
- },
- {0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
- }
- } ;
- INT8U ucFuck[3][32] =
- {
- {0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
- 0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00
- },
- {0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,
- 0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00
- },
- {0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00
- }
- };
- /*********************************************************************************************
- Function : Game_CreateSnake(INT8U row, INT8U col)
- Content : 创建一条蛇
- Parameter : row 蛇位置的行
- col 蛇位置的列
- Return : none
- Date : 2012/09/18
- Description: 创建一条蛇
- Remark :
- **********************************************************************************************/
- void Game_CreateSnake(INT8U row, INT8U col)
- {
- ucRow = row;
- ucCol = col;
- Snake[0][0] = row;
- Snake[0][1] = row;
- Snake[0][2] = row;
- Snake[1][0] = col - 2;
- Snake[1][1] = col - 1;
- Snake[1][2] = col;
- LCD2_DisPoint(row, col--);
- LCD2_DisPoint(row, col--);
- LCD2_DisPoint(row, col--);
- SEnd = 0;
- SHead = 3;
- Game_CreateFood();
- ucSnakeSpeed = 20;
- LCD1_DisplaySco(uiScore);
- LCD1_DisplayTime(time);
- LCD1_ShowInt(0xcb, uiLevel);
- }
- /*********************************************************************************************
- Function : Game_CreateFood(void)
- Content : 食物生成的实现函数
- Parameter : void
- Return : none
- Date : 2012/09/18
- Description: 食物生成的实现函数
- Remark :
- **********************************************************************************************/
- void Game_CreateFood(void)
- {
- TEST:
- do
- {
- Row_Food = (int)rand() % 14 + 1;
- Col_Food = (int)rand() % 30 + 1;
- }while(LCD2_IfPoint(Row_Food, Col_Food));
- LCD2_DisPoint(Row_Food, Col_Food);
- if(!LCD2_IfPoint(Row_Food, Col_Food))
- goto TEST;
- return;
- }
- /*********************************************************************************************
- Function : Game_AddSnake(void)
- Content : 蛇节点的增加
- Parameter : void
- Return : none
- Date : 2012/09/18
- Description: 蛇增加一个节点,并重新随机生成一个食物
- Remark :
- **********************************************************************************************/
- void Game_AddSnake(void)
- {
- switch(ucKey)
- {
- case UP:
- {
- Snake[0][SHead] = ucRow - 1;
- Snake[1][SHead] = ucCol;
- if(SHead + 1 == LEN)
- SHead = 0;
- else
- SHead++;
- ucRow--;
- Game_CreateFood();
- }break;
- case DOWN:
- {
- Snake[0][SHead] = ucRow + 1;
- Snake[1][SHead] = ucCol;
- if(SHead + 1 == LEN)
- SHead = 0;
- else
- SHead++;
- ucRow++;
- Game_CreateFood();
- }break;
- case LEFT:
- {
- Snake[0][SHead] = ucRow;
- Snake[1][SHead] = ucCol - 1;
- if(SHead + 1 == LEN)
- SHead = 0;
- else
- SHead++;
- ucCol--;
- Game_CreateFood();
- }break;
- case RIGHT:
- {
- Snake[0][SHead] = ucRow;
- Snake[1][SHead] = ucCol + 1;
- if(SHead + 1 == LEN)
- SHead = 0;
- else
- SHead++;
- ucCol++;
- Game_CreateFood();
- }break;
- }
- Music_AddSnake();
- uiScore = uiScore + (22 - ucSnakeSpeed);
- if((uiScore > (uiLevel * 2 + 1) * 6) && ucSnakeSpeed > 10)
- {
- uiLevel++;
- if(ucSnakeSpeed > 10)
- ucSnakeSpeed = ucSnakeSpeed - 5;
- }
- LCD1_DisplaySco(uiScore);
- LCD1_ShowInt(0xcb, uiLevel);
- return;
- }
- /*********************************************************************************************
- Function : Game_Over(void)
- Content : 游戏结束
- Parameter : void
- Return : none
- Date : 2012/09/18
- Description: 游戏结束界面的绘制
- Remark :
- **********************************************************************************************/
- void Game_Over(void)
- {
- ucGameBegin_Flag = 0;
- ucGameOver_Flag = 1;
- uiScore = 0;
- uiLevel = 0;
- time = 0;
- ucSnakeSpeed = 20;
- LCD2_Init();
- LCD2_DispChinese(4, 30, ucGameOver, 5);
- LCD2_DispChinese(2, 40, ucFuck, 3);
- Music_GameOver();
- return;
- }
- /*********************************************************************************************
- Function : Game_Collide(void)
- Content : 碰撞检测
- Parameter : void
- Return : int
- Date : 2012/09/18
- Description: 蛇与障碍物的碰撞检测,还有与食物的碰撞检测
- Remark :
- **********************************************************************************************/
- int Game_Collide(void)
- {
- switch(ucKey)
- {
- case UP :
- {
- if((ucRow - 1) == Row_Food && ucCol == Col_Food)
- Game_AddSnake();
- else
- {
- Game_Over();
- return 0;
- }
- }break;
- case DOWN:
- {
- if((ucRow + 1) == Row_Food && ucCol == Col_Food)
- Game_AddSnake();
- else
- {
- Game_Over();
- return 0;
- }
- }break;
- case LEFT:
- {
- if(ucRow == Row_Food && (ucCol - 1) == Col_Food)
- Game_AddSnake();
- else
- {
- Game_Over();
- return 0;
- }
- }break;
- case RIGHT:
- {
- if(ucRow == Row_Food && (ucCol + 1) == Col_Food)
- Game_AddSnake();
- else
- {
- Game_Over();
- return 0;
- }
- }break;
- }
- return 1;
- }
- /*********************************************************************************************
- Function : Game_SnakeMove(void)
- Content : 蛇的移动
- Parameter : void
- Return : none
- Date : 2012/09/18
- Description: 蛇的移动
- Remark :
- **********************************************************************************************/
- void Game_SnakeMove(void)
- {
- int i;
- while(ucGamePause_Flag)
- Key_Scan();
- switch(ucKey)
- {
- case UP:
- {
- if(ucLastKey == DOWN)
- {
- ucLastKey = DOWN;
- if(LCD2_IfPoint(ucRow + 1, ucCol))
- i = Game_Collide();
- if(i == 0) return;
- LCD2_DisPoint(++ucRow, ucCol);
-
- }
- else
- {
- ucLastKey = UP;
- if(LCD2_IfPoint(ucRow - 1, ucCol))
- i = Game_Collide();
- if(i == 0) return;
- LCD2_DisPoint(--ucRow, ucCol);
-
- }
-
- }break;
- case DOWN:
- {
- if(ucLastKey == UP)
- {
- ucLastKey = UP;
- if(LCD2_IfPoint(ucRow - 1, ucCol))
- i = Game_Collide();
- if(i == 0) return;
- LCD2_DisPoint(--ucRow, ucCol);
-
- }
- else
- {
- ucLastKey = DOWN;
- if(LCD2_IfPoint(ucRow + 1, ucCol))
- i = Game_Collide();
- if(i == 0) return;
- LCD2_DisPoint(++ucRow, ucCol);
-
- }
-
- }break;
- case LEFT:
- {
- if(ucLastKey == RIGHT)
- {
- ucLastKey = RIGHT;
- if(LCD2_IfPoint(ucRow, ucCol + 1))
- i = Game_Collide();
- if(i == 0) return;
- LCD2_DisPoint(ucRow, ++ucCol);
-
- }
- else
- {
- ucLastKey = LEFT;
- if(LCD2_IfPoint(ucRow, ucCol - 1))
- i = Game_Collide();
- if(i == 0) return;
- LCD2_DisPoint(ucRow, --ucCol);
-
- }
-
- }break;
- case RIGHT :
- {
- if(ucLastKey == LEFT)
- {
- ucLastKey = LEFT;
- if(LCD2_IfPoint(ucRow, ucCol - 1))
- i = Game_Collide();
- if(i == 0) return;
- LCD2_DisPoint(ucRow, --ucCol);
-
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
my_snake.zip
(312 KB, 下载次数: 43)
|