Answer to Question #288535 in Java | JSP | JSF for Jak

Question #288535

Maze Game


General Rules:


1. Use the collision detection method for the wall(obstacle).


2. Once collided with any part of the obstacles, the object will go back to its starting position and lose 1 life(3 lives


in total). It must indicate how many lives are left during the game.


3. Once life falls below zero(0), the last collision will automatically stop the game and display “Game Over” in any


part of the program.


4. After the object reaches the finish line, the text “Congratulations” must appear in any part of the program



The movement of the object depends on the keyboard event triggered by the user. It must be W, A, S, D or


Arrow UP, DOWN, LEFT, RIGHT. (Just like pacman game movement)

1
Expert's answer
2022-01-19T01:55:08-0500
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.util.LinkedList;
import java.util.List;

public class Main extends Application {

    public static final byte[][] MAP = {
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1},
            {1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1},
            {1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1},
            {1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1},
            {1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1},
            {1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1},
            {1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1},
            {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};

    public static final int BLOCK_WIDTH = 40;
    public static final int BLOCK_HEIGHT = 40;

    public static final int PLAYER_WIDTH = 10;
    public static final int PLAYER_HEIGHT = 10;

    public static final double PLAYER_SPEED = 1.75;

    private static List<Rectangle> blocks;
    private static Rectangle player;
    private static Rectangle finish;

    private static Label livesLabel;
    private static Label resultLabel;

    private static KeyCode direction;

    private static int lives;

    private static boolean gameOver;


    public static boolean intersection(Rectangle a, Rectangle b) {
        double xALeft = a.getTranslateX();
        double xARight = a.getTranslateX() + a.getWidth();
        double yATop = a.getTranslateY();
        double yABottom = a.getTranslateY() + a.getHeight();

        double xBLeft = b.getX();
        double xBRight = b.getX() + b.getWidth();
        double yBTop = b.getY();
        double yBBottom = b.getY() + b.getHeight();

        return !(xBLeft > xARight || xALeft > xBRight || yABottom < yBTop || yBBottom < yATop);
    }

    private static void setup() {
        lives = 3;
        direction = KeyCode.ESCAPE;
        gameOver = false;
        createBlocks();
        createPlayer();
        createLabels();
    }

    private static void createBlocks() {
        blocks = new LinkedList<>();
        for (int i = 0; i < MAP.length; i++) {
            for (int j = 0; j < MAP[i].length; j++) {
                if (MAP[i][j] == 1) {
                    blocks.add(new Rectangle(j * BLOCK_WIDTH, i * BLOCK_HEIGHT, BLOCK_WIDTH, BLOCK_HEIGHT));
                    blocks.get(blocks.size() - 1).setFill(Color.DARKGRAY);
                } else if (MAP[i][j] == 2) {
                    finish = new Rectangle(j * BLOCK_WIDTH, i * BLOCK_HEIGHT, BLOCK_WIDTH, BLOCK_HEIGHT);
                    finish.setFill(Color.LIGHTGREEN);
                }
            }
        }
    }

    private static void createPlayer() {
        player = new Rectangle(PLAYER_WIDTH, PLAYER_HEIGHT, Color.DARKORANGE);
        player.setTranslateX(BLOCK_WIDTH + 1);
        player.setTranslateY(BLOCK_HEIGHT + 1);
    }

    private static void createLabels() {
        livesLabel = new Label("Lives: " + lives);
        livesLabel.setFont(Font.font(20));
        livesLabel.setTranslateX(10);
        livesLabel.setTranslateY(15);

        resultLabel = new Label();
        resultLabel.setFont(Font.font(40));
        resultLabel.setTranslateX(320);
        resultLabel.setTranslateY(290);
        resultLabel.setVisible(false);
    }

    private static void animation() {
        AnimationTimer animationTimer = new AnimationTimer() {
            @Override
            public void handle(long now) {
                if (gameOver) {
                    resultLabel.setVisible(true);
                    this.stop();
                }
                switch (direction) {
                    case LEFT:
                    case A:
                        player.setTranslateX(player.getTranslateX() - PLAYER_SPEED);
                        break;
                    case RIGHT:
                    case D:
                        player.setTranslateX(player.getTranslateX() + PLAYER_SPEED);
                        break;
                    case UP:
                    case W:
                        player.setTranslateY(player.getTranslateY() - PLAYER_SPEED);
                        break;
                    case DOWN:
                    case S:
                        player.setTranslateY(player.getTranslateY() + PLAYER_SPEED);
                        break;
                }
                for (Rectangle block : blocks) {
                    if (intersection(player, block)) {
                        lives--;
                        if (lives < 0) {
                            resultLabel.setText("Game Over");
                            gameOver = true;
                        } else {
                            livesLabel.setText("Lives: " + lives);
                            player.setTranslateX(BLOCK_WIDTH + 1);
                            player.setTranslateY(BLOCK_HEIGHT + 1);
                            direction = KeyCode.ESCAPE;
                        }
                        break;
                    }
                }
                if (intersection(player, finish)) {
                    resultLabel.setText("Congratulations");
                    gameOver = true;
                }
            }
        };
        animationTimer.start();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        setup();

        Pane pane = new Pane();
        pane.getChildren().addAll(blocks);
        pane.getChildren().addAll(finish, player, livesLabel, resultLabel);

        animation();
        Scene scene = new Scene(pane, 800, 600);
        scene.setOnKeyReleased(keyEvent -> direction = keyEvent.getCode());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog