超級瑪麗Java課程設(shè)計_第1頁
超級瑪麗Java課程設(shè)計_第2頁
超級瑪麗Java課程設(shè)計_第3頁
超級瑪麗Java課程設(shè)計_第4頁
超級瑪麗Java課程設(shè)計_第5頁
已閱讀5頁,還剩62頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、 面向?qū)ο蟪绦蛟O(shè)計課程設(shè)計報告題 目: 超級瑪麗軟件的設(shè)計與實現(xiàn) 院 (系): 信息科學(xué)與工程學(xué)院 專業(yè)班級: 計算機科學(xué)與技術(shù)1201班 學(xué)生姓名: 程偉 學(xué) 號: 20121183011 指導(dǎo)教師: 吳奕 20 14 年 12 月 29 日至20 15 年 1 月 9 日華中科技大學(xué)武昌分校制 面向?qū)ο蟪绦蛟O(shè)計 課程設(shè)計任務(wù)書一、設(shè)計(調(diào)查報告/論文)題目超級瑪麗軟件的設(shè)計與實現(xiàn)二、設(shè)計(調(diào)查報告/論文)主要內(nèi)容內(nèi)容:開發(fā)超級瑪麗能夠在場景中完成各種動作,并且有場景的切換功能。設(shè)計一個Mario類、一個場景類、以及敵人類、障礙物類。實現(xiàn)有一定的挑戰(zhàn)的休閑單機小游戲。 基本功能與要求:設(shè)計一

2、個超級瑪麗游戲?qū)崿F(xiàn)以下功能:1、 點擊“空格”可以使游戲人物進行跳躍。2、 點擊方向鍵左右可以使游戲人物前進或者后退。3、 設(shè)計障礙物,增加游戲的難度。4、 設(shè)計敵人類,增加游戲難度。5、 設(shè)計跳躍踩死敵人或者跳躍越過障礙物,保證可以正常前進。6、 保證通過所有障礙后可以通關(guān)。三、原始資料1.面向?qū)ο蟪绦蛟O(shè)計課程設(shè)計指導(dǎo)書2. 耿祥義JAVA大學(xué)實用教程北京:清華大學(xué)出版社,2009四、要求的設(shè)計(調(diào)查/論文)成果1.課程設(shè)計報告2.課程設(shè)計軟件五、進程安排布置任務(wù),查找資料、需求分析1天總體設(shè)計1天詳細設(shè)計1.5天編制源程序?qū)崿F(xiàn)3.5天測試與修改1天撰寫課設(shè)報告2天六、主要參考資料1 張廣彬

3、. Java課程設(shè)計案例精編(第二版).北京:清華大學(xué)出版社, 2011. 2 耿祥義. Java課程設(shè)計(第二版).北京:清華大學(xué)出版社, 2008.3 耿祥義JAVA大學(xué)實用教程北京:清華大學(xué)出版社,20094 鄒林達陳國君 Java2程序設(shè)計基礎(chǔ)北京:清華大學(xué)出版社,20105 鄒林達Java2程序設(shè)計基礎(chǔ)實驗指導(dǎo)北京:清華大學(xué),2010指導(dǎo)教師(簽名): 20 年 月 日目錄1需求與總體設(shè)計51.1需求分析51.2程序功能圖51.3程序類圖62詳細設(shè)計72.1 MyFrame實現(xiàn)7窗體對象的序列圖72.1.2 Myframe詳細類圖82.2 Mario實現(xiàn)82.2.1 Mario對象的

4、序列圖82.2.2 Mario類圖92.3 Enemy類的實現(xiàn)102.4 Obstruction類的實現(xiàn)112.5 BackGround類實現(xiàn)113編碼實現(xiàn)123.1 MyFrame類的詳細代碼123.2 BackGround類的詳細代碼133.3 Mario類的詳細代碼163.4 Enemy類的詳細代碼243.5 Obstruction類的詳細代碼314系統(tǒng)測試334.1 游戲開始界面的測試334.2 游戲運行的測試334.3 排除錯誤的處理36總 結(jié)37 1需求與總體設(shè)計1.1需求分析提供一個友好的用戶交互界面,簡單明了容易操作。并且游戲有一定的難度和娛樂性。開發(fā)一個Mario類。一個場景

5、類,以及敵人類,障礙物類。讓超級瑪麗能夠在場景中完成各種動作。并且有場景的切換等等功能。游戲中設(shè)計了一些很有意思的陷阱,玩家一步小心掉入陷阱,超級瑪麗就會死亡。這樣會給人意想不到的驚奇,達到娛樂大眾,增加游戲難度的效果。游戲的設(shè)計匠心獨具,并且都經(jīng)過了各種測試,除了為玩家增加一些意想不到的游戲陷阱以外,還必須保證玩家能夠通關(guān)。1.2程序功能圖如圖1所示:圖11.3程序類圖系統(tǒng)的整體類圖的框架如圖2所示。首先是窗體類。他需要調(diào)用到Mario和BackGround類中的方法。以便動態(tài)的顯示Mario類和BackGround。而BackGround里面又包含了Enemy和Obstruction。En

