Java 写的贪吃蛇游戏 带详细设计报告 功能非常齐全 完整源码 您所在的位置:网站首页 游戏设计报告 Java 写的贪吃蛇游戏 带详细设计报告 功能非常齐全 完整源码

Java 写的贪吃蛇游戏 带详细设计报告 功能非常齐全 完整源码

2024-07-15 00:32| 来源: 网络整理| 查看: 265

今天为大家分享一个java语言编写的贪吃蛇系统,目前系统功能已经很全面,后续会进一步完善。整个系统界面漂亮,有完整得源码,希望大家可以喜欢。喜欢的帮忙点赞和关注。一起编程、一起进步

开发环境

开发语言为Java,开发环境Eclipse或者IDEA都可以,数据为MySQL。运行主程序,或者执行打开JAR文件即可以运行本程序。

系统框架

利用JDK自带的SWING框架开发,下载。纯窗体模式,直接运行Main文件即可以。同时带有详细得设计文档。

系统主要功能 系统开发背景

贪吃蛇游戏的设计比较复杂,它涉及面广、知识点比较多,如果不好好考虑和设计,将难以成功开发出运行流畅,界面友好的游戏。在这个游戏的设计中,牵涉到图形界面的显示与更新、数据的收集与更新,并且在这个游戏的开发中,还要应用类的继承机制以及一些设计模式。因此,如何设计和开发好这个贪吃蛇游戏,对于提高Java开发水平和系统的设计能力有极大的帮助。在设计开发过程中,需要处理好各个类之间的继承关系,还要处理各个类相应的封装,并且还要协调好各个模块之间的逻辑依赖关系和数据通信关系。“贪吃蛇”游戏是一个经典的游戏,它因操作简单、娱乐性强而广受欢迎。本文基于Java7,在eclipse里面开发了一个操作简单、界面美观、功能较齐全的“贪吃蛇”游戏。整个游戏程序分为二个功能模块,实现了游戏的开始、暂停、结束,背景改变,设置速度,统计游戏积分等。通过贪吃蛇游戏的开发,把课堂上学到的java技术运用到实际的项目中来。

主要功能

软件需求分析的任务就是深入描述软件的功能和性能,确定软件设计的约束和软件同其他系统元素的接口细节,定义软件的其他有效性需求,借助于当前系统的逻辑模型导出目标系统逻辑模型,解决目标系统“做什么”的问题。

本系统主要是完成贪吃蛇游戏的基本操作。用户可以自己练习和娱乐。本系统需要满足以下几点要求:

利用方向键来改变蛇的运行方向。

空格键暂停或继续游戏,并在随机的地方产生食物。

可以通过设置区域的设置按钮来改变游戏的背景颜色,游戏中蛇的蛇头颜色,蛇身颜色,食物颜色以及石头的颜色.

通过按键来调节蛇的移动速度,来增加游戏的难度.

在面板上添加不同的地图布局,来使游戏更加好玩.

吃到食物就变成新的蛇体,碰到障碍物石头或自身则游戏结束,否则正常运行

动态获取游戏的积分

