java实现飞机大战游戏功能

飞机大战是一款经典的游戏,Java语言可以通过图形界面的编程来实现该游戏的功能。下面是一个简单的Java实现飞机大战的示例代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PlaneWar extends JFrame implements ActionListener, KeyListener, Runnable {
    private Image bufferImage;
    private Graphics bufferImageGraphics;
    private final int WIDTH = 400;
    private final int HEIGHT = 400;
    private final int PLANE_SIZE = 20;
    private final int BULLET_SIZE = 5;
    private final int MAX_BULLET_COUNT = 10;
    private final int ENEMY_SIZE = 20;
    private final int MAX_ENEMY_COUNT = 5;
    private final int ENEMY_SPEED = 2;
    private int planeX, planeY;
    private boolean[] moveKey;
    private boolean fireKey;
    private int[] bulletX, bulletY;
    private boolean[] bulletIsFired;
    private int[] enemyX, enemyY;
    private boolean[] enemyIsAlive;
    private Thread gameLoop;
    public PlaneWar() {
        setTitle("Plane War");
        setSize(WIDTH, HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setFocusable(true);
        addKeyListener(this);
        setVisible(true);
        bufferImage = createImage(WIDTH, HEIGHT);
        bufferImageGraphics = bufferImage.getGraphics();
        planeX = WIDTH / 2 - PLANE_SIZE / 2;
        planeY = HEIGHT - PLANE_SIZE;
        moveKey = new boolean[4];
        bulletX = new int[MAX_BULLET_COUNT];
        bulletY = new int[MAX_BULLET_COUNT];
        bulletIsFired = new boolean[MAX_BULLET_COUNT];
        enemyX = new int[MAX_ENEMY_COUNT];
        enemyY = new int[MAX_ENEMY_COUNT];
        enemyIsAlive = new boolean[MAX_ENEMY_COUNT];
        gameLoop = new Thread(this);
        gameLoop.start();
    }
    public void paint(Graphics g) {
        bufferImageGraphics.clearRect(0, 0, WIDTH, HEIGHT);
        bufferImageGraphics.setColor(Color.RED);
        bufferImageGraphics.fillRect(planeX, planeY, PLANE_SIZE, PLANE_SIZE);
        bufferImageGraphics.setColor(Color.BLUE);
        for (int i = 0; i < MAX_BULLET_COUNT; i++) {
            if (bulletIsFired[i]) {
                bufferImageGraphics.fillOval(bulletX[i], bulletY[i], BULLET_SIZE, BULLET_SIZE);
            }
        }
        bufferImageGraphics.setColor(Color.GREEN);
        for (int i = 0; i < MAX_ENEMY_COUNT; i++) {
            if (enemyIsAlive[i]) {
                bufferImageGraphics.fillRect(enemyX[i], enemyY[i], ENEMY_SIZE, ENEMY_SIZE);
            }
        }
        g.drawImage(bufferImage, 0, 0, this);
    }
    public void actionPerformed(ActionEvent e) {}
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        switch (keyCode) {
            case KeyEvent.VK_UP:
                moveKey[0] = true;
                break;
            case KeyEvent.VK_DOWN:
                moveKey[1] = true;
                break;
            case KeyEvent.VK_LEFT:
                moveKey[2] = true;
                break;
            case KeyEvent.VK_RIGHT:
                moveKey[3] = true;
                break;
            case KeyEvent.VK_SPACE:
                fireKey = true;
                break;
        }
    }
    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
        switch (keyCode) {
            case KeyEvent.VK_UP:
                moveKey[0] = false;
                break;
            case KeyEvent.VK_DOWN:
                moveKey[1] = false;
                break;
            case KeyEvent.VK_LEFT:
                moveKey[2] = false;
                break;
            case KeyEvent.VK_RIGHT:
                moveKey[3] = false;
                break;
            case KeyEvent.VK_SPACE:
                fireKey = false;
                break;
        }
    }
    public void keyTyped(KeyEvent e) {}
    public void run() {
        while (true) {
            // 移动飞机
            if (moveKey[0]) {
                planeY -= 5;
            }
            if (moveKey[1]) {
                planeY += 5;
            }
            if (moveKey[2]) {
                planeX -= 5;
            }
            if (moveKey[3]) {
                planeX += 5;
            }
            // 发射子弹
            if (fireKey) {
                for (int i = 0; i < MAX_BULLET_COUNT; i++) {
                    if (!bulletIsFired[i]) {
                        bulletX[i] = planeX + PLANE_SIZE / 2 - BULLET_SIZE / 2;
                        bulletY[i] = planeY;
                        bulletIsFired[i] = true;
                        break;
                    }
                }
            }
            // 移动子弹
            for (int i = 0; i < MAX_BULLET_COUNT; i++) {
                if (bulletIsFired[i]) {
                    bulletY[i] -= 10;
                    if (bulletY[i] < 0) {
                        bulletIsFired[i] = false;
                    }
                }
            }
            // 移动敌机
            for (int i = 0; i < MAX_ENEMY_COUNT; i++) {
                if (enemyIsAlive[i]) {
                    enemyY[i] += ENEMY_SPEED;
                    if (enemyY[i] > HEIGHT) {
                        enemyIsAlive[i] = false;
                    }
                } else {
                    if (Math.random() < 0.01) {
                        enemyX[i] = (int)(Math.random() * WIDTH);
                        enemyY[i] = 0;
                        enemyIsAlive[i] = true;
                    }
                }
            }
            // 检测碰撞
            for (int i = 0; i < MAX_BULLET_COUNT; i++) {
                if (bulletIsFired[i]) {
                    for (int j = 0; j < MAX_ENEMY_COUNT; j++) {
                        if (enemyIsAlive[j] && bulletX[i] >= enemyX[j] && bulletX[i] <= enemyX[j] + ENEMY_SIZE && bulletY[i] >= enemyY[j] && bulletY[i] <= enemyY[j] + ENEMY_SIZE) {
                            bulletIsFired[i] = false;
                            enemyIsAlive[j] = false;
                        }
                    }
                }
            }
            repaint();
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        new PlaneWar();
    }
}

该飞机大战游戏游戏示例代码使用了Java的Swing图形界面库,通过继承JFrame类,实现了游戏的界面和基本功能。其中,通过键盘事件监听实现了飞机的移动和子弹的发射,通过线程控制游戏的循环和敌机的移动,通过图形绘制实现了游戏的可视化效果。