0

Alright, im working on a project in Python, which is my first REAL Python project. Now, my problem: Graphics. I thought the simplist way would be Dwarf Fortress style graphics, were each tile is an ASCII colors letter. Now, I have a way to do this, but I think it would not be very efficient. This is my code:

import os
import time
import random
clear = lambda: os.system('clear')

g = '\033[32m' + '# '
t = '\033[31m' + '^ '
l = [g, g, g, g, g, g,
     g, g, g, g, g, g,
     g, g, g, g, g, g,
     g, g, g, g, g, g,
     g, g, g, g, g, g,
     g, g, g, g, g, g]

def level():
    i = 0
    while i < 1000:
        print l[0] + l[1] + l[2] + l[3] + l[4] + l[5]
        print l[6] + l[7] + l[8] + l[9] + l[10] + l[11]
        print l[12] + l[13] + l[14] + l[15] + l[16] + l[17]
        print l[18] + l[19] + l[20] + l[21] + l[22] + l[23]
        print l[24] + l[25] + l[26] + l[27] + l[28] + l[29]
        print l[30] + l[31] + l[32] + l[33] + l[34] + l[35]
        i += 1

        for b in l:
            ch = round(random.random())
            if ch:
                l[l.index(b)] = g
            else:
                l[l.index(b)] = t       
        clear()
level()

I got the clear thing from somewhere here on stack overflow, but it shouldn't work on windows, considering on windows the clear command is 'clr' and on Mac and Linux, its 'clear'. Helpature?

EDIT - 'The problem is not the color, its the printing. Basically, how can I print it so I dont have to clear screen, how can I print it, and just change it, or something.

Spudley
  • 166,037
  • 39
  • 233
  • 307
Thor Correia
  • 1,528
  • 3
  • 23
  • 30

2 Answers2

3

As far as I'm aware all this kind of ASCII based terminal graphics manipulation stuff is usually done using the Curses library, so I recommend you try that.

The documentation states that "The curses library keeps two data structures, one representing the current physical screen contents and a virtual screen representing the desired next state. The doupdate() ground updates the physical screen to match the virtual screen." which seems to be exactly what you want. You just draw whatever you want into the virtual screen, and once you are ready, you call the curses.doupdate() function which updates the physical screen to match the virtual screen.

ZeroOne
  • 3,041
  • 3
  • 31
  • 52
0

ANSI Escape codes does not work on Windows unless you install some tool to enable it.

Since you're using Python, I found your solution right here at SO (question Print in terminal with colors using Python?)

Go to colorama: http://pypi.python.org/pypi/colorama . Just install it (it's pretty straightforward) and you're ready to go.

Community
  • 1
  • 1
Robson França
  • 661
  • 8
  • 15
  • No.... thats not what I ment, I ment I have the colors, thats not the problem, the problem is clear. Clear screen. But thanks anyways! – Thor Correia Apr 21 '12 at 22:22