gpt4 book ai didi

java - 如何重新启动 GUI 应用程序游戏,例如再次玩

转载 作者:行者123 更新时间:2023-12-01 17:00:30 25 4
gpt4 key购买 nike

抱歉,意大利面条代码!这是我的第一个图形用户界面。一般来说,对于编码来说非常陌生。我知道 while 循环可以在 gui 之外使用,但显然不适用于这个哈哈。所以我想知道如何在我制作的“private void runAway()”方法中重新启动我的java游戏?

public class EnemiesAttackApp extends Application {
@Override
public void start (Stage primaryStage) throws Exception {
primaryStage.setScene(new Scene(createContent()));
primaryStage.setTitle("Enemies Attack!");
primaryStage.show();

}

//System objects
Scanner in = new Scanner(System.in);
Random rand = new Random();

//Game variables
String[] enemies = {"Skeleton", "Zombie", "Warrior", "Assassin"};
int enemyAttackDamage = 25;
int enemyHealth = 0 + rand.nextInt(75);
String enemy = enemies[rand.nextInt(enemies.length)];

// Player variables
int health = 100; etc



private void initGame() {
println("Welcome to Enemies Attack v 0.1");
println("-------------------------------");


if (enemyHealth > 0)
println("\t# " + enemy + " appeared! #\n");
println("\tYOUR HP: " + health);
println("\t" + enemy + "'s HP: " + enemyHealth);
println("\n\tWhat would you like to do?");
println("\t1. Attack enemy");
println("\t2. Drink potion");
println("\t3. Run");

initCommands();
}



`private void runAway() {

println("\t You ran away from the " + enemy + "!");

// how do I restart the gui here? }

最佳答案

创建一个控制当前“场景”的对象并在每次游戏重新启动时重新实例化该对象可能是明智的做法。无论如何,这是您修改后的代码,应该可以工作:

public class EnemiesAttackApp extends Application {
@Override
public void start (Stage primaryStage) throws Exception {
primaryStage.setScene(new Scene(createContent()));
primaryStage.setTitle("Enemies Attack!");
primaryStage.show();
}

//System objects
Scanner in = new Scanner(System.in);
Random rand = new Random();

//Game variables
String[] enemies = {"Skeleton", "Zombie", "Warrior", "Assassin"};
int enemyAttackDamage = 25;
int enemyHealth = 0;
String enemy = null;

// Player variables
int health = 0;



private void initGame() {
enemyHealth = 0 + rand.nextInt(75);
enemy = enemies[rand.nextInt(enemies.length)];
health = 100;

println("Welcome to Enemies Attack v 0.1");
println("-------------------------------");

if (enemyHealth > 0) {
println("\t# " + enemy + " appeared! #\n");
println("\tYOUR HP: " + health);
println("\t" + enemy + "'s HP: " + enemyHealth);
println("\n\tWhat would you like to do?");
println("\t1. Attack enemy");
println("\t2. Drink potion");
println("\t3. Run");

initCommands();
}
}


private void runAway() {
println("\t You ran away from the " + enemy + "!");

initGame();
}

}

它基本上遵循一个逻辑,即有一个重置所有值的方法(在我的示例中为 init game)。

关于java - 如何重新启动 GUI 应用程序游戏,例如再次玩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61512892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com