溫馨提示×

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

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

java如何實(shí)現(xiàn)雷霆戰(zhàn)機(jī)

發(fā)布時(shí)間:2022-06-10 15:47:40 來(lái)源:億速云 閱讀:147 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本篇內(nèi)容主要講解“java如何實(shí)現(xiàn)雷霆戰(zhàn)機(jī)”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“java如何實(shí)現(xiàn)雷霆戰(zhàn)機(jī)”吧!

GameFame.java

package cn. tx;
 
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import java.util.Vector;
 
 class GameFrame extends JFrame {
 
    HeroPlane heroPlane;
 
    //定義子彈的集合
    Vector<Bullet> bullets = new Vector<>();
    //敵機(jī)集合
     Vector<EnemyPlane> enemys = new Vector<>();
 
     GameFrame  frame;
 
    public GameFrame () {
        frame = this;
        //創(chuàng)建英雄機(jī)
        heroPlane =new HeroPlane();
        heroPlane.start();
        //設(shè)置窗體的寬高
        this.setSize(450, 730);
        //標(biāo)題
        this.setTitle("雷霆戰(zhàn)機(jī)");
        this.setResizable(false);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        //窗口可見(jiàn)
        this.setVisible(true);
 
 
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    repaint();
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
 
        //產(chǎn)生敵機(jī)的線(xiàn)程
        new Thread(new Runnable() {
            //創(chuàng)建隨機(jī)數(shù)的對(duì)象
            Random r = new Random();
 
            @Override
            public void run() {
                while (true){
                    //啟動(dòng)敵機(jī)
                    EnemyPlane enemyPlane = new EnemyPlane(r.nextInt(500), 0, frame);
                    enemyPlane.start();
                    //添加敵機(jī)的時(shí)候,讓x軸隨機(jī)
                    enemys.add(enemyPlane);
                    try{
                        Thread.sleep(500);
                    } catch (InterruptedException e){
                        e.printStackTrace();
                    }
                }
 
            }
        }).start();
 
    }
 
 
//        *在窗口上畫(huà),內(nèi)容,paint這 個(gè)畫(huà)筆的方法在窗口初始化的時(shí)候會(huì)默認(rèn)的執(zhí)行
//        @param g
 
        public void paint (Graphics g) {
            //System.out.println("繪制畫(huà)板");
            //兩背景
            BufferedImage image = (BufferedImage) this.createImage(this.getSize().width, this.getSize().height);
            //高效緩存的畫(huà)筆
            Graphics bi = image.getGraphics();
            //畫(huà)背景
            bi.drawImage(new ImageIcon("img/MAP02_01.png").getImage(),0,0,null);
            //畫(huà)戰(zhàn)斗機(jī)
            bi.drawImage(heroPlane.img, heroPlane.x,heroPlane.y, heroPlane.width,heroPlane.heigth,null);
            //飛機(jī)發(fā)射炮彈
            for (int i = 0; i < bullets.size(); i++) {
                System.out.println(bullets);
                Bullet bullet = bullets.get(i);
                if(bullet.y > 0)
                    bi.drawImage(bullet.image, bullet.x,bullet.y -= bullet.speed, bullet.width,bullet.height, null);
                else
                    bullets.remove(bullet);
            }
            //畫(huà)敵機(jī)
            for (int i = 0; i < enemys.size(); i++) {
                System.out.println(enemys);
                EnemyPlane ep = enemys.get(i);
                if(ep.y < 730 )
                    bi.drawImage(ep.img, ep.x,ep.y += ep.speed, ep.width,ep.heigth,null);
                else
                    enemys.remove(ep);
            }
 
 
            //生效
            g.drawImage(image,0,0,null);
        }
        public static void main (String[]args){
            GameFrame frame = new GameFrame();
            Player player = new Player(frame);
            frame.addKeyListener(player);
        }
    }

HeroPlane

package cn.tx;
import javax.swing.*;
import java.awt.*;
 
public class HeroPlane extends Thread{
    //英雄機(jī)在畫(huà)板上的位置
    int x=200, y=600;
 
    int width = 50, heigth = 50;
    //飛機(jī)的速度
    int speed = 10;
 
    Image img = new ImageIcon("img/10011.png").getImage();
 
