开发者

What do i need to create a CLI game? Any useful resources?

开发者 https://www.devze.com 2023-02-14 01:11 出处:网络
So have spent some time, and have decided to make a small game as a sideproject while I\'m in school.

So have spent some time, and have decided to make a small game as a sideproject while I'm in school.

I'd like to make a command line game, perhaps some kind of rpg or mud. My question is, what do I need to make this a reality?

I've looked at ncurses, and have been reading some documentation, is this a necessary step, or is there something easier? Are there any other particular technologies I will need compared to a regular command line application I might make it schoo开发者_如何学JAVAl?

Thanks.


You need two things:

  1. A game engine
  2. An interface

This is the exact same thing you need for a GUI game. The only only ONLY differences are your input and output mechanism.

So, write your game engine, make sure it's adaptable, and then your command line interface to control that engine.

========= Edit in regards to comments: =========

The goal of any interface is to map user requests to engine requests. Without knowing what language you're in, the specifics are going to be a bit... well ... nonspecific.

I can tell you how I would do it in Java.

I would create a UserRequest class with the following basic characteristics:

abstract class 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");
}

}

In my interface, I would have a Map<String,UserRequest> that is populated like this:

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!
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消