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

下载本文档

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

文档简介

面向对象程序设计课程设计报告题 目: 超级玛丽软件的设计与实现 院 (系): 信息科学与工程学院 专业班级: 计算机科学与技术1201班 学生姓名: 程伟 学 号: 20121183011 指导教师: 吴奕 20 14 年 12 月 29 日至20 15 年 1 月 9 日华中科技大学武昌分校制 面向对象程序设计 课程设计任务书一、设计(调查报告/论文)题目超级玛丽软件的设计与实现二、设计(调查报告/论文)主要内容内容:开发超级玛丽能够在场景中完成各种动作,并且有场景的切换功能。设计一个Mario类、一个场景类、以及敌人类、障碍物类。实现有一定的挑战的休闲单机小游戏。 基本功能与要求:设计一个超级玛丽游戏实现以下功能:1、 点击“空格”可以使游戏人物进行跳跃。2、 点击方向键左右可以使游戏人物前进或者后退。3、 设计障碍物,增加游戏的难度。4、 设计敌人类,增加游戏难度。5、 设计跳跃踩死敌人或者跳跃越过障碍物,保证可以正常前进。6、 保证通过所有障碍后可以通关。三、原始资料1.面向对象程序设计课程设计指导书2. 耿祥义JAVA大学实用教程北京:清华大学出版社,2009四、要求的设计(调查/论文)成果1.课程设计报告2.课程设计软件五、进程安排布置任务,查找资料、需求分析1天总体设计1天详细设计1.5天编制源程序实现3.5天测试与修改1天撰写课设报告2天六、主要参考资料1 张广彬. 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 Mario对象的序列图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类。一个场景类,以及敌人类,障碍物类。让超级玛丽能够在场景中完成各种动作。并且有场景的切换等等功能。游戏中设计了一些很有意思的陷阱,玩家一步小心掉入陷阱,超级玛丽就会死亡。这样会给人意想不到的惊奇,达到娱乐大众,增加游戏难度的效果。游戏的设计匠心独具,并且都经过了各种测试,除了为玩家增加一些意想不到的游戏陷阱以外,还必须保证玩家能够通关。1.2程序功能图如图1所示:图11.3程序类图系统的整体类图的框架如图2所示。首先是窗体类。他需要调用到Mario和BackGround类中的方法。以便动态的显示Mario类和BackGround。而BackGround里面又包含了Enemy和Obstruction。Enemy,Obstruction和Mario类又调用静态类StaticValue的方法,用以显示真实的图片。而StaticValue则是一个静态类,将存储在硬盘的图片资源加载进入内存以便其他模块调用。图22详细设计2.1 MyFrame实现2.1.1窗体对象的序列图窗体对象继承了JFrame类并且实现了Runnable接口。作为窗体对象,它首先显示窗体,绘制背景。然后启动线程。在run方法里面动态得绘制窗体中需要显示的Mario的图片,障碍物的图片,敌人的图片。该窗体的run方法是一个死循环,每次执行一个 循环调用sleep方法睡眠50毫秒。序列图如图3所示。图32.1.2 Myframe详细类图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方法修改Mario的显示图片。再根据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 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坐标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 BufferedImage 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;/标志是否降旗结束详细类图如下: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.JOptionPane;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 = 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.getDefaultToolkit().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, 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);/绘制背景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(image, 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.getKeyCode()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 keyTyped(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.sleep();/进入下一张图片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.getAllEnemy().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.drawImage(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.setDead(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 = new 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 void 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 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)bgImage = 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.allEnemy.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).starMove();/创建第一张图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 Obstruction(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,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);/创建出所有的敌人,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.allEnemy.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.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.allObstruction.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 Obstruction(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(780,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 Obstruction(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+)/创建出所有地面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, 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, this);/创建敌人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 Enemy(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(new 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 BufferedImage 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, 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);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

提交评论