溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java如何實(shí)現(xiàn)彈幕小游戲

發(fā)布時(shí)間:2021-08-04 09:16:47 來(lái)源:億速云 閱讀:207 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹java如何實(shí)現(xiàn)彈幕小游戲,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

具體內(nèi)容如下

父類

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(){ }

 //返回物體所在的矩形,便于后續(xù)的碰撞檢測(cè)
 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);
 }
}

飛機(jī)類

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; //飛機(jī)是否活著
 boolean leftflag,rightflag,upflag,downflag;//這些布爾值判斷當(dāng)碰到上下左右的邊框時(shí)的狀態(tài)
 //如果活著畫出來(lái)
 public void drawSelf(Graphics g){
 if(live) {
 g.drawImage(img, (int) x, (int) y, null);

 //根據(jù)方向進(jìn)行不同的移動(dòng)
 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);
 }

 //按下某個(gè)鍵,增加相應(yīng)的方向
 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;

 }
 }

 //按下某個(gè)鍵,取消相應(yīng)的方向
 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;

/**
 *飛機(jī)游戲的主窗口
 */
class PlayGameFrame extends Frame {
 Image planeImg= GameUtil.getImage("image/plane.png");
 Image nightsky = GameUtil.getImage("image/nightsky.jpg");

 //定義飛機(jī)的坐標(biāo)
 Plane plane = new Plane(planeImg,250,250);
 //定義炮彈
 Shell[] shells = new Shell[50];

 Explode boom;

 Date startTime = new Date();
 Date endTime;

 int period;//游戲持續(xù)的時(shí)間
 /**
 *畫圖
 */
 @Override
 public void paint(Graphics g) { //自動(dòng)被調(diào)用,g這個(gè)變量相當(dāng)于一個(gè)畫筆
 Color c = g.getColor();
 super.paint(g);//防止黑屏
 g.drawImage(nightsky,0,0,null);
 plane.drawSelf(g);//畫飛機(jī)

 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);

 //檢測(cè)炮彈是否和飛機(jī)相撞
 boolean peng = shells[i].getRect().intersects(plane.getRect());
 if(peng){
 plane.live = false;
 if(boom == null) { //如果不加這個(gè)if,飛機(jī)在碰到炮彈的時(shí)候會(huì)不停地生成爆炸類
 boom = new Explode(plane.x, plane.y);

 endTime = new Date();
 period = (int)((endTime.getTime() - startTime.getTime())/1000);
 }
 boom.draw(g);
 }

 //計(jì)時(shí)功能
 if(!plane.live) {
 g.setColor(Color.red);
 Font f = new Font("宋體",Font.BOLD,50);
 g.setFont(f);
 g.drawString("時(shí)間:" + period + "秒", (int) plane.x, (int) plane.y);
 }
 }
 g.setColor(c);
 /*Color c=g.getColor(); //保存先顏色,等使用完之后要復(fù)原為最初的顏色
 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);
 隨堂練習(xí)用,建立圖片?。?
 */
 }

 //多線程,幫助我們反復(fù)的重畫窗口
 class PaintThread extends Thread{
 @Override
 public void run() {
 while(true){
 repaint(); //重畫窗口
 try {
 Thread.sleep(40);//重畫次數(shù)
 } catch (InterruptedException e) {
 e.printStackTrace();
 }

 }
 }
 }

 //定義鍵盤監(jiān)聽的內(nèi)部類
 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("拉 拉 人");/*給自己的窗口起個(gè)標(biāo)題*/
 this.setVisible(true);/*默認(rèn)窗口可見*/
 this.setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);/*給窗口設(shè)定大小*/
 this.setLocation(300,300);/*定義窗口的位置*/

 /**
 *關(guān)閉動(dòng)作
 */
 this.addWindowListener(new WindowAdapter() {
 @Override
 public void windowClosing(WindowEvent e) {
 System.exit(0);/*表示正常結(jié)束*/
 }
 });

 //啟動(dòng)重畫窗口的線程
 new PaintThread().start();
 //給窗口增加鍵盤的監(jiān)聽
 addKeyListener(new KeyMonitor());

 //初始化50個(gè)炮彈
 for(int i=0;i<shells.length;i++){
 shells[i] = new Shell();
 }
 }

 public static class GameUtil {
 // 工具類最好將構(gòu)造器私有化。
 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();
 }

 //添加雙緩沖技術(shù)
 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如何實(shí)現(xiàn)彈幕小游戲”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI