Java 在我的RPG游戏中,我如何回到某一行进行检查点?
我需要帮助在我的文字RPG游戏的检查点。我给用户两个选择,其中一个会让他们丧命。他们死后,我希望他们回到检查站的起点,但我不知道该怎么办 我已经试过了,(真的){,continue;,break;,它只是把我的代码弄乱了。我发布的部分非常混乱,但我希望你能看到我在尝试做什么Java 在我的RPG游戏中,我如何回到某一行进行检查点?,java,Java,我需要帮助在我的文字RPG游戏的检查点。我给用户两个选择,其中一个会让他们丧命。他们死后,我希望他们回到检查站的起点,但我不知道该怎么办 我已经试过了,(真的){,continue;,break;,它只是把我的代码弄乱了。我发布的部分非常混乱,但我希望你能看到我在尝试做什么 import java.util.Scanner; public class Tester { public static void main (String []args) { Scanner sc =
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,但仅供参考,您可以将
抛出InterruptedException
添加到主方法签名中,这样您就不必捕获所有这些异常
实现RPG游戏的一种基本方法是使用对象图,也就是说,您有一个类选项
和场景
,并遍历它来播放RPG。例如:
import java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
课堂擦伤{
公共静态void main(字符串[]args)引发InterruptedException{
扫描仪=新的扫描仪(System.in);
场景根=新场景(“欢迎来到森林”,500);
Scenario left=新场景(“你向左走了!”,500);
Scenario right=新场景(“你走对了!”,500);
选项step1a=新选项(“向左”,向左);
选项step1b=新选项(“向右”,向右);
选项step1c=新选项(“返回开始”,根);
根.分枝.添加(步骤1a);
根.分枝.添加(步骤1b);
根.分枝.添加(步骤1c);
场景当前=根;
while(true){
System.out.println(current.text);
如果(current.branchs.size()==0)中断;
线程睡眠(当前延迟);
System.out.println(“您想做什么?”);
对于(int i=0;i
如果您有任何问题,请告诉我。我知道这不是CodeReview,但仅供参考,您可以将
抛出InterruptedException
添加到主方法签名中,这样您就不必捕获所有这些异常
实现RPG游戏的一种基本方法是使用对象图,也就是说,您有一个类选项
和场景
,并遍历它来播放RPG。例如:
import java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
课堂擦伤{
公共静态void main(字符串[]args)引发InterruptedException{
扫描仪=新的扫描仪(System.in);
场景根=新场景(“欢迎来到森林”,500);
Scenario left=新场景(“你向左走了!”,500);
Scenario right=新场景(“你走对了!”,500);
选项step1a=新选项(“向左”,向左);
选项step1b=新选项(“向右”,向右);
选项step1c=新选项(“返回开始”,根);
根.分枝.添加(步骤1a);
根.分枝.添加(步骤1b);
根.分枝.添加(步骤1c);
场景当前=根;
while(true){
System.out.println(current.text);
如果(current.branchs.size()==0)中断;
线程睡眠(当前延迟);
System.out.println(“您想做什么?”);
对于(int i=0;i
如果您有任何问题,请告诉我。您需要删除
系统。退出(0);
行
如果删除该行,则
while
循环将再次启动,因为执行了continue
,并且将显示检查点消息。您需要删除系统。退出(0);
行
如果删除该行,则
while
循环将再次启动,因为执行了continue
,并且将显示检查点消息。System.exit(0);continue;
-您认为这里会发生什么?是的,我已将其删除System.exit(0);继续;
-你认为这里会发生什么?是的,我摆脱了it@Andreas为什么不呢?如果删除该行,则while
将再次启动,因为执行了continue
。@Andreas我添加了更多详细信息,但解决方案是相同的。如果OP想要一个不在游戏开始时的检查点呢?Sure、 “继续”将重置它,但它将删除玩家取得的所有进展。@VillatSo我删除了系统。退出(0);行,它确实返回到检查点。但问题是,当我选择其他选项(保存人员)时,即使在