gpt4 book ai didi

java - 如何返回 RPG 游戏中的某个检查点行?

转载 作者:行者123 更新时间:2023-12-02 01:16:56 27 4
gpt4 key购买 nike

我需要帮助在我的文字 RPG 游戏中设置检查点。我给用户两种选择,其中之一会导致他们被杀。他们死后,我想让他们回到检查点的起点,但我不知道该怎么办。

我尝试过 while (true) {, continue;, break;,但它只是搞乱了我的代码。我发布的部分非常困惑,但我希望您能看到我正在尝试做的事情。

import java.util.Scanner;

public class Tester {
public static void main (String []args) {

Scanner sc = new Scanner(System.in);

while (true) {
System.out.println("These men are on the other side of the street. They're carrying large laser rifles, and pointing them at civilians.");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {}
System.out.println("You quickly find cover behind a large tree near the Square, and they haven't seen you yet.");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {}
System.out.println("Do you choose to shoot as many as you can, or find a way to save the civilians? (Hint: shoot them or save people)");
String input2 = sc.nextLine();

switch(input2) {
case "shoot them":
System.out.println("You decide to try to shoot as many as you can. You take down 2, but the 5 men remaining have taken cover. They now know exactly where you are now. They toss a grenade your way.");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {}
System.out.println("*BOOM*");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
System.out.println("You died by explosion.");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {}
System.out.println("GAME OVER");
continue;
case "save people":
System.out.println("Good decision...");
break;



}
}
}
}

如果用户写“射击他们”,结果是他们死亡,但代码不会返回到检查点。

最佳答案

我知道这不是 CodeReview,只是仅供引用,您可以将 throws InterruptedException 添加到您的主方法签名中,这样您就不必捕获所有这些异常。

实现 RPG 游戏的一种基本方法是使用对象图,也就是说,您有一个类 OptionScenario 并遍历它来玩角色扮演游戏。例如:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Scratch {

public static void main(String[] args) throws InterruptedException {
Scanner scanner = new Scanner(System.in);

Scenario root = new Scenario("Welcome to the forest", 500);
Scenario left = new Scenario("You went left!", 500);
Scenario right = new Scenario("You went right!", 500);

Option step1a = new Option("Go left", left);
Option step1b = new Option("Go right", right);
Option step1c = new Option("Go back to the beginning", root);

root.branches.add(step1a);
root.branches.add(step1b);
root.branches.add(step1c);

Scenario current = root;

while (true) {
System.out.println(current.text);

if (current.branches.size() == 0) break;

Thread.sleep(current.delay);
System.out.println("What would you like to do?");
for (int i = 0; i < current.branches.size(); i++) {
System.out.printf("%s. %s\n", i, current.branches.get(i).text);
}
int choice = Integer.parseInt(scanner.nextLine());
current = current.branches.get(choice).result;
}

System.out.println("Game over!");
}
}

class Scenario {

List<Option> branches = new ArrayList<>();
String text;
int delay;

public Scenario(String text, int delay) {
this.text = text;
this.delay = delay;
}

}

class Option {

Scenario result;
String text;

public Option(String text, Scenario result) {
this.text = text;
this.result = result;
}

}

如果您有任何疑问,请告诉我。

关于java - 如何返回 RPG 游戏中的某个检查点行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58423558/

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