【Java】飞机大战游戏运行原理代码

运行效果:

源代码: 

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Iterator;
import java.util.Vector;

class Base
{
    int x, y; //坐标
    int vx, vy; //移动速度
    String name;
    int type;
    Color color;

    Base(int x, int y, int vx, int vy, String name, int type,
         Color color)
    {
        this.x = x;
        this.y = y;
        this.vx = vx;
        this.vy = vy;
        this.name = name;
        this.type = type;
        this.color = color;
    }
}

class Plane extends Base
{
    int fireType;
    int fireInterval;
    Plane(int x, int y, int vx, int vy, String name, int type, int fireType, int fireInterval, Color color) {
        super(x, y, vx, vy, name, type, color);
        this.fireType = fireType;
        this.fireInterval = fireInterval;
    }

    void fire(Vector<Bullet> bullets)
    {
        switch (fireType)
        {
            case 1:
                bullets.add(new Bullet(x, y, -3, -12, "bullet" + "1", type, color));
                bullets.add(new Bullet(x, y, 0, -12, "bullet" + "2", type, color));
                bullets.add(new Bullet(x, y, 3, -12, "bullet" + "3", type, color));
                break;

            case 2:
                bullets.add(new Bullet(x - 80, y, 0, -12, name + "1", type, color));
                bullets.add(new Bullet(x, y, 0, -12, name + "2", type, color));
                bullets.add(new Bullet(x + 80, y, 0, -12, name + "3", type, color));
                break;

            case 3:
                bullets.add(new Bullet(x, y, 0, 2 * vy, name + "1", type, color));
                break;
        }
    }

    void moveRight() {
        this.x += vx;
    }

    void moveLeft() {
        this.x -= vx;
    }

    void moveUp() {
        this.y -= vy;
    }

    void moveDown() {
        this.y += vy;
    }
}

class Bullet extends Base
{
    Bullet(int x, int y, int vx, int vy, String name, int type, Color color)
    {
        super(x, y, vx, vy, name, type, color);
    }

    void move() {
        this.x += vx;
        this.y += vy;
    }
}

public  class GameFrame extends JFrame {
    static Graphics graphics;
    private final Image buffer;  // 双缓冲图像
    private final Graphics bufferGraphics;  // 双缓冲图像的绘图上下文
    static Vector<Plane> friendPlanes;
    static Vector<Plane> enemyPlanes;
    static int enemyAddInterval;
    static int enemyCount;
    static Vector<Bullet> friendBullets;
    static Vector<Bullet> enemyBullets;
    int timer;

    static {
        friendPlanes = new Vector<>();
        enemyPlanes = new Vector<>();
        friendBullets = new Vector<>();
        enemyBullets = new Vector<>();
        enemyAddInterval = 18;
    }

    GameFrame() {
        setLayout(null);
        setSize(700, 900);
        setLocationRelativeTo(null);
        setVisible(true);
        graphics = getContentPane().getGraphics();
        buffer = createImage(700, 900);
        bufferGraphics = buffer.getGraphics();
        bufferGraphics.setFont(new Font("", Font.BOLD, 18));
        friendPlanes.add(new Plane(150, 800, 10, 10, "friend1", 1, 1, 7, new Color(0,0,255)));
        friendPlanes.add(new Plane(450, 800, 10, 10, "friend2", 1, 2, 5, new Color(0,255,0)));

        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                Plane player2 = friendPlanes.get(1);
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_UP: player2.moveUp(); break;

                    case KeyEvent.VK_DOWN:  player2.moveDown();  break;

                    case KeyEvent.VK_RIGHT:  player2.moveRight();  break;

                    case KeyEvent.VK_LEFT:  player2.moveLeft();  break;
                }
            }
        });
        addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Point mousePoint = e.getPoint();
                Plane player1 = friendPlanes.get(0);
                if (mousePoint.x > player1.x)
                    player1.moveRight();
                else
                    player1.moveLeft();

                if (mousePoint.y > player1.y)
                    player1.moveDown();
                else
                    player1.moveUp();
            }

            @Override
            public void mousePressed(MouseEvent e)  {}

            @Override
            public void mouseReleased(MouseEvent e)  {}

            @Override
            public void mouseEntered(MouseEvent e)  {}

            @Override
            public void mouseExited(MouseEvent e) {}
        });
    }

    void bulletsOperation(Vector<Bullet> bullets)
    {
        Iterator<Bullet> iterator = bullets.iterator();
        while (iterator.hasNext())
        {
            Bullet bullet = iterator.next();
            bufferGraphics.setColor(bullet.color);
            bufferGraphics.drawString(bullet.name, bullet.x, bullet.y); //画子弹
            bullet.move(); //子弹要移动,并且是自动移动,而飞机是通过方向键移动的

            //如果子弹超出窗口的边界,就要把子弹删除
            if (bullet.x < 0 || bullet.x > 700 || bullet.y < 0 || bullet.y > 900)
                iterator.remove();
        }
    }

    void hit(Vector<Plane> planes, Vector<Bullet> bullets)
    {
        Iterator<Bullet> iteratorBullet = bullets.iterator();
        while (iteratorBullet.hasNext())
        {
            Bullet bullet = iteratorBullet.next();

            Iterator<Plane> iteratorPlane = planes.iterator();
            while (iteratorPlane.hasNext())
            {
                Plane plane = iteratorPlane.next();
                double d = Math.pow(Math.abs(plane.x - bullet.x),2) + Math.pow(Math.abs(plane.y - bullet.y),2);
                //System.out.println("x^2 + y^2: " + d);
                if (d < 600)
                {
                    iteratorPlane.remove();
                }
            }
        }
    }

    void planesOperation(Vector<Plane> planes, Vector<Bullet> bullets)
    {
        Iterator<Plane> iterator = planes.iterator();
        while (iterator.hasNext())
        {
            Plane plane = iterator.next();
            bufferGraphics.setColor(plane.color);
            bufferGraphics.drawString(plane.name, plane.x, plane.y); //画子弹
            if (plane.type == 2)
                plane.moveDown(); //子弹要移动,并且是自动移动,而飞机是通过方向键移动的

            if (timer % plane.fireInterval == 0) {
                plane.fire(bullets);
            }

            //如果超出窗口的边界,就要删除
            if (plane.x < 0 || plane.x > 700 || plane.y < 0 || plane.y > 900)
                iterator.remove();
        }
    }

    void updateWindow() {
        timer++;
        bufferGraphics.clearRect(0, 0, 700, 1000);

        planesOperation(friendPlanes, friendBullets);
        bulletsOperation(friendBullets);

        //下边是敌方飞机处理
        if (timer % enemyAddInterval == 0) //timer每过一段时间,添加敌机
        {
            enemyCount++;
            enemyPlanes.add(new Plane((int) (100 + 500 * Math.random()), 0, 0, 10, "bullet" + enemyCount, 2, 3, 15, new Color(255,0,0)));
        }

        planesOperation(enemyPlanes, enemyBullets);
        bulletsOperation(enemyBullets);
        hit(enemyPlanes, friendBullets);

        graphics.drawImage(buffer, 0, 0, null);
    }

    public static void main(String[] args) throws InterruptedException {
        GameFrame gameFrame = new GameFrame();
        while (true) {
            gameFrame.updateWindow();
            Thread.sleep(70);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值