how to do draw pattern with python?

Hans Mulder hansmu at xs4all.nl
Sat Sep 22 15:34:58 EDT 2012


On 21/09/12 19:32:20, Ian Kelly wrote:
> On Fri, Sep 21, 2012 at 10:50 AM, Ismael Farfán <sulfurfff at gmail.com> wrote:
>> 2012/9/21 Peter Otten <__peter__ at web.de>:
>>> echo.hping at gmail.com wrote:
>>>
>>>     print "\x1b[2J\x1b[0;0H" # optional
>>
>> Nice code : )
>>
>> Could you dissect that weird string for us?
>>
>> It isn't returning the cursor to (0,0), it's just like executing
>> clear(1), and looks like those line coloring scape sequences for bash.
> 
> They're called "ANSI escape codes". :-)
> 
> CSI 2J clears the screen.
> CSI 0;0H means "move the cursor to row 0, column 0".  However, I don't
> think that's valid ANSI, as the coordinates are 1-based.  Probably it
> should have been "\x1b[2J\x1b[1;1H".

Yes, the coordinates are 1-base, so it should have been
"\x1b[2J\x1b[1;1H".  Or, since 1;1 is the default, "\x1b[2J\x1b[H".

On my machine, clear(1) uses "\x1b[H\x1b[2J".

Using clear(1) appears to be the most portable way to do it:

import os, time

data = """\
x....x
.x..x.
..xx..
..xx..
.x..x.
x....x

""".splitlines()

data = [line * 12 for line in data] # optional

try:
    while True:
	os.system("clear") # optional
	for i, line in enumerate(data):
	    print line
	    data[i] = line[1:] + line[:1]
	time.sleep(.1)
except KeyboardInterrupt:
    pass



Hope this helps,

-- HansM



More information about the Python-list mailing list