游戏开发课程设计报告模版-扫雷游戏报告(定稿)
中原工学院信息商务学院
课程设计报告
2016~2017学年第一学期
课学学专指
业导
班教生
姓
程 游戏开发课程设计
扫雷游戏 *** ***** 数媒141 郑全录
名 号 级 师
课程设计名称
2016年12月
目录
第一章设计内容及要求 . ............................................................................................... 1
1.1 扫雷游戏的基本要求 . ..................................................................................... 1 1.2 游戏需要实现的主要功能 . ............................................................................. 1 第二章需求分析 . ........................................................................................................... 2
2.1扫雷游戏界面设计 . .......................................................................................... 2 2.2 雷区的设计 . ..................................................................................................... 2 2.3 音效的设计 . ..................................................................................................... 3 2.4 扫雷英雄榜的设计 . ......................................................................................... 3 第三章总体设计 . ........................................................................................................... 5
3.1 总体功能图 . ..................................................................................................... 5 3.2 总体流程图 . ..................................................................................................... 5 第四章主要功能设计流程 . ........................................................................................... 6
4.1 雷区设计流程图 . ............................................................................................ 6 4.2 音效设计流程图 . ............................................................................................ 6 4.3 英雄榜设计流程图 . ........................................................................................ 7 第五章主要功能设计流程 . ........................................................................................... 8
5.1 类之间关系图 . ................................................................................................ 8 5.2程序运行效果图 . ............................................................................................. 8
5.2.1 游戏登录界面 . ..................................................................................... 8 5.2.2游戏菜单界面 . ...................................................................................... 9 5.2.3游戏运行界面 . .................................................................................... 10 5.2.4游戏英雄榜界面 . ................................................................................ 12
第六章总结 . ................................................................................................................. 13 参考文献 . ..................................................................................................................... 14 附录 . ............................................................................................................................. 15
第一章 设计内容及要求
1.1扫雷游戏的基本要求
当前各种游戏层出不穷,因为游戏的开发成本非常大,所以游戏的开发具有一定的风险性,但是一些小游戏的开发成本具有成本小,编写简单的优势,所以这些小游戏在游戏开发中也占有一席之地。在这类小游戏中包括益智类游戏,它以画面精美、游戏方法简单等特点得到大家的认可。成为人们在工作之余不可或缺的好伙伴。针对这种情况我选择了扫雷游戏该课题作为我的课程题目。
Windows XP和Windows 7系统提供的扫雷游戏是一个很有趣的游戏。本次课程设计的主要任务是使用JA V A 语言编写一个与其类似的扫雷游戏。具体要求如下:
1、扫雷游戏分为初级、中级和高级三个级别,同时扫雷英雄榜存储每个级别的最好成绩,即挖出全部的地雷且用时最少者。单击游戏菜单可以选择“初级”、“中级”和“高级”或“查看英雄版”。
2、选择级别后将出现相应级别的扫雷区域,这时用户使用鼠标左键单击雷区中任何一个方块便启动了计时器。
3、用户要揭开某个方块,可单击它。若所揭方块下有雷,用户便输了这一局,若所揭方块下没有雷,则显示一个数字,该数字代表此方块周围的8个方块中共有多少颗雷。
4、如果用户认为某个方块下埋着雷,单击右键可以在方块上标识一个用户认为是雷的图标,即给出一个扫雷标记。用户每标记出一个扫雷标记(无论用户的标记是否正确) ,窗口左上角的雷数计数器将会减少一个。
5、扫雷胜利后(用时最少者) ,程序弹出保存成绩的对话框。
6、用户如果遇到疑问的方块,可以勾选游戏菜单下的标记选项卡(或者用快捷键Alt+G然后点击M )即可标记该可疑方块,此时双击右键并可出现“? ”标记。另有颜色选项,当用户选中该项时,游戏将以最低图像像素来显示。
7、如果用户需要使用提示声,可选中声音选项,此时将听到计时的咔嗒声。当用户点中雷时,将有爆炸声产生;胜利时,将有凯旋的旋律播放,此时企鹅出现欢悦表情。
1.2 游戏需要实现的主要功能
1、用户可以自定义级别并且可以任意输入雷数;
2、具有计时功能,既显示用户完成所有扫雷任务所需要的时间; 3、自动保存扫雷英雄榜。
第二章 需求分析
2.1 扫雷游戏界面设计
系统的整体布局为:CardLayout 布局,采用了菜单、按钮、面板等组件,菜单主要包括开始、选择级别、标记、扫雷英雄榜、退出等选项。
2.2 雷区的设计
MineArea 类是java.swing 包中Jpanel 容器的子类,实现了ActionListener 和MouseListener 接口,所创建的对象:mineArea 是MineGame 类中最重要的成员之一,作为一个容器添加到MineGame 窗口的中心。其中MineArea 类的主要成员变量、方法以及和MineGame 类之间组合关系的UML 图如图2-1所示。
图2-1 MineArea类的UML 图
以下是UML 图中有关数据和方法的详细说明。 1、成员变量
(1)block是Block 类型的数组,用来确定雷区有多少需进行扫雷的方块。 (2)blockView是BlockView 类型的数组,负责为block 数组中的Block 对象提供视图。
(3)lay是LayMines 类型的对象,负责设置block 数组中的哪些方块是雷或不是雷。
(4)record负责提供保存成绩的界面,是一个对话框,默认不可见。用户只有扫雷成功后(用时最少) ,才可以看见对话框。
(5)reStart是一个按钮对象,用户单击它重新开始游戏。 (6)time是计时器对象,负责计算用户的用时。 2、方法
(1)initMineArea(int,int,int,int)方法可根据参数提供的数据设置雷区的宽度、高度、雷的数目以及雷区的级别。
(2)actionPerformed(ActionEvent)是MineArea 类实现的ActionListener
接口中的方法。当用户单击blockView 中的某个方块时,actionPerformed(ActionEvent)方法负责执行有关算法,例如,当用鼠标左键单击方块上的按钮后,若该方块下有雷,actionPerformed(ActionEvent)方法将使用户输掉本局,若该方块下无雷,actionPerformed(ActionEvent)方法将显示blockView 对象中的标签,该标签上是一个数字,该数字代表当前方块的周围的8个方块中共有多少颗雷。
(3)show()方法是一个递归方法。actionPerformed(ActionEvent)方法执行时将调用show 方法进行扫雷。
(4)mousePressed(MouseEvent)方法是MineArea 类实现的MouseListener 接口中的方法,当用户按下鼠标有件事mousePressed(MouseEvent)方法负责让方块上显示一个探雷标记。
(5)inquireWin()方法用来判断用户是否扫雷成功,如果成功该方法负责让record 对话框可见。所谓扫雷成功是指不仅找到了全部的累而且用时最少。
2.3音效的设计
music 类是java.awt 包中Frame 的子类,通过构造音效声音方法,在MainFrame 主类中,通过ActionListener 来监视是否已开始游戏,来决定播放计时声,之后又通过ActionListener 来监视是否踩到雷和是否完成来决定,播放相应的音乐。下面标明music 类的主要成员变量、方法以及和MineFrame 类之间组合关系的UML 图如图2-2所示。
图2-2 MineArea 类的UML 图
2.4扫雷英雄榜的设计
LogDialog 是javax.swing 包中Jdialog 对话框的子类,并实现了ActiongListener 接口。LogDialog 创建的对象dialog 是MainFrame 类(主类)的成员之一,当用户选择查看“英雄榜”菜单项时,该对话框可见,提供了查看扫雷的最好成绩的界面。标明LogDialog 类的主要成员变量、方法以及和
MainFrame 类之间组合关系的UML 图如图2-3所示。
图2-3 LogDialog 类的UML 图
第三章 总体设计
3.1总体功能图
图3-1总体功能图
3.2总体流程图
图3-2总体流程图
第四章 主要功能设计流程
4.1雷区设计流程图
图4-1雷区设计流程图
4.2音效设计流程图
图4-2音效设计流程图
4.3英雄榜设计流程图
N
图4-3排行榜设计流程图
第五章 运行结果
5.1类之间关系图
图5-1类之间关系图
5.2 程序运行效果图
5.2.1游戏登录界面
图5-2初级扫雷界面图
图5-3初级扫雷界面图
图5-4高级扫雷界面图
5.2.2游戏菜单界面
图5-5游戏菜单界面
5.2.3游戏运行界面
图5-6游戏自定义雷区界面
图5-7游戏运行主界面
图5-8游戏失败界面
图5-9游戏胜利界面
5.2.4游戏英雄榜界面
图5-10游戏英雄榜界面
第六章 总结
此次课程设计的题目为扫雷游戏,通过课程设计,进一步巩固、加深我们所学专业课程《JAVA 语言教程》的基本理论知识,理论联系实际,进一步培养我们的综合分析问题,解决问题的能力;全面考核我们所掌握的基本理论知识及其实际业务能力,从而达到提高学生素质的最终目的;利用所学知识,开发小型应用系统,掌握运用JAVA 语言编写调试应用系统程序,训练独立开发应用系统,进行数据处理的综合能力;对于给定的设计题目,如何进行分析,理清思路,并给出相应的数学模型;熟练掌握JAVA 语言的GUI 设计、线程技术,灵活运用各种类库,为今后从事实际工作打下坚实的基础。通过该课程设计,全面系统的理解了JAVA 语言的一般原理和基本实现方法。把死板的课本知识变得生动有趣,激发了学习的积极性。把学过的JAVA 的知识强化,能够把课堂上学的知识通过自己设计的程序表示出来,加深了对理论知识的理解。
我在网上查询了许多与扫雷游戏程序的相关资料,终于完成了扫雷游戏的设计。程序基本实现了该课程设计的基本要求。在设计的过程中了解到自己的知识还是比较有限,更肯定了自己再以后学习编程的道路上的坚定不移,同时也让我懂得组合作的重要性。但由于只学习了一个学期的Java 语言,自己水平有限,使得程序还是有些不完善的地方。回顾起此次Java 课程设计,至今我仍感慨颇多,的确,从拿到题目的开始,从理论到实践,在整整两个星期的日子里,可以说是苦多于甜,但是可以学到很多很多的东西,同时不仅可以巩固了以前所学的知识,而且学到了很多在书本上所没有学到过的知识。通过这次课程设计使我懂得了理论与实践相结合是很重要的,只有理论知识是远远不够的,只有把所学的理论知识与实践相结合起来,从理论中得出结论,才能真正掌握这门技术,也提高了自己的独立思考的能力。在设计的过程遇到问题,可以说得上是困难重重,这毕竟第一次做的,难免会遇到各种各样的问题,同时在设计的过程中发现了自己的不足之处,对以前所学过的知识理解得不够深刻,掌握得不够牢固,通过这次课程设计之后,一定把以前所学的知识重新温故。
参考文献
[1]吕凤羽、马皓《JA V A 程序设计》清华大学出版社
[2] 耿祥义《JA V A 课程设计》清华大学出版
[3] 黄小东《Java 课程设计案例精编》中国水利水电出版社
[4]印旻《Java 与面向对象程序设计》清华大学出版社
附 录
1、 游戏登陆界面部分源代码
public class LogDialog extends JDialog implements ActionListener { private LevelLog levelLog[];
private JTextArea logArea;
private JButton resetButton;
private JButton confirmButton;
public LogDialog(JFrame frame, LevelLog log[]) {
super(frame, "扫雷英雄榜", true); getContentPane().set Layout(null);
levelLog = log;
logArea = new JTextArea(); logArea.setEditable(false);
logArea.setBackground(UIManager.getColor("CheckBox.background"));
logArea.setBounds(10,10,160,60); getContentPane().add(logArea, null); resetButton = new JButton("重新计分"); resetButton.setBounds(10,70,90,25); resetButton.addActionListener(this); getContentPane().add(resetButton, null); setTextArea(); confirmButton = new JButton("确定"); confirmButton.setBounds(105,70,60,25); confirmButton.addActionListener(this); getContentPane().add(confirmButton, null);
setSize(180,140);
setLocationRelativeTo(frame);
setResizable(false);
show();
}
private void setTextArea() {
}
public void actionPerformed(ActionEvent e) {
}
} if(e.getSource()==resetButton) { for(int i=0;i
2、 雷区设计代码
public class LedNumber extends Component {
private Polygon segmentPolygon[];
private int numberSegment[][] = {
{0, 1, 2, 3, 4, 5 }, //0
{1, 2 }, //1
{0, 1, 3, 4, 6 }, //2
{0, 1, 2, 3, 6 }, //3
{1, 2, 5, 6 }, //4
{0, 2, 3, 5, 6 }, //5
{0, 2, 3, 4, 5, 6 }, //6
{0, 1, 2 }, //7
{0, 1, 2, 3, 4, 5, 6 }, //8
{0, 1, 2, 3, 5, 6 } //9 };
private int div[] = {1,10,100,1000,10000,100000};
private Image numberImage[];
private Color fontColor = Color.red; //the color of number
private Color bgColor = Color.black;
private Color maskColor = Color.darkGray;
private int dWidth = 12;
private int dHeight = 21;
public LedNumber() {
init(); }
public LedNumber(Color fc) {
fontColor = fc;
init(); }
public LedNumber(Color fc, Color bgc) {
bgColor = bgc;
fontColor = fc;
init();}
public LedNumber(Color fc,Color bgc,Color mc) {
bgColor = bgc;
fontColor = fc;
maskColor = mc;
init(); }
public Image getLedImage(int dg, int bound) {
dg %= div[bound];
Image image = new
dHeight,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
bound--;
for(int i = bound;i>=0;i--) {
g.drawImage(numberImage[dg/div[i]],(bound-i)*dWidth,0,this);
dg %= div[i]; }
return image; }
public void init() {
segmentPolygon = new Polygon[7];
numberImage = new Image[10];
//setup polygons
setNumberPolygon();
setNumberImage();}
public void setBackGround(Color bgc) {
bgColor = bgc; }
public void setFontColor(Color fc) {
BufferedImage(dWidth*bound,
fontColor = fc; }
public void setMaskColor(Color mkc) {
maskColor = mkc; }
public void setDigitWidth(int w) {
dWidth = w;
init(); }
public void setDigitHeight(int h) {
dHeight = h;
init(); }
public void setDigitSize(int w, int h) {
dWidth = w;
dHeight = h;
init(); }
private void setNumberImage() {
int i = 0;
int j = 0;
int k;
Graphics g;
while(i
numberImage[i] = new BufferedImage(15,20,BufferedImage.TYPE_INT_RGB); g = numberImage[i].getGraphics();
g.setColor(bgColor);
g.fillRect(0,0,15,20);
g.setColor(Color.DARK_GRAY);
j = 0;
while(j
k = numberSegment[8][j];
g.fillPolygon(segmentPolygon[k]);
j++; }
g.setColor(fontColor);
j = 0;
while(j
k = numberSegment[i][j];
g.fillPolygon(segmentPolygon[k]);
j++; }
i++; }
}
public void setNumberPolygon() {
int mid = dHeight/2+1;
segmentPolygon[0] = new Polygon();
segmentPolygon[0].addPoint(2, 1);
segmentPolygon[0].addPoint(dWidth-2,1);
segmentPolygon[0].addPoint(dWidth-5,4);
segmentPolygon[0].addPoint(4,4);
segmentPolygon[1] = new Polygon();
segmentPolygon[1].addPoint(dWidth-1, 1);
segmentPolygon[1].addPoint(dWidth-1, mid-1);
segmentPolygon[1].addPoint(dWidth-2, mid-1);
segmentPolygon[1].addPoint(dWidth-4, mid-3);
segmentPolygon[1].addPoint(dWidth-4, 4);
segmentPolygon[2] = new Polygon();
segmentPolygon[2].addPoint(dWidth-1, mid);
segmentPolygon[2].addPoint(dWidth-1, dHeight-2);
segmentPolygon[2].addPoint(dWidth-4, dHeight-5);
segmentPolygon[2].addPoint(dWidth-4, mid+1);
segmentPolygon[2].addPoint(dWidth-3, mid);
segmentPolygon[3] = new Polygon();
segmentPolygon[3].addPoint(dWidth-2, dHeight-1);
segmentPolygon[3].addPoint(1, dHeight-1);
segmentPolygon[3].addPoint(4, dHeight-4);
segmentPolygon[3].addPoint(dWidth-4, dHeight-4);
segmentPolygon[4] = new Polygon();
segmentPolygon[4].addPoint(1, dHeight-2);
segmentPolygon[4].addPoint(1, mid);
segmentPolygon[4].addPoint(3, mid);
segmentPolygon[4].addPoint(4, mid+1);
segmentPolygon[4].addPoint(4, dHeight-5);
segmentPolygon[5] = new Polygon();
segmentPolygon[5].addPoint(1, mid-1);
segmentPolygon[5].addPoint(1, 1);
19
segmentPolygon[5].addPoint(4, 4);
segmentPolygon[5].addPoint(4, mid-3);
segmentPolygon[5].addPoint(2, mid-1);
segmentPolygon[6] = new Polygon();
segmentPolygon[6].addPoint(3, mid-1);
segmentPolygon[6].addPoint(4, mid-2);
segmentPolygon[6].addPoint(dWidth-4, mid-2);
segmentPolygon[6].addPoint(dWidth-3, mid-1);
segmentPolygon[6].addPoint(dWidth-5, mid+1);
segmentPolygon[6].addPoint(4, mid+1);
}
}
3、 自定义扫雷界面代码
public class CustomDialog extends JDialog implements ActionListener { private JTextField widthField;
private JTextField heightField;
private JTextField mineField; private JButton confirmButton;
private JButton cancelButton;
private static LevelInfo level;
public CustomDialog(Frame frame, LevelInfo levelInfo) {
super(frame,"自定义雷区",true); getContentPane().set Layout(null); JLabel tempLabel = new JLabel("高度:"); tempLabel.setBounds(10,10,30,20); heightField = new JTextField(""+levelInfo.getXBound()); heightField.setBounds(50,10,40,20); getContentPane().add(tempLabel,null); getContentPane().add(heightField,null); tempLabel = new JLabel("宽度:"); tempLabel.setBounds(10,40,30,20); widthField = new JTextField(""+levelInfo.getYBound()); widthField.setBounds(50,40,40,20); getContentPane().add(tempLabel,null); getContentPane().add(widthField,null); 20
tempLabel = new JLabel("雷数:"); tempLabel.setBounds(10,70,30,20); mineField = new JTextField(""+levelInfo.getMineCount()); mineField.setBounds(50,70,40,20); getContentPane().add(tempLabel,null); getContentPane().add(mineField,null); confirmButton = new JButton("确定"); confirmButton.addActionListener(this); confirmButton.setBounds(100,10,60,25); getContentPane().add(confirmButton,null); cancelButton = new JButton("取消"); cancelButton.addActionListener(this); cancelButton.setBounds(100,45,60,25); getContentPane().add(cancelButton,null);
setSize(180,137);
setLocationRelativeTo(frame);
setResizable(false);
show();
}
public void actionPerformed(ActionEvent e) {
}
public static LevelInfo getUserLevel(JFrame frame, LevelInfo levelInfo) {
}
} CustomDialog dialog = new CustomDialog(frame, levelInfo); return level; level = null; if(e.getSource()==confirmButton) { } dispose(); int x = Integer.parseInt(heightField.getText()); int y = Integer.parseInt(widthField.getText()); int m = Integer.parseInt(mineField.getText()); level = new LevelInfo(x,y,m);
21
4、 扫雷英雄榜界面代码
public class LogDialog extends JDialog implements ActionListener {
private LevelLog levelLog[]; private JTextArea logArea; private JButton resetButton; private JButton confirmButton;
public LogDialog(JFrame frame, LevelLog log[]) {
super(frame, "扫雷英雄榜", true); getContentPane().set Layout(null); levelLog = log; logArea = new JTextArea(); logArea.setEditable(false); logArea.setBackground(UIManager.getColor("CheckBox.background")); logArea.setBounds(10,10,160,60); getContentPane().add(logArea, null); resetButton = new JButton("重新计分"); resetButton.setBounds(10,70,90,25); resetButton.addActionListener(this); getContentPane().add(resetButton, null); setTextArea(); confirmButton = new JButton("确定"); confirmButton.setBounds(105,70,60,25); confirmButton.addActionListener(this); getContentPane().add(confirmButton, null);
setSize(180,140);
setLocationRelativeTo(frame);
setResizable(false);
show();
}
private void setTextArea() {
}
22
logArea.setText("初级:" + levelLog[0].toString() + "中级:" + levelLog[1].toString() + "高级:" + levelLog[2].toString());
public void actionPerformed(ActionEvent e) {
if(e.getSource()==resetButton) { for(int i=0;i
setTextArea();
} else {
dispose();
}
}
}
23