python 如何最好地在控制台中绘制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/106850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:30:12  来源:igfitidea点击:

How best to draw in the console?

pythonconsolestdout

提问by

I'm trying to write a console (as in terminal, not gaming console) pong game in python and I'm having trouble figuring how best to (re)draw the game.

我正在尝试用 python 编写一个控制台(如在终端中,而不是在游戏控制台中)乒乓球游戏,但我无法确定如何最好地(重新)绘制游戏。

I was thinking of having an 2d array as a sort of bitmap, editing the array to reflect the ball/paddles new positions and then casting each row to a string and printing it. However that means that the old "frames" will remain, and if the dimensions of the game are smaller than the console window, old frames will still be visible.

我正在考虑将二维数组作为一种位图,编辑数组以反映球/桨的新位置,然后将每一行转换为字符串并打印出来。然而,这意味着旧的“框架”将保留,如果游戏的尺寸小于控制台窗口,旧的框架仍然可见。

Is there a way to delete characters from the console? '\b' I've heard is unreliable.

有没有办法从控制台中删除字符?'\b' 我听说是不可靠的。

Or is there an easier alternative route to outputting to the console for this sort of app?

或者是否有更简单的替代路线来输出到此类应用程序的控制台?

回答by matt b

It looks like there is a cursesport/library for Python:

看起来Python有一个Curses端口/库:

https://docs.python.org/library/curses.html

https://docs.python.org/library/curses.html

回答by Paolo B.

Try urwid. One of the examples bundled with urwid is a simulator for animated bar graphs. The bar graphs clear the screen well, without leaving artifacts of the old "frame".

试试urwid。与 urwid 捆绑的示例之一是动画条形图模拟器。条形图可以很好地清除屏幕,而不会留下旧“框架”的伪影。

回答by Enno

There are actually twolibraries that solve this, the older cursesand the newer S-Lang. Curses has a tendency to make buggy line art, especially on Windows and on unicode consoles (it's unicode support is shit). S-Lang's screen management functionsare better.

实际上有两个库可以解决这个问题,旧的curses和新的S-Lang。Curses 倾向于制作有问题的线条艺术,尤其是在 Windows 和 unicode 控制台上(它的 unicode 支持很糟糕)。S-Lang 的屏幕管理功能更好。

While I haven't used either of them in Python, and it seems curses is better supported, in C at least I'm switching my code to S-Lang because of those issues, and because deep down I never really liked the curses API.

虽然我没有在 Python 中使用它们中的任何一个,而且似乎curses 得到了更好的支持,但至少在 C 中,由于这些问题,我将代码切换到 S-Lang,并且因为在内心深处我从来没有真正喜欢过 curses API .

回答by Peter Brittain

I have recently been developing an ASCII animation package (https://github.com/peterbrittain/asciimatics) which faced similar issues. While it doesn't have everything you need to write a game, it should give you most of what you want.

我最近一直在开发一个 ASCII 动画包(https://github.com/peterbrittain/asciimatics),它面临类似的问题。虽然它没有您编写游戏所需的一切,但它应该能满足您的大部分需求。

The Sprite class in particular will help you handle redrawing issues. There are plenty of samples to help you get to grips with various ways to use them and other effects in the package. Here's a little demo I put together as a tribute to one of my favourite games of yesteryear...

特别是 Sprite 类将帮助您处理重绘问题。有大量示例可帮助您掌握使用它们的各种方法以及包中的其他效果。这是我整理的一个小演示,以向我过去最喜欢的游戏之一致敬...

Pac man

吃豆人

回答by nosklo

You can use curses.

你可以使用诅咒。

It has a Windows Portand Unix Port, and plenty of documentation. You can also use some helper libs.

它有一个Windows 端口Unix 端口,以及大量的文档。您还可以使用一些辅助库

回答by Greg Hewgill

I would investigate using the cursesmodule. It will take care of a lot of the details and let you focus on the higher level stuff.

我会使用curses模块进行调查。它会处理很多细节,让你专注于更高层次的东西。