3

I was wondering if I could draw an image using a list of ones and zeros where a 1 would mean

pygame.draw.rect(DISPLAY_SURF,(0,0,0), (0,0,10,10))

and 0 would mean a space. it would look something like this:

blockMap = [
    [0,1,1,0,0,0,1,1,0],
    [0,1,1,0,0,0,1,1,0],
    [0,0,0,0,0,0,0,0,0],
    [1,0,0,0,0,0,0,0,1],
    [0,1,1,1,1,1,1,1,0],
    [0,0,0,0,0,0,0,0,0]]

I've not tried I lot because I have looked all over the internet unsuccessfully and obviously could not assign an integer to a string.

blockMap = [
    [0,1,1,0,0,0,1,1,0],
    [0,1,1,0,0,0,1,1,0],
    [0,0,0,0,0,0,0,0,0],
    [1,0,0,0,0,0,0,0,1],
    [0,1,1,1,1,1,1,1,0],
    [0,0,0,0,0,0,0,0,0]]

in blockMap if 1:
    pygame.draw.rect(DISPLAY_SURF, (0,0,0), (0,0,10,10))
else:
    pygame.draw.rect(DISPLAY_SURF, ((BG_COLOUR)), (0,0,10,10))

This should be displaying a smiley face in black against a white background but definitely is not. I know this is totally wrong, I've been trying for ages and that was just a very desperate attempt that I was just hoping to work.

finefoot
  • 9,914
  • 7
  • 59
  • 102
John Mobey
  • 79
  • 1
  • 8
  • Take a look at this [answer](https://stackoverflow.com/questions/52948752/which-approach-for-making-cozmos-expressions-in-a-graphical-python-application/52950553#52950553) – sloth Jan 07 '19 at 07:05

1 Answers1

2

You are giving constant arguments to the drawing function. You need to change the coordinates according to the matrix.

for example:

square_size = 30 # example
for y, row in enumerate(blockMap):
    for x, val in enumerate(row):

        upper_left_point = x * square_size, y * square_size
        upper_right_point = (x + 1) * square_size, y * square_size
        lower_left_point = x * square_size, (y + 1) * square_size
        lower_right_point = (x + 1) * square_size, (y + 1) * square_size

        pointlist = [upper_left_point, 
                    upper_right_point,
                    lower_right_point,
                    lower_left_point]

        pygame.draw.polygon(Surface, color, pointlist, width=0)

This is a very basic example, so try fully understanding the code before using it. You will need to know these concepts before you actually start visualizing anything.

Don't forget to adjust the square_size's to the screen size.

Rockybilly
  • 2,938
  • 1
  • 13
  • 38