Question

Can anyone tell me why num_chars and num_rows have to be the same?

from ctypes import *

num_chars = 8
num_rows = 8
num_cols = 6

buffer = create_string_buffer (num_chars*num_rows*num_cols+num_chars)

for char in range(num_chars):
        for row in range(num_rows):
                for col in range(num_cols):
                        if char == num_chars-1 and col == num_cols-1:
                                buffer[row*num_rows*num_cols+char*num_cols+col+row] = '|'
                                buffer[row*num_rows*num_cols+char*num_cols+col+row+1] = '\n'
                        elif col == num_cols-1:
                                buffer[row*num_rows*num_cols+char*num_cols+col+row] = '|'
                        else:
                                buffer[row*num_rows*num_cols+char*num_cols+col+row] = ('.', '*')[char>row]

print buffer.value

The output

.....|*****|*****|*****|*****|*****|*****|*****|
.....|.....|*****|*****|*****|*****|*****|*****|
.....|.....|.....|*****|*****|*****|*****|*****|
.....|.....|.....|.....|*****|*****|*****|*****|
.....|.....|.....|.....|.....|*****|*****|*****|
.....|.....|.....|.....|.....|.....|*****|*****|
.....|.....|.....|.....|.....|.....|.....|*****|
.....|.....|.....|.....|.....|.....|.....|.....|

And now changing num_chars to 15.

.....|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
*****|*****|*****|*****|*****|*****|*****|*****|
.....|*****|*****|*****|*****|*****|*****|*****|
Was it helpful?

Solution

You said you are using ctypes because you want mutable char buffer for this. But you can get the output you want from list comprehension

num_chars = 5
num_rows = 8
empty = ['.' * num_chars]
full = ['*' * num_chars]
print '\n'.join(
    '|'.join(empty * (i + 1) + (num_rows - i - 1) * full)
    for i in xrange(num_rows)
)

.....|*****|*****|*****|*****|*****|*****|*****
.....|.....|*****|*****|*****|*****|*****|*****
.....|.....|.....|*****|*****|*****|*****|*****
.....|.....|.....|.....|*****|*****|*****|*****
.....|.....|.....|.....|.....|*****|*****|*****
.....|.....|.....|.....|.....|.....|*****|*****
.....|.....|.....|.....|.....|.....|.....|*****
.....|.....|.....|.....|.....|.....|.....|.....

EDIT

I'll show you how you can use list comprehensions to draw whatever char bitmap you want to draw. The idea is simple. Build a boolean array with True in the places you want to print the character and False otherwise. And just use the 'or' trick to print the right character. This example will build a chess like board. You can use the same concept to draw any shape you want.

rows = 5
cols = 6
char = '#'
empty = '.'
bitmap = [[ (i + j)%2 == 0 for i in xrange(cols)] for j in xrange(rows)]
print '\n'.join(
    '|'.join(bitmap[j][i] * char or empty for i in xrange(cols))
    for j in xrange(rows)
)

OTHER TIPS

There we go. I had rownum_rows instead of rownum_chars I must need a Dr Pepper. And by the way, this wasn't homework. It's for an LCD project.

num_chars = 10
num_rows = 8
num_cols = 6

buffer = create_string_buffer (num_chars*num_rows*num_cols+num_chars)

for char in range(num_chars):
        for row in range(num_rows):
                for col in range(num_cols):
                        if char == num_chars-1 and col == num_cols-1:
                                buffer[row*num_chars*num_cols+char*num_cols+col+row] = '|'
                                buffer[row*num_chars*num_cols+char*num_cols+col+row+1] = '\n'
                        elif col == num_cols-1:
                                buffer[row*num_chars*num_cols+char*num_cols+col+row] = '|'
                        else:
                                buffer[row*num_chars*num_cols+char*num_cols+col+row] = ('.', '*')[char>row]

print repr(buffer.raw)
print buffer.value
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top