    //定義方向鍵的標(biāo)志
    boolean up,down,left,right;
 
    public HeroPlane() {
    }
 
    public HeroPlane(int x, int y, int width, int heigth, Image img) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.heigth = heigth;
    }
 
    @Override
    public void run() {
        while (true){
            if (up){
                y -= speed;
            }
            if (down){
                y += speed;
            }
            if (left){
                x -= speed;
            }
            if (right){
                x += speed;
            }
 
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Player

package cn.tx;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//定義一個(gè)玩家
public class Player extends KeyAdapter {
    GameFrame frame;
    HeroPlane heroPlane;
    public Player(GameFrame frame) {
       this.frame=frame;
    }
 
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        //38、40、37、39
        switch (keyCode){
            case 38:
                frame.heroPlane.up = true;
                break;
            case 40:
                frame. heroPlane.down = true;
                break;
            case 37:
                frame. heroPlane.left = true;
                break;
            case 39:
                frame. heroPlane.right = true;
                break;
            case 66:
                addBullut();
                break;
        }
 
    }
 
    @Override
    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        //38、40、37、39
        switch (keyCode){
            case 38:
                frame.heroPlane.up = false;
                break;
            case 40:
                frame. heroPlane.down = false;
                break;
            case 37:
                frame.  heroPlane.left = false;
                break;
            case 39:
                frame. heroPlane.right = false;
                break;
        }
    }
    public void addBullut(){
        frame.bullets.add(new Bullet( frame.heroPlane.x+5,  frame.heroPlane.y - 20));
    }
}

EnemyPlane

package cn.tx;
 
import javax.swing.*;
import java.awt.*;
 
 
public class EnemyPlane extends Thread {
    public GameFrame gf;
    //子彈的坐標(biāo),大小速度
    public int x, y;
    public int width = 50;
    public int heigth = 50;
    public int speed = 2;
    public Image img = new ImageIcon("img/10021.png").getImage();
 
    public EnemyPlane(int x, int y, GameFrame gf) {
        super();
        this.x = x;
        this.y = y;
        this.gf = gf;
    }
 
    public EnemyPlane(int x, int y, int width, int heigth, GameFrame gf) {
        super();
        this.x = x;
        this.y = y;
        this.width = width;
        this.heigth = heigth;
        this.gf = gf;
    }
 
    //瑪麗飛翔的邏輯;移動(dòng)的邏輯都在這里。
    public void run() {
        while (true) {
            //向左走
            if (hit()) {
                System.out.println("hit................");
                this.speed = 0;
                this.img = new ImageIcon("img/300350.png").getImage();
                    try {
                        this.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    gf.enemys.remove(this);
                    break;
                }
                if (this.y >= 760) {
                    break;
                }
                try {
                    this.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        }
    }
 
 
    //檢測(cè)碰撞
    public boolean hit() {
        // swing在水中,人家已經(jīng)提供了
        Rectangle myrect = new Rectangle(this.x, this.y, this.width, this.heigth);
        Rectangle rect = null;
        for (int i = 0; 1 < gf.bullets.size(); i++) {
            Bullet bullet = gf.bullets.get(i);
            System.out.println("test hit");
            rect = new Rectangle(bullet.x, bullet.y - 1, bullet.width, bullet.height);
            //碰撞檢測(cè)
            if (myrect.intersects(rect)) {
                return true;
            }
        }
        return false;
    }
 
    @Override
    public String toString() {
        return "EnemyPlane{" +
                "x=" + x +
                ", y=" + y +
                ", width=" + width +
                ", height=" + heigth +
                '}';
    }
}

Bullet

package cn.tx;
import javax.swing.*;
import java.awt.*;
 
public class Bullet {
    //在面板上的坐標(biāo)
    int x, y;
    int width= 50,height = 50;
 
    //定義飛機(jī)默認(rèn)速度
    int speed = 5;
 
    Image image = new ImageIcon("img/30022.png").getImage();
 
    public Bullet(int x, int y) {
        this.x = x;
        this.y = y;
    }
 
    public Bullet(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
}

到此,相信大家對(duì)“java如何實(shí)現(xiàn)雷霆戰(zhàn)機(jī)”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!

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

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guā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