超级玛丽Java课程设计_第1页
超级玛丽Java课程设计_第2页
超级玛丽Java课程设计_第3页
超级玛丽Java课程设计_第4页
超级玛丽Java课程设计_第5页
已阅读5页,还剩62页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 面向对象程序设计课程设计报告题 目: 超级玛丽软件的设计与实现 院 (系): 信息科学与工程学院 专业班级: 计算机科学与技术1201班 学生姓名: 程伟 学 号: 20121183011 指导教师: 吴奕 20 14 年 12 月 29 日至20 15 年 1 月 9 日华中科技大学武昌分校制 面向对象程序设计 课程设计任务书一、设计(调查报告/论文)题目超级玛丽软件的设计与实现二、设计(调查报告/论文)主要内容内容:开发超级玛丽能够在场景中完成各种动作,并且有场景的切换功能。设计一个mario类、一个场景类、以及敌人类、障碍物类。实现有一定的挑战的休闲单机小游戏。 基本功能与要求:设计一

2、个超级玛丽游戏实现以下功能:1、 点击“空格”可以使游戏人物进行跳跃。2、 点击方向键左右可以使游戏人物前进或者后退。3、 设计障碍物,增加游戏的难度。4、 设计敌人类,增加游戏难度。5、 设计跳跃踩死敌人或者跳跃越过障碍物,保证可以正常前进。6、 保证通过所有障碍后可以通关。三、原始资料1.面向对象程序设计课程设计指导书2. 耿祥义java大学实用教程北京:清华大学出版社,2009四、要求的设计(调查/论文)成果1.课程设计报告2.课程设计软件五、进程安排布置任务,查找资料、需求分析1天总体设计1天详细设计1.5天编制源程序实现3.5天测试与修改1天撰写课设报告2天六、主要参考资料1 张广彬

3、. java课程设计案例精编(第二版).北京:清华大学出版社, 2011. 2 耿祥义. java课程设计(第二版).北京:清华大学出版社, 2008.3 耿祥义java大学实用教程北京:清华大学出版社,20094 邹林达陈国君 java2程序设计基础北京:清华大学出版社,20105 邹林达java2程序设计基础实验指导北京:清华大学,2010指导教师(签名): 20 年 月 日目录1需求与总体设计51.1需求分析51.2程序功能图51.3程序类图62详细设计72.1 myframe实现72.1.1窗体对象的序列图72.1.2 myframe详细类图82.2 mario实现82.2.1 mar

4、io对象的序列图82.2.2 mario类图92.3 enemy类的实现102.4 obstruction类的实现112.5 background类实现113编码实现123.1 myframe类的详细代码123.2 background类的详细代码133.3 mario类的详细代码163.4 enemy类的详细代码243.5 obstruction类的详细代码314系统测试334.1 游戏开始界面的测试334.2 游戏运行的测试334.3 排除错误的处理36总 结37 1需求与总体设计1.1需求分析提供一个友好的用户交互界面,简单明了容易操作。并且游戏有一定的难度和娱乐性。开发一个mario类

5、。一个场景类,以及敌人类,障碍物类。让超级玛丽能够在场景中完成各种动作。并且有场景的切换等等功能。游戏中设计了一些很有意思的陷阱,玩家一步小心掉入陷阱,超级玛丽就会死亡。这样会给人意想不到的惊奇,达到娱乐大众,增加游戏难度的效果。游戏的设计匠心独具,并且都经过了各种测试,除了为玩家增加一些意想不到的游戏陷阱以外,还必须保证玩家能够通关。1.2程序功能图如图1所示:图11.3程序类图系统的整体类图的框架如图2所示。首先是窗体类。他需要调用到mario和background类中的方法。以便动态的显示mario类和background。而background里面又包含了enemy和obstructi

