Java小程序之山寨版超級瑪麗_第1頁
Java小程序之山寨版超級瑪麗_第2頁
Java小程序之山寨版超級瑪麗_第3頁
Java小程序之山寨版超級瑪麗_第4頁
Java小程序之山寨版超級瑪麗_第5頁
已閱讀5頁,還剩12頁未讀, 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、Java小程序之山寨版超級瑪麗一、游戲基本功能1、能夠向左向右行走(鍵盤監聽)2、能夠跳躍3、能夠發射子彈4、能夠檢測和障礙物之間的碰撞5、背景圖片的移動二、游戲運行界面三、游戲大致實現思路:1.窗體2.自己角色的添加3.背景圖片的添加4.背景圖片的移動5.人物的移動和跳躍6.磚頭、水管等等障礙物的添加7.任務和障礙物的碰撞難點分析:1.人物的多鍵控制1)給人物設定方向boolean變量:向左、向右、向上、向下2)通過鍵盤監聽來修改方向的變量值 按下某個鍵的時候,我們把相應方向改為true,釋放的時候改false2.地圖配置自定義文件讀取方式實現:文件流的使用和字符串String類的方法調用3

2、.碰撞檢測封裝一個Rectangle類的對象4.子彈添加1)先定義一個容器,這個用于封裝所有的子彈對象2)按下某個鍵的時候,創建一個子彈對象(以角色的坐標為基準初始化)3)把子彈對象添加到容器當中4)在paint方法中,遍歷容器,取出子彈對象并進行繪制5)檢測子彈如果超出了窗體邊界,則需要把當前子彈從容器當中移除掉四、程序源代碼:代碼結構圖:分了三個包、敵人類包、游戲界面類包、游戲地圖配置包com.huaxin.mario包:java view plain copy print?在CODE上查看代碼片派生到我的代碼片package com.huaxin.mario; import java.a

3、wt.Color; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.util.ArrayList; import javax.swing.ImageIcon; import javax.swing.JFrame; import com.huaxin.enery.Enery; import com.huaxin.enery.Pipe; import Util.Map; public class GameFrame extends JFrame public Mario mario; public

4、 Enery pipe,cion,brick; /背景圖片 public BackgroundImage bg ; /容器裝敵人 public ArrayList<Enery> eneryList = new ArrayList<Enery>(); /子彈容器 public ArrayList<Boom> boomList = new ArrayList<Boom>(); /子彈的速度 public int bspeed=0; /畫地圖,制定規則,是1畫磚頭,是2畫金幣,是3畫水管 public int map =null; /構造函數里面初始化

5、背景圖片和馬里奧對象 public GameFrame() throws Exception mario = new Mario(this); mario.start(); Map mp= new Map(); bg = new BackgroundImage(); /窗體重繪線程 new Thread() public void run() while(true) /重繪窗體 repaint(); /檢查子彈是否出界 checkBoom(); try Thread.sleep(10); catch (InterruptedException e) e.printStackTrace(); .

