JAVA贪吃蛇开发全过程0贪吃蛇姓名:摘要:本文用J2SE实现大家耳熟能详的一个贪吃蛇游戏来综合运用所学知识,本游戏运用软件工程思想(螺旋模型),打好游戏主体框架,JAVA的面向对象思想,封装类,接口等概念,来完成本游戏,打到综合运用知识的目的。⑴.本游戏开发平台:WINXP;⑵.JAVA开发环境:JDK1.6+Eclipse;⑶.开发语言:J2SE关键词:中央控制器;游戏面板;食物;蛇;石头TheGreedSnakeAbstract:Inthispaper,J2SEimplementationofaSnakegamefamiliartotheintegrateduseofwhattheyhavelearned,thisgameistheuseofsoftwareengineeringthinking(spiralmodel),themainframeworkofthefightgame,JAVAobject-orientedthinking,wrapperclasses,interfaceconceptstocompletethisgame,hittingtheintegrateduseofknowledgeandpurpose.⑴.Thegamedevelopmentplatform:WINXP;⑵.JAVADevelopmentEnvironment:JDK1.6+Eclipse;⑶.DevelopmentLanguages:J2SEKeywords:Controller;GamePanel;Food;Snake;GroundJAVA贪吃蛇开发全过程1前言贪吃蛇游戏背景:蛇引诱夏娃吃了苹果之后,就被贬为毒虫,阴险的象征。而蛇吃东西是整只动物吞进去的,大概在文艺复兴的时候(好象是那个时候但是不确定)就有人发明的一种游戏,是现在贪吃蛇的前身。后来慢慢的发展就变成了今天的贪吃蛇了、现在贪吃蛇主要应用于手机游戏。本文运用软件工程思想、JAVA面向对象编程、类的封装、抽象类的使用(接口),游戏编码规范遵循MVC模式,循序渐进实现贪吃蛇的相关功能。一:游戏中的面向对象分析与设计㈠●涉及到几个对象(类)㈡●每个类有什么方法1.首先得有个游戏显示面板(GamePanel)类该类有一个方法voiddisplay();//用来显示游戏面板2.得有个蛇(Snake)类该类有以下几个方法:voidmove();//移动(走一步)voidchangeDirection();//改变方向voideatFood();//吃食物(身体变长)vooleanisEatBody();//蛇是否吃到了自己的身体voiddrawMe();//显示3.一个食物类(Food)类该类有以下方法:booleanisSnakeEatFood(Snake);//判断蛇是否吃到了食物voiddrawMe();//显示4.一个背景石头(Ground)类该类有以下方法:BooleanisSnakeEatRock(Snake);//蛇是否吃到了石头voiddrawMe();//显示此时类之间的关系如下图所示:JAVA贪吃蛇开发全过程2图1各个类之间的关系二:编写贪吃蛇游戏中的各个类的主体框架性代码本有序遵循MVC(Model-View-Controller)所以首先建立以上几个实体类并定义相应的包名:liang.snake.entities包下定义Food、Ground、Snake三个类,再在liang.snake.view包下定义GamePanel类,再在,按照以上说明的各个类具有的方法,写上如下代码:packageliang.snake.entities;publicclassFood{publicbooleanisSnakeEatFood(Snakesnake){System.out.println(Snake'seatfood);returnfalse;}publicvoiddrawme(){System.out.println(Food'sdrawme);}}packageliang.snake.entities;publicclassGround{publicbooleanisSnakeEatRock(){JAVA贪吃蛇开发全过程3returnfalse;}publicvoiddrawme(){System.out.println(Ground'sdrawme);}}packageliang.snake.entities;publicclassSnake{publicvoidmove(){System.out.println(Snake'smove);}publicvoidchangeDirection(intdirection){System.out.println(Snake'schangeDirection);}publicvoideatFood(){System.out.println(Snake'seatfood);}publicbooleanisEatBody(){System.out.println(Snake'seatbody);returnfalse;}publicvoiddrawMe(){System.out.println(Snake'sdrawMe);}packageliang.snake.view;JAVA贪吃蛇开发全过程4importjava.awt.Graphics;importjavax.swing.JPanel;importliang.snake.entities.Food;importliang.snake.entities.Ground;importliang.snake.entities.Snake;publicclassGamePanelextendsJPanel{privateSnakesnake;privateGroundground;privateFoodfood;publicvoidpaintComponent(Graphicsg){snake.drawMe();ground.drawme();food.drawme();}publicvoiddisplay(Snakesnake,Groundground,Foodfood){this.snake=snake;this.ground=ground;this.food=food;System.out.println(GamePanel'sdisplay);this.repaint();}三:编写Controler类与实现蛇移动的事件监听由于蛇不能直接操作显示面板(GamePanel)所以这里定义一个监听器SnakeListener:里面定义一个方法:voidsnakeMove(Snake);每当蛇移动一步就触发监听器,调用snakeMoved()由Controller去实现监听器的接口,然后由控制器去触发显示面板的事件源,让显示面板完成相应操作。逻辑图如下:JAVA贪吃蛇开发全过程5图2事件监听建立联系在Snake中增加蛇的方向常量:publicstaticfinalintUP=1;publicstaticfinalintDOWN=-1;publicstaticfinalintLEFT=2;publicstaticfinalintRIGHT=-2;并添加一个接口:packageliang.snake.Listener;importliang.snake.entities.Snake;publicinterfaceSnakeListener{publicvoidSnakeMove(Snakesnake);}Controler实现SnakeListener接口并处理案件消息:代码如下:packageliang.snake.Controller;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importliang.snake.Listener.SnakeListener;importliang.snake.entities.Food;importliang.snake.entities.Ground;importliang.snake.entities.Snake;JAVA贪吃蛇开发全过程6importliang.snake.view.GamePanel;publicclassControllerextendsKeyAdapterimplementsSnakeListener{Snakesnake=newSnake();Groundground=newGround();Foodfood=newFood();GamePanelgamepanel=newGamePanel();publicController(Snakesnake,Groundground,Foodfood,GamePanelgamepanel){this.snake=snake;this.ground=ground;this.food=food;this.gamepanel=gamepanel;}publicvoidkeyReleased(KeyEvente){switch(e.getKeyCode()){caseKeyEvent.VK_UP:snake.changeDirection(Snake.UP);break;caseKeyEvent.VK_DOWN:snake.changeDirection(Snake.DOWN);break;caseKeyEvent.VK_LEFT:snake.changeDirection(Snake.LEFT);break;caseKeyEvent.VK_RIGHT:snake.changeDirection(Snake.RIGHT);break;}}JAVA贪吃蛇开发全过程7publicvoidnewgame(){snake.start();}publicvoidSnakeMove(Snakesnake){gamepanel.display(snake,ground,food);}}开发到此时,Snake中应该增加监听器,并另启动一个线程来不断调用蛇的move();在Snake中提供一个启动线程的方法,在Snake中增加如下代码:privateSetSnakeListenerlistener=newHashSetSnakeListener();publicvoidaddSnakeListener(SnakeListenerl){if(l!=null)this.listener.add(l);}}publicvoidstart(){newThread(newSnakeDriver()).run();}并在Run方法中增加for(SnakeListenerl:listener){l.SnakeMove(Snake.this);}try{Thread.sleep(1000);}catch(InterruptedExceptione){JAVA贪吃蛇开发全过程8e.printStackTrace();}四:编写对各个类进行测试的程序代码构建一个新类Game来测试程序代码:packageliang.snake.Game;importjava.applet.AudioClip;importjava.awt.BorderLayout;importjava.io.File;importjava.net.MalformedURLException;importjava.net.URL;importjavax.swing.JFrame;importliang.snake.Controller.Controller;importliang.snake.entities.Food;importliang.snake.entities.Ground;importliang.snake.entities.