您好,欢迎访问三七文档
JavaOOPDay04Top1.贪吃虫游戏——实现Worm方向改变2.贪吃虫游戏——实现Food抓取3.DMS2.0——完成DMSFrame窗体的设计和布局4.DMS2.0——完成DMSFrame监听方法的实现1贪吃虫游戏——实现Worm方向改变1.1问题如何利用键盘的方向键驱动贪吃虫的运行方向.1.2方案利用Swing的监听键盘事件的方法监听键盘事件,根据获取的按键值驱动贪吃虫向不同的方向运行:1.publicvoidaction(){2.worm=newWorm();3.food=randomFood();4.timer.schedule(newCreepTask(this),0,200);5.requestFocus();//请求当前绘图面板获取输入焦点6.//添加键盘事件监听器7.addKeyListener(newKeyAdapter(){8.//在有按键按下时候执行9.publicvoidkeyPressed(KeyEvente){10.intkeyCode=e.getKeyCode();//获取按下的按键值11.switch(keyCode){//检查按键值12.caseKeyEvent.VK_DOWN://方向键下13.worm.changeDirection(Worm.DIRECTION_DOWN);14.creepForFood();15.break;16.caseKeyEvent.VK_UP:17.worm.changeDirection(Worm.DIRECTION_UP);18.creepForFood();19.break;20.caseKeyEvent.VK_LEFT:21.worm.changeDirection(Worm.DIRECTION_LEFT);22.creepForFood();23.break;24.caseKeyEvent.VK_RIGHT:25.worm.changeDirection(Worm.DIRECTION_RIGHT);26.creepForFood();27.break;28.}29.}30.});31.}1.3实现重构Worm类,增加改变运行方向的方法:1./**改变方向*/2.publicbooleanchangeDirection(intdirection){3.if(direction+currentDirection!=0){4.currentDirection=direction;5.returntrue;6.}7.returnfalse;8.}重构WormStage类的action方法,增加对键盘事件的监听1.publicvoidaction(){2.worm=newWorm();3.food=randomFood();4.timer.schedule(newCreepTask(this),0,200);5.requestFocus();//请求当前绘图面板获取输入焦点6.//添加键盘事件监听器7.addKeyListener(newKeyAdapter(){8.//在有按键按下时候执行9.publicvoidkeyPressed(KeyEvente){10.intkeyCode=e.getKeyCode();//获取按下的按键值11.switch(keyCode){//检查按键值12.caseKeyEvent.VK_DOWN://方向键下13.worm.changeDirection(Worm.DIRECTION_DOWN);14.creepForFood();15.break;16.caseKeyEvent.VK_UP:17.worm.changeDirection(Worm.DIRECTION_UP);18.creepForFood();19.break;20.caseKeyEvent.VK_LEFT:21.worm.changeDirection(Worm.DIRECTION_LEFT);22.creepForFood();23.break;24.caseKeyEvent.VK_RIGHT:25.worm.changeDirection(Worm.DIRECTION_RIGHT);26.creepForFood();27.break;28.}29.}30.});31.}完整代码参考(不包含Cell类型,Cell类型代码参考以前章节):1./**2.*定时器任务,用于驱动舞台上的贪吃虫自动运行3.*/4.publicclassCreepTaskextendsTimerTask{5./**舞台对象的引用*/6.privateWormStagewormStage;7./**创建定时器任务时需要舞台对象的引用*/8.publicCreepTask(WormStagewormStage){9.this.wormStage=wormStage;10.}11./**实现定时任务的执行方法run(),驱动贪吃虫的运行*/12.publicvoidrun(){13.wormStage.creepForFood();14.}15.}16.17./**18.*贪吃虫类型19.*/20.publicclassWorm{21./**方向上*/22.publicstaticfinalintDIRECTION_UP=1;23./**方向下*/24.publicstaticfinalintDIRECTION_DOWN=-1;25./**方向左*/26.publicstaticfinalintDIRECTION_LEFT=2;27./**方向右*/28.publicstaticfinalintDIRECTION_RIGHT=-2;29.30./**虫的当前运行方向*/31.privateintcurrentDirection=DIRECTION_DOWN;32.33./**节点数组*/34.privateCell[]cells;35.36.publicWorm(){37.//WormStage.WORM_LEN默认贪吃虫的长度38.cells=newCell[WormStage.WORM_LEN];39.for(inti=0;icells.length;i++){40.cells[i]=newCell(i,0);41.}42.}43./**44.*检查是否将要撞击头部,只是检查下一步的情况,不执行爬行动作45.*包括:撞击边缘,吃到自己46.*@paramdirection将要运行的方向47.*@return如果发生撞击就返回true,否则返回false48.*/49.publicbooleanhit(intdirection){50.//增加边界判断51.CellnextHead=newHead(direction);//临时创建下一个头节点52.if(nextHead.getX()0||nextHead.getY()053.||nextHead.getX()=WormStage.CELL_COLS54.||nextHead.getY()=WormStage.CELL_ROWS){55.returntrue;56.}57.//扩展部分判断是否吃到自己58.if(this.contains(nextHead.getX(),nextHead.getY())){59.returntrue;60.}61.returnfalse;62.}63./**64.*检查当前方向上是否会撞头65.*@return如果发生撞击就返回true,否则返回false66.*/67.publicbooleanhit(){68.returnhit(currentDirection);69.}70.71./**改变方向*/72.publicbooleanchangeDirection(intdirection){73.if(direction+currentDirection!=0){74.currentDirection=direction;75.returntrue;76.}77.returnfalse;78.}79.80./**81.*爬行(creep)方法,爬行方法:移除末尾节点,增加头节点82.*@paramdirection爬行方向83.*/84.publicvoidcreep(intdirection){85.if(direction+currentDirection!=0){//方向不能相反86.//更换方向87.currentDirection=direction;88.CellnewHead=newHead(direction);89.if(newHead!=null){90.//移除末尾节点91.for(inti=cells.length-1;i=1;i--){92.cells[i]=cells[i-1];93.}94.//插入新头节点95.cells[0]=newHead;96.}97.}98.}99.100.publicvoidcreep(){101.creep(currentDirection);102.}103.104.publicvoidpaint(Graphicsg,Colorb,Colorf){105.for(inti=0;icells.length;i++){106.cells[i].paint(g,b,f);107.}108.}109.110./**111.*检查当前贪吃虫身上是否包含x,y坐标节点112.*@paramx113.*@paramy114.*@return如果包含节点(x,y)就返回true115.*/116.publicbooleancontains(intx,inty){117.for(inti=0;icells.length;i++){118.if(x==cells[i].getX()&&y==cells[i].getY()){119.returntrue;120.}121.}122.returnfalse;123.}124.125.privateCellnewHead(intdirection){126.CellnewHead=null;127.Cellhead=cells[0];128.switch(direction){129.caseDIRECTION_UP:130.newHead=newCell(head.getX(),head.getY()-1);131.break;132.caseDIRECTION_DOWN:133.newHead=newCell(head.getX(),head.getY()+1);134.break;135.caseDIRECTION_LEFT:136.newHead=newCell(head.getX()-1,head.getY());137.break;138.caseDIRECTION_RIGHT:139.newHead=newCell(head.getX()+1,head.getY());140.break;141.}142.returnnewHead;143.}144.}145.146.publicclassWormStageextendsJPanel{147.148./**显示单元格大小像素个数*/149.publicstaticfinalintCELL_SIZE=10;150./**舞台显示单元格的列数*/151.publicstaticfinalintCELL_COLS=35;152./**舞台显示单元格的行数*/153.publicstaticfinalintCELL_ROWS=35;154./**默认贪吃虫的长度*/155.publicstaticfinalintWORM_LEN=12;156./**贪吃虫*/157.privateWormworm;158./**食物*/159.privateCellfood;160./**驱动贪吃虫自动运行的定时器*/161
本文标题:JAVA面向对象
链接地址:https://www.777doc.com/doc-2881640 .html