《嵌入式期末考之贪吃蛇游戏机的设计.doc》由会员分享,可在线阅读,更多相关《嵌入式期末考之贪吃蛇游戏机的设计.doc(32页珍藏版)》请在三一办公上搜索。
1、嵌入式期末考之贪吃蛇游戏机的设计一、 项目总体说明1、用arm7系列型号为lpc2131的芯片设计一个贪吃蛇游戏机,其中还加入了UCOS II操作系统。2、用LCD12864来显示游戏的主界面,以及用LCD1602显示游戏开始的计时(Time)、游戏所得的分数(Score)和游戏通过的关数(Lv)。其中“蛇”每吃的一个果实分数加1分,吃到2个果实游戏的关数加1,随着分数和关数的增加,“蛇”跑动的速度越快,“蛇”的长度也越长,游戏的难度也越大。当“蛇”碰到游戏界面的边框时,游戏结束.3、用5个独立按键来控制“蛇”的上、下、左、右,以及游戏的重新开始。二、 项目硬件设计1、电源模块:用3.3V的直
2、流电源。2、控制模块:采用独立式按键,独立式按键电路配置灵活,硬件结构简单,但每个按键必须占用一根I/O口线。在按键数量较多时,I/O口浪费较大。故只在按键数量不多时,采用这种按键。3、显示模块:采用LCD12864和LCD1602,它是一种专门用于显示字母、数字、符号、字符等点阵式LCD。其有显示质量高、数字接口、功耗低、体积小等优点。LPC2131LCD12864控制模块电源模块LCD1602图2.1 硬件模块划分三、 项目软件设计1、本设计包含了Configure.h、Includes.H、Os_cfg.h、LCD1602.h、LCD12864.h、Font.h、Graphic.h等头文
3、件。以及包含了游戏显示信息GameInfo.c,按键处理KeyProcess.c,游戏处理GamePlay.c,游戏图形处理GameGraph.c,游戏动作处理GameActive.c等.c文件。2、主函数main.c里面包括了系统初始化函数OSInit(),界面初始化函数vGUIInit(),游戏初始化函数vGameInit(),以及系统开始函数OSStart()。3、UCOS II操作系统中,在游戏初始化函数vGameInit()中创建了一个游戏任务vTaskGame,优先级为6;然后在游戏任务vTaskGame中创建了按键任务TaskKey,优先级为10,以及显示游戏信息任务vTaskI
4、nfo,优先级为20.四、 系统运行截图1、 系统运行截图:2、 “蛇”吃了一个果实之后,Score加1:3、“蛇”吃了2个果实后,Lv加1:五、软件源程序/*main.c*/*/*UCOS+GUI(Greedy Snake) */*在LPC2131上使用的UCOS和12864的GUI,以及1602驱动。 */*#include Configure.h/* 名称:main* 功能:主函数*/int main (void)/ OSInit(); / vGUIInit(); / vGameInit(); / OSStart(); / return 0;/*/* */*显示游戏信息* */* */*
5、#include Configure.h/OS_STK st_InfoStkTASK_STK_SIZE;/* 名称:vShowTime* 功能:显示游戏时间*/INT8U uca_TimeStr=Time ;void vShowTime(INT32U ulTime)INT8U ucMinute,ucSecond;ucSecond=ulTime%60;ucMinute=ulTime/60%60;vWriteCMD1(FIRST_LINE+1);vShowString1(uca_TimeStr);vShowChar1(ucMinute/10+0);vShowChar1(ucMinute%10+0)
6、;vShowChar1(:);vShowChar1(ucSecond/10+0);vShowChar1(ucSecond%10+0);/* 名称:uca_ScoreStr* 功能:显示游戏分数*/INT8U uca_ScoreStr=Score:;void vShowScore(INT32U ulScore)vWriteCMD1(SECOND_LINE);vShowString1(uca_ScoreStr); vShowChar1(ulScore/1000%10+0); vShowChar1(ulScore/100%10+0); vShowChar1(ulScore/10%10+0); vSh
7、owChar1(ulScore%10+0);/* 名称:uca_LevelStr* 功能:显示游戏等级*/INT8U uca_LevelStr=Lv:;void vShowLevel(INT8U ucLevel)vWriteCMD1(SECOND_LINE+11);vShowString1(uca_LevelStr);vShowChar1(ucLevel/10%10+0);vShowChar1(ucLevel%10+0);/* 名称:vTaskInfo* 功能:显示游戏信息*/void vTaskInfo(void *pdata)while(1)vShowTime(st_Snake.ulTim
8、e+);vShowScore(st_Snake.ulScore);vShowLevel(st_Snake.ucLevel);OSTimeDlyHMSM(0,0,0,80);/*/* */*按键处理* */* */*#include Configure.hOS_STK st_KeyStkTASK_STK_SIZE;/#define KEY_SEL PINSEL1#define KEY_DIR IODIR#define KEY_SET IOSET#define KEY_CLR IOCLR#define KEY_PIN IOPIN #define KEY_BEG 25/* 名称:vKeyInit*
9、功能:按键初始化*/void vKeyInit(void)KEY_SEL&=0xFC000000;/设置P0.16P0.28为GPIO口KEY_DIR|=0xFF00FFFF;/设置P0.25P0.28为输入KEY_SET|=0xFF00FFFF;/* 名称:vKeyProcess* 功能:按键处理*/void vKeyProcess(void)INT8U ucKeyCode,ucTemp;ucKeyCode=(INT8U)(KEY_PIN25)&0x0F;if(!ucKeyCode)return;switch(ucKeyCode)case 1:ucTemp=ASPECT_LEFT ;brea
10、k; /leftcase 2:ucTemp=ASPECT_TOP ;break; /upcase 4:ucTemp=ASPECT_RIGHT ;break; /rightcase 8:ucTemp=ASPECT_BOTTOM;break; /downdefault:break; if(st_Snake.ucAspect+ucTemp)!=5) st_Snake.ucAspect=ucTemp;/* 名称:vTaskKey* 功能:按键处理*/void vTaskKey(void *pdata)while(1)vKeyProcess();OSTimeDlyHMSM(0,0,0,22);/*/*
11、*/*游戏处理* */* */*#include Configure.h/SNAKE st_Snake;/* 名称:ucMoveDecide()* 功能:判断是否可以移动*/INT8U ucMoveDecide(void)INT8U ucRow,ucColumn;ucRow=*(st_Snake.pucMap);ucColumn=*(st_Snake.pucMap+1);switch(st_Snake.ucAspect)case ASPECT_LEFT:ucColumn-;break;case ASPECT_TOP:ucRow -;break;case ASPECT_RIGHT:ucColum
12、n+;break;case ASPECT_BOTTOM:ucRow +;break;default:break;if(ucRowROW_MAX|ucColumnCOL_MAX)return GAME_OVER;if(ucRow=st_Snake.ucBeanRow)&(ucColumn=st_Snake.ucBeanColumn)return GAME_BEAN;if(ucGetBean(ucRow,ucColumn)=TRUE)return GAME_OVER;return GAME_MOVE;/* 名称:vGameOver()* 功能:游戏结束*/void vGameOver(void)/
13、vGameInit();/* 名称:vUpdateLevel()* 功能:更新分数*/void vUpdateLevel(void)st_Snake.ucLevel=st_Snake.ulScore/2;st_Snake.ucSpeed=80-st_Snake.ucLevel*10;/* 名称:vUpdateScore()* 功能:更新分数*/void vUpdateScore(void)st_Snake.ulScore+;vUpdateLevel();/* 名称:vGameBean()* 功能:得到豆子*/void vGameBean(void)vAddSnake();vCreateBean
14、();vUpdateScore();/* 名称:vGamePlay()* 功能:游戏动作处理*/void vGamePlay(void)switch(ucMoveDecide()case GAME_OVER:vGameOver();break;case GAME_BEAN:vGameBean();case GAME_MOVE:vMoveSnake();break;default:break;/OS_STK st_GameStkTASK_STK_SIZE;/* 名称:vTaskGame()* 功能:游戏任务*/void vTaskGame(void *pdata)TargetInit();vWr
15、iteCMD1(FIRST_LINE); OSTaskCreate(vTaskKey ,(void *)0, &st_KeyStkTASK_STK_SIZE - 1 ,10); OSTaskCreate(vTaskInfo,(void *)0, &st_InfoStkTASK_STK_SIZE - 1, 20);while(1)vGamePlay();OSTimeDlyHMSM(0,0,0,st_Snake.ucSpeed);/*/* */*游戏图形* */* */*#include Configure.h/INT8U uca_BeanMapROW_MAXCOL_MAX;/* 名称:vDraw
16、Bean()* 功能:绘制一个点*/void vDrawBean(INT8U ucRow,INT8U ucColumn) /21123:026;459:014 /025;0154INT8U ucXPos,ucYPos;INT8U ucX,ucY;if(ucRowROW_MAX|ucColumnCOL_MAX)return;uca_BeanMapucRowucColumn=TRUE;ucXPos=FRAME_LEFT+ucColumn*4;ucYPos=FRAME_TOP +ucRow*4;for(ucX=0;ucX4;ucX+)for(ucY=0;ucYROW_MAX|ucColumnCOL_
17、MAX)return;uca_BeanMapucRowucColumn=FALSE;vSetColour(COL_WHITE);ucXPos=FRAME_LEFT+ucColumn*4;ucYPos=FRAME_TOP +ucRow*4;for(ucX=0;ucX4;ucX+)for(ucY=0;ucY4;ucY+)vDrawPixel(ucXPos+ucX,ucYPos+ucY);vSetColour(COL_BLACK);/*/* 名称:ucGetBean()/* 功能:获取一个点/*INT8U ucGetBean(INT8U ucRow,INT8U ucColumn) /21123:02
18、6;459:014 /025;015return uca_BeanMapucRowucColumn;INT8U uca_TAN= /贪0x20,0x20,0x10,0x90,0xA8,0xA4,0xAA,0xB1,0xA2,0xE4,0xA8,0x88,0x10,0x30,0x10,0x00,0x00,0x00,0x80,0x9F,0x40,0x20,0x10,0x0E,0x10,0x20,0x60,0xDF,0x00,0x00,0x00,0x00;INT8U uca_CHI= /吃0x00,0xFC,0x04,0x04,0xFC,0x20,0x58,0x4F,0x4A,0x48,0x48,0
19、xC8,0x08,0x08,0x08,0x00,0x00,0x0F,0x02,0x02,0x07,0x00,0x30,0x48,0x44,0x42,0x41,0x40,0x40,0x40,0x70,0x00;INT8U uca_SHE= /蛇0x00,0xF0,0x10,0xFF,0x10,0xF0,0x20,0x18,0xE8,0x09,0x0E,0x0A,0xA8,0x18,0x08,0x00,0x20,0x23,0x21,0x1F,0x11,0x3B,0x10,0x00,0x3F,0x42,0x41,0x41,0x40,0x38,0x00,0x00;/* 名称:vGameInit()*
20、功能:游戏初始化*/void vGameInit(void)INT8U ucRow,ucColumn;for(ucRow=0;ucRow=ROW_MAX;ucRow+)for(ucColumn=0;ucColumn=COL_MAX;ucColumn+);vClearBean(ucRow,ucColumn);/ vShowChin(1,2,uca_TAN); vShowChin(3,2,uca_CHI); vShowChin(5,2,uca_SHE); /vDrawRect(FRAME_LEFT-1,FRAME_TOP-1,FRAME_RIGHT+1,FRAME_BOTTOM+1);vDrawR
21、ect(FRAME_LEFT-2,FRAME_TOP-2,FRAME_RIGHT+2,FRAME_BOTTOM+2);/ vKeyInit(); / st_Snake.ulTime =0; st_Snake.ulScore =0; st_Snake.ucLevel =0; st_Snake.ucSpeed =80; st_Snake.ucAspect=ASPECT_RIGHT; /vCreateSnake();/vCreateBean(); / OSTaskCreate(vTaskGame, (void *)0, &st_GameStkTASK_STK_SIZE - 1, 6);/*/* */
22、*游戏动作* */* */*#include Configure.h/INT8U uca_Bean100;/*/* 名称:vCreateSnake()/* 功能:创建一条蛇/*void vCreateSnake(void)INT8U ucCount;/ st_Snake.pucMap =uca_Bean; st_Snake.ucLength=3; *(st_Snake.pucMap)=5; *(st_Snake.pucMap+1)=10; *(st_Snake.pucMap+2)=5; *(st_Snake.pucMap+3)=9; *(st_Snake.pucMap+4)=5; *(st_S
23、nake.pucMap+5)=8; *(st_Snake.pucMap+6)=5; *(st_Snake.pucMap+7)=7; *(st_Snake.pucMap+8)=5; *(st_Snake.pucMap+9)=6; *(st_Snake.pucMap+10)=5; *(st_Snake.pucMap+11)=5; *(st_Snake.pucMap+12)=5; *(st_Snake.pucMap+13)=4; *(st_Snake.pucMap+14)=5; *(st_Snake.pucMap+15)=3; *(st_Snake.pucMap+16)=5; *(st_Snake.pucMap+17)=2; *(st_Snake.pucMap+18)=5; *(st_Snake.pucMap+19)=1; for(ucCount=0;ucCountst_Snake.ucLength;ucCount+)vDrawBean(*(st_Snake.pucMap+ucCount*2),*(st_Snake.pucMap+ucCount*2+1);