如何使用Java创建第二级

时间:2017-03-12 03:11:07

标签: java javafx

所以我建立了一个游戏,到目前为止我只有一个级别,我想添加一个不同的箱子位置,钻石位置和墙位置的第二个级别,但我不确定最好的方法采取。

你会建议我让我的第二级(以及更多)继承我的第一级并改变游戏代码或以稍微不同的方式进行吗?就像我可以创建另一个名为“level2”的类并在我当前的游戏类中添加一些代码,这会让玩家在第一级完成时跳到2级?<​​/ p>

public class Game extends Application {

    Pane backgroundPane;
    Pane playfieldLayer;
    Pane scoreLayer;

    Image playerImage;
    Image wallImage;
    Image foodImage;
    Image diamondImage;

    List<Player> players = new ArrayList<>();
    List<Food> foods = new ArrayList<>();
    List<Wall> walls = new ArrayList<>();
    List<Diamonds> diamonds = new ArrayList<>();

    boolean collision;
    boolean wallCollision;
    boolean foodWallCollision;
    boolean won = false;

    GridPane gameGrid;
    static Scene scene;
    Stage theStage;

    @Override

    public void start(Stage theStage) {

        // theStage.setWidth(Settings.SCENE_WIDTH);
        // theStage.setHeight(Settings.SCENE_HEIGHT);

        Menu menu = new Menu();
        Scene menuView = new Scene(menu, Settings.SCENE_HEIGHT, Settings.SCENE_WIDTH);

        theStage.setScene(menuView);
        // theStage.setResizable(false);

        theStage.show();

        Menu.start.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event) {

                runGame(theStage);

            }
        });

    }

    public void runGame(Stage theStage) {

        // Input input = new Input(scene);

        Group root = new Group();

        GridPane gameGrid = new GamePane(14, 14);
        gameGrid.setStyle("-fx-background-color: white; -fx-grid-lines-visible:true");
        backgroundPane = new Pane();
        backgroundPane.setId("root");
        playfieldLayer = new Pane();
        scoreLayer = new Pane();

        playfieldLayer.getChildren().add(gameGrid);
        root.getChildren().add(backgroundPane);
        root.getChildren().add(playfieldLayer);

        // scene = new Scene(root, (columnAmount * 40) + 66, (rowAmount * 40) +
        // 66, Color.WHITE);

        scene = new Scene(root, 600, 500);
        backgroundPane.getStylesheets().addAll(this.getClass().getResource("application.css").toExternalForm());

        // theStage.setResizable(false);
        theStage.setScene(scene);
        theStage.show();

        loadGame();

        try {

            createPlayers();
            spawnFood();
            spawnWall();
            spawnDiamonds();

        } catch (NullPointerException e) {

            System.out.println("You are missing the pictures for the spawn methods");
        }

        AnimationTimer gameLoop = new AnimationTimer() {

            @Override
            public void handle(long now) {

                // player input
                players.forEach(sprite -> sprite.processInput());
                foods.forEach(sprite -> sprite.processInput());

                // movement
                players.forEach(sprite -> sprite.move());
                foods.forEach(sprite -> sprite.move());
                // ((Player) players).stopInput();

                // check collisions

                checkCollisions();
                // GamePane.checkWallCollisions();
                checkWallCollisions();
                checkfoodwallCollisions();
                checkDiamondCollisions();
                checkWallPlayerCollisions();
                checkWin();

                // update sprites in scene
                players.forEach(sprite -> sprite.updateUI());
                foods.forEach(sprite -> sprite.updateUI());

            }

        };
        gameLoop.start();

    }

    private void loadGame() {
        playerImage = new Image(getClass().getResource("warehouse.png").toExternalForm());
        wallImage = new Image(getClass().getResource("Wall.png").toExternalForm());
        foodImage = new Image(getClass().getResource("food.png").toExternalForm());
        diamondImage = new Image(getClass().getResource("Chef.png").toExternalForm());
    }

    private void createPlayers() {

        Input input = new Input(scene);

        input.addListeners(scene);

        Image image = playerImage;

        double x = (Settings.SCENE_WIDTH - image.getWidth()) / 1.5;
        double y = Settings.SCENE_HEIGHT * 0.2;

        Player player = new Player(playfieldLayer, image, x, y, 0, 0, 0, input);

        players.add(player);

    }

    private void spawnFood() {

        Input input = new Input(scene);

        input.addListeners(scene);

        Image image = foodImage;

        double x = (Settings.SCENE_WIDTH - image.getWidth()) / 2.0;
        double y = Settings.SCENE_HEIGHT * 0.4;

        Food food = new Food(playfieldLayer, image, x, y, 0, 0, input);

        foods.add(food);

    }

    private void spawnWall() {

        Input input = new Input(scene);

        input.addListeners(scene);

        Image image = wallImage;

        double x = Settings.SCENE_WIDTH * 0.01;
        double y = Settings.SCENE_HEIGHT * 0.01;

        Wall wall = new Wall(playfieldLayer, image, x, 100, 0, 0, input);
        Wall wall2 = new Wall(playfieldLayer, image, 570, 150, 0, 0, input);

        walls.add(wall);
        walls.add(wall2);
        for (int i = 0; i < 9; i++) {

            y = 200;
            x = x + 50;

            Wall wall1 = new Wall(playfieldLayer, image, x, y, 0, 0, input);

            walls.add(wall1);

        }

        for (int i = 0; i < 2; i++) {

            y = y -40;
            x = x - 50;

            Wall wall4 = new Wall(playfieldLayer, image, x, y, 0, 0, input);

            walls.add(wall4);

        }


        for (int i = 0; i < 3; i++) {

            y = y + 150;
            x = x -100;

            Wall wall5 = new Wall(playfieldLayer, image, x, y, 0, 0, input);

            walls.add(wall5);

        }

    }

    private void spawnDiamonds() {

        Input input = new Input(scene);

        input.addListeners(scene);

        Image image = diamondImage;

        double x = (Settings.SCENE_WIDTH - image.getWidth()) / 3.0;
        double y = Settings.SCENE_HEIGHT * 0.1;

        Diamonds diamond = new Diamonds(playfieldLayer, image, x, y, 0, 0, input);

        diamonds.add(diamond);

    }

    private boolean checkCollisions() {

        collision = false;

        for (Player player : players) {
            for (Food food : foods) {
                if (player.collidesWith(food)) {

                    player.setDy(-player.getDy());
                    player.setDx(-player.getDx());

                    Food.collision = true;

                }

            }
        }
        return collision;

    }

    //other collisions exist but not added due to simplicity

    public void checkWin() {

        if (won == true) {

            System.out.println("won");

        }

    }

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

    }

}

