2017-02-25 83 views
-1

我对python非常陌生,并试图制作经典文本冒险。目前我的基本游戏只是在命令提示符下播放。 我如何去制作一个简单的用户界面,:在Python中为文字冒险制作用户界面的最佳方式

  1. 打印在白色背景上
  2. 在一个大的黑色字体游戏文本已在它收集将被 interpretted输入底部的输入框。 (就像命令提示符一样)

本质上类似于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() 

回答

0

尝试使用PyQt4,有利于年初GUI,那么你可以移植到PyQt5容易。 的simpe Hello World程序的示例: 进口SYS 从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

希望它能帮助。

+0

嗯看起来很有趣,我看看吧!每次他们输入命令时,我会如何将新的文本打印到屏幕上? –

+0

我现在无法解释你。您可以创建按钮作为选项,因为它可以帮助用户只需点击而不是输入整个选项。首先你必须学习PyQt4的基础知识,然后你可以通过谷歌获得帮助来获得基本的东西。 :) 如果您喜欢,请将答案标记为正确。 –

相关问题