6、on。enemy,obstruction和mario类又调用静态类staticvalue的方法,用以显示真实的图片。而staticvalue则是一个静态类,将存储在硬盘的图片资源加载进入内存以便其他模块调用。图22详细设计2.1 myframe实现2.1.1窗体对象的序列图窗体对象继承了jframe类并且实现了runnable接口。作为窗体对象,它首先显示窗体,绘制背景。然后启动线程。在run方法里面动态得绘制窗体中需要显示的mario的图片,障碍物的图片,敌人的图片。该窗体的run方法是一个死循环,每次执行一个 循环调用sleep方法睡眠50毫秒。序列图如图3所示。图32.1.2 myfra

7、me详细类图private mario mario;/mario对象private boolean isstart = false;/标识游戏是否已经开始this.paintallenemys(g2);/绘制所有的敌人this.paintallobstruction(g2);/绘制所有障碍物2.2 mario实现2.2.1 mario对象的序列图因为需要有一个专门的线程来实时监测mario的移动状况,和障碍物和敌人的碰撞情况。所以让mario类继承了runnable接口。mario先启动线程。该线程的run方法也是一个死循环。首先根据mario的状态changemarioimage方法修改ma

8、rio的显示图片。再根据mario的determinexy方法决定mario的移动。然后通过determariowithobstruction方法检测mario和障碍物之间的碰撞,在根据mariotouchenemys方法检测mario和敌人之间的碰撞。然后调用sleep()方法让线程休息5毫秒。如此结束一个循环。序列图如图4所示。图42.2.2 mario类图类图中:private int x;/mario的x坐标private int y;/mario的y坐标private int life;/mario的生命background bg = null;/mario所在的背景private

9、int uptime;/mario的上升时间string status;/mario的移动状态int score;/mario的分数thread t;/mario的线程int xmove;/mario的x移动距离int ymove;/mario的y移动距离private boolean isclear = false;/标识用户是否通关。isclear为true时代表用户通关了2.3 enemy类的实现private int x;/敌人的x坐标private int y;/敌人的y坐标private int startx;/敌人的初始x坐标private int starty;/敌人的初始y坐

10、标private boolean startisleftorup;/敌人初始的移动方向private int startimagetype;/初始的显示图片private int type;/敌人的类型1代表蘑菇,2代表食人花,3代表乌龟private bufferedimage bufferedimage;private background bg;/障碍物所在的背景private int imagetype;/移动的极限范围详细类图如下:2.4 obstruction类的实现private int x;/障碍物的x坐标private int y;/障碍物的y坐标private buffer

11、edimage showimage = null;/障碍物的显示图片private int type;/障碍物的类型private int starttype;/障碍物的初始类型private background bg;/标识该障碍物在哪一个场景中详细类图如下:2.5 background类实现private int sort;/背景的序列号private boolean flag;/标识是否是最后一张图片,flag为true时代表到了最后一张图片private boolean isover = false;/标识游戏是否已经结束private boolean isdown = false;

12、/标志是否降旗结束详细类图如下:3编码实现源代码import java.awt.image.bufferedimage;import java.util.arraylist;import java.util.list;import java.awt.graphics;import java.awt.toolkit;import java.awt.event.keyevent;import java.awt.event.keylistener;import java.util.iterator;import javax.swing.jframe;import javax.swing.joptio

13、npane;import java.io.file;import javax.imageio.imageio;public class myframe extends jframe implements keylistener,runnablepublic static void main(string args)new myframe();private list allbg = new arraylist();/当前背景private background nowbg = null;private mario mario;/mario对象private boolean isstart =

14、false;/标识游戏是否已经开始public void setnowbg(background nowbg) this.nowbg = nowbg;private thread t;/* */public myframe()this.setsize(900,600);this.settitle(马里奥游戏);this.setdefaultcloseoperation(jframe.exit_on_close);int width = toolkit.getdefaulttoolkit().getscreensize().width;int height = toolkit.getdefaul

