如何“设计”基于文本的刽子手游戏

时间:2011-05-27 04:08:04

标签: python terminal text-based

我的问题很简单,刽子手游戏看起来像这样:

enter image description here

我正在以一种我认为不太好的方式进行缩进。

我只有var = "\t"并在每次打印的乞讨时添加它,这看起来不切实际且难以维护。

你有其他方式吗?这通常如何管理?

提前致谢!

4 个答案:

答案 0 :(得分:1)

问题:

print('\tthis')
print('\tis')
print('\tan')
print('\texample')

solution = abstraction!:

def echo(string, indent=1):  # name it whatever you want, e.g. p
    print('\t'*indent + string)

echo('this')
echo('is')
echo('an')
echo('abstraction')

更好的解决方案=模板:

template = """
{partial}
 ______
{hangman}
|_________

Your points so far: {points}
You've entered (wrong): {wrong}

Choose a letter:
"""

print(
    template.format(
        partial=..., 
        hangman='\n'.join('|'+line for line in hangmanAscii(wrong=2).splitlines()), 
        points=..., 
        wrong=', '.join(...)
    )
)

答案 1 :(得分:0)

Templates不是HTML的

答案 2 :(得分:0)

我可能会在“你这个词看起来像这样”中修正错字。然后查看printf样式格式,如here所述。

答案 3 :(得分:0)

快速解决您的问题是辅助功能:

def put(text): print "\t" + text

对于更有趣的东西,有一个基于终端的“图形”类应用程序的库。它被称为“curses”,也可以在python中使用:http://docs.python.org/library/curses.html。这里有一个教程http://docs.python.org/howto/curses.html,您可以在线找到更多信息。

Curses可以毫不费力地创建更有趣的基于文本的UI。要复制你拥有的东西,你可能会创建一个具有正确大小和位置的窗口(使用newwin),然后在没有任何缩进的情况下打印文本(使用addstr)。 Curses提供了许多选项和几种操作模式。学习起来不是很快,所以只有在你计划用基于终端的应用程序做更多事情时才有意义。

相关问题