在Python中为文本冒险制作UI的最佳方法

时间:2017-02-25 08:57:43

标签: python user-interface text

我是python的新手并试图制作经典的文本冒险。目前我的基本游戏只是在命令提示符下播放。 我将如何制作一个简单的用户界面:

  1. 在白色背景上以大黑字体打印游戏文字
  2. 底部有一个输入框,用于收集要输入的内容 interpretted。 (与命令提示符一样)
  3. 基本上是与Zork类似的UI。 我尝试使用tkinter,但最终我感到困惑和极其低效。此外,如果您想知道,我不想只使用命令提示符,因为文本非常小而且我很难阅读。

    这是主要的游戏代码,如果有帮助:

    from player import Player
    
    #intro screen
    def intro():
        print('Incarnation')
        print('''
        Welcome to Incarnation, please type an option:
        - Play
        - Load
        - Instructions
        - Credits
        - Quit
        ''')
        option = input('>').lower()
        if option == 'play':
            play()
        elif option == 'instructions':
    
            print("""
            Objective: The objective of the game is to complete the narrative by exploring
            the world, collecting items, defeating enemies and solving puzzles.
            You control your character by typing commands.
            Here are some essential commands, make sure to experiment! They are not case sensitive.
            N, S, E, W: Move your character in the cardinal directions
            I: Open your inventory
            H: Heal with an item in your inventory
            A *item*: Attack with an item. Replace *item* with the name of the item.
            T *NPC*: Talk to a present NPC. Repalce *NPC* with the name of the person.
            """)
        elif option == 'credits':
            print('made by Lilian Wang')
        elif option == 'load':
            pass
        elif option == 'quit':
            quit()
        else:
            print("That's not an option.")
    
    player = Player()
    
    # Possible player actions
    def actions(action):
        if action == 'n':
            player.move_north()
        elif action == 's':
            player.move_south()
        elif action == 'e':
            player.move_east()
        elif action == 'w':
            player.move_west()
        elif 'heal' in str(action):
            player.heal(action)
        else:
            print("You can't do that.")
            player.previousLocation = player.location
    
    
    # Main game function
    def play():
        print(player.location.name)
        while player.gameover == False:
            if player.previousLocation != player.location:
                print(player.location.name)
            action = input(">")
            actions(action)
    
    intro()
    

1 个答案:

答案 0 :(得分:0)

尝试使用PyQt4,非常适合从GUI开始,然后您可以轻松地移植到PyQt5。 simpe Hello World计划的一个例子: 导入系统 来自PyQt4导入QtGui

def window():
   app = QtGui.QApplication(sys.argv)
   w = QtGui.QWidget()
   b = QtGui.QLabel(w)
   b.setText("Hello World!")
   w.setGeometry(100,100,200,50)
   b.move(50,20)
   w.setWindowTitle("PyQt")
   w.show()
   sys.exit(app.exec_())

if __name__ == '__main__':
   window() 

输出:

enter image description here

PyQt4是GUI设计的最佳模块,非常容易学习。我最近正在使用PyQt4制作的潜在高峰:

enter image description here

您可以开始学习PyQt4 here

希望它有所帮助。