What is the best way to draw in the console?

I'm trying to write a console (as in the terminal, not in the game console) a python pong game, and it's hard for me to figure out how best to (re) play the game.

I thought that a 2d array is a kind of bitmap, editing an array to reflect the ball / paddles new positions, and then throwing each line into a line and printing it. However, this means that the old β€œframes” will remain, and if the game is smaller than the console window, the old frames will still be visible.

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

Or is there a simpler alternative way to output such an application to the console?

+6
python stdout console
source share
7 answers

There seems to be a curses port / library for Python:

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

+6
source share

There are actually two libraries that solve this problem, the older curses and the new S-Lang . Curses tends to create linear art buggies, especially on Windows and on unicode consoles (Unicode support is crap). S-Lang screen management functions better.

So far I have not used any of them in Python, and it seems that curses are better supported, in C, at least I will switch my code to S-Lang because of these problems, but because in my heart I never liked the API curses.

+3
source share

Try urwid . One example bundled with urwid is a simulator for animated histograms . Histograms clear the screen well, leaving no artifacts of the old "frame".

+2
source share

You can use curses.

It has a Windows port and a Unix Port and lots of documentation . You can also use helper libraries .

+1
source share

I would explore using the curses module. He will take care of many details and allow you to focus on higher-level materials.

0
source share

This fooobar.com/questions/562132 / ... should provide you with more useful information.

0
source share

I recently developed an ASCII animation package ( https://github.com/peterbrittain/asciimatics ) that ran into similar issues. Although he does not have everything you need to write a game, it should give you most of what you want.

In particular, the Sprite class will help you deal with redrawing issues. There are many examples that will help you deal with various ways to use them and other effects in the package. Here is a small demonstration that I have collected as a tribute to one of my favorite games of yesteryear ...

Pac man

0
source share

All Articles