1

So I decided to make an array visualizer in pygame, and I decided to program the basic visualizer.

Here is the code which is arranging the rectangles, but it doesn't work:

#other code

list = list(range(129))
random.shuffle(list)

#other code

ended = False
while not ended:    
    
    #other code
    
    for i in range(len(list)):

        x1 = i*15
        y1 = 1080-list[i]*8

        x2 = 15+i*15
        y2 = 1080

        pygame.draw.rect(screen, (255, 255, 255), pygame.Rect(x1, y1, x2, y2))
    
    pygame.display.update()

Here is expectation vs reality

1 Answers1

1

pygame.Rect params are (left, top, width, height), not (left, top, right, bottom): https://www.pygame.org/docs/ref/rect.html#pygame.Rect

Try something like:

    for i in range(len(list)):

        x1 = i*15
        y1 = 1080-list[i]*8

        x2 = 15
        y2 = list[i]*8

x2 should be the width of your rect and y2 its height.

Welcome to StackOverflow btw :)

Beniamin H
  • 2,048
  • 1
  • 11
  • 17