6、start(); map=mp.readMap(); /讀取地圖,并配置地圖 for (int i = 0; i < map.length; i+) for (int j = 0; j < map0.length; j+) /讀取到的是1,畫磚頭 if(mapij=1) brick = new Pipe(j*30,i*30,30,30,new ImageIcon("image/brick.png").getImage(); eneryList.add(brick); /讀到2畫金幣 if(mapij=2) cion = new Pipe(j*30,i*30,30

7、,30,new ImageIcon("image/coin_brick.png").getImage(); eneryList.add(cion); /讀到3畫水管 if(mapij=3) pipe = new Pipe(j*30,i*30,60,120,new ImageIcon("image/pipe.png").getImage(); eneryList.add(pipe); /設置背景音樂 com.huaxin.music.Util.startMusic("music/bg1.wav"); public void initFr

8、ame() /設置窗體相關屬性 this.setSize(800,450); this.setTitle("山寨版超級瑪麗"); this.setResizable(false); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(3); this.setVisible(true); /該窗體添加鍵盤監聽 KeyListener kl = new KeyListener(this); this.addKeyListener(kl); public void paint(Graphics g) /利

9、用雙緩沖畫背景圖片和馬里奧 BufferedImage bi =(BufferedImage)this.createImage(this.getSize().width,this.getSize().height); Graphics big =bi.getGraphics(); big.drawImage(bg.img, bg.x, bg.y, null); for (int i = 0; i <eneryList.size(); i+) Enery e =eneryList.get(i); big.drawImage(e.img, e.x, e.y, e.width, e.heigh

10、t,null); /畫子彈 for (int i = 0; i < boomList.size(); i+) Boom b =boomList.get(i); Color c =big.getColor(); big.setColor(Color.red); big.fillOval(b.x+=b.speed, b.y, b.width, b.width); big.setColor(c); /畫人物 big.drawImage(mario.img, mario.x, mario.y, mario.width, mario.height,null); g.drawImage(bi,0,0

11、,null); /檢查子彈是否出界,出界則從容器中移除,不移除的話,內存會泄漏 public void checkBoom() for (int i = 0; i < boomList.size(); i+) Boom b = boomList.get(i); if(b.x<0 | b.x>800) boomList.remove(i); java view plain copy print?在CODE上查看代碼片派生到我的代碼片package com.huaxin.mario; import java.awt.Image; import java.awt.Rectangle

12、; import javax.swing.ImageIcon; import com.huaxin.enery.Enery; /自己的角色類 public class Mario extends Thread public GameFrame gf; public boolean jumpFlag=true; /馬里奧的坐標 public int x=0,y=358; /馬里奧的速度 public int xspeed=5,yspeed=1; /馬里奧的寬高 public int width=30,height=32; /馬里奧的圖片 public Image img = new ImageI

13、con("image/mari1.png").getImage(); public boolean left=false,right=false,down=false,up=false; public String Dir_Up="Up",Dir_Left="Left",Dir_Right="Right",Dir_Down="Down" public Mario (GameFrame gf) this.gf=gf; this.Gravity(); public void run() while(

14、true) /向左走 if(left) /碰撞到了 if(hit(Dir_Left) this.xspeed=0; if(this.x>=0) this.x-=this.xspeed; this.img=new ImageIcon("image/mari_left.gif").getImage(); this.xspeed=5; /向右走 if(right) if(hit(Dir_Right) this.xspeed=0; /任人物向右移動 if(this.x<400) this.x+=this.xspeed; this.img=new ImageIcon(&q

15、uot;image/mari_right.gif").getImage(); if(this.x>=400) /背景向左移動 gf.bg.x-=this.xspeed; /障礙物項左移動 for (int i = 0; i <gf.eneryList.size(); i+) Enery enery = gf.eneryList.get(i); enery.x-=this.xspeed; this.img=new ImageIcon("image/mari_right.gif").getImage(); this.xspeed=5; /向上跳 if(up

16、) if(jumpFlag && !isGravity) jumpFlag=false; new Thread() public void run() jump(); jumpFlag=true; .start(); try this.sleep(20); catch (InterruptedException e) e.printStackTrace(); /向上跳的函數 public void jump() int jumpHeigh=0; for (int i = 0; i < 150; i+) gf.mario.y-=this.yspeed; jumpHeigh+

17、; if(hit(Dir_Up) break; try Thread.sleep(5); catch (InterruptedException e) e.printStackTrace(); for (int i = 0; i <jumpHeigh; i+) gf.mario.y+=this.yspeed; if(hit(Dir_Down) this.yspeed=0; try Thread.sleep(5); catch (InterruptedException e) e.printStackTrace(); this.yspeed=1;/還原速度 /檢測碰撞 public boo

18、lean hit(String dir) Rectangle myrect = new Rectangle(this.x,this.y,this.width,this.height); Rectangle rect =null; for (int i = 0; i < gf.eneryList.size(); i+) Enery enery = gf.eneryList.get(i); if(dir.equals("Left") rect = new Rectangle(enery.x+2,enery.y,enery.width,enery.height); else

19、 if(dir.equals("Right") rect = new Rectangle(enery.x-2,enery.y,enery.width,enery.height); else if(dir.equals("Up") rect = new Rectangle(enery.x,enery.y+1,enery.width,enery.height); else if(dir.equals("Dwn") rect = new Rectangle(enery.x,enery.y-2,enery.width,enery.height

20、); /碰撞檢測 if(ersects(rect) return true; return false; /檢查是否貼地 public boolean isGravity=false; public void Gravity() new Thread() public void run() while(true) try sleep(10); catch (InterruptedException e) e.printStackTrace(); if(!jumpFlag) while(true) if(!jumpFlag) break; if(hit(Dir_Down) b

21、reak; if(y>=358) isGravity=false; else isGravity=true; y+=yspeed; try sleep(10); catch (InterruptedException e) e.printStackTrace(); .start(); java view plain copy print?在CODE上查看代碼片派生到我的代碼片package com.huaxin.mario; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swi

22、ng.ImageIcon; /鍵盤按下監聽類 public class KeyListener extends KeyAdapter public GameFrame gf; public boolean jumpFlag=true; public KeyListener(GameFrame gf) this.gf=gf; /鍵盤監聽 public void keyPressed(KeyEvent e) int code = e.getKeyCode(); switch(code) /向右走 case 39: gf.mario.right=true; break; /向左走 case 37:

23、gf.mario.left=true; break; case 66: addBoom(); break; /向上跳 case 74: gf.mario.up=true; break; /添加子彈 public void addBoom() Boom b = new Boom(gf.mario.x,gf.mario.y+5,10); if(gf.mario.left) b.speed=-2; if(gf.mario.right) b.speed=2; gf.boomList.add(b); /鍵盤釋放監聽 public void keyReleased(KeyEvent e) int code

24、=e.getKeyCode(); if(code=39) gf.mario.right=false; gf.mario.img=new ImageIcon("image/mari1.png").getImage(); if(code=37) gf.mario.left=false; gf.mario.img=new ImageIcon("image/mari_left1.png").getImage(); if(code=74) gf.mario.up=false; java view plain copy print?在CODE上查看代碼片派生到我的代

25、碼片package com.huaxin.mario; import java.awt.Image; import javax.swing.ImageIcon; public class BackgroundImage public int x=0,y=0; public int ox=0,oy=0; public Image img=new ImageIcon("image/startBack.jpg").getImage(); java view plain copy print?在CODE上查看代碼片派生到我的代碼片package com.huaxin.mario;

26、/子彈類 public class Boom /子彈的坐標,大小,速度 int x,y; int width; int speed=1; public Boom(int x, int y, int width) super(); this.x = x; this.y = y; this.width = width; 主函數類,作為整個程序的入口java view plain copy print?在CODE上查看代碼片派生到我的代碼片package com.huaxin.mario; public class Test /主函數,程序入口 public static void main(Str

27、ing args) throws Exception GameFrame gf = new GameFrame(); gf.initFrame(); com.huaxin.enery包:java view plain copy print?在CODE上查看代碼片派生到我的代碼片<span style="color:#333333;">package com.huaxin.enery; import java.awt.Image; /障礙物的抽象父類 public abstract class Enery public int x,y; public int wi

28、dth,height; public Image img; public Enery(int x, int y, int width, int height,Image img) this.x = x; this.y = y; this.width = width; this.height = height; this.img=img; </span><span style="color:#ff0000;"> </span> java view plain copy print?在CODE上查看代碼片派生到我的代碼片package com

29、.huaxin.enery; import java.awt.Image; /金幣類 public class Coin extends Enery public Coin(int x, int y, int width, int height, Image img) super(x, y, width, height, img); java view plain copy print?在CODE上查看代碼片派生到我的代碼片package com.huaxin.enery; import java.awt.Image; /磚頭類 public class Brick extends Enery

30、 public Brick(int x, int y, int width, int height, Image img) super(x, y, h, height, img); java view plain copy print?在CODE上查看代碼片派生到我的代碼片package com.huaxin.enery; import java.awt.Image; /水管類 public class Pipe extends Enery public Pipe(int x, int y, int width, int height, Image img) super(x, y, width

31、, height, img); com.huaxin.util包:java view plain copy print?在CODE上查看代碼片派生到我的代碼片package Util; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStreamReader; import java.util.ArrayList; /地圖配置類 public class Map /數據容器 public ArrayLi

32、st<String> list = new ArrayList<String>(); public int map=null; public int readMap() throws Exception / 構造文件輸入流 FileInputStream fis = new FileInputStream("map.txt"); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); /直接讀取一行數據 String value =br.readLine(); while(value!=null) /將讀取到的一行數據加入到容器中 list.add(

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論