如何使用 pygame 制作游戏内命令控制台



我正在使用python和pygame制作一个小型的2d overveiw RPG。现在我正在创建 GUI(用于与 NPC 交谈和键入命令的文本输入)。

我想这样做,以便我可以在玩游戏时输入命令。我已经弄清楚了如何获取文本输入,但是如何解析输入以查看它是命令还是其他内容?

  • 如何分析输入以查看它是否是逗号?
  • 如果是命令,如何运行它?

示例:spawn mob 000001 这应该会生成一个妖精。

编辑:如何将控制台插入 pyGame 窗口?这看起来与我的问题相似,但它没有回答我的问题。

一个非常简单的例子来说明如何做到这一点:

def parse(user_input):
    words = user_input.split()
    command = words[0]        # first word is the command
    parameters = words[1:]    # the rest are parameters for the command
    if command == 'spawn':
        spawn(*parameters)
    elif command == 'foo':
        foo(*parameters)
    # etc.
    else:
        print('Command not recognised')
def spawn(type_, number):
    print('spawning', type_, number)
parse('spawn mob 000001')

最新更新