java实现飞机大战(简单版)

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;

public class PlaneWar extends Application {

    private final int WIDTH = 800;
    private final int HEIGHT = 600;
    private final int PLAYER_SPEED = 5;
    private final int BULLET_SPEED = 10;
    private final int ENEMY_SPEED = 3;
    private final int SCORE_STEP = 10;
    private final int ENEMY_GENERATE_INTERVAL = 100;
    private final Image playerImage = new Image("file:player.png");
    private final Image enemyImage = new Image("file:enemy.png");
    private final Image bulletImage = new Image("file:bullet.png");

    private int score = 0;
    private int enemyGenerateCount = 0;

    private boolean upPressed = false;
    private boolean downPressed = false;
    private boolean leftPressed = false;
    private boolean rightPressed = false;

    private List<Bullet> playerBullets = new ArrayList<>();
    private List<Bullet> enemyBullets = new ArrayList<>();
    private List<Enemy> enemies = new ArrayList<>();

    private Player player = new Player(100, 300, playerImage);

    private class Bullet {
        private int x, y;
        private Image image;

        public Bullet(int x, int y, Image image) {
            this.x = x;
            this.y = y;
            this.image = image;
        }

        public void move(boolean up) {
            if (up) {
                y -= BULLET_SPEED;
            } else {
                y += BULLET_SPEED;
            }
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        public Image getImage() {
            return image;
        }
    }

    private class Enemy {
        private int x, y;
        private Image image;
        private int speed;
        private int health;

        public Enemy(int x, int y, Image image, int speed, int health) {
            this.x = x;
            this.y = y;
            this.image = image;
            this.speed = speed;
            this.health = health;
        }

        public void move() {
            x -= speed;
        }

        public boolean shoot() {
            if (Math.random() < 0.01) {
                enemyBullets.add(new Bullet(x + 25, y + 50, bulletImage));
                return true;
            }
            return false;
        }

        public boolean isHit(int x, int y) {
            if (x > this.x && x < this.x + 50 && y > this.y && y < this.y + 50) {
                return true;
            }
            return false;
        }

        public void hit() {
            health--;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        public Image getImage() {
            return image;
        }

        public int getHealth() {
            return health;
        }
    }

    private class Player {
        private int x, y;
        private Image image;
        private int health;

        public Player(int x, int y, Image image) {
            this.x = x;
            this.y = y;
            this.image = image;
            this.health = 3;
        }

        public void move() {
            if (upPressed && y > 0) {
                y -= PLAYER_SPEED;
            }
            if (downPressed && y < HEIGHT - 50) {
                y += PLAYER_SPEED;
            }
            if (leftPressed && x > 0) {
                x -= PLAYER_SPEED;
            }
            if (rightPressed && x < WIDTH - 50) {
                x += PLAYER_SPEED;
            }
        }

        public boolean shoot() {
            if (Math.random() < 0.05) {
                playerBullets.add(new Bullet(x + 25, y - 10, bulletImage));
                return true;
            }
            return false;
        }

        public boolean isHit(int x, int y) {
            if (x > this.x && x < this.x + 50 && y > this.y && y < this.y + 50) {
                return true;
            }
            return false;
        }

        public void hit() {
            health--;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }

        public Image getImage() {
            return image;
        }

        public int getHealth() {
            return health;
        }
    }

    private void update() {
        player.move();
        for (Bullet bullet : playerBullets) {
            bullet.move(true);
        }
        for (Bullet bullet : enemyBullets) {
            bullet.move(false);
        }
        enemyGenerateCount++;
        if (enemyGenerateCount >= ENEMY_GENERATE_INTERVAL) {
            enemies.add(new Enemy(WIDTH, (int) (Math.random() * (HEIGHT - 50)), enemyImage, ENEMY_SPEED, 1));
            enemyGenerateCount = 0;
        }
        for (Enemy enemy : enemies) {
            enemy.move();
            if (enemy.shoot()) {
                continue;
            }
            for (Bullet bullet : playerBullets) {
                if (enemy.isHit(bullet.getX(), bullet.getY())) {
                    enemy.hit();
                    if (enemy.getHealth() == 0) {
                        enemies.remove(enemy);
                        score += SCORE_STEP;
                    }
                    playerBullets.remove(bullet);
                    break;
                }
            }
        }
        for (Bullet bullet : enemyBullets) {
            if (player.isHit(bullet.getX(), bullet.getY())) {
                player.hit();
                enemyBullets.remove(bullet);
                break;
            }
        }
        for (Enemy enemy : enemies) {
            if (player.isHit(enemy.getX() + 25, enemy.getY() + 50)) {
                player.hit();
                enemies.remove(enemy);
                break;
            }
        }
    }

    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, WIDTH, HEIGHT, Color.BLACK);
        Canvas canvas = new Canvas(WIDTH, HEIGHT);
        root.getChildren().add(canvas);
        GraphicsContext gc = canvas.getGraphicsContext2D();

        scene.setOnKeyPressed(event -> {
            if (event.getCode() == KeyCode.UP) {
                upPressed = true;
            }
            if (event.getCode() == KeyCode.DOWN) {
                downPressed = true;
            }
            if (event.getCode() == KeyCode.LEFT) {
                leftPressed = true;
            }
            if (event.getCode() == KeyCode.RIGHT) {
                rightPressed = true;
            }
            if (event.getCode() == KeyCode.SPACE) {
                player.shoot();
            }
        });

        scene.setOnKeyReleased(event -> {
            if (event.getCode() == KeyCode.UP) {
                upPressed = false;
            }
            if (event.getCode() == KeyCode.DOWN) {
                downPressed = false;
            }
            if (event.getCode() == KeyCode.LEFT) {
                leftPressed = false;
            }
            if (event.getCode() == KeyCode.RIGHT) {
                rightPressed = false;
            }
        });

        new AnimationTimer() {
            public void handle(long currentNanoTime) {
                gc.clearRect(0, 0, WIDTH, HEIGHT);
                update();
                gc.drawImage(player.getImage(), player.getX(), player.getY());
                for (Bullet bullet : playerBullets) {
                    gc.drawImage(bullet.getImage(), bullet.getX(), bullet.getY());
                }
                for (Bullet bullet : enemyBullets) {
                    gc.drawImage(bullet.getImage(), bullet.getX(), bullet.getY());
                }
                for (Enemy enemy : enemies) {
                    gc.drawImage(enemy.getImage(), enemy.getX(), enemy.getY());
                }
                gc.setFill(Color.WHITE);
                gc.fillText("Score: " + score, 10, 20);
                gc.fillText("Health: " + player.getHealth(), 10, 40);
                if (player.getHealth() == 0) {
                    gc.setFill(Color.RED);
                    gc.fillText("Game Over", WIDTH / 2 - 50, HEIGHT / 2);
                    this.stop();
                }
            }
        }.start();

        primaryStage.setTitle("Plane War");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这是一个基于JavaFX的游戏框架,实现了玩家飞机和敌机的移动、射击等基本功能。你可以在此基础上进行开发和完善。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值