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

发布于 2024-11-09 17:29:02 字数 232 浏览 2 评论 0原文

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

在此处输入图像描述

我正在以我自己的方式进行缩进觉得不是很好。

我只是有 var = "\t" 并在每次打印的开头添加它,这似乎不切实际且难以维护。

你还有其他办法吗?这一般是如何管理的?

提前致谢!

My question is simple, the hangman game looks like this:

enter image description here

I'm doing the indentation in a way I don't think is very good.

I just have var = "\t" and add it at the begging of every print, this seem impractical and hard to maintain.

Would you have it any other way? How is this generally managed?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浅浅 2024-11-16 17:29:02

问题:

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

解决方案=抽象!:

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(...)
    )
)

problem:

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')

better solution = templates:

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(...)
    )
)
沫尐诺 2024-11-16 17:29:02

模板不仅仅适用于 HTML。

Templates aren't just for HTML.

醉南桥 2024-11-16 17:29:02

帮助函数是解决当前问题的快速解决方案:

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 提供了很多选项和多种操作模式。它学习起来并不快,因此只有当您计划使用基于终端的应用程序执行更多操作时才有意义。

A quick solution to your immediate problem is a helper function:

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

For more interesting stuff, there is a library for terminal-based "graphical"-like applications. It is called "curses", and is available in python too: http://docs.python.org/library/curses.html. There is a tutorial here http://docs.python.org/howto/curses.html, and you can find more online.

Curses makes it possible to do more interesting text-based UI without too much effort. To just replicate what you have, you would probably create a window with the correct size and position (using newwin), and then print text to it without any indent (using addstr). Curses offers a lot of options and several modes of operation. It's not quick to learn, so only makes sense if you plan to do more with terminal based applications.

半世晨晓 2024-11-16 17:29:02

我可能会修复“你的词看起来像这样”中的拼写错误。然后查看 printf 样式格式,如此处所述。

I'd probably fix the typo in "You word looks like this". Then look at printf style formatting, as described here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文