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