1 个答案:

答案 0 :(得分:1)

我不是真正的Java开发人员,尽管我很久以前就开始研究一些Java项目。尽管如此,以下建议适用于任何面向对象的项目,因此可能会有所帮助。

  • 类定义对象的类型。如果两个对象属于同一类型,则它们应该来自同一个类。

  • 有时,您想要修改类型。这是通过扩展课程来实现的。

  • 扩展类通常具有其他属性或方法;那就是它会有额外的数据或功能。

  • 拥有类的重点是拥有多个具有相同属性和方法的对象。属性可能会更改,但对象仍然属于同一类型。

鉴于你问题的性质,我倾向于建议:

  • 附加级别可能只是具有不同属性值的附加对象。我不认为你真的需要一个新的课程。

  • 在运行期间,您可能每次只处理一个级别,因此您甚至可能使用相同的对象,并使用不同的属性级别重新初始化。

修改

同样,我离开了我的领域,所以术语我错了。

如果我有多个级别,我可能会创建一个描述符类,它具有所有可变元素的属性。然后我将创建此类的对象数组,表示每个对象的属性值。

最后,我要证实矿石使用构造函数中的特定数组元素重新初始化一个级别。像这样:

level2 = new Level(levels[2]);  //  new object
//  or
level = level.init(levels[2]);  //  existing object