Using Pygame's pixArray to draw a square

99 views Asked by At

I'm currently creating a drawing program using pygame. Ti draw, I'm using a pixarray. I want the drawing size to be changeable, so I set up my pixarray like this:

pixArray[p1+i][p2+i] = mousecolor
pixArray[p1-i][p2+i] = mousecolor
pixArray[p1+i][p2-i] = mousecolor
pixArray[p1-i][p2-i] = mousecolor

where i is coming from

    for i in range(0,size):

Currently, this draws the pixels in a x shape. How do I make it draw a square?

1

There are 1 answers

0
hajtos On BEST ANSWER

To get a square you need to loop over 2 dimensions, for example like this:

for i in range(0, size):
    for j in range(0, size):
        pixArray[p1+i][p2+j] = mousecolor
        pixArray[p1-i][p2+j] = mousecolor
        pixArray[p1+i][p2-j] = mousecolor
        pixArray[p1-i][p2-j] = mousecolor

You can't make a quadratic number of points with one linear loop.