面向对象课程设计--java__贪吃蛇.docx
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《面向对象课程设计--java__贪吃蛇.docx》由会员分享,可在线阅读,更多相关《面向对象课程设计--java__贪吃蛇.docx(20页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、面向对象课程设计-java_贪吃蛇 面向对象课程设计1.需求分析 1.1游戏开发的可行性 游戏开发至今已经有30多年,在这个短暂的时期里,随着硬件水平的提高,游戏开发新技术层出不穷,经典游戏比比皆是。游戏这个名称一直就存在于每个人的日常生活中,如猜拳游戏、猜谜游戏、大地游戏、球类游戏等,林林总总,不胜枚举,甚至于有些流行歌曲把人生也比喻为游戏,好比“一场游戏一场梦”。因此,游戏对于现代人的成长历程,绝对是一个不可或缺的重要角色。 1.2设计目的 综合运用在校期间所学理论知识和技能,设计开发贪吃蛇游戏,使自己熟悉应用系统的开发过程,培养独立思考能力,检验学习效果和动手能力,提高工程实践能力。 1
2、.3游戏需求 给出1条小蛇和随机出1个食物当小蛇吃到食物时就加分。 1.3.1游戏界面需求 良好的用户界面,有分数显示。有存取档功能,并且可以暂停,还有“英雄榜”记录分数排名情况,并且可调节小蛇运动速度。 1.3.2游戏形状(方块)需求 良好的方块形状设计,绘制小蛇和食物,小蛇由几个方块组成,食物由1个方块组成。 1.3.3键盘处理事件 小蛇可通过键盘方向键(上、下、左、右键)进行向上、向下、向左、向右移动。 1.3.4显示需求 当小蛇吃掉几个食物后会变长,并且吃完食物时食物会消失,并立即随机生成1个新的食物。 1.3.5 接口控制 本软件需求通过键盘进行操作,在Windows的操作系统下,利
3、用键盘的上、下、左、右键对小蛇进行移动操作,要使用键盘的接口事件。 2.功能设计及源程序的主要部分 2.1 游戏流程图 2.2 功能的实现,类的实现及类层次 2.2.1游戏界面 public SnakeFrame() setSize(width, height); /设置窗体大小 setTitle(贪吃蛇); /标题 SnakePanel panel = new SnakePanel();/游戏面板 add(panel); /将面板添加到窗体中 setLocation(260, 160); /设置窗体位置 setVisible(true); setResizable(false); /禁用窗体
4、最大化 2.2.2功能按钮 public SnakePanel() /构造函数 /添加功能按钮 setLayout(null); add(startGame); add(stopGame); add(saveGame); add(listGame); add(newGame); add(exitGame); /设置位置 startGame.setBounds(402, 50, 80, 40); stopGame.setBounds(402, 100, 80, 40); saveGame.setBounds(402, 150, 80, 40); listGame.setBounds(402,20
5、0, 80, 40); newGame.setBounds(402, 250, 80, 40); exitGame.setBounds(402, 300, 80, 40); /添加监视器 ButtonListener btnListener=new ButtonListener(); startGame.addActionListener(btnListener); stopGame.addActionListener(btnListener); saveGame.addActionListener(btnListener); listGame.addActionListener(btnLis
6、tener); newGame.addActionListener(btnListener); exitGame.addActionListener(btnListener); /取消所有Button 的焦点 startGame.setFocusable(false); stopGame.setFocusable(false); saveGame.setFocusable(false); listGame.setFocusable(false); newGame.setFocusable(false); /速度 String speed=1,2,3,4,5,预设; combspeed=new
7、JComboBox(speed); combspeed.setBorder(BorderFactory.createTitledBorder(速度); add(combspeed);/添加组合框 combspeed.setBounds(402,340,80,46); comboBoxListener comblistener=new comboBoxListener(); combspeed.addActionListener(comblistener);/添加监视器 combspeed.setFocusable(false); /取消焦点 initGame(); /初始化游戏 addKeyL
8、istener(new Listener();/添加键盘按键监视器 setFocusable(true); /将焦点设置在当前面板上以便通过键盘控制游戏 thiswin=this; 2.2.3键盘监视器 class Listener implements KeyListener public void keyPressed(KeyEvent e) int key = e.getKeyCode();/获得键码 if (key = KeyEvent.VK_LEFT & direction != 2) direction = 0; /左 else if (key = KeyEvent.VK_UP &
9、 direction != 3) direction = 1; /上 else if (key = KeyEvent.VK_RIGHT & direction != 0) direction = 2; /右 else if (key = KeyEvent.VK_DOWN & direction != 1) direction = 3; /下 public void keyReleased(KeyEvent e) Override public void keyTyped(KeyEvent e) public void keyPressed(KeyEvent e) int key = e.get
10、KeyCode();/获得键码 if (key = KeyEvent.VK_LEFT & direction != 2) direction = 0; /左 else if (key = KeyEvent.VK_UP & direction != 3) direction = 1; /上 else if (key = KeyEvent.VK_RIGHT & direction != 0) direction = 2; /右 else if (key = KeyEvent.VK_DOWN & direction != 1) direction = 3; /下 2.2.4蛇和食物的变化 2.2.4
11、.1蛇和食物的产生: public void drawSnake(Graphics g) Graphics2D g2d = (Graphics2D) g; for(int i=0;i for(int j=0;j if(arrayListij = 1) /蛇身 g.setColor(frontcolor); /蛇身颜色 else if(arrayListij = 2)/蛇头 g.setColor(Color.RED); /蛇头颜色 else if(arrayListij = 3) /食物 g.setColor(foodcolor); /食物颜色 if (arrayListij != 0) int
12、 curx = border_left + i * 10; int cury = border_top + j * 10; g2d.draw(new Rectangle2D.Double(curx, cury, 8, 8); 2.2.4.2小蛇移动和吃掉食物: public boolean Move() /移动 if (direction = 0) x -= 1; /左 else if (direction = 1) y -= 1; /上 else if (direction = 2) x += 1; /右 else if (direction = 3) y += 1; /下 /向前移动一步
13、if(GameOver()/游戏结束 clock.stop(); gameover=true; flag=false; /不再产生新的豆豆 if(x =arrayList.length | y = arrayList0.length) else arrayListxy=2; /此时蛇头位置 int headx=snakeBody.get(0).x; /头部坐标 int heady=snakeBody.get(0).y; arrayListheadxheady=1; /蛇头变蛇身 int tailx=snakeBody.get(snakeBody.size()-1).x;/尾部坐标 int ta
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向 对象 课程设计 java_ 贪吃
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内