Вам нужно начать игру с Application.launch(args)
, поэтому рассмотрите следующую структуру:
public class Game extends Application{
private Player player;
private Map map;
@Override
public void start(Stage primaryStage) throws Exception {
MainMenu mm = new MainMenu();
player = mm.getPlayer();
map = mm.getMap();
//rest of the code remains the same
}
public static void main(String[] args) {
launch(args);
}
}
MainMenu
принимает только пользовательский ввод и создает Player
и Map
:
public class MainMenu {
private String name,age,gender,chosenCharacter;
private final Player player;
private final Map map;
public MainMenu() {
getUserInput();
player = new Player(name,age,gender,chosenCharacter);
map = new Map(25,25 , player);
}
private void getUserInput(){ // returns Users response
//todo
}
Player getPlayer() {
return player;
}
Map getMap() {
return map;
}
}
Лучший вариант - получить пользовательский ввод с помощью GUI:
public class GetUserInput {
private Player player;
private Map map;
private final TextField nameTf, ageTf, genderTf, characterTf;
GetUserInput() {
Dialog<Void> dialog = new Dialog<>();
dialog.setTitle("Game Setting");
dialog.setHeaderText("Please enter your prefrences");
// Set the button types.
ButtonType buttonType = new ButtonType("OK", ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(buttonType, ButtonType.CANCEL);
//Add event handler to ok button
Button okButton = (Button) dialog.getDialogPane().lookupButton(buttonType);
okButton.setOnAction(e-> processInput());
//todo hanlde response when cancel button is pressed
// Create labels and fields.
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
nameTf = new TextField();
nameTf.setPromptText("Name");
ageTf = new TextField();
ageTf.setPromptText("Age");
genderTf = new TextField();
genderTf.setPromptText("Gender");
characterTf = new TextField();
characterTf.setPromptText("Character");
//todo add validation to all text fields
grid.add(new Label("Name:"), 0, 0);
grid.add(nameTf, 1, 0);
grid.add(new Label("Age:"), 0, 1);
grid.add(ageTf, 1, 1);
grid.add(new Label("Gender:"), 0, 2);
grid.add(genderTf, 1, 2);
grid.add(new Label("Character:"), 0, 3);
grid.add(characterTf, 1, 3);
dialog.getDialogPane().setContent(grid);
// Request focus on the name field
Platform.runLater(() -> nameTf.requestFocus());
dialog.showAndWait();
}
private void processInput() {
String name = nameTf.getText();
String age = ageTf.getText();
String gender = genderTf.getText();
String chosenCharacter = characterTf.getText();
player = new Player(name,age,gender,chosenCharacter);
map = new Map(25,25 , player);
}
Player getPlayer() {
return player;
}
Map getMap() {
return map;
}
}
и использовать его с помощью:
GetUserInput getUserInput = new GetUserInput();
player = getUserInput.getPlayer();
map = getUserInput.getMap();