15、ttoolkit().getscreensize().height;this.setlocation(width - 900) / 2,(height - 600) / 2);this.addkeylistener(this);staticvalue.init();this.setvisible(true);/绘制出所有的背景for(int i = 1; i = 4; i+)this.allbg.add(new background(i, i = 4 ? true : false);this.nowbg = this.allbg.get(0);this.mario = new mario(0,

16、 480);this.mario.setbg(nowbg);this.setresizable(false);t = new thread(this);t.start();overridepublic void paint(graphics g)bufferedimage image = new bufferedimage(900, 600, bufferedimage.type_4byte_abgr);if(isstart)graphics g2 = image.getgraphics();g2.drawimage(this.nowbg.getbgimage(), 0, 0, this);/

17、绘制背景g2.drawstring(生命: + this.mario.getlife(), 800, 60);/显示生命数g2.drawstring(分数: + this.mario.getscore(), 60, 60);/显示生命数this.paintallenemys(g2);/绘制所有的敌人this.paintallobstruction(g2);/绘制所有障碍物elsegraphics g2 = image.getgraphics();g2.drawimage(staticvalue.startimage, 0, 0, this);/绘制游戏开始的图片g.drawimage(imag

18、e, 0, 0, this);/* */overridepublic void keypressed(keyevent e)/int k = 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的生命加10if(70 = e.get

19、keycode()this.mario.setlife(this.mario.getlife() + 10);elseif(e.getkeycode() = 32)this.isstart = true;this.nowbg.enemysstartmove();/* */overridepublic void keyreleased(keyevent e)if(e.getkeycode() = 39)this.mario.rightstop();if(e.getkeycode() = 37)this.mario.leftstop();/* */overridepublic void keyty

20、ped(keyevent e)/ todo auto-generated method stuboverridepublic void run()while(true)this.repaint();if(840 = this.mario.getx()onewbackground();/进入下一张图片if(this.mario.isclear()joptionpane.showmessagedialog(this, 恭喜您通关了,敬请期待新的关卡);system.exit(0);if(this.mario.isdead()this.gameover();/游戏结束this.sle

21、ep();/进入下一张图片private void intonewbackground()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();/绘制所有的敌人private void paintallenemys(graphics g2)iterator iterenemy = this.nowbg.geta

22、llenemy().iterator();while(iterenemy.hasnext()enemy e = iterenemy.next();g2.drawimage(e.getbufferedimage(), e.getx(), e.gety(), this);/绘制所有障碍物private void paintallobstruction(graphics g2)iterator iter = this.nowbg.getallobstruction().iterator();while(iter.hasnext()obstruction ob = iter.next();g2.dra

23、wimage(ob.getshowimage(), ob.getx(), ob.gety(), this);g2.drawimage(mario.getshowimage(), mario.getx(), mario.gety(), this);private void sleep()trythread.sleep(20);catch (interruptedexception e)e.printstacktrace();/游戏结束private void gameover()joptionpane.showmessagedialog(this, 马里奥死亡);this.mario.setde

24、ad(false);system.exit(0);class backgroundprivate bufferedimage bgimage = null;private int sort;/背景的序列号private boolean flag;/标识是否是最后一张图片,flag为true时代表到了最后一张图片private list allenemy = new arraylist();/所有敌人的集合/所有障碍物的集合private list allobstruction = new arraylist();/所有被移除的敌人的集合private list removeenemy = ne

25、w arraylist();/所有被移除的障碍物的集合private list removeobstruction = new arraylist();private boolean isover = false;/标识游戏是否已经结束private boolean isdown = false;/标志是否降旗结束public boolean isdown() return isdown;public void setdown(boolean isdown) this.isdown = isdown;public boolean isover() return isover;public vo

26、id setover(boolean isover) this.isover = isover;public boolean isflag() return flag;public void setflag(boolean flag) this.flag = flag;public int getsort() return sort;public void setsort(int sort) this.sort = sort;public bufferedimage getbgimage()return bgimage;public list getallobstruction()return

27、 allobstruction;public list getremoveobstruction() return removeobstruction;public list getallenemy() return allenemy;public list getremoveenemy() return removeenemy;/* * * param sort背景的序列号 * param flag标识是否是最后一张背景 */public background(int sort, boolean flag)this.sort = sort;this.flag = flag;if(flag)b

28、gimage = staticvalue.endimage;elsebgimage = staticvalue.bgimage;switch(sort)case 1:this.creat1map();break;case 2:this.creat2map();break;case 3:this.creat3map();break;case 4:this.creat4map();break;public void reset()this.allenemy.addall(removeenemy);for(int i = 0; i this.allenemy.size(); +i)this.alle

29、nemy.get(i).reset();this.removeenemy.clear();this.allobstruction.addall(removeobstruction);for(int i = 0; i this.allobstruction.size(); +i)this.allobstruction.get(i).reset();this.removeobstruction.clear();public void enemysstartmove()for(int i =0; i this.allenemy.size(); +i)this.allenemy.get(i).star

30、move();/创建第一张图private void creat1map()for(int i = 0; i 15; i+)/创建出所有地面this.allobstruction.add(new obstruction(i * 60,540,9, this);/创建出所有砖块,10代表问好,0代表木块this.allobstruction.add(new obstruction(120,300,10, this);this.allobstruction.add(new obstruction(60,300,10, this);this.allobstruction.add(new obstru

31、ction(0,300,10, this);this.allobstruction.add(new obstruction(300,360,0, this);this.allobstruction.add(new obstruction(360,360,10, this);this.allobstruction.add(new obstruction(420,360,0, this);this.allobstruction.add(new obstruction(480,360,10, this);this.allobstruction.add(new obstruction(540,360,

32、0, this);this.allobstruction.add(new obstruction(420,180,10, this);/创建水管this.allobstruction.add(new obstruction(660,540,6, this);this.allobstruction.add(new obstruction(720,540,7, this);this.allobstruction.add(new obstruction(660,480,4, this);this.allobstruction.add(new obstruction(720,480,5, this);

33、/创建出所有的敌人,1代表三角菇,2代表食人花this.allenemy.add(new enemy(600, 480, true, 1, this);this.allenemy.add(new enemy(540, 300, true, 1, this);this.allenemy.add(new enemy(480, 300, true, 1, this);this.allenemy.add(new enemy(420, 300, true, 1, this);this.allenemy.add(new enemy(360, 300, true, 1, this);this.allenem

34、y.add(new enemy(690, 540, true, 2, 540, 420, this);/创建第二张图private void creat2map()for(int i = 0; i 15; i+)if(i != 10)this.allobstruction.add(new obstruction(i * 60,540,9, this);/创建水管this.allobstruction.add(new obstruction(60,540,6, this);this.allobstruction.add(new obstruction(120,540,7, this);this.

35、allobstruction.add(new obstruction(60,480,6, this);this.allobstruction.add(new obstruction(120,480,7, this);this.allobstruction.add(new obstruction(60,420,4, this);this.allobstruction.add(new obstruction(120,420,5, this);/创建水管this.allobstruction.add(new obstruction(240,540,6, this);this.allobstructi

36、on.add(new obstruction(300,540,7, this);this.allobstruction.add(new obstruction(240,480,6, this);this.allobstruction.add(new obstruction(300,480,7, this);this.allobstruction.add(new obstruction(240,420,6, this);this.allobstruction.add(new obstruction(300,420,7, this);this.allobstruction.add(new obst

37、ruction(240,360,4, this);this.allobstruction.add(new obstruction(300,360,5, this);/创建水管this.allobstruction.add(new obstruction(720,540,6, this);this.allobstruction.add(new obstruction(780,540,7, this);this.allobstruction.add(new obstruction(720,480,6, this);this.allobstruction.add(new obstruction(78

38、0,480,7, this);this.allobstruction.add(new obstruction(720,420,6, this);this.allobstruction.add(new obstruction(780,420,7, this);this.allobstruction.add(new obstruction(720,360,4, this);this.allobstruction.add(new obstruction(780,360,5, this);/创建砖块for(int i = 0; i 6; +i)this.allobstruction.add(new o

39、bstruction(360 + i * 60,360,3, this);/创建敌人this.allenemy.add(new enemy(90, 480, true, 2, 480, 360, this);/食人花this.allenemy.add(new enemy(270, 420, true, 2, 420, 300, this);/食人花this.allenemy.add(new enemy(750, 420, true, 2, 420, 300, this);/食人花/创建第三张图private void creat3map()for(int i = 0; i 15; i+)/创建

40、出所有地面this.allobstruction.add(new obstruction(i * 60,540,9, this);/创建砖块this.allobstruction.add(new obstruction(60, 360, 0, this);this.allobstruction.add(new obstruction(120, 360, 0, this);this.allobstruction.add(new obstruction(180, 360, 0, this);this.allobstruction.add(new obstruction(360, 360, 10,

41、this);this.allobstruction.add(new obstruction(360, 180, 3, this);this.allobstruction.add(new obstruction(420, 180, 3, this);this.allobstruction.add(new obstruction(540, 360, 10, this);this.allobstruction.add(new obstruction(540, 180, 10, this);this.allobstruction.add(new obstruction(720, 360, 10, th

42、is);/创建敌人this.allenemy.add(new enemy(840, 480, true, 3, 4, this);this.allenemy.add(new enemy(540, 480, true, 3, this);this.allenemy.add(new enemy(840, 480, true, 1, this);this.allenemy.add(new enemy(780, 480, true, 1, this);this.allenemy.add(new enemy(720, 480, true, 1, this);this.allenemy.add(new e

43、nemy(660, 360, true, 1, this);this.allenemy.add(new enemy(600, 360, true, 1, this);private void creat4map()for(int i = 0; i 15; i+)/创建出所有地面this.allobstruction.add(new obstruction(i * 60,540,9, this);/绘制旗子this.allobstruction.add(new obstruction(550, 190, 11, this);/绘制旗子下面的砖块this.allobstruction.add(ne

44、w obstruction(520,480,2, this); class enemy implements runnable private int x;/敌人的x坐标private int y;/敌人的y坐标private int startx;/敌人的初始x坐标private int starty;/敌人的初始y坐标private boolean startisleftorup;/敌人初始的移动方向private int startimagetype;/初始的显示图片private int type;/敌人的类型1代表蘑菇,2代表食人花,3代表乌龟private bufferedimag

45、e bufferedimage;private background bg;/障碍物所在的背景/敌人的显示图片,不同的type代表不同的敌人显示图片,通过显示图片的切换,达到动态的效果private int imagetype;/移动的极限范围private int upmax;private int downmax;private boolean isleftorup;thread t = new thread(this);/普通敌人的构造方法,加入显示图片参数public enemy(int x, int y, boolean isleft, int type, int imagetype

46、, background bg)this.x = x;this.y = y;this.startx = x;this.starty = y;this.isleftorup = isleft;this.startisleftorup = isleft;this.type = type;this.bg = bg;this.imagetype = imagetype;this.startimagetype = imagetype;switch(type)case 1:/蘑菇/取蘑菇的图片this.bufferedimage = staticvalue.alltriangleimage.get(0);

47、break;case 3:/乌龟this.bufferedimage = staticvalue.allturtleimage.get(0);break;t.start();/启动线程t.suspend();/抑制线程启动/普通敌人的构造方法public enemy(int x, int y, boolean isleft, int type, background bg)this.x = x;this.y = y;this.startx = x;this.starty = y;this.isleftorup = isleft;this.startisleftorup = isleft;this.type = type;this.bg = bg;switch(type)case 1:/蘑菇/取蘑菇的图片this.bufferedimage = staticvalue.alltriangleimage.get(0);break;case 3:/乌龟this.bufferedimage = staticvalue.allturtleimage.get(0);break;t.start();/启动线程t.suspend();/抑制线程启动/食

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论