1
votes

I'm trying to draw a series of rectangles given a coordinate, which is simply a list of x and y values.

Here's my code:

import pygame

pygame.init()

screen = pygame.display.set_mode((400, 400))

x = [5, 5, 4, 5]
y = [2, 3, 4, 4]


def draw(x, y):
    block = 30

    coord = zip(x, y)

    for _x, _y in coord:

        _x1 = _x * block
        _y1 = _y * block
        x2 = _x1 + block
        y2 = _y1 + block

        pygame.draw.rect(screen, 0xffffff, (_x1, _y1, x2, y2))


running = True

while running:

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            running = False
    draw(x, y)
    pygame.display.flip()

pygame.quit()
quit()

This code should draw a shape that resembles this:

 0
 0
00

Instead, it's drawing something without coherent shape.

1

1 Answers

1
votes

change this line:

pygame.draw.rect(screen, 0xffffff, (_x1, _y1, x2, y2))

to this:

pygame.draw.rect(screen, 0xffffff, (_x1, _y1, block, block))

draw.rect takes rectangle as an argument which specified by (x, y, width, height)

for _x1 = 5, _y1 =2 if you do (_x1, _y1, x2, y2) it means (x=150, y=60, width=180, height=90)