四川师范大学本科毕业设计
基于 Java 的游戏“贪吃蛇”
的设计和实现
——实现游戏主控制功能
作
院
专
年
学
者:
系:
业:
级:
号:
指导教师:
基于 Java 的游戏“贪吃蛇”的设计和实现
学生:
指导教师:
内容提要:
近年来,Java 作为一种新的编程语言,以其简单性、可移植性和平台无
关性等优点,得到了广泛地应用,特别是 Java 与万维网的完美结合,使其成
为网络编程和嵌入式编程领域的首选编程语言。JBuilder 是 Borland 公司用于
快速开发 Java 应用的一款优秀的集成开发环境,它以其友好的开发界面、强
大的组件支持等优点,得到广大程序员的接受和认可。
“贪吃蛇”游戏是一个经典的游戏,它因操作简单、娱乐性强而广受欢迎。
本文基于 Java 技术和 JBuilder 开发环境,开发了一个操作简单、界面美观、功能
较齐全的“贪吃蛇”游戏。整个游戏程序分为二个功能模块,六个类模块,实
现了游戏的开始、暂停、结束。通过本游戏的开发,达到学习 Java 技术和熟悉软
件开发流程的目的。
本文在介绍 Java 和 JBuilder 相关技术和国内外发展现状的基础上,对“贪
吃蛇”游戏的整个生命周期的各个开发阶段进行了详细地介绍。首先,分析
了开发本游戏软件的可行性,重点分析本设计所采用的技术的可行性。其次,
从游戏主界面等几方面,分 析了本游戏的功能需求;从可使用性和安全性方
面分析了属性需求。然后, 进行了游戏的概要设计和详细设计,这也是本文
的重点。概要设计给出二个 功能模块的主框架以及六个类模块的函数调用关
系;详 细设计介绍了 Java2 标 准运行环境的配置,重点给出各个类模块的程
序流程图以及 UML 图,列表介绍了各个类包含的变量、使用的方法,展示了
游戏的界面。为了保证程序代码的正确性和功能实现的可靠性,本文还介绍了
游戏软件的程序调试和功能测试结果,并且对本设计的成果和不足进行了总结。
最后,本文还提出软件的维护措施。
关键词:java;JBuilder;“贪吃蛇”游戏
Content Abstract:
Recently, due to its simplicity, transportability and depend from
the platform, the programme language of Java has got popular application
especially when it has been combined with WWW as the first-chosen programme
language in the field of network programme and programme-embeded. JBuilder
is an excellent integrated development environment, which was utilized by
the corporation of Borland to design the application of Java and has got
extensive recognition among most programmer due to its good design interface,
the support of perfect modules and so on.
“贪吃蛇”is a classical and traditional game.It is very popular because
of its simple operation and strong amusement.There has been more
requirement on function of the game that the old function of traditional game
has not been enough to people. Based on the Java technology and JBuilder
integrated development environment,the paper design a game of“贪吃蛇”with
simple operation,perfect interface and complete functions. The programme are
composed of two functional modules and six class-modules, all of which will
realize the following functions: game’s begin, pause, end.The purpose of
developing this game gains the ends of not noly the study of Java technology
but also the familiarity of software development process.
Based on the introduction of Java and JBuilder technology,this paper
specifies every process of the lifecycle of the game of “贪吃蛇”.First
of all,this paper analyses the feasibility of developing the game software
and emphasizes the technical feasibility of the design .Secondly,the
requirement on function of this game is analysed on several aspects:the main
interface of the game.In addition,the requirement on property is also
analysed on both usability and security. And then,this paper carries through
the general design and the detail design which are the emphases of this paper.
Main frame of two functional modules and the function-called relationship
of six class-modules are shown in the general design;the detail design
introduces the configuration of Java 2 Runtime environment,and point out
programme flow chart and UML chart of every class module. The detail design
also introduces the variables and methods included in every class and
specifies how to make installation of this game software.The interface of
game was displayed in the end of the detail design. In order to ensure
correctness of code and dependability of the function,the programme has been
debugged,and the function has been tested,and then the fruit and shortage
of this design has been summarized. Finally,this paper proposes the
maintenance measures of the game software.
Key Words: Java;JBuilder;the game of“贪吃蛇”
目 录
第 1 章 绪论 ............................................................ 1
1.1 开发环境配置 ..................................................... 1
1.1.1 Java2 的标准运行环境 .......................................... 1
1.1.1.1 J2SE SDK .................................................. 1
1.1.1.2 J2SE JRE .................................................. 1
1.1.1.3 J2SE Documentation ........................................ 1
1.1.2 J2SE 的安装与配置 ............................................. 1
1.1.2.1 安装过程 .................................................. 1
1.1.2.2 配置环境变量 .............................................. 4
1.2 本设计的目的和意义 ............................................... 8
1.3 国内外研究现状 ................................................... 9
1.4 设计研究的主要内容、目标与工具 .................................. 11
1.4.1 设计的主要内容 ...............................................11
1.4.2 设计要达到的目标 .............................................11
1.4.3 设计方法介绍 .................................................11
第 2 章 可行性分析 ..................................................... 15
2.1 可行性分析 ...................................................... 15
2.1.1 可行性研究的前提 .............................................15
2.1.1.1 要求 ..................................................... 15
2.1.1.2 目标 ..................................................... 16
2.1.1.3 条件、假定和限制 ......................................... 16
2.1.2 对现有软件的分析 .............................................16
2.2 所建议的软件 .................................................... 17
2.2.1 游戏处理流程 .................................................17
2.2.2 社会可行性分析 ...............................................18
2.2.3 技术可行性分析 ...............................................19
2.2.3.1 执行平台方面 ............................................. 19
2.2.3.2 执行速度方面 ............................................. 20
2.2.3.3 语言特性与功能方面 ....................................... 20
2.2.3.4 开发环境方面 ............................................. 21
2.2.3.5 JBuilder 开发工具 ........................................ 21
2.2.4 经济可行性 ...................................................21
第 3 章 需求分析 ....................................................... 22
3.1 任务概述 ........................................................ 22
3.1.1 目标 .........................................................22
3.1.2 用户的特点 ...................................................22
3.2 具体需求 ........................................................ 22
3.2.1 功能需求 .....................................................22
3.2.1.1 游戏主界面模块需求 ....................................... 22
3.2.1.2 游戏图形区域界面的需求 ................................... 23
3.2.2 属性需求 .....................................................23
第 4 章 概要设计 ....................................................... 24
4.1 游戏功能 ........................................................ 24
4.2 功能模块设计 .................................................... 24
4.2.1 总设计模块的划分 .............................................24
4.2.2 游戏主界面模块 ...............................................24
4.2.3 游戏控制模块 .................................................25
4.3 类模块设计 ...................................................... 26
4.3.1 类模块之间关系 ...............................................26
4.3.2 各类模块设计概要 .............................................26
4.4 程序设计的重点 .................................................. 27
4.4.1 游戏图形界面的图形显示更新功能 ...............................27
4.4.2 贪吃蛇蛇身的设计 .............................................27
4.5 接口设计 ........................................................ 27
4.5.1 外部接口 .....................................................27
4.5.2 内部接口 .....................................................28
4.6 维护设计 ........................................................ 28
4.7 故障处理 ........................................................ 28
第 5 章 详细设计 ....................................................... 29
5.1 程序主结构 ...................................................... 29
5.2 类模块程序流程图 ................................................ 29
5.2.1 SnakeApp 类 .................................................. 29
5.2.2 SnakeFrame 类 ................................................ 30
5.2.3 SnakePanel 类 ................................................ 31
5.2.4 SnakeModel 类 ................................................ 31
5.2.5 ArrayIndexReadOnly 类 ........................................ 33
5.2.6 ArrayIndex 类 ................................................ 33
5.2.7 listener 类 .................................................. 33
5.3 类模块具体设计 .................................................. 34
5.3.1 SnakeApp.java 程序 ........................................... 34
5.3.2 SnakeFrame.java 程序 ......................................... 35
5.3.2.1 SnakekFrame 类程序 ....................................... 35
5.3.2.2 listener 类程序 .......................................... 36
5.3.2.3 SnakeFrame.java 的 UML 图 ................................. 36
5.3.3 ArrayIndex.java 程序 ......................................... 37
5.3.4 ArrayIndexReadOnly.java 程序 ................................. 38
5.3.5 SnakePanel.java 程序 ......................................... 39
5.3.6 SnakeModel.java 程序 ......................................... 41
5.4 安装文件的生成 .................................................. 43
5.4.1 inno setup 简介 .............................................. 43
5.4.2 安装文件制作步骤 .............................................43
第 6 章 软件维护 ....................................................... 48
结论 .................................................................. 49
参考文献 .............................................................. 51
附录 “贪吃蛇游戏”程序源代码 ......................................... 53
第 1 章 绪论
1.1 开发环境配置
1.1.1 Java2 的标准运行环境
Java2 的标准运行环境从 Sun Microsystems 的网站:http://java.sun.com,免费下
载许多相关文件,其中包括:J2SE SDK、J2SE JRE、J2SE Documentation 等。
1.1.1.1 J2SE SDK
Java2 Standard Edition Software Development Kit,包含创建 Java2 应用的开发工
具(Development Tools)、示例 (Demos)、源代码(Source Code) 、JRE、插件(Plug-in)
等,是 J2SE JRE 的超级,是创建 Java2 应用的必要文件。
J2sdk-1_4_2_05-windows-i586-p.exe(for Windows)
J2sdk-1_4_2_05-linux-i586.bin(for Linux)
1.1.1.2 J2SE JRE
Java2 Standard Edition Java Runtime Environment,包含允许终端用户运行 Java 2
应用的环境,是 J2SE SDK 的子级,是运行 Java2 应用的必要文件。
J2re-1_4_2_05-windows-i586-p.exe(for Windows)
J2re-1_4_2_05-linux-i586.bin(for Linux)
1.1.1.3 J2SE Documentation
Java2 Standard Edition Documentation 包含 Java2 的在线帮助文档。
J2sdk-1_4_2-doc.zip
1.1.2 J2SE 的安装与配置
1.1.2.1 安装过程
Java2 的标准运行环境在 Windows 平台下的安装过程如图 1-1 所示。
Step1.从官方网站下载免费安装软件:网址 http://java.sun.com
Step2.运行安装文件 J2sdk-1_4_2_05-windows-i586-p.exe;如图 1-1 所示:
1