超级玛丽Java课程设计文档格式.docx
《超级玛丽Java课程设计文档格式.docx》由会员分享,可在线阅读,更多相关《超级玛丽Java课程设计文档格式.docx(80页珍藏版)》请在冰点文库上搜索。
![超级玛丽Java课程设计文档格式.docx](https://file1.bingdoc.com/fileroot1/2023-5/8/b1d709a6-3ffe-4f5b-a548-0ef5bc74b684/b1d709a6-3ffe-4f5b-a548-0ef5bc74b6841.gif)
清华大学出版社,2009.
四、要求的设计(调查/论文)成果
1.课程设计报告
2.课程设计软件
五、进程安排
布置任务,查找资料、需求分析1天
总体设计1天
详细设计1.5天
编制源程序实现3.5天
测试与修改1天
撰写课设报告2天
六、主要参考资料
[1]张广彬.Java课程设计案例精编(第二版).北京:
清华大学出版社,2011.
[2]耿祥义.Java课程设计(第二版).北京:
清华大学出版社,2008.
[3]耿祥义.JAVA大学实用教程.北京:
[4]邹林达.陈国君Java2程序设计基础.北京:
清华大学出版社,2010.
[5]邹林达.Java2程序设计基础实验指导.北京:
清华大学,2010.
指导教师(签名):
20年月日
1需求与总体设计
1.1需求分析
提供一个友好的用户交互界面,简单明了容易操作。
并且游戏有一定的难度和娱乐性。
开发一个Mario类。
一个场景类,以及敌人类,障碍物类。
让超级玛丽能够在场景中完成各种动作。
并且有场景的切换等等功能。
游戏中设计了一些很有意思的陷阱,玩家一步小心掉入陷阱,超级玛丽就会死亡。
这样会给人意想不到的惊奇,达到娱乐大众,增加游戏难度的效果。
游戏的设计匠心独具,并且都经过了各种测试,除了为玩家增加一些意想不到的游戏陷阱以外,还必须保证玩家能够通关。
1.2程序功能图
如图1所示:
图1
1.3程序类图
系统的整体类图的框架如图2所示。
首先是窗体类。
他需要调用到Mario和BackGround类中的方法。
以便动态的显示Mario类和BackGround。
而BackGround里面又包含了Enemy和Obstruction。
Enemy,Obstruction和Mario类又调用静态类StaticValue的方法,用以显示真实的图片。
而StaticValue则是一个静态类,将存储在硬盘的图片资源加载进入内存以便其他模块调用。
图2
2详细设计
2.1MyFrame实现
2.1.1窗体对象的序列图
窗体对象继承了JFrame类并且实现了Runnable接口。
作为窗体对象,它首先显示窗体,绘制背景。
然后启动线程。
在run方法里面动态得绘制窗体中需要显示的Mario的图片,障碍物的图片,敌人的图片。
该窗体的run方法是一个死循环,每次执行一个循环调用sleep方法睡眠50毫秒。
序列图如图3所示。
图3
2.1.2Myframe详细类图
privateMariomario;
//Mario对象
privatebooleanisStart=false;
//标识游戏是否已经开始
this.paintAllEnemys(g2);
//绘制所有的敌人
this.paintAllObstruction(g2);
//绘制所有障碍物
2.2Mario实现
2.2.1Mario对象的序列图
因为需要有一个专门的线程来实时监测Mario的移动状况,和障碍物和敌人的碰撞情况。
所以让Mario类继承了Runnable接口。
Mario先启动线程。
该线程的run方法也是一个死循环。
首先根据Mario的状态changeMarioImage方法修改Mario的显示图片。
再根据Mario的determinexy方法决定Mario的移动。
然后通过deterMarioWithObstruction方法检测Mario和障碍物之间的碰撞,在根据marioTouchEnemys方法检测Mario和敌人之间的碰撞。
然后调用sleep()方法让线程休息5毫秒。
如此结束一个循环。
序列图如图4所示。
图4
2.2.2Mario类图
类图中:
privateintx;
//Mario的x坐标
privateinty;
//Mario的y坐标
privateintlife;
//Mario的生命
BackGroundbg=null;
//Mario所在的背景
privateintuptime;
//Mario的上升时间
Stringstatus;
//Mario的移动状态
intscore;
//Mario的分数
Threadt;
//Mario的线程
intxmove;
//Mario的x移动距离
intymove;
//Mario的y移动距离
privatebooleanisClear=false;
//标识用户是否通关。
isClear为true时代表用户通关了
2.3Enemy类的实现
//敌人的x坐标
//敌人的y坐标
privateintstartX;
//敌人的初始x坐标
privateintstartY;
//敌人的初始y坐标
privatebooleanstartIsLeftOrUp;
//敌人初始的移动方向
privateintstartImageType;
//初始的显示图片
privateinttype;
//敌人的类型1代表蘑菇,2代表食人花,3代表乌龟
privateBufferedImagebufferedImage;
privateBackGroundbg;
//障碍物所在的背景
privateintimageType;
//移动的极限范围
详细类图如下:
2.4Obstruction类的实现
//障碍物的x坐标
//障碍物的y坐标
privateBufferedImageshowImage=null;
//障碍物的显示图片
//障碍物的类型
privateintstartType;
//障碍物的初始类型
//标识该障碍物在哪一个场景中
2.5BackGround类实现
privateintsort;
//背景的序列号
privatebooleanflag;
//标识是否是最后一张图片,flag为true时代表到了最后一张图片
privatebooleanisOver=false;
//标识游戏是否已经结束
privatebooleanisDown=false;
//标志是否降旗结束
3编码实现
源代码
importjava.awt.image.BufferedImage;
importjava.util.ArrayList;
importjava.util.List;
importjava.awt.Graphics;
importjava.awt.Toolkit;
importjava.awt.event.KeyEvent;
importjava.awt.event.KeyListener;
importjava.util.Iterator;
importjavax.swing.JFrame;
importjavax.swing.JOptionPane;
importjava.io.File;
importjavax.imageio.ImageIO;
publicclassMyFrameextendsJFrameimplementsKeyListener,Runnable
{
publicstaticvoidmain(String[]args)
{
newMyFrame();
}
privateList<
BackGround>
allBg=newArrayList<
();
//当前背景
privateBackGroundnowBg=null;
privateMariomario;
privatebooleanisStart=false;
publicvoidsetNowBg(BackGroundnowBg){
this.nowBg=nowBg;
privateThreadt;
/**
*/
publicMyFrame()
this.setSize(900,600);
this.setTitle("
马里奥游戏"
);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
intwidth=Toolkit.getDefaultToolkit().getScreenSize().width;
intheight=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setLocation((width-900)/2,(height-600)/2);
this.addKeyListener(this);
StaticValue.init();
this.setVisible(true);
//绘制出所有的背景
for(inti=1;
i<
=4;
i++)
{
this.allBg.add(newBackGround(i,i==4?
true:
false));
}
this.nowBg=this.allBg.get(0);
this.mario=newMario(0,480);
this.mario.setBg(nowBg);
this.setResizable(false);
t=newThread(this);
t.start();
@Override
publicvoidpaint(Graphicsg)
BufferedImageimage=newBufferedImage(900,600,BufferedImage.TYPE_4BYTE_ABGR);
if(isStart)
Graphicsg2=image.getGraphics();
g2.drawImage(this.nowBg.getBgImage(),0,0,this);
//绘制背景
g2.drawString("
生命:
"
+this.mario.getLife(),800,60);
//显示生命数
分数:
+this.mario.getScore(),60,60);
this.paintAllEnemys(g2);
this.paintAllObstruction(g2);
else
g2.drawImage(StaticValue.startImage,0,0,this);
//绘制游戏开始的图片
g.drawImage(image,0,0,this);
publicvoidkeyPressed(KeyEvente)
//
intk=e.getKeyCode();
//获得键盘的ASCII码
System.out.println(k);
if(this.isStart)
if(e.getKeyCode()==39)
{
this.mario.rightMove();
}
if(e.getKeyCode()==37)
this.mario.leftMove();
if(e.getKeyCode()==32)
this.mario.jump();
//按下空格键,Mario的生命加10
if(70==e.getKeyCode())
this.mario.setLife(this.mario.getLife()+10);
this.isStart=true;
this.nowBg.enemysStartMove();
publicvoidkeyReleased(KeyEvente)
if(e.getKeyCode()==39)
this.mario.rightStop();
if(e.getKeyCode()==37)
this.mario.leftStop();
publicvoidkeyTyped(KeyEvente)
//TODOAuto-generatedmethodstub
publicvoidrun()
while(true)
this.repaint();
if(840==this.mario.getX())
this.intoNewBackGround();
//进入下一张图片
if(this.mario.isClear())
JOptionPane.showMessageDialog(this,"
恭喜您通关了,敬请期待新的关卡"
System.exit(0);
if(this.mario.isDead())
this.gameOver();
//游戏结束
this.sleep();
//进入下一张图片
privatevoidintoNewBackGround()
this.nowBg=this.allBg.get(this.nowBg.getSort());
this.mario.setBg(this.nowBg);
this.mario.setX(0);
this.mario.setY(480);
this.mario.setUptime(0);
this.nowBg.enemysStartMove();
//绘制所有的敌人
privatevoidpaintAllEnemys(Graphicsg2)
Iterator<
Enemy>
iterEnemy=this.nowBg.getAllEnemy().iterator();
while(iterEnemy.hasNext())
Enemye=iterEnemy.next();
g2.drawImage(e.getBufferedImage(),e.getX(),e.getY(),this);
//绘制所有障碍物
privatevoidpaintAllObstruction(Graphicsg2)
Obstruction>
iter=this.nowBg.getAllObstruction().iterator();
while(iter.hasNext())
Obstructionob=iter.next();
g2.drawImage(ob.getShowImage(),ob.getX(),ob.getY(),this);
g2.drawImage(mario.getShowImage(),mario.getX(),mario.getY(),this);
privatevoidsleep()
try
Thread.sleep(20);
catch(InterruptedExceptione)
e.printStackTrace();
//游戏结束
privatevoidgameOver()
JOptionPane.showMessageDialog(this,"
马里奥死亡"
this.mario.setDead(false);
System.exit(0);
}
classBackGround{
privateBufferedImagebgImage=null;
privateintsort;
privatebooleanflag;
//标识是否是最后一张图片,flag为true时代表到了最后一张图片
allEnemy=newArrayList<
//所有敌人的集合
//所有障碍物的集合
allObstruction=newArrayList<
//所有被移除的敌人的集合
removeEnemy=newArrayList<
//所有被移除的障碍物的集合
removeObstruction=newArrayList<
privatebooleanisOver=false;
privatebooleanisDown=false;
publicbooleanisDown(){
returnisDown;
publicvoidsetDown(booleanisDown){
this.isDown=isDown;
publicbooleanisOver(){
returnisOver;
publicvoidsetOver(booleanisOver){
this.isOver=isOver;
publicbooleanisFlag(){
returnflag;
publicvoidsetFlag(booleanflag){
this.flag=flag;
publicintgetSort(){
returnsort;
publicvoidsetSort(intsort){
this.sort=sort;
publicBufferedImagegetBgImage()
returnbgImage;
publicList<
getAllObstruction()
returnallObstruction;
getRemoveObstruction(){
returnremoveObstruction;
getAllEnemy(){
returnallEnemy;
getRemoveEnemy(){
returnremoveEnemy;
*
*@paramsort背景的序列号
*@paramflag标识是否是最后一张背景
publicBackGround(intsort,booleanflag)
if(flag)
bgImage=StaticValue.endImage;
bgImage=StaticValue.bgImage;
switch(sort)
case1:
this.creat1Map();
break;
case2:
this.creat2Map();
case3:
this.creat3Map();
case4:
this.creat4Map();
publicvoidreset()
thi