Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JavaFX help I'm trying to make a maze like game where you are able to move the player using the arrow keys and try to

JavaFX help

I'm trying to make a maze like game where you are able to move the player using the arrow keys and try to get away from a person chasing you, but I keep getting an error when I start the program can you look at my code and tell me what I'm doing wrong.

/****************************************************************/

package lab8;

import javafx.application.Application; import javafx.application.Platform; import javafx.event.Event; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.input.MouseEvent; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.stage.Stage;

public class MazeGUIPane extends Application { private BorderPane border = new BorderPane(); private GridPane grid = new GridPane(); private Label[][] labelCoordinates = new Label[25][25]; private StreetMap map = new StreetMap(); private Scene scene = new Scene(border); private Runner runner = new Runner(1,1); int row; int column; Label square = new Label();

public void start(Stage stage) { scene.getStylesheets().add(this.getClass().getResource("style.css").toExternalForm()); grid.getStyleClass().add("grid"); border.getStyleClass().add("border"); Label pamplonaTitle = new Label("Map Of Pamplona"); pamplonaTitle.getStyleClass().add("text"); // Button startButton = new Button("Start"); Button resetButton = new Button("Reset"); HBox actualGrid = new HBox(); HBox bottomButtons = new HBox(); HBox title = new HBox(); map.grid(); for (column = 0; column < 25; column++) { for (row = 0; row < 25; row++) { if (map.coordinates[column][row].getValue() == ' ') { labelCoordinates[column][row].getStyleClass().add("path"); } else if (map.coordinates[column][row].getValue() == 'S') { labelCoordinates[column][row].setText("Start"); labelCoordinates[column][row].getStyleClass().add("start-end"); } else if (map.coordinates[column][row].getValue() == 'P') { labelCoordinates[column][row].getStyleClass().add("player"); } else if (map.coordinates[column][row].getValue() == 'E') { labelCoordinates[column][row].setText("End"); labelCoordinates[column][row].getStyleClass().add("startt-end"); } else { labelCoordinates[column][row].getStyleClass().add("wall"); }

// StreetMap temp = new StreetMap(); // temp.getStreetMap();

square.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler() {

@Override public void handle(Event event) { if (map.coordinates[column][row].getValue() == 'W') { labelCoordinates[column][row].getStyleClass().clear(); labelCoordinates[column][row].getStyleClass().add("path"); map.coordinates[column][column].setValue(' '); } else { labelCoordinates[column][row].getStyleClass().clear(); labelCoordinates[column][row].getStyleClass().add("wall"); map.coordinates[column][row].setValue('W'); }

} });

border.addEventFilter(KeyEvent.KEY_PRESSED, e -> { int y = runner.getCol(); int x = runner.getRow(); if (e.getCode().equals(KeyCode.UP)) { if (map.coordinates[y - 1][x].getValue() == ' ' ) { labelCoordinates[column][row].getStyleClass().clear(); labelCoordinates[column][row].getStyleClass().add("path"); map.coordinates[y][x].setValue('P'); labelCoordinates[y - 1][x].getStyleClass().clear(); labelCoordinates[y - 1][x].getStyleClass().add("player"); runner.setColumn(y - 1); runner.setRow(x); } } if (e.getCode().equals(KeyCode.DOWN)) { if (map.coordinates[y + 1][x].getValue() == 'P'){ labelCoordinates[column][row].getStyleClass().clear(); labelCoordinates[column][row].getStyleClass().add("path"); map.coordinates[y][x].setValue('P'); labelCoordinates[y + 1][row].getStyleClass().clear(); labelCoordinates[y + 1][x].getStyleClass().add("player"); runner.setColumn(y + 1); runner.setRow(x); } } if (e.getCode().equals(KeyCode.LEFT)) { if (map.coordinates[y][x - 1].getValue() == 'P') { labelCoordinates[column][row].getStyleClass().clear(); labelCoordinates[column][row].getStyleClass().add("path"); map.coordinates[y][x].setValue('P'); labelCoordinates[y][x - 1].getStyleClass().clear(); labelCoordinates[y][x - 1].getStyleClass().add("player"); runner.setColumn(y); runner.setRow(x - 1); } } if (e.getCode().equals(KeyCode.RIGHT)) { if (map.coordinates[y][x + 1].getValue() == 'P') { labelCoordinates[column][row].getStyleClass().clear(); labelCoordinates[column][row].getStyleClass().add("path"); map.coordinates[y][x].setValue('P'); labelCoordinates[y][x + 1].getStyleClass().clear(); labelCoordinates[y][x + 1].getStyleClass().add("player"); runner.setColumn(y); runner.setRow(x + 1); } } e.consume();

});

square.getStyleClass().add("player"); grid.add(square, column, row); } } title.getStyleClass().add("title"); title.getChildren().add(pamplonaTitle); bottomButtons.setPadding(new Insets(15, 12, 15, 12)); bottomButtons.setSpacing(10); bottomButtons.getStyleClass().add("start-button"); bottomButtons.getChildren().add(resetButton); actualGrid.getChildren().add(grid); border.setTop(title); border.setCenter(actualGrid); border.setBottom(bottomButtons); stage.setScene(scene); stage.show();