6、emy,Obstruction和Mario類又調(diào)用靜態(tài)類StaticValue的方法,用以顯示真實的圖片。而StaticValue則是一個靜態(tài)類,將存儲在硬盤的圖片資源加載進入內(nèi)存以便其他模塊調(diào)用。圖22詳細設(shè)計2.1 MyFrame實現(xiàn)窗體對象的序列圖窗體對象繼承了JFrame類并且實現(xiàn)了Runnable接口。作為窗體對象,它首先顯示窗體,繪制背景。然后啟動線程。在run方法里面動態(tài)得繪制窗體中需要顯示的Mario的圖片,障礙物的圖片,敵人的圖片。該窗體的run方法是一個死循環(huán),每次執(zhí)行一個 循環(huán)調(diào)用sleep方法睡眠50毫秒。序列圖如圖3所示。圖32.1.2 Myframe詳細類圖priv

7、ate Mario mario;/Mario對象private boolean isStart = false;/標(biāo)識游戲是否已經(jīng)開始this.paintAllEnemys(g2);/繪制所有的敵人this.paintAllObstruction(g2);/繪制所有障礙物2.2 Mario實現(xiàn)2.2.1 Mario對象的序列圖因為需要有一個專門的線程來實時監(jiān)測Mario的移動狀況,和障礙物和敵人的碰撞情況。所以讓Mario類繼承了Runnable接口。Mario先啟動線程。該線程的run方法也是一個死循環(huán)。首先根據(jù)Mario的狀態(tài)changeMarioImage方法修改Mario的顯示圖片。再

8、根據(jù)Mario的determinexy方法決定Mario的移動。然后通過deterMarioWithObstruction方法檢測Mario和障礙物之間的碰撞,在根據(jù)marioTouchEnemys方法檢測Mario和敵人之間的碰撞。然后調(diào)用sleep()方法讓線程休息5毫秒。如此結(jié)束一個循環(huán)。序列圖如圖4所示。圖42.2.2 Mario類圖類圖中:private int x;/Mario的x坐標(biāo)private int y;/Mario的y坐標(biāo)private int life;/Mario的生命BackGround bg = null;/Mario所在的背景private int uptime

9、;/Mario的上升時間String status;/Mario的移動狀態(tài)int score;/Mario的分?jǐn)?shù)Thread t;/Mario的線程int xmove;/Mario的x移動距離int ymove;/Mario的y移動距離private boolean isClear = false;/標(biāo)識用戶是否通關(guān)。isClear為true時代表用戶通關(guān)了2.3 Enemy類的實現(xiàn)private int x;/敵人的x坐標(biāo)private int y;/敵人的y坐標(biāo)private int startX;/敵人的初始x坐標(biāo)private int startY;/敵人的初始y坐標(biāo)private b

10、oolean startIsLeftOrUp;/敵人初始的移動方向private int startImageType;/初始的顯示圖片private int type;/敵人的類型1代表蘑菇,2代表食人花,3代表烏龜private BufferedImage bufferedImage;private BackGround bg;/障礙物所在的背景private int imageType;/移動的極限范圍詳細類圖如下:2.4 Obstruction類的實現(xiàn)private int x;/障礙物的x坐標(biāo)private int y;/障礙物的y坐標(biāo)private BufferedImage sh

11、owImage = null;/障礙物的顯示圖片private int type;/障礙物的類型private int startType;/障礙物的初始類型private BackGround bg;/標(biāo)識該障礙物在哪一個場景中詳細類圖如下:2.5 BackGround類實現(xiàn)private int sort;/背景的序列號private boolean flag;/標(biāo)識是否是最后一張圖片,flag為true時代表到了最后一張圖片private boolean isOver = false;/標(biāo)識游戲是否已經(jīng)結(jié)束private boolean isDown = false;/標(biāo)志是否降旗結(jié)束詳

12、細類圖如下:3編碼實現(xiàn)源代碼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;impo

13、rt 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();/當(dāng)前背景private BackGround nowBg = null;private Mario mario;/Mario對象private boolean isStart = false;/標(biāo)識游

14、戲是否已經(jīng)開始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()

15、.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

16、.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.dra

17、wString(生命: + this.mario.getLife(), 800, 60);/顯示生命數(shù)g2.drawString(分?jǐn)?shù): + this.mario.getScore(), 60, 60);/顯示生命數(shù)this.paintAllEnemys(g2);/繪制所有的敵人this.paintAllObstruction(g2);/繪制所有障礙物elseGraphics g2 = image.getGraphics();g2.drawImage(StaticValue.startImage, 0, 0, this);/繪制游戲開始的圖片g.drawImage(image, 0, 0, t

18、his);/* */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()t

