我需要创建一个CLI游戏吗?任何有用的资源?

时间:2011-02-25 21:26:53

标签: command-line ncurses

所以我花了一些时间,并决定在我上学的时候做一个小型游戏作为旁边项目。

我想做一个命令行游戏,也许某种RPG或泥。 我的问题是,我需要做些什么来实现这一目标?

我看过ncurses,并且已经阅读了一些文档,这是必要的步骤,还是有更容易的东西?与常规命令行应用程序相比,我还需要其他任何特定技术吗?我可能会将它作为学校吗?

感谢。

1 个答案:

答案 0 :(得分:1)

你需要两件事:

  1. 游戏引擎
  2. 界面
  3. 这与GUI游戏所需的完全相同。唯一的唯一区别是您的输入和输出机制。

    因此,编写您的游戏引擎,确保它具有适应性,然后使用命令行界面来控制该引擎。

    =========关于评论的编辑:=========

    任何界面的目标都是将用户请求映射到引擎请求。在不知道你所使用的语言的情况下,细节会有点......非......非特定的。

    我可以告诉你我将如何用Java做这件事。

    我将创建一个具有以下基本特征的UserRequest类:

    抽象类UserRequest {

    protected GameEngine engine;
    protected String command;
    protected int numArgs;
    
    public UserRequest(GameEngine engine) { 
        this.engine = engine; 
        this.command= command; 
        this.numArgs= numArgs; 
    }
    
    public abstract void callback(User user, String[] args);
    
    protected void checkArgs(String[] args) {
        if(args == null || args.length != numArgs) {
            throw new IllegalArgumentException("your args suck...");
        }
        if(!args[0].equals(command)) throw new IllegalArgumentException("commands don't match");
    }
    

    }

    在我的界面中,我会有一个Map<String,UserRequest>,其填充方式如下:

    Map<String,UserRequest> behaviors = new HashMap<String,UserRequest>();
    behaviors.add("MOVE",new UserRequest(engine,"MOVE",2) {
        // define the engine callback
        public void callback(User user, String[] args) {
            // assume args[0] is command
            int x = Integer.parseInt(args[1]);
            int y = Integer.parseInt(args[2]);
            engine.move(user,x,y);
        }
    }
    
    public void repl() {
        while(true) {
            // assume you have a form of input - scanner perhaps?
            String inputline = acquireNextInputLine();
            String[] tokens = inputline.split("\\s"); // default split on white space
            UserRequest behavior = behavior.get(tokens[0]);
            if(tokens == null) displayError(inputLine);
            try { behavior.callback(currentUser,tokens); }
            catch(Exception e) displayError(e); // assume you have better error reporting than this!
        }
    }