Java语言程序设计的课程设计项目-利用Java实现吞(贪)食蛇游戏项目完整实现实例(第2部分)_第1页
Java语言程序设计的课程设计项目-利用Java实现吞(贪)食蛇游戏项目完整实现实例(第2部分)_第2页
Java语言程序设计的课程设计项目-利用Java实现吞(贪)食蛇游戏项目完整实现实例(第2部分)_第3页
Java语言程序设计的课程设计项目-利用Java实现吞(贪)食蛇游戏项目完整实现实例(第2部分)_第4页
Java语言程序设计的课程设计项目-利用Java实现吞(贪)食蛇游戏项目完整实现实例(第2部分)_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、利用Java实现吞(贪)食蛇游戏项目完整实现实例(第2部分)扩展主窗口的GUI及交换功能1了解ArrayList和LinkedList的用法区别(1)ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。(2)对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针。(3)对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据。2添加项目中所需要的各个图像文件主要的图像文件如下3、改变主窗口的默认图标package com.px1987.greedsnake

2、.mainframe;import java.awt.BorderLayout;import java.awt.Image;import java.awt.Toolkit;import javax.swing.JFrame;import javax.swing.JPanel;import com.px1987.greedsnake.util.GreedSnakeAppVO;public class GreedSnakeMainFrame extends JFrame private static final long serialVersionUID = 1L;private JPanel c

