您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 基于Freescale三轴加速度传感器LCD的贪食蛇游戏设计
//说明:该程序是基于freescaleMCU、三轴加速度传感器、ili9320LCD所写的贪食蛇游//戏,上传代码拥有全部的设计思路以及大部分函数主体//=================================================================//宏定义按键//=================================================================#defineKey_Up1#defineKey_Down4#defineKey_Left2#defineKey_Right3#defineucharunsignedchar//=================================================================//使用结构体定义变量使程序的名称更加的清楚//=================================================================struct{ucharX;ucharY;ucharYes;}Food;//食物结构体#defineSNAKE_Max_Long20//最大长度struct{ucharX[SNAKE_Max_Long];ucharY[SNAKE_Max_Long];ucharLong;ucharLife;ucharScore;//蛇的分数ucharLevel;//蛇的等级}Snake;//蛇结构体ucharKey_V_temp=0;//取出上次按键值ucharKey_Value=0;staticintl=0;staticuint16KEY_Init[3]={0,0,0};staticuint16KEY_BASE[3]={0,0,0};score[20]={0,10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160,170,180,190};intlevel[20]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};staticconstbyteChannels[]={13,14,15};/*Contentsforthedevicecontrolregister*/staticwordMMA_7260_OutV[3];staticbyteSumChan;staticbyteOutFlg;voidMMA_7260_Init_3(void){OutFlg=FALSE;/*Nomeasuredvalue*//*ATD0CTL4:SMP2=0,SMP1=0,SMP0=0,PRS4=0,PRS3=0,PRS2=1,PRS1=0,PRS0=1*/ATD0CTL4=5;/*Setsampletimeandprescaler*//*ATD0CTL3:DJM=0,S8C=0,S4C=0,S2C=0,S1C=1,FIFO=0,FRZ1=0,FRZ0=0*/ATD0CTL3=8;/*SetATDcontrolregister3*//*ATD0CTL0:??=0,??=0,??=0,??=0,WRAP3=1,WRAP2=1,WRAP1=1,WRAP0=1*/ATD0CTL0=15;/*Setwraparound*//*ATD0CTL1:ETRIGSEL=0,SRES1=1,SRES0=0,SMP_DIS=1,ETRIGCH3=1,ETRIGCH2=1,ETRIGCH1=1,ETRIGCH0=1*/ATD0CTL1=95;/*Setresolutionanddischarge*//*ATD0CTL2:??=0,AFFC=1,ICLKSTP=0,ETRIGLE=0,ETRIGP=0,ETRIGE=0,ASCIE=0,ACMPIE=0*/ATD0CTL2=64;/*SetATDcontrolregister2*/}//-----------------------------------------------------staticvoidMMA_7260_Measure_3(void){for(SumChan=0;SumChan3;SumChan++){ATD0CTL5=Channels[SumChan];/*Startmeasurementofnextchannel*/while(!ATD0STAT0_SCF){}/*WaitforADconversioncomplete*/MMA_7260_OutV[SumChan]=ATD0DR0;/*Savemeasuredvalue*/}OutFlg=TRUE;/*Measuredvaluesareavailable*/}//-----------------------------------------------------byteMMA_7260_GetValue16_3(word*Values){if(!OutFlg){/*Ismeasuredvalue(s)available?*/return0;/*Ifnothenerror*/}/*Note:Next4linesarespeedoptimized*/*Values++=MMA_7260_OutV[0];/*Savemeasuredvaluestotheoutputbuffer*/*Values++=MMA_7260_OutV[1];/*Savemeasuredvaluestotheoutputbuffer*/*Values++=MMA_7260_OutV[2];/*Savemeasuredvaluestotheoutputbuffer*/OutFlg=FALSE;return1;/*OK*/}//=================================================================////////////////////////贪食蛇游戏///////////////////////////////////=================================================================//函数名称:Trun_On_PointTrun_Off_Point(ucharx,uchary)//函数功能:点亮或者熄灭一个点。用于显示蛇身体//入口参数:无//出口参数:无//特别说明:此Trun_On,函数换成其他的就可以兼容其他的液晶//入8*8点阵12864等//=================================================================voidTrun_On_Point(ucharx,uchary,ucharcolor){ili9320_Rectangle((4*(x+2)),(4*(y+2)),(4*(x+3)),(4*(y+3)),color,color);//Bar((4*(x+2)),(4*(y+2)),(4*(x+3)),(4*(y+3)),color);}voidTrun_Off_Point(ucharx,uchary){ili9320_Rectangle((4*(x+2)),(4*(y+2)),(4*(x+3)),(4*(y+3)),0xFFFF,0xFFFF);}//=================================================================//函数名称:voidInit_SNAKE(void)//函数功能:绘制游戏界面,初始化游戏等级等参数//入口参数:无//出口参数:无//=================================================================voidInit_SNAKE(void){uchari;//***********获取方向基准********************//MMA_7260_Init_3();MMA_7260_Measure_3();MMA_7260_GetValue16_3(&KEY_BASE);//以下语句可以通过串口检验三轴加速度数据/*if(Flag){printp(GetXChannel:%10D,KEY_BASE[0]);printp(GetYChannel:%10D,KEY_BASE[1]);printp(GetZChannel:%10D,KEY_BASE[2]);}*///***********绘制游戏界面********************//ili9320_Rectangle(4,4,166,206,0,0);ili9320_Rectangle(5,5,167,207,0,0);ili9320_Rectangle(6,6,168,208,0,0);ili9320_Rectangle(5,5,169,209,0,0);ili9320_Rectangle(7,7,167,207,0xFFFF,0xFFFF);//绘制一个矩形框,160*240ili9320_Write_multi_HZ_ASCII(167+10,32,32,32,少女32.FON,少符32.FON,07电信一,0xF800);ili9320_Write_multi_HZ_ASCII(167+10,66,32,32,少女32.FON,少符32.FON,郑友金,0xF800);ili9320_Write_multi_HZ_ASCII(167+10,100,32,32,少女32.FON,少符32.FON,贪食蛇,0xF800);//***********初始化蛇参数********************//Snake.Long=2;//定义初始化蛇的长度Snake.Life=0;//初始化蛇还活着Snake.Score=0;Food.Yes=1;Key_Value=0;for(i=0;iSnake.Long;i++)//将蛇给赋值{Snake.X[i]=i+10;Snake.Y[i]=10;}for(i=0;iSnake.Long;i++)//将蛇给画出来{Trun_On_Point(Snake.X[i],Snake.Y[i],0);}}//=================================================================//函数名称:voidDisplay_dead(uchari)//函数功能:显示死亡界面//入口参数:无//出口参数:无//=================================================================voidDisplay_dead(uchari){l=0;Snake.Level=0;ili9320_Text(7+30,7+40,GAMEOVER,9,0,0xffff);ili9320_Delay(3000);Init_SNAKE();}//=================================================================//函数名称:voidDisplay_Pass(uchari)//函数功能:显示过关界面//入口参数:无//出口参数:无//=================================================================voidDisplay_Pass(uchari){l++;Snake.Level++;ili9320_Text(7+30,7+40,YOUWIN,7,0,0xffff);ili9320_Delay(3000);Init_SNAKE();}//==========================================
本文标题:基于Freescale三轴加速度传感器LCD的贪食蛇游戏设计
链接地址:https://www.777doc.com/doc-2569741 .html