views:

457

answers:

8

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.

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.

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

+4  A: 

It looks like there is a curses port/library for Python:

http://docs.python.org/lib/module-curses.html

matt b
Do not forget to specify that this does not work under Windows.
Sorin Sbarnea
What makes you say that? The python module page states "While curses is most widely used in the Unix environment, versions are available for DOS, OS/2, and possibly other systems as well".
matt b
A: 

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

Greg Hewgill
+2  A: 

This doesn't answer your question, but why not just use PyGame? PyGame makes it very to create draw 2d sprite based games.

Corey
A: 

This previous StackOverflow question should give you some more useful information.

Ash
A: 

Well I cannot add a comment so .. FYI, no curses implemented in Windows AFAIK...

wbkang
yes it is. http://adamv.com/dev/python/curses/
nosklo
+1  A: 

You can use curses.

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

nosklo
+2  A: 

There are actually two libraries that solve this, the older curses and 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 functions are better.

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.

Enno
+1  A: 

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".

Paolo B.