19、his.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(KeyEve

20、nt 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, 恭喜您通關(guān)了,敬請期待新的關(guān)卡);System.exit(0);if(this.mario.isDead()this.gameOver();/游戲結(jié)束this.sleep();/進入下一

21、張圖片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().

22、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.

23、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();/游戲結(jié)束private void gameOver()JOptionPane.showMessageDialog(this, 馬里奧死亡);this.mario.setDead(false);

24、System.exit(0);class BackGroundprivate BufferedImage bgImage = null;private int sort;/背景的序列號private boolean flag;/標(biāo)識是否是最后一張圖片,flag為true時代表到了最后一張圖片private List allEnemy = new ArrayList();/所有敵人的集合/所有障礙物的集合private List allObstruction = new ArrayList();/所有被移除的敵人的集合private List removeEnemy = new ArrayLis

25、t();/所有被移除的障礙物的集合private List removeObstruction = new ArrayList();private boolean isOver = false;/標(biāo)識游戲是否已經(jīng)結(jié)束private boolean isDown = false;/標(biāo)志是否降旗結(jié)束public boolean isDown() return isDown;public void setDown(boolean isDown) this.isDown = isDown;public boolean isOver() return isOver;public void setOver

26、(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 allObstru

27、ction;public List getRemoveObstruction() return removeObstruction;public List getAllEnemy() return allEnemy;public List getRemoveEnemy() return removeEnemy;/* * * param sort背景的序列號 * param flag標(biāo)識是否是最后一張背景 */public BackGround(int sort, boolean flag)this.sort = sort;this.flag = flag;if(flag)bgImage = S

28、taticValue.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

29、).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();/創(chuàng)建

30、第一張圖private void creat1Map()for(int i = 0; i 15; i+)/創(chuàng)建出所有地面this.allObstruction.add(new Obstruction(i * 60,540,9, this);/創(chuàng)建出所有磚塊,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,30

31、0,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);t

32、his.allObstruction.add(new Obstruction(420,180,10, this);/創(chuàng)建水管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);/創(chuàng)建出所有的敵人,

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.allEnemy.add(new

34、Enemy(690, 540, true, 2, 540, 420, this);/創(chuàng)建第二張圖private void creat2Map()for(int i = 0; i 15; i+)if(i != 10)this.allObstruction.add(new Obstruction(i * 60,540,9, this);/創(chuàng)建水管this.allObstruction.add(new Obstruction(60,540,6, this);this.allObstruction.add(new Obstruction(120,540,7, this);this.allObstruc

35、tion.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);/創(chuàng)建水管this.allObstruction.add(new Obstruction(240,540,6, this);this.allObstruction.add(new

36、 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(24

37、0,360,4, this);this.allObstruction.add(new Obstruction(300,360,5, this);/創(chuàng)建水管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, t

38、his);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);/創(chuàng)建磚塊for(int i = 0; i 6; +i)this.allObstruction.add(new Obstruction

39、(360 + i * 60,360,3, this);/創(chuàng)建敵人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);/食人花/創(chuàng)建第三張圖private void creat3Map()for(int i = 0; i 15; i+)/創(chuàng)建出所有地面this.

40、allObstruction.add(new Obstruction(i * 60,540,9, this);/創(chuàng)建磚塊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

41、.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);/創(chuàng)建敵人t

42、his.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,

43、360, true, 1, this);this.allEnemy.add(new Enemy(600, 360, true, 1, this);private void creat4Map()for(int i = 0; i 15; i+)/創(chuàng)建出所有地面this.allObstruction.add(new Obstruction(i * 60,540,9, this);/繪制旗子this.allObstruction.add(new Obstruction(550, 190, 11, this);/繪制旗子下面的磚塊this.allObstruction.add(new Obstruct

44、ion(520,480,2, this); class Enemy implements Runnable private int x;/敵人的x坐標(biāo)private int y;/敵人的y坐標(biāo)private int startX;/敵人的初始x坐標(biāo)private int startY;/敵人的初始y坐標(biāo)private boolean startIsLeftOrUp;/敵人初始的移動方向private int startImageType;/初始的顯示圖片private int type;/敵人的類型1代表蘑菇,2代表食人花,3代表烏龜private BufferedImage buffered

45、Image;private BackGround bg;/障礙物所在的背景/敵人的顯示圖片,不同的type代表不同的敵人顯示圖片,通過顯示圖片的切換,達到動態(tài)的效果private int imageType;/移動的極限范圍private int upMax;private int downMax;private boolean isLeftOrUP;Thread t = new Thread(this);/普通敵人的構(gòu)造方法,加入顯示圖片參數(shù)public Enemy(int x, int y, boolean isLeft, int type, int imageType, BackGrou

46、nd 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

47、 3:/烏龜this.bufferedImage = StaticValue.allTurtleImage.get(0);break;t.start();/啟動線程t.suspend();/抑制線程啟動/普通敵人的構(gòu)造方法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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論