Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using a nested for loop to create a grid of rectangles in Pygame, why isn't it drawing correctly?

I’m trying to create the grid of 5×5 rectangles from part 4 of the Pygame lab here. I’ve created my loop which seems to make logical sense, and the coordinates update correctly while debugging, but when it draws to the screen it comes out in a strange pattern like this:

enter image description here

Here is the loop I’m using:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for x in range(0, 100, 10):
    for y in range(0, 100, 10):
        pygame.draw.rect(screen, GREEN, [(x, y),(x + 5, y + 5)], 1)

I’m obviously doing something stupid here, but can’t figure out what I’m doing wrong. I’ve made the width 1 to help with troubleshooting the issue, that will be removed once it’s fixed to make the rectangles solid.

>Solution :

A pygame rectangle is specified by the position and size. The size of the rectangle is always (5, 5) instead of (x + 5, y + 5):

pygame.draw.rect(screen, GREEN, [(x, y),(x + 5, y + 5)], 1)

pygame.draw.rect(screen, GREEN, [(x, y), (5, 5)], 1)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading