下载
加入VIP
  • 专属下载特权
  • 现金文档折扣购买
  • VIP免费专区
  • 千万文档免费下载

上传资料

关闭

关闭

关闭

封号提示

内容

首页 java版贪吃蛇小游戏

java版贪吃蛇小游戏.doc

java版贪吃蛇小游戏

小丑样的掩悲伤演欢笑
2017-09-27 0人阅读 举报 0 0 暂无简介

简介:本文档为《java版贪吃蛇小游戏doc》,可适用于高等教育领域

java版贪吃蛇小游戏辅助类Coordinatejava复制代码packagecomnnutil***坐标点*publicclassCoordinate{publicintxpublicintypublicCoordinate(intnewX,intnewY){x=newXy=newY}publicbooleanequals(Coordinateother){if(x==otherxy==othery){returntrue}returnfalse}OverridepublicStringtoString(){return"Coordinate:"x","y""}}复制代码接口Direction复制代码packagecomnnentitypublicinterfaceDirection{publicclassDir{publicstaticfinalintUP=publicstaticfinalintDOWN=publicstaticfinalintLEFT=publicstaticfinalintRIGHT=}}复制代码实体类Snakejava复制代码packagecomnnentityimportjavautilArrayListimportcomnnentityDirectionDirimportcomnnutilCoordinatepublicclassSnake{小方块x坐标privateintx小方块y坐标privateinty宽度publicstaticfinalintTILEW=高度publicstaticfinalintTILEH=下一个方向privateintmNextDirection=DirRIGHT游戏是否结束privatebooleanisEnd存放贪吃蛇privateArrayList<Coordinate>mSnakeTrail=newArrayList<Coordinate>()存放豆子privateArrayList<Coordinate>mAppleList=newArrayList<Coordinate>()publicSnake(){initNewGame()}初始化新游戏publicvoidinitNewGame(){初始化蛇身mSnakeTrailadd(newCoordinate(,))mSnakeTrailadd(newCoordinate(,))mSnakeTrailadd(newCoordinate(,))mSnakeTrailadd(newCoordinate(,))mSnakeTrailadd(newCoordinate(,))mSnakeTrailadd(newCoordinate(,))初始化豆子mAppleListadd(newCoordinate(,))设置初始状态为falseisEnd=false}publicArrayList<Coordinate>getmSnakeTrail(){returnmSnakeTrail}publicArrayList<Coordinate>getmAppleList(){returnmAppleList}publicbooleanisEnd(){returnisEnd}publicintgetmNextDirection(){returnmNextDirection}publicvoidsetEnd(booleanisEnd){thisisEnd=isEnd}}复制代码frame类继承JFrameSnakeFramejava复制代码packagecomnnuiimportjavaawtDimensionimportjavaawtToolkitimportjavaxswingJFrame***游戏界面*publicclassSnakeFrameextendsJFrame{privatestaticfinallongserialVersionUID=L宽度privatestaticfinalintwidth=高度privatestaticfinalintheight=高度偏移量privatestaticfinalintoffset=publicSnakeFrame(){init()}***初始化游戏界面*privatevoidinit(){thissetTitle("Java版贪吃蛇")frame的高度宽度thissetSize(width,height)窗口居中显示Toolkittoolkit=ToolkitgetDefaultToolkit()Dimensionscreen=toolkitgetScreenSize()intw=screenwidthinth=screenheight计算左上角坐标点的xy值thissetLocation((wthisgetWidth()),(hthisgetHeight())offset)设置默认关闭的操作thissetDefaultCloseOperation(EXITONCLOSE)设置窗口大小不可改变thissetResizable(false)设置可见性thissetVisible(true)thissetLayout()}}复制代码panelj继承JPanelSnakePaneljava复制代码packagecomnnuiimportjavaawtColorimportjavaawtFontimportjavaawtGraphicsimportjavaawteventActionEventimportjavaawteventActionListenerimportjavautilArrayListimportjavaxswingJButtonimportjavaxswingJPanelimportcomnncontrolPlayControlimportcomnnentitySnakeimportcomnnutilCoordinatepublicclassSnakePanelextendsJPanel{privatestaticfinallongserialVersionUID=Lpanel的宽度privatestaticfinalintwidth=panel的高度privatestaticfinalintheight=坐标点xprivatestaticfinalintx=坐标点yprivatestaticfinalinty=方块尺寸privatestaticfinalintSIZE=是否开启网格privatebooleanisOpen=falseprivateSnakesnake开启作弊privateJButtongirdButtonprivateJButtonaboutButton暂停继续按钮privateJButtonPauseButton重新开始游戏privateJButtonStartButtonpublicSnakegetSnake(){returnsnake}privateArrayList<Coordinate>mSnakeTrail=privateArrayList<Coordinate>mAppleList=publicSnakePanel(){snake=newSnake()thissetLayout()StartButton=newJButton("重新开始")StartButtonsetLocation(,)StartButtonsetSize(,)thisadd(StartButton)PauseButton=newJButton("暂停游戏")PauseButtonsetLocation(,)PauseButtonsetSize(,)thisadd(PauseButton)girdButton=newJButton("开启网格")girdButtonsetLocation(,)girdButtonsetSize(,)thisadd(girdButton)aboutButton=newJButton("关于游戏")aboutButtonsetLocation(,)aboutButtonsetSize(,)thisadd(aboutButton)girdButtonaddActionListener(newGirdActionListener())aboutButtonaddActionListener(newAboutActionListerner())}classGirdActionListenerimplementsActionListener{inti=publicvoidactionPerformed(ActionEvente){if(i==){girdButtonsetText("关闭网格")isOpen=falsei}elseif(i==){girdButtonsetText("开启网格")isOpen=truei}}}classAboutActionListernerimplementsActionListener{publicvoidactionPerformed(ActionEvente){}}OverridepublicvoidpaintComponent(Graphicsg){superpaintComponent(g)creatGameWindow(g)if(snakeisEnd()){paintWords(g)}将焦点定在Jpanel上thisrequestFocus()}***安装玩家控制器*paramcontrol*publicvoidsetGameControl(PlayControlcontrol){thisaddKeyListener(control)}privatevoidcreatGameWindow(Graphicsg){gsetColor(ColorBLACK)gdrawRect(x,y,width,height)gsetColor(ColorWHITE)gfillRect(x,y,width,height)gsetColor(ColorGRAY)***************test******************paintLine(g)***************test******************mSnakeTrail=snakegetmSnakeTrail()mAppleList=snakegetmAppleList()if(mSnakeTrail==){return}gsetColor(ColorBLACK)for(inti=i<mSnakeTrailsize()i){gfillRect(thisxmSnakeTrailget(i)x*SIZE,thisymSnakeTrailget(i)y*SIZE,SnakeTILEW,SnakeTILEH)}画豆子gfillRect(thisxmAppleListget()x*SIZE,thisymAppleListget()y*SIZE,SnakeTILEW,SnakeTILEH)}画网格privatevoidpaintLine(Graphicsg){for(inti=i<i){gdrawLine(thisx,i*thisy,thisx,i*thisy)}for(inti=i<i){gdrawLine(i*thisx,thisy,i*thisx,thisy)}}结束提示privatevoidpaintWords(Graphicsg){Fontfont=newFont("宋体",FontBOLD,)gsetFont(font)gsetColor(ColorRED)gdrawString("游戏结束",,)}}复制代码最关键的类控制类PlayControljava复制代码packagecomnncontrolimportjavaawteventKeyEventimportjavaawteventKeyListenerimportjavautilArrayListimportjavautilRandomimportcomnnentityDirectionDirimportcomnnuiSnakePanelimportcomnnutilCoordinatepublicclassPlayControlimplementsKeyListener{privateSnakePanelpanel蛇头在list中索引privateintheadIndex***贪吃蛇列表*ArrayList<Coordinate>mSnakeTrail**豆子列表*privateArrayList<Coordinate>mAppleList新产生的豆子CoordinatenewCoordinateprivateintmNextDirection是否绘制出结束提示privatebooleanisDraw=falseprivateRandomrandom=newRandom()publicPlayControl(SnakePanelpanel){thispanel=panelmSnakeTrail=panelgetSnake()getmSnakeTrail()mAppleList=panelgetSnake()getmAppleList()mNextDirection=panelgetSnake()getmNextDirection()headIndex=mSnakeTrailsize()Threadthread=newThread(runnable)threadstart()}响应键盘按下事件publicvoidkeyPressed(KeyEvente){if(isDraw){return}switch(egetKeyCode()){caseKeyEventVKUP:if(mNextDirection!=DirDOWN){mNextDirection=DirUPmoveUp()}breakcaseKeyEventVKDOWN:if(mNextDirection!=DirUP){mNextDirection=DirDOWNmoveDown()}breakcaseKeyEventVKLEFT:if(mNextDirection!=DirRIGHT){mNextDirection=DirLEFTmoveLeft()}breakcaseKeyEventVKRIGHT:if(mNextDirection!=DirLEFT){mNextDirection=DirRIGHTmoveRight()}break}}OverridepublicvoidkeyReleased(KeyEvente){}OverridepublicvoidkeyTyped(KeyEvente){}向上移动privatevoidmoveUp(){if(!isEndGame()){每移动一次先在头部加一块list增加一个元素mSnakeTrailadd(newCoordinate(mSnakeTrailget(headIndex)x,mSnakeTrailget(headIndex)y))如果判断吃到豆子if(isEat()){吃到豆子的处理afterEatApple()}否则移除第一个元素else{mSnakeTrailremove()}重新绘制panelrepaint()}}向下移动privatevoidmoveDown(){if(!isEndGame()){mSnakeTrailadd(newCoordinate(mSnakeTrailget(headIndex)x,mSnakeTrailget(headIndex)y))if(isEat()){afterEatApple()}else{mSnakeTrailremove()}panelrepaint()}}向左移动privatevoidmoveLeft(){if(!isEndGame()){mSnakeTrailadd(newCoordinate(mSnakeTrailget(headIndex)x,mSnakeTrailget(headIndex)y))if(isEat()){afterEatApple()}else{mSnakeTrailremove()}panelrepaint()}}向右移动privatevoidmoveRight(){if(!isEndGame()){mSnakeTrailadd(newCoordinate(mSnakeTrailget(headIndex)x,mSnakeTrailget(headIndex)y))if(isEat()){afterEatApple()}else{mSnakeTrailremove()}panelrepaint()}}增加一个坐标点到存放蛇的list添加到list的末尾privatevoidafterEatApple(){每增加一个索引值加重新开始游戏时要回复初始值headIndexmAppleListadd(newCoordinate(randomnextInt(),randomnextInt()))mAppleListremove()newCoordinate=mAppleListget()Systemoutprintln("蛇身长度:"mSnakeTrailsize())}判断蛇是否吃到豆子privatebooleanisEat(){returnmSnakeTrailget(mSnakeTrailsize())equals(mAppleListget())}判断是否到边界或者碰到自己的身体privatebooleanisEndGame(){intx=mSnakeTrailget(headIndex)xinty=mSnakeTrailget(headIndex)yif(touchBody()||x<||x>||y<||y>){afterEndGame()returntrue}returnfalse}碰到自己身体privatebooleantouchBody(){for(inti=i<mSnakeTrailsize()i){if(mSnakeTrailget(headIndex)equals(mSnakeTrailget(i))){returntrue}}returnfalse}游戏结束处理privatevoidafterEndGame(){mSnakeTrailremove(headIndex)panelgetSnake()setEnd(true)panelrepaint()thisisDraw=true}Runnablerunnable=newRunnable(){publicvoidrun(){moveRight()while(!isEndGame()){if(mNextDirection==DirUP)moveUp()elseif(mNextDirection==DirDOWN)moveDown()elseif(mNextDirection==DirLEFT)moveLeft()elseif(mNextDirection==DirRIGHT)moveRight()try{Threadsleep()}catch(InterruptedExceptione){eprintStackTrace()}}}}}复制代码最后是测试类SnakeTestjava复制代码packagecomnnmainimportcomnncontrolPlayControlimportcomnnuiSnakeFrameimportcomnnuiSnakePanel***启动游戏**publicclassSnakeTest{publicstaticvoidmain(Stringargs){SnakeFrameframe=newSnakeFrame()SnakePanelpanel=newSnakePanel()PlayControlplayControl=newPlayControl(panel)panelsetGameControl(playControl)setContentPane()把panel设置为JFrame的内容面板framesetContentPane(panel)}}

用户评价(0)

关闭

新课改视野下建构高中语文教学实验成果报告(32KB)

抱歉,积分不足下载失败,请稍后再试!

提示

试读已结束,如需要继续阅读或者下载,敬请购买!

文档小程序码

使用微信“扫一扫”扫码寻找文档

1

打开微信

2

扫描小程序码

3

发布寻找信息

4

等待寻找结果

我知道了
评分:

/22

java版贪吃蛇小游戏

VIP

在线
客服

免费
邮箱

爱问共享资料服务号

扫描关注领取更多福利