您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > java编写的贪吃蛇游戏源码
一、函数程序结构图二、题目分析通过分析该程序基本需要以下几个类:Text(Main方法)、Snake(蛇)、Food(食物)、Yard(窗口)、Map(地图)、Node(节点)、PaintThread(线程)、Dir(方向)、KeyMonitor(键盘监听)。主要实现蛇运动的基本操作和一些事件反应、游戏分数的记录、暂停、重新开始、更改蛇蛇颜色,蛇的移动速度,地图、死亡模式的改变等功能。贪吃蛇DrawreAppeargetMsetMgetNsetNpaintScoreCheck.stopgetscoresetscoreprocessKeyMian函数DeadifTailaddDeltailHeadaddTailadd()Deltail()HeadadddrawmoveeatkeyPressedRunPauseRestartgo_onisPausesetPauseinstall定义方向变量L,U,D,RDrawKeypressedYardTextSnakePaintThreadNodeKeyMonitorFoodDirectionMapDraw本程序运用Java语言知识,用面向对象的思维设计“贪吃蛇”游戏。三、结果分析运行程序出现游戏界面,蛇(Snake)头自动向前移动,用键盘的上(Up)下(Down)左(Left)右(Right)键控制蛇头的移动方向。游戏窗体中在随机位置出现紫色的食物(Food)。1、每吃一个加5分2、默认撞墙不死3、默认速度34、默认地图15、默认普通蛇身6、默认30分提升一个速度档次7、默认每个速度都走完后更换地图并且速度恢复默认,积分归零功能键:1、按F1重新开始2、按空格键暂停/继续3、按1/2/3/4/5数字键设置蛇的移动速度(逐渐变快)4、按F2更换地图(3种)5、按F3普通蛇/彩蛇的切换6、按F4更换游戏模式(蛇撞到最外围为死亡还是从另一端返回)该程序初步完成了“贪吃蛇”的游戏雏形,但是缺乏完善。比如:界面效果十分粗糙、屏幕闪烁、缺少声音等。但是,我们通过编写贪吃蛇,能更好的运用面向对象的思维方式了,并学到了许多有用的经验。四、附录(程序清单)1、Snake.javaimportjava.awt.Color;importjava.awt.Graphics;importjava.awt.event.KeyEvent;publicclassSnake{Nodehead=null;Nodetail=null;intsize=0;Yardy;Noden=newNode(1,2,Dir.D);publicSnake(Yardy){head=n;tail=n;size=1;this.y=y;for(inti=0;i3;i++)this.Headadd();}publicvoidTailadd(){Nodenode=null;switch(tail.dir){caseL:node=newNode(tail.m+1,tail.n,tail.dir);break;caseU:node=newNode(tail.m,tail.n+1,tail.dir);break;caseR:node=newNode(tail.m-1,tail.n,tail.dir);break;caseD:node=newNode(tail.m,tail.n+1,tail.dir);break;}tail.next=node;node.pre=tail;tail=node;size++;}publicvoidDeltail(){if(size==0)return;tail=tail.pre;tail.next=null;}publicvoidHeadadd(){Nodenode=null;switch(head.dir){caseL:node=newNode(head.m-1,head.n,head.dir);break;caseU:node=newNode(head.m,head.n-1,head.dir);break;caseR:node=newNode(head.m+1,head.n,head.dir);break;caseD:node=newNode(head.m,head.n+1,head.dir);break;}node.next=head;head.pre=node;head=node;size++;}publicvoiddraw(Graphicsg){if(size=0)return;move();for(Noden=head;n!=null;n=n.next){if(head==n){g.setColor(Color.BLUE);if(n.dir==Dir.L||n.dir==Dir.R)g.fillOval(n.m*Yard.B_WIDTH,n.n*Yard.B_WIDTH,Yard.B_WIDTH,Yard.B_WIDTH-3);elseg.fillOval(n.m*Yard.B_WIDTH,n.n*Yard.B_WIDTH,Yard.B_WIDTH-3,Yard.B_WIDTH);}elsen.draw(g);}}publicvoidDeadif(){switch(Yard.STYLE){case2:if(head.m0||head.n30/Yard.B_WIDTH||head.m=(Yard.WIDTH/Yard.B_WIDTH)||head.n=(Yard.HEIGHT/Yard.B_WIDTH)){y.stop();}break;case1:intcols=Yard.WIDTH/Yard.B_WIDTH;introws=Yard.HEIGHT/Yard.B_WIDTH;if(head.m0)head.m=cols;elseif(head.m=cols)head.m=0;elseif(head.n30/Yard.B_WIDTH)head.n=rows;elseif(head.n=rows)head.n=30/Yard.B_WIDTH;break;}for(Nodep=head.next;p!=null;p=p.next){if(head.m==p.m&&head.n==p.n){y.stop();}}switch(Yard.MAP){case1://地图1break;case2://地图2for(intj=10;jYard.WIDTH/Yard.B_WIDTH-10;j++){if(head.m==j&&head.n==10)y.stop();}for(intj=10;jYard.WIDTH/Yard.B_WIDTH-10;j++){if(head.m==j&&head.n==20)y.stop();}break;case3://地图3for(intj=10;jYard.WIDTH/Yard.B_WIDTH-10;j++){if(head.m==j&&head.n==10)y.stop();}for(intj=10;jYard.WIDTH/Yard.B_WIDTH-10;j++){if(head.m==j&&head.n==20)y.stop();}for(intj=20;j26;j++){if(head.m==Yard.WIDTH/Yard.B_WIDTH-10&&head.n==j)y.stop();}for(intj=5;j10;j++){if(head.m==10&&head.n==j)y.stop();}break;default:break;}}voidmove(){Yard.MOVECHECK=true;Headadd();Deltail();Deadif();}publicvoideat(foode){if(y.f.getM()==y.s.head.m&&y.f.getN()==y.s.head.n){e.reAppear();this.Tailadd();y.setscore(y.getscore()+5);}}publicvoidkeyPressed(KeyEvente){intkey=e.getKeyCode();switch(key){caseKeyEvent.VK_LEFT:if(head.dir!=Dir.R)head.dir=Dir.L;break;caseKeyEvent.VK_UP:if(head.dir!=Dir.D)head.dir=Dir.U;break;caseKeyEvent.VK_RIGHT:if(head.dir!=Dir.L)head.dir=Dir.R;break;caseKeyEvent.VK_DOWN:if(head.dir!=Dir.U)head.dir=Dir.D;break;}}}2、Food.javaimportjava.awt.Color;importjava.awt.Graphics;importjava.util.Random;publicclassFood{publicstaticintFOODSTYLE=1;privateintm=r.nextInt(Yard.WIDTH/Yard.B_WIDTH);//横格privateintn=r.nextInt(Yard.HEIGHT/Yard.B_WIDTH-30/Yard.B_WIDTH)+30/Yard.B_WIDTH;//竖格publicstaticRandomr=newRandom();publicintgetM(){returnm;}publicvoidsetM(intm){this.m=m;}publicintgetN(){returnn;}publicvoidsetN(intn){this.n=n;}publicvoidreAppear(){this.m=r.nextInt(Yard.WIDTH/Yard.B_WIDTH);this.n=r.nextInt(Yard.HEIGHT/Yard.B_WIDTH-30/Yard.B_WIDTH)+30/Yard.B_WIDTH;}publicvoiddraw(Graphicsg){switch(Yard.MAP){case2://地图1for(intj=10;jYard.WIDTH/Yard.B_WIDTH-10;j++){if(m==j&&n==10)this.reAppear();}for(intj=10;jYard.WIDTH/Yard.B_WIDTH-10;j++){if(m==j&&n==20)this.reAppear();}break;case3://地图2for(intj=10;jYard.WIDTH/Yard.B_WIDTH-10;j++){if(m==j&&n==10)this.reAppear();}for(intj=10;jYard.WIDTH/Yard.B_WIDTH-10;j++){if(m==j&&n==20)this.reAppear();}for(intj=20;j26;j++){if(m==Yard.WIDTH/Yard.B_WIDTH-10&&n==j)this.reAppear();}for(intj=5;j10;j++){if(m==10&&n==j)this.reAppear();}break;default:break;}Colorold=g.getColor();g.setColor(newColor(100,13,250));g.fillOval(m*Yard.B_WIDTH,n*Yard.B_WIDTH,Yard.B_WIDTH,Yard.B_WIDTH);g.setColor(old);}}3、Yard.javaimportjava.awt.Color;importjava.awt.Font;importjava.awt.Frame;importjava.awt.Graphics;importjava.awt.event.KeyEve
本文标题:java编写的贪吃蛇游戏源码
链接地址:https://www.777doc.com/doc-2135234 .html