如何在基于文本的游戏Java中使用while循环?

时间:2017-07-21 18:39:40

标签: java loops while-loop logic game-engine

所以首先只是一些文本显示我的思维过程在代码应该如何工作的方式。我是一般的编程新手,几天前就开始学习java了。我想我对oop如何工作有一个基本的了解,但我无法在我的代码中很好地实现它。使用while循环对我来说有点意义,所以我只是从那些开始; D

我想要的东西:

包含3个选项的主菜单

菜单(营地)

  1. 任务(激活任务循环)

  2. 前往Town(激活城镇循环)

  3. 退出游戏(退出程序)

  4. 任务     打击随机怪物和获得金币/得分的游戏循环     “怪物出现”

    1. 打怪物         对怪物造成伤害         怪物会伤害回来         回到主循环

    2. 使用项目         选择项目         使用项目         项目效果应用         回到主循环

    3. 运行 返回主菜单(激活主菜单)

    4. 镇 允许你在“装备”上花金来增加健康和损害价值

      “我是铁匠等等等等。”

      1. 升级武器(增加伤害)
      2. 升级护甲(增加健康)
      3. 离开Town(将玩家返回主菜单)
      4. 游戏结束时玩家死亡或选择退出游戏

        显示分数并感谢玩家玩游戏

        下面只是逻辑的原型

        我觉得这应该可行,但每次运行时, 它不能正常工作,最终只能做一个 无限循环。我希望你们其中一个人能够 看看为什么它不起作用,有点引导我在右边 方向。任何事情都会非常感激!

        还有关于营地,地下城,城镇的那些布尔的任何评论? 我不知道我是否真的需要那些,而且可能只是 一些额外无用的代码,但我真的不确定。

        import java.util.Scanner;
        
        
        public class logic
        {
            public static void main(String[] args)
            {
        
            boolean running = true;
            Scanner in = new Scanner(System.in);
            System.out.println("This is a test");
        
            while(running)
            {
                boolean camp = true;
                boolean dungeon = false;
                boolean town = false;
                String input = in.nextLine();
                while(camp)
                {
                    System.out.println("what u do?");
                    System.out.println("1. go dungeon");
                    System.out.println("2. go town");
                    System.out.println("3. quit");
                    if (input.equals("1"))
                    {
                        dungeon = true;
                        while(dungeon)
                        {
                            System.out.println("you are in the dungeon");
                            dungeon = false;
                            break;
        
                        }
        
        
        
                    }
                    else if (input.equals("2"))
                    {
                        dungeon = false;
                        town = true;
                        while(town)
                        {
                            System.out.println("you are in the town");
                            town = false;
                            break;
        
                        }
        
        
                    }
                    else if (input.equals("3"))
                    {
                        break;
        
        
                    }
                    else
                    {
                        System.out.println("invalid command!");
        
        
                    }
        
        
                }
        
        
            }
        
            }
        
        }
        

2 个答案:

答案 0 :(得分:0)

就个人而言,我不会在循环中使用这么多,而是将每个部分的逻辑放入其自己的函数中,然后使用if-else-if / switch语句来决定应该调用哪个函数并执行在一个while循环中,遗憾的是我无法对此进行测试,因为我没有得到Java环境,因为我使用Java已经有一段时间了。

答案 1 :(得分:0)

这是我提出的新代码。我能够弄清楚如何将所有不同的区域分解成他们自己的方法。这应该会让游戏世界变得更加容易:D

import java.util.Scanner;


public class LogicTest
{
    Scanner in = new Scanner(System.in);

    public static void main(String[] args)
    {
        LogicTest game;
        game = new LogicTest(); // a new instance of the public class folder has to be created in order to use all the other methods

        game.run();


    }

    public void run() // this method is used to run the game from the main method
        {
            camp();
        }

    public void quest() 
    {
        System.out.println("you go on quest");
        camp();
    }
    public void town()
    {
        System.out.println("you go to town");
        camp();
    }
    public void camp() // this method acts as the game hub where the player can choose what they want to do next
    {
        System.out.println("you are in your camp");
        System.out.println("----------------------");
        System.out.println("what do you want to do?");
        System.out.println("1. go quest");
        System.out.println("2. go town");
        System.out.println("3. quit");


        String input = in.nextLine(); 
        // each one of these options just calls its respective method
                                    // the methods are what have the menus and everything for each location
        if (input.equals("1"))
        {
            quest();
        }
        else if (input.equals("2"))
        {
            town();
        }
        else if(input.equals("3")) // by leaving this empty the program has nothing else to read with this option and so it closes
        {

        }
        else 
        {
            camp();
        }
    }



}