您好,登錄后才能下訂單哦!
這篇文章主要介紹java如何實現彈幕小游戲,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
具體內容如下
父類
import java.awt.*; public class GameObject { //游戲物體的父類 Image img; double x,y; int speed = 3; int width,height; public void drawSelf(Graphics g){ g.drawImage(img,(int)x,(int)y,null); } public GameObject(Image img, double x, double y, int X_speed,int Y_speed, int width, int height) { this.img = img; this.x = x; this.y = y; this.speed = speed; this.width = width; this.height = height; } public GameObject(Image img, double x, double y) { this.img = img; this.x = x; this.y = y; } public GameObject(){ } //返回物體所在的矩形,便于后續的碰撞檢測 public Rectangle getRect(){ return new Rectangle((int)x,(int)y,width,height); } }
彈幕類
import com.sun.xml.internal.ws.model.wsdl.WSDLPortProperties; import java.awt.*; public class Shell extends GameObject { double degree; public Shell() { x = 200; y = 200; width = 10; height = 10; speed = 2; speed = 2; //弧度 degree = Math.random()*Math.PI*2; } public void draw(Graphics g){ Color c = g.getColor(); g.setColor(Color.YELLOW); g.fillOval((int)x,(int)y,width,height); //炮彈沿著任意角度去飛 x+=speed*Math.cos(degree); y+=speed*Math.sin(degree); if(x<0 || x> Constant.GAME_WIDTH-width){ degree = Math.PI - degree; } if(y<40 || y> Constant.GAME_HEIGHT-height){ degree = -degree; } g.setColor(c); } }
飛機類
import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; public class Plane extends GameObject{ boolean left,right,up,down; boolean live = true; //飛機是否活著 boolean leftflag,rightflag,upflag,downflag;//這些布爾值判斷當碰到上下左右的邊框時的狀態 //如果活著畫出來 public void drawSelf(Graphics g){ if(live) { g.drawImage(img, (int) x, (int) y, null); //根據方向進行不同的移動 if (left) { x -= speed; } if (right) { x += speed; } if (up) { y -= speed; } if (down) { y += speed; } } } public Plane(Image img,double x,double y) { this.img = img; this.x = x; this.y = y; this.speed = 3; this.width = img.getWidth(null); this.height = img.getHeight(null); } //按下某個鍵,增加相應的方向 public void addDirection(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_LEFT: left = true; break; case KeyEvent.VK_UP: up = true; break; case KeyEvent.VK_RIGHT: right = true; break; case KeyEvent.VK_DOWN: down = true; break; } } //按下某個鍵,取消相應的方向 public void minusDirection(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_LEFT: left = false; break; case KeyEvent.VK_UP: up = false; break; case KeyEvent.VK_RIGHT: right = false; break; case KeyEvent.VK_DOWN: down = false; break; } } }
爆炸類
import java.awt.*; //爆炸類 public class Explode { double x,y; //爆炸的位置 static Image[] imgs = new Image[16]; static { for(int i=0;i<16;i++){ imgs[i] = PlayGameFrame.GameUtil.getImage("image/explode/e"+(i+1)+".gif"); imgs[i].getWidth(null); } } int count; public void draw(Graphics g){ if(count <= 15){ g.drawImage(imgs[count],(int)x,(int)y,null); count++; } } public Explode(double x,double y){ this.x = x; this.y = y; } }
常量類
public class Constant { public static final int GAME_WIDTH = 500; public static final int GAME_HEIGHT = 500; }
主類
import jdk.internal.cmm.SystemResourcePressureImpl; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; import java.awt.Rectangle; import java.util.Date; /** *飛機游戲的主窗口 */ class PlayGameFrame extends Frame { Image planeImg= GameUtil.getImage("image/plane.png"); Image nightsky = GameUtil.getImage("image/nightsky.jpg"); //定義飛機的坐標 Plane plane = new Plane(planeImg,250,250); //定義炮彈 Shell[] shells = new Shell[50]; Explode boom; Date startTime = new Date(); Date endTime; int period;//游戲持續的時間 /** *畫圖 */ @Override public void paint(Graphics g) { //自動被調用,g這個變量相當于一個畫筆 Color c = g.getColor(); super.paint(g);//防止黑屏 g.drawImage(nightsky,0,0,null); plane.drawSelf(g);//畫飛機 if(plane.x <= Constant.GAME_WIDTH-plane.width && plane.x >= 30 && plane.y <= Constant.GAME_HEIGHT-30 && plane.y >= 0) plane.speed = 10; else plane.speed = -plane.speed; //畫出所有的炮彈 for(int i=0;i<shells.length;i++) { shells[i].draw(g); //檢測炮彈是否和飛機相撞 boolean peng = shells[i].getRect().intersects(plane.getRect()); if(peng){ plane.live = false; if(boom == null) { //如果不加這個if,飛機在碰到炮彈的時候會不停地生成爆炸類 boom = new Explode(plane.x, plane.y); endTime = new Date(); period = (int)((endTime.getTime() - startTime.getTime())/1000); } boom.draw(g); } //計時功能 if(!plane.live) { g.setColor(Color.red); Font f = new Font("宋體",Font.BOLD,50); g.setFont(f); g.drawString("時間:" + period + "秒", (int) plane.x, (int) plane.y); } } g.setColor(c); /*Color c=g.getColor(); //保存先顏色,等使用完之后要復原為最初的顏色 g.setColor(Color.BLUE); g.drawLine(100,100,300,300);//直線 g.drawRect(100 ,100 ,300 ,300);//矩形 g.drawOval(100,100,300,300);//橢圓 g.fillRect(100,100,40,40);//填充矩形 g.drawString("Ning",100 ,200);//寫字符串 g.drawImage(ball,250,250,null); g.setColor(c); 隨堂練習用,建立圖片!! */ } //多線程,幫助我們反復的重畫窗口 class PaintThread extends Thread{ @Override public void run() { while(true){ repaint(); //重畫窗口 try { Thread.sleep(40);//重畫次數 } catch (InterruptedException e) { e.printStackTrace(); } } } } //定義鍵盤監聽的內部類 class KeyMonitor extends KeyAdapter{ @Override public void keyPressed(KeyEvent e) { plane.addDirection(e); } @Override public void keyReleased(KeyEvent e) { plane.minusDirection(e); } } /** *初始化窗口 */ public void launchFrame() { this.setTitle("拉 拉 人");/*給自己的窗口起個標題*/ this.setVisible(true);/*默認窗口可見*/ this.setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);/*給窗口設定大小*/ this.setLocation(300,300);/*定義窗口的位置*/ /** *關閉動作 */ this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0);/*表示正常結束*/ } }); //啟動重畫窗口的線程 new PaintThread().start(); //給窗口增加鍵盤的監聽 addKeyListener(new KeyMonitor()); //初始化50個炮彈 for(int i=0;i<shells.length;i++){ shells[i] = new Shell(); } } public static class GameUtil { // 工具類最好將構造器私有化。 private GameUtil() { } public static Image getImage(String path) { BufferedImage bi = null; try { URL u = GameUtil.class.getClassLoader().getResource(path); bi = ImageIO.read(u); } catch (IOException e) { e.printStackTrace(); } return bi; } } public static void main(String[] args){ PlayGameFrame f = new PlayGameFrame(); f.launchFrame(); } //添加雙緩沖技術 private Image offScreenImage = null; public void update(Graphics g) { if (offScreenImage == null) offScreenImage = this.createImage(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);//這是游戲窗口的寬度和高度 Graphics gOff = offScreenImage.getGraphics(); paint(gOff); g.drawImage(offScreenImage, 0, 0, null); } }
以上是“java如何實現彈幕小游戲”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。