Graphics Programming Python

Python + Curses + Numpy -> Ascii Art Rotating Cube

Well, the title says it all, or at least most of it.
If you are still not convinced, a picture’s worth 500 dwords:

        / |\
       /  | \
      /   |  \
    //    |   \\
   /      |     \
  /       \      \
 |       / \      |
 |\     /   \    /|
 | \   /     \\ / |
 |  \ /       //  |
 |  /\       /  \ |
 | /  \\    /    \|
 |/     \  /      |
 /       \/       /
  \       |      /
   \      |     /
    \\    |   //
      \   |  /
       \  | /
        \  /

I’ve been toying with the idea of writing a roguelike, and I thought that it would be really fun if it had an ‘Eye-Of-The-Beholder’ kind of view as well, just in ascii-art. There are plenty of things I wanted to do with ascii-art, since it’s such a nice toy. So I started writing some code, and this is a small test on the way.

Nowadays, when I discuss 3d ascii-art, people usually think about a picture transformed with aalib or something similar. Although it is a (very) nice idea, I think it would be more fun to play with ascii-art directly.

Other playing possibilities include a ray-tracer. You could write some fps with ray-tracing ascii-art graphics, because there are so few pixels.
Maybe I’ll write one, when I get to it.

Oh! The code, what about the code?
Here it is.
A few notes:
1. The code ain’t too pretty, as it is a work in progress that doesn’t get much time invested in it. However, I’d still rather publish it.
2. It uses curses. Under windows you’ll have to get some replacement module.
3. It uses numpy. If you don’t have it already, be sure to get it.
4. Run ‘’. Use the direction keys to rotate the cube, ‘q’ to exit.

6 replies on “Python + Curses + Numpy -> Ascii Art Rotating Cube”

Cool! Very cool! I think QuakeII will look even better with this algorithm…

How can you play with ascii art directly using ray-tracing?

Well, ray-tracing will be more like the aalib approach. You would render pixels to a buffer, and render this buffer to ascii. An extreme case is one ray per character. Viewed in this light, any more rays may be considered anti-aliasing.

That’s a nice ass-cube! XD

By the way, I’m missing a rotational axis here – the screen-y-aligned lines always remain pointing in the same direction. Coincidence or hard to code?

(Added yours too btw)

I don’t understand if you’re referring to the picture here, or to the script.
The picture looks like it is just because I thought it was good looking.

If you look at the script, you’ll see it supports rotations around the 3 axes. However, the UI only allows you to rotate around X and Z. The actual reason I did it was because I wanted the UI to use just the four arrow keys.

One of the next things I’ll be writing for will be a camera-like view which will allow fps-like movement.

(By the way, the real issue with the picture is the distorted ratio. The script actually allows other ratios, but I just didn’t use a better one for the picture.)

To make this work on windows I downloaded WCurses, extracted it into python\Lib\site-packaces\curses, copied from python\Lib\curses to the WCurses folder, renamed python\Lib\curses to ccurses (or whatever) and it worked.

I too have been working on a rotating ASCII cube, but mine is for a 3D chess game. And mine cycles through the different rotations upon receiving a from the user. Also, mine is written in C++ rather than Python. You interested in seeing what I have so far?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.