3、ontentPanel=null;public GreedSnakeMainFrame()super();initThisMainFrame();contentPanel=new JPanel();contentPanel.setLayout(new BorderLayout();this.setContentPane(contentPanel);this.setSize(AllGreedSnakeGameAppConstant.greedSnakePanelWidth+5, AllGreedSnakeGameAppConstant.greedSnakePanelHeight+5);this.

4、setLocationRelativeTo(null); /将窗口居中显示public void initThisMainFrame()this.setTitle(吞(贪)食蛇游戏);Image oneIconImage=Toolkit.getDefaultToolkit().getImage(getClass().getResource(/images/mainframeIconImage.jpg);this.setIconImage(oneIconImage);this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXI

5、T_ON_CLOSE);this.setAlwaysOnTop(true);4、实现贪食蛇中间面板的背景图的初始化加载package com.px1987.greedsnake.mainframe;import java.awt.BorderLayout;import java.awt.Image;import java.awt.MediaTracker;import java.awt.Toolkit;import javax.swing.JFrame;import javax.swing.JPanel;import com.px1987.greedsnake.util.GreedSnakeA

6、ppVO;public class GreedSnakeMainFrame extends JFrame private static final long serialVersionUID = 1L;private JPanel contentPanel=null;public GreedSnakeMainFrame()super();loadGreenGrassBackgroundImage();initThisMainFrame();contentPanel=new JPanel();contentPanel.setLayout(new BorderLayout();this.setCo

7、ntentPane(contentPanel);e(AllGreedSnakeGameAppConstant.greedSnakePanelWidth+5, AllGreedSnakeGameAppConstant.greedSnakePanelHeight+5);this.setLocationRelativeTo(null); /将窗口居中显示public void initThisMainFrame()this.setTitle(吞(贪)食蛇游戏);Image oneIconImage=Toolkit.getDefaultToolkit().getImage(getClass().get

8、Resource(/images/mainframeIconImage.jpg);this.setIconImage(oneIconImage);this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setAlwaysOnTop(true);private Image greenGrassBackGroundImage=null;public Image getGreenSnakeGrassBackGroundImage() return greenGrassBackGroundIma

9、ge;private void loadGreenGrassBackgroundImage() /由于背景图片比较大,需要进行跟踪greenGrassBackGroundImage=Toolkit.getDefaultToolkit().getImage(this.getClass().getResource(/images/greengrass.jpg);MediaTracker oneMediaTracker=new MediaTracker(this);int trackedImageID=0;oneMediaTracker.addImage(greenGrassBackGroundIm

10、age,trackedImageID);tryoneMediaTracker.waitForID(trackedImageID);/oneMediaTracker.waitForAll();catch(Exception e)e.printStackTrace();5、添加封装蛇身实体SnakeBodyAllNodeVO(1)添加SnakeBodyAllNodeVO实体类,包名称为(2)SnakeBodyAllNodeVO实体类的代码package com.px1987.greedsnake.util;import java.util.Arrays;import java.util.Linke

11、dList;import java.util.Random;import com.px1987.greedsnake.mainframe.GreedSnakeMainFrame;public class SnakeBodyAllNodeVO private boolean snakeMatrix;private LinkedList snakeBodyNodeLinkedList =null;public LinkedList getSnakeBodyNodeLinkedList() return snakeBodyNodeLinkedList;private NodeEntity snake

12、FoodTargetNode;public NodeEntity getSnakeFoodTargetNode() return snakeFoodTargetNode;private int maxGridColNumber, maxGridRowNumber;private GreedSnakeMainFrame oneGreedSnakeMainFrame;public SnakeBodyAllNodeVO() public SnakeBodyAllNodeVO(GreedSnakeMainFrame oneGreedSnakeMainFrame) this.oneGreedSnakeM

13、ainFrame = oneGreedSnakeMainFrame;initSnakeBodyAllNode();private void initSnakeBodyAllNode()maxGridColNumber=AllGreedSnakeGameAppConstant.greedSnakePanelWidth/AllGreedSnakeGameAppConstant.someOneNodeWidth;maxGridRowNumber=AllGreedSnakeGameAppConstant.greedSnakePanelHeight/AllGreedSnakeGameAppConstan

14、t.someOneNodeHeight;/* * 保存最大格子数 */GreedSnakeAppVO.getGreedSnakeAppVO().setMaxGridColNumber(maxGridColNumber);GreedSnakeAppVO.getGreedSnakeAppVO().setMaxGridRowNumber(maxGridRowNumber);/* * 实例化LinkedList集合 */snakeBodyNodeLinkedList = new LinkedList();/* * 初始化二维数组snakeMatrix,该数组的长、宽为横、纵格子数 */snakeMat

15、rix = new booleanmaxGridColNumber;for(int i=0; i 20)?10:maxGridColNumber/2;for(int i = 0; i snakeBodyNodeNumber; +i)int snakeBodyNodeColNo = maxGridColNumber/2+i;int snakeBodyNodeRowNo = maxGridRowNumber/2;snakeBodyNodeLinkedList.addLast(new NodeEntity(snakeBodyNodeColNo, snakeBodyNodeRowNo);snakeMa

16、trixsnakeBodyNodeColNosnakeBodyNodeRowNo = true;/格子值为true代表蛇身节点/* * 随机创建食物节点,并在数组中标识出该节点 */snakeFoodTargetNode = createSnakeFoodNode();snakeMatrixsnakeFoodTargetNode.getNodeColNo()snakeFoodTargetNode.getNodeRowNo() = true;public boolean driveSnakeStepMovingOn()/* * 显示出当前的步数 */StringBuffer stepCounte

17、rTextBuffer=new StringBuffer(); /最好不要使用String类stepCounterTextBuffer.append(步数:);stepCounterTextBuffer.append(GreedSnakeAppVO.getGreedSnakeAppVO().getSnakeMovingStepCounter();oneGreedSnakeMainFrame.getOneGreedSnakeMainToolBar().getSnakeMovingStepCounterLabel().setText(stepCounterTextBuffer.toString()

18、;/* * 获得蛇身的开始节点(蛇头)节点的位置(纵、横格子号),并按照运动的方向改变其格子位置号 */NodeEntity snakeBodyFirstNode = (NodeEntity)snakeBodyNodeLinkedList.getFirst();();int currentSnakeHeadNodeRowNo = snakeBodyFirstNode.getNodeRowNo();switch(GreedSnakeAppVO.getGreedSnakeAppVO().getSnakeCurrentMovingDirection()case AllGreedSnakeGameAp

19、pConstant.MovingUPDirection:currentSnakeHeadNodeRowNo-;break;case AllGreedSnakeGameAppConstant.MovingDOWNDirection:currentSnakeHeadNodeRowNo+;break;case AllGreedSnakeGameAppConstant.MovingLEFTDirection:currentSnakeHeadNodeColNo-;break;ion:currentSnakeHeadNodeColNo+;break;/* * 识别蛇身是否与屏幕的四周出现碰壁,则为失败(返

20、回false) */boolean snakeMovingIsNotOutMaxColNo=(0 = currentSnakeHeadNodeColNo)&(currentSnakeHeadNodeColNo GreedSnakeAppVO.getGreedSnakeAppVO().getMaxGridColNumber(); boolean snakeMovingIsNotOutMaxRowNo=(0 = currentSnakeHeadNodeRowNo)&(currentSnakeHeadNodeRowNo 0)? gameScoreResultValue:10);GreedSnakeA

21、ppVO.getGreedSnakeAppVO().setSnakeMovingStepCounter(0);/重新记数snakeFoodTargetNode = createSnakeFoodNode();snakeMatrixsnakeFoodTargetNode.getNodeColNo()snakeFoodTargetNode.getNodeRowNo() = true;return true;else /蛇身相连(碰到自己的身子),失败!return false; else /蛇身前进一格/* * 只要知道蛇头的位置就可以了, 后面一节点在下一时刻的移动位置永远都是前面一节点在没有移

22、动前的位置。 */snakeBodyNodeLinkedList.addFirst(new NodeEntity(currentSnakeHeadNodeColNo,currentSnakeHeadNodeRowNo);snakeMatrixcurrentSnakeHeadNodeColNocurrentSnakeHeadNodeRowNo = true;snakeBodyFirstNode = (NodeEntity)snakeBodyNodeLinkedList.removeLast(); /除掉最后的节点,并返回新的最后节点snakeMatrixsnakeBodyFirstNode.ge

23、tNodeColNo()snakeBodyFirstNode.getNodeRowNo() = false;/初始化新的最后节点数值为falseGreedSnakeAppVO.getGreedSnakeAppVO().setSnakeMovingStepCounter(GreedSnakeAppVO.getGreedSnakeAppVO().getSnakeMovingStepCounter()+1);return true;private NodeEntity createSnakeFoodNode() /* * 用随机函数产生食物出现位置的坐标(值不是true的格子节点),因为蛇身节点的值

24、为true, * 从而可以避免食物出现在蛇身中 */int foodNodeColNo;int foodNodeRowNo;doRandom snakePositionRandom = new Random();foodNodeColNo = snakePositionRandom.nextInt(GreedSnakeAppVO.getGreedSnakeAppVO().getMaxGridColNumber();foodNodeRowNo = snakePositionRandom.nextInt(GreedSnakeAppVO.getGreedSnakeAppVO().getMaxGrid

25、RowNumber();while(snakeMatrixfoodNodeColNofoodNodeRowNo);return new NodeEntity(foodNodeColNo,foodNodeRowNo);6添加SnakeMoveThread线程类(1)添加SnakeMoveThread线程类,实现接口,包名称为(2)编程该SnakeMoveThread线程类package com.px1987.greedsnake.thread;import javax.swing.JOptionPane;import com.px1987.greedsnake.mainframe.GreedSn

26、akeMainFrame;import com.px1987.greedsnake.util.GreedSnakeAppVO;import com.px1987.greedsnake.util.SnakeBodyAllNodeVO;public class SnakeMoveThread implements Runnableprivate GreedSnakeMainFrame oneGreedSnakeMainFrame;private SnakeBodyAllNodeVO oneSnakeBodyAllNodeVO=null;public SnakeBodyAllNodeVO getOneSnakeBodyAllNodeVO() return oneSnakeBodyAllNodeVO;public SnakeMoveThread()public SnakeMoveThread(GreedSnakeMainFrame oneGreedSnakeMainFrame)this.oneGreedSnakeMainFrame = oneGreedSnakeMainFrame;oneSnakeBodyAllNodeVO=new SnakeBodyAllNodeVO(this.one

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论