基于51单片机的小游戏设计

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

重庆文理学院11电子信息科学与技术第1页共24页基于51单片机的小游戏专业:电子信息科学与技术小组成员:孙远(201108051014)刘林(201108051009)王然(201108051004)黄漠啸(201108051038)第2页共24页目录一、制作所需模块、材料。........................................................................................3二、硬件连接电路........................................................................................................3三、软件程序................................................................................................................31、头文件、端口等定义。..........................................................................................32、12864.h(包含文件库)........................................................................................44、为伪随机数发生器播种:......................................................................................45、初始化MPU:............................................................................................................56、画墙壁,初始化界面:.............................................................................................67、打印成绩:.................................................................................................................78、打印速度级别:......................................................................................................79、游戏结束处理:......................................................................................................810、游戏的具体过程,也是贪吃蛇算法的关键部分:............................................811、LCD12864.............................................................................................................12四、调试结果..............................................................................................................231、开始画面:............................................................................................................232、游戏界面:............................................................................................................233、游戏结束画面:....................................................................................................24五、功能介绍..............................................................................................................25六、参考。..................................................................................................................25第3页共24页一、制作所需模块、材料。1、单片机最小系统。2、万能板。3、12864液晶显示屏。4、按键、绑线等。买到后依照电路原理图自己焊接即可。二、硬件连接电路三、软件程序1、头文件、端口等定义。第4页共24页2、12864.h(包含文件库)3、伪随机数发生器:4、为伪随机数发生器播种:第5页共24页5、初始化MPU:/*********************************初始化MPU**********************************/voidInitCpu(void){TMOD=0x0;TH0=0;TL0=0;TR0=1;ET0=1;EA=1;}#defineN20structFood{unsignedcharx;unsignedchary;unsignedcharyes;}food;//食物结构体structSnake{unsignedcharx[N];unsignedchary[N];unsignedcharnode;unsignedchardirection;unsignedcharlife;}snake;//蛇结构体unsignedcharFlag=0;第6页共24页unsignedcharScore=0;unsignedcharSpeed=1;unsignedcharKeyBuffer=0;#defineFUNC1#defineUP2#defineDOWN3#defineLEFT4#defineRIGHT5#definePASSSCORE20//预定义过关成绩voidTimer0Int(void)interrupt1{switch(OSReadKey()){case9:KeyBuffer=FUNC;/*if(++Speed=10)Speed=1;Flag|=11;//速度变化标志置1*/break;case13:KeyBuffer=DOWN;/*if(snake.direction!=2)snake.direction=1;*/break;case17:KeyBuffer=UP;/*if(snake.direction!=1)snake.direction=2;*/break;case21:KeyBuffer=RIGHT;/*if(snake.direction!=4)snake.direction=3;*/break;case25:KeyBuffer=LEFT;/*if(snake.direction!=3)snake.direction=4;*/break;default:break;}}6、画墙壁,初始化界面:第7页共24页7、打印成绩:8、打印速度级别:第8页共24页9、游戏结束处理:10、游戏的具体过程,也是贪吃蛇算法的关键部分:voidGamePlay(void){unsignedcharn;InitRandom(TL0);food.yes=1;//1表示需要出现新事物,0表示已经存在食物尚未吃掉snake.life=0;//表示蛇还活着snake.direction=DOWN;snake.x[0]=6;snake.y[0]=6;snake.x[1]=3;snake.y[1]=6;snake.node=2;PrintScore();PrintSpeed();while(1){if(food.yes==1){while(1){food.x=Random()*85+3;food.y=Random()*55+3;//获得随机数while(food.x%3!=0)food.x++;while(food.y%3!=0)第9页共24页food.y++;for(n=0;nsnake.node;n++)//判断产生的食物坐标是否和蛇身重合{if((food.x==snake.x[n])&&(food.y==snake.y[n]))break;}if(n==snake.node){food.yes=0;break;//产生有效的食物坐标}}}if(food.yes==0){Lcd_Rectangle(food.x,food.y,food.x+2,food.y+2,1);}for(n=snake.node-1;n0;n--){snake.x[n]=snake.x[n-1];snake.y[n]=snake.y[n-1];}switch(snake.direction){caseDOWN:snake.x[0]+=3;break;caseUP:snake.x[0]-=3;break;caseRIGHT:snake.y[0]-=3;break;caseLEFT:snake.y[0]+=3;break;default:break;}for(n=3;nsnake.node;n++)//从第三节开始判断蛇头是否咬到自己{if(snake.x[n]==snake.x[0]&&snake.y[n]==snake.y[0]){GameOver();snake.life=1;break;}}if(snake.x[0]3||snake.x[0]=90||snake.y[0]3||snake.y[0]=60)//判蛇头是否撞到墙壁{GameOver();snake.life=1;}第10页共24页if(snake.life==1)break;//蛇死,则跳出while(1)循环if(snake.x[0]==food.x&&snake.y[0]==food.y)//判蛇是否吃到食物{Lcd_Rectangle(food.x,food.y,food.x+2,food.y+2,0);//消隐食物snake.x[snake.node]=200;snake.y[snake.node]=200;//产生蛇新的节坐标先放在看不见的位置snake.node++;//蛇节数加1food.yes=1;//食物标志置1if(++Score=PASSSCORE){PrintScore();GameOver();break;}PrintScore();}for(n=0;nsnake.node;n++){Lcd_Rectangle(snake.x[n],snake.y[n],snake.x[n]+2,snake.y[n]+2,1);}//根据蛇的节数画出蛇delay(Speed*1000);Lcd_Rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+2,snake.y[snake.node-1]+2,0);switch(KeyBu

1 / 24
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功