如何“设计”风格基于文本的刽子手游戏
我的问题很简单,刽子手游戏看起来像这样:
我正在以我自己的方式进行缩进觉得不是很好。
我只是有 var = "\t"
并在每次打印的开头添加它,这似乎不切实际且难以维护。
你还有其他办法吗?这一般是如何管理的?
提前致谢!
My question is simple, the hangman game looks like this:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题:
解决方案=抽象!:
更好的解决方案=模板:
problem:
solution = abstraction!:
better solution = templates:
模板不仅仅适用于 HTML。
Templates aren't just for HTML.
帮助函数是解决当前问题的快速解决方案:
对于更有趣的东西,有一个用于基于终端的“图形”类应用程序的库。它被称为“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:
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 (usingaddstr
). 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.我可能会修复“你的词看起来像这样”中的拼写错误。然后查看
printf
样式格式,如此处所述。I'd probably fix the typo in "You word looks like this". Then look at
printf
style formatting, as described here.