resetButton.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler() {

@Override public void handle(Event event) { stage.close(); Platform.runLater(() -> new MazeGUIPane().start(new Stage())); stage.setScene(scene); }

});

}

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

}

/****************************************************************/

package lab8;

public class Runner{ int column; int row; public Runner(int columnIn, int rowIn){ column = columnIn; row = rowIn; } public void setRow(int rowIn){ row = rowIn; } public void setColumn(int columnIn){ column = columnIn; } public int getCol(){ return column; } public int getRow(){ return row; } }

/****************************************************************/

package lab8;

public class Coordinate { private int row; private int column; private char value;

public Coordinate() { column = 0; row = 0; value = ' '; }

public int getColumn() { return column; }

public int getRow() { return row; }

public char getValue() { return value; }

public void setColumn(int columnInput) { column = columnInput; }

public void setRow(int rowInput) { row = rowInput; }

public void setValue(char valueInput) { value = valueInput; }

}

/****************************************************************/

package lab8;

import lab8.Coordinate;

public class StreetMap { Coordinate[][] coordinates = new Coordinate[25][25];

public void grid() { /* * String testRandomness = ""; for(int counter = 0; counter < 20; * counter++){ testRandomness += ((int)(Math.random() * 4)) + " "; } * System.out.println(test); */ for (int column = 0; column < 25; column++) { for (int row = 0; row < 25; row++) { coordinates[column][row] = new Coordinate();

if (row == 0 || column == 0 || row == 24 || column == 24 || row == 1 && column == 1) { if (column == 1 && row == 0) { coordinates[column][row].setValue('S'); coordinates[column][row].setColumn(column); coordinates[column][row].setRow(row); } else if (column == 1 && row == 1) { coordinates[column][row].setValue('P'); coordinates[column][row].setColumn(column); coordinates[column][row].setRow(row); } else if (column == 24 && row == 23) { coordinates[column][row].setValue('E'); coordinates[column][row].setColumn(column); coordinates[column][row].setRow(row); } else { coordinates[column][row].setValue('W'); coordinates[column][row].setColumn(column); coordinates[column][row].setRow(row); }

} else { if (((int) (Math.random() * 5)) == 1 && row != 1 && column != 1) { coordinates[column][row].setValue('W'); coordinates[column][row].setColumn(column); coordinates[column][row].setRow(row); } else { coordinates[column][row].setValue(' '); coordinates[column][row].setColumn(column); coordinates[column][row].setRow(row); } } } }

}

public static void main(String[] args) { StreetMap streetMap = new StreetMap(); streetMap.grid(); streetMap.getStreetMap(); } public void getStreetMap(){ for(int column = 0; column < 25; column++){ for(int row = 0; row< 25; row++){ System.out.print(coordinates[column][row].getValue()); } System.out.println(); } }

}

/****************************************************************/

.path { -fx-background-color: green; -fx-min-height: 30.0px; -fx-min-width: 30.0px; -fx-border-color: black; -fx-border-width: 1.0px; }

.wall { -fx-background-color: black; -fx-min-height: 30.0px; -fx-min-width: 30.0px; -fx-border-width: 1.0px; -fx-border-color: grey; }

.start-end { -fx-background-color: white; -fx-font-size: 90.0%; -fx-min-height: 30.0px; -fx-min-width: 30.0px; -fx-border-width: 1.0px; -fx-border-color: grey; -fx-text-fill: black; }

.player{ -fx-background-color: lightblue; -fx-min-height: 30.0px; -fx-min-width: 30.0px; -fx-border-width: 1.0px; -fx-border-color: black; }

.bull{ -fx-background-color: red; -fx-min-height: 30.0px; -fx-minwidth: 30.0px; -fx-border-width: 1.0px; -fx-border-color: black; }

.title { -fx-font-size: 250.0%; -fx-background-color: #333333; -fx-alternative-column-fill-visible: true; -fx-alternative-row-fill-visible: true; }

.border { -fx-alignment: center; -fx-background-color: #333333; }

.grid { -fx-alignment: top-center; -fx-min-height: 600.0px; -fx-min-width: 600.0px; -fx-background-color: #333333; }

.text { -fx-text-fill: yellow; }

.start-button{ -fx-font-size: 20.0px; -fx-background-color: #333333; -fx-alignment: top-center; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Databases Concepts And Practice

Authors: Suzanne W Dietrich

1st Edition

1119827949, 9781119827948

More Books

Students also viewed these Databases questions