10

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?

  • This doesn't answer your question, but why not just use [PyGame](http://www.pygame.org/news.html)? PyGame makes it very to create draw 2d sprite based games. – Corey Sep 20 '08 at 02:51
  • This [previous StackOverflow question](https://stackoverflow.com/questions/43267/good-resources-for-writing-console-style-applications-for-windows) should give you some more useful information. – Ash Sep 20 '08 at 02:51

6 Answers6

7

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

twasbrillig
  • 17,084
  • 9
  • 43
  • 67
Paolo B.
  • 969
  • 1
  • 7
  • 6
6

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

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

user3666197
  • 1
  • 6
  • 50
  • 92
matt b
  • 138,234
  • 66
  • 282
  • 345
  • 2
    Do not forget to specify that this does not work under Windows. – sorin Aug 25 '09 at 16:58
  • 1
    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 Aug 25 '09 at 18:32
4

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.

twasbrillig
  • 17,084
  • 9
  • 43
  • 67
Enno
  • 1,736
  • 17
  • 32
4

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.

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

Pac man

Peter Brittain
  • 13,489
  • 3
  • 41
  • 57
1

You can use curses.

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

twasbrillig
  • 17,084
  • 9
  • 43
  • 67
nosklo
  • 217,122
  • 57
  • 293
  • 297
0

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
  • 951,095
  • 183
  • 1,149
  • 1,285