实现效果 关键源码 package com.huowolf.controller; //控制器负责处理各组件的变化 import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JColorChooser; import javax.swing.JOptionPane; import com.huowolf.entities.Food; import com.huowolf.entities.Ground; import com.huowolf.entities.Snake; import com.huowolf.listener.SnakeListener; import com.huowolf.view.BottonPanel; import com.huowolf.view.GameMenu; import com.huowolf.view.GamePanel; public class Controller extends KeyAdapter implements SnakeListener{ private Snake snake; private Food food; private Ground ground; private GamePanel gamePanel; private GameMenu gameMenu; private BottonPanel bottonPanel; public Controller(Snake snake, Food food, Ground ground,GamePanel gamePanel,GameMenu gameMenu,BottonPanel bottonPanel) { this.snake = snake; this.food = food; this.ground = ground; this.gamePanel = gamePanel; this.gameMenu = gameMenu; this.bottonPanel = bottonPanel; init(); } //控制器的初始化,主要初始化对控件的监听 public void init() { bottonPanel.getStartButton().addActionListener(new startHandler()); bottonPanel.getPauseButton().addActionListener(new pauseHandler()); bottonPanel.getEndButton().addActionListener(new endHandler()); gameMenu.getItem1().addActionListener(new Item1Handler()); gameMenu.getItem2().addActionListener(new Item2Handler()); gameMenu.getItem3().addActionListener(new Item3Handler()); gameMenu.getItem4().addActionListener(new Item4Handler()); gameMenu.getSpItem1().addActionListener(new spItem1Handler()); gameMenu.getSpItem2().addActionListener(new spItem2Handler()); gameMenu.getSpItem3().addActionListener(new spItem3Handler()); gameMenu.getSpItem4().addActionListener(new spItem4Handler()); gameMenu.getMapItem1().addActionListener(new mapItem1Handler()); gameMenu.getMapItem2().addActionListener(new mapItem2Handler()); gameMenu.getMapItem3().addActionListener(new mapItem3Handler()); gameMenu.getAbItem().addActionListener(new abortItemHandler()); bottonPanel.getStartButton().setEnabled(true); } //获取各对象 public Snake getSnake() { return snake; } public Ground getGround() { return ground; } public GamePanel getGamePanel() { return gamePanel; } public GameMenu getGameMenu() { return gameMenu; } public BottonPanel getBottonPanel() { return bottonPanel; } //键盘按键的监听 @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_UP: snake.changeDirection(Snake.UP); break; case KeyEvent.VK_DOWN: snake.changeDirection(Snake.DOWN); break; case KeyEvent.VK_LEFT: snake.changeDirection(Snake.LEFT); break; case KeyEvent.VK_RIGHT: snake.changeDirection(Snake.RIGHT); break; case KeyEvent.VK_W: snake.speedUp(); break; case KeyEvent.VK_S: snake.speedDown(); break; default: break; } } //实现蛇移动的接口,处理蛇移动过程发生的各种事情 @Override public void snakeMoved(Snake snake) { //每移动一次,就更新一次面板 gamePanel.display(snake, food, ground); if(food.isFoodEated(snake)) { snake.eatFood(); food.newFood(ground.getPoint()); //更新得分显示面板 bottonPanel.repaint(); setScore(); } if(ground.isGroundEated(snake)) { snake.die(); bottonPanel.getStartButton().setEnabled(true); } if(snake.isEatBody()) { snake.die(); bottonPanel.getStartButton().setEnabled(true); } } // public void setScore() { int score = snake.getFoodCount() ; bottonPanel.setScore(score); } // 开始一个新游戏 public void newGame() { ground.clear(); switch (ground.getMapType()) { case 1: ground.generateRocks1(); break; case 2: ground.generateRocks2(); break; case 3: ground.generateRocks3(); break; } food.newFood(ground.getPoint()); bottonPanel.setScore(0); bottonPanel.repaint(); } //开始游戏按钮的监听 class startHandler implements ActionListener { @Override public void actionPerformed(ActionEvent arg0) { gamePanel.requestFocus(true); //!!使游戏面板区获得焦点 snake.clear(); snake.init(); snake.begin(); newGame(); bottonPanel.getStartButton().setEnabled(false); } } //暂停按钮的监听 class pauseHandler implements ActionListener { @Override public void actionPerformed(ActionEvent e) { gamePanel.requestFocus(true); snake.changePause(); if(e.getActionCommand()=="暂停游戏") bottonPanel.getPauseButton().setText("继续游戏"); else { bottonPanel.getPauseButton().setText("暂停游戏"); } } } //结束游戏按钮的监听 class endHandler implements ActionListener { @Override public void actionPerformed(ActionEvent e) { snake.die(); bottonPanel.getStartButton().setEnabled(true); } } //菜单栏各按钮的监听 class Item1Handler implements ActionListener { @Override public void actionPerformed(ActionEvent e) { Color color = JColorChooser.showDialog(gamePanel, "请选择颜色", Color.BLACK); gamePanel.backgroundColor = color; } } class Item2Handler implements ActionListener { @Override public void actionPerformed(ActionEvent e) { Color color = JColorChooser.showDialog(gamePanel, "请选择颜色", Color.BLACK); food.setFoodColor(color); } } class Item3Handler implements ActionListener { @Override public void actionPerformed(ActionEvent e) { Color color = JColorChooser.showDialog(gamePanel, "请选择颜色", Color.BLACK); snake.setHeadColor(color); } } class Item4Handler implements ActionListener { @Override public void actionPerformed(ActionEvent e) { Color color = JColorChooser.showDialog(gamePanel, "请选择颜色", Color.BLACK); snake.setBodyColor(color); } } class spItem1Handler implements ActionListener { @Override public void actionPerformed(ActionEvent arg0) { snake.setSleepTime(600); } } class spItem2Handler implements ActionListener { @Override public void actionPerformed(ActionEvent arg0) { snake.setSleepTime(350); } } class spItem3Handler implements ActionListener { @Override public void actionPerformed(ActionEvent arg0) { snake.setSleepTime(150); } } class spItem4Handler implements ActionListener { @Override public void actionPerformed(ActionEvent arg0) { snake.setSleepTime(80); } } class mapItem1Handler implements ActionListener { @Override public void actionPerformed(ActionEvent arg0) { ground.setMapType(1); } } class mapItem2Handler implements ActionListener { @Override public void actionPerformed(ActionEvent arg0) { ground.setMapType(2); } } class mapItem3Handler implements ActionListener { @Override public void actionPerformed(ActionEvent arg0) { ground.setMapType(3); } } class abortItemHandler implements ActionListener { @Override public void actionPerformed(ActionEvent e) { StringBuffer sb= new StringBuffer(); sb.append("方向键控制方向\n"); sb.append("w键、s键分别控制使其加速、减速\n"); sb.append("贪吃蛇游戏\n"); String message = sb.toString(); JOptionPane.showMessageDialog(null, message, "使用说明",JOptionPane.INFORMATION_MESSAGE); } } }



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有