《java版的贪吃蛇毕业论文.docx》由会员分享,可在线阅读,更多相关《java版的贪吃蛇毕业论文.docx(21页珍藏版)》请在优知文库上搜索。
1、贪吃蛇姓名:摘要:本文用J2SE实现大家耳熟能详的一个贪吃蛇游戏来综合运用所学知识,本游戏运用软件工程思想(螺旋模型),打好游戏主体框架,JAVA的面向对象思想,封装类,接口等概念,来完成本游戏,打到综合运用知识的目的。(1) .本游戏开发平台:WINXP:(2) .JAVA开发环境:JDKl.6+Eclipse;(3) .开发语言:J2SE关键词:中央控制器;游戏面板;食物;蛇;石头TheGreedSnakeAbstract:Inthispaper,J2SEimplementationofaSnakegamefamiliartotheintegrateduseofwhattheyhaveIe
2、amed,thisgameistheuseofsoftwareengineeringthinking(spiralmodel),themainframeworkofthefightgame,JAVAobject-orientedthinking,wrapperclasses,interfaceconceptstocompletethisgame,hittingtheintegrateduseofknowledgeandpurpose.(1) .Thegamedevelopmentplatform:WINXP;(2) .JAVADevelopmentEnvironment:JDK1.6+Ecli
3、pse;(3) .DevelopmentLanguages:J2SEKeywords:COntrOller;GamePane1;Food;Snake;GrOUnd前言贪吃蛇游戏背景:蛇引诱夏娃吃了苹果之后,就被贬为毒虫,阴险的象征。而蛇吃东西是整只动物吞进去的,大概在文艺复兴的时候(好象是那个时候但是不确定)就有人发明的一种游戏,是现在贪吃蛇的前身。后来慢慢的发展就变成了今天的贪吃蛇了、现在贪吃蛇主要应用于手机游戏。本文运用软件工程思想、JAVA面向对象编程、类的封装、抽象类的使用(接口),游戏编码规范遵循MVC模式,循序渐进实现贪吃蛇的相关功能。一:游戏中的面向对象分析与设计涉及到几个对象(
4、类)每个类有什么方法1 .首先得有个游戏显示面板(GamePaneI)类该类有一个方法voiddisplay();用来显示游戏面板2 .得有个蛇(SlIake)类该类有以下几个方法:voidmove();移动(走一步)voidChangeDireCtioIl0;改变方向voideatFood();吃食物(身体变长)VooleanisEatBody();蛇是否吃到了自己的身体voiddrawMe();显示3 .一个食物类(Food)类该类有以下方法:booleanisSlIakeEatFOod(SlIake);判断蛇是否吃到了食物voidCiraWMe();显示4 .一个背景石头(GrOUlId
5、)类该类有以下方法:BooleanisSnakeEatRock(Snake);蛇是否吃到了石头voiddrawMe();显示此时类之间的关系如下图所示:二:编写贪吃蛇游戏中的各个类的主体框架性代码本有序遵循MVC(ModeI-View-ControIIer)所以首先建立以上几个实体类并定义相应的包名:liang.snake.entities包下定义Food、GroundSnake三个类,再在IiaiIg.snake.view包下定义GamePalIel类,再在,按照以上说明的各个类具有的方法,写上如下代码:packageIiang.snake.entities;publicclassFoodp
6、ublicbooleanIsSnakeEatFood(Snakesnake)(System.out.printin(Snake,seatfood);returnfalse;publicvoiddrawme()(System.out.printin(,Food,sdrawme,);packageIiang.snake.entities;publicclassGroundpublicbooleanIsSnakeEatRock()returnfalse;publicvoiddrawme()(System.out.printin(,Ground,sdrawme,);packageIiang.snake
7、.entities;publicclassSnakepublicvoidmove()System.out.printin(Snakesmove,);publicvoidChangeDirection(intdirection)ChangeDirection);System.out.printin(Snake,spublicvoideatFood()(System.out.printin(Snakeseatfood);publicbooleanIsEatBody()(System.out.printin(Snake,seatbody);returnfalse;publicvoiddrawMe()
8、System.out.printin(,Snake,sdrawMe);packageIiang.snake.view;importjava.awt.Graphics;importjavax.swing.JPanel;importIiang.snake.entities.Food;importIiang.snake.entities.Ground;importIiang.snake.entities.Snake;publicclassGamePanelextendsJPanelprivateSnakesnake;privateGroundground;privateFoodfood;public
9、voidpaintComponent(Graphicsg)snake.drawMe();ground.drawme();food.drawme();publicvoiddisplay(SnakesnakezGroundground,Foodfood)(this.snake=snake;this.ground=ground;this.food=food;System.out.printin(GamePanel,sdisplay);this.repaint();三:编写ControIer类与实现蛇移动的事件监听由于蛇不能直接操作显示面板(GamePaiIeD所以这里定义一个监听器SnakeList
10、ener:里面定义一个方法:voidSnakeMove(Snake);每当蛇移动一步就触发监听器,调用SilakeMOVed()由Controller去实现监听器的接口,然后由控制器去触发显示面板的事件源,让显示面板完成相应操作。逻辑图如下:通过事件监听建立联系图2事件监听建立联系在Sllake中增加蛇的方向常量:public static finalint UP=I;public static finalint DOWN=-I;public static finalint LEFT=2;public staticfinal int RIGHT=-2;并添加一个接口:packageIiang.
11、snake.Listener;importIiang.snake.entities.Snake;publicinterfaceSnakeListenerpublicvoidSnakeMove(Snakesnake);Controler实现SnakeListener接口并处理案件消息:代码如下:packageIiang.snake.Controller;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importIiang.snake.Listener.SnakeListener;importIiang.snake.en
12、tities.Food;importIiang.snake.entities.Ground;importIiang.snake.entities.Snake;importIiang.snake.view.GamePanel;PubIiCclassControllerextendsKeyAdapterimplementsSnakeListenerSnakesnake=newSnake();Groundground=newGround();Foodfood=newFood();GamePanelgamepanel=newGamePanel();publicController(Snakesnake
13、zGroundground,FoodfoodzGamePanelgamepanel)this.snake=snake;this.ground=ground;this.food=food;this.gamepanel=gamepanel;pxblicvoidkeyReleased(KeyEvente)switch(e.getKeyCode()(caseKeyEvent.VK_UP:snake.ChangeDirection(Snake.UP);break;caseKeyEvent.VK_DOWN:snake.ChangeDirection(Snake.DOWN);break;caseKeyEve
14、nt.VK_LEFT:snake.ChangeDirection(Snake.LEFT);break;caseKeyEvent.VK_RIGHT:snake.ChangeDirection(Snake.RIGHT);break;publicvoidnewgame()snake.start();publicvoidSnakeMove(Snakesnake)gamepanel.display(snake,ground,food);开发到此时,Snake中应该增加监听器,并另启动一个线程来不断调用蛇的move();在Snake中提供一个启动线程的方法,在Snake中增加如下代码:privateSet
15、listener=newHashSet();publicvoidaddSnakeListener(SnakeListener1)if(1!=null)this.listener.add(1);publicvoidstart()(newThread(newSnakeDriver().run();并在RUn方法中增加for(SnakeListener1:listener)(1.SnakeMove(Snake.this);try(Thread.sleep(100O);catch(InterruptedExceptione)e.printStackTrace();四:编写对各个类进行测试的程序代码构建一个新类Game来测试程序代码:packageIiang.snake.Game;impo