RocktheFries
RocktheFries

Reputation: 221

How do I draw rectangles using recursion to make a table in pygame?

I'm trying to create an excel document look alike using recursion in pygame. I got the first if statement to fill the top row of the screen, and looking for it to go down by 50 (height of my rectangle) each time and keep going until it hits the edge of my screen again, filling the screen completely. I did another for loop to try this but it stops and misses one rectangle at (0,0), is there any way to do this in one loop so the screen will fill and make a bunch of columns and rows? Thanks.

 """
    Recursively draw rectangles.
    Sample Python/Pygame Programs
    Simpson College Computer Science
    http://programarcadegames.com/
    http://simpson.edu/computer-science/
    """
    import pygame
    # Colors
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    def recursive_draw(x, y, width, height):
        """ Recursive rectangle function. """
        pygame.draw.rect(screen, BLACK,
        [x, y, width, height],
        1)
        # Is the rectangle wide enough to draw again?
        if(x < 750):
            # Scale down
            x += 150
            y = 0
            width = 150
            height = 50
            # Recursively draw again
            recursive_draw(x, y, width, height)
        if (x < 750):
            # Scale down
            x += 0
            y += 50
            width = 150
            height = 50
            # Recursively draw again
            recursive_draw(x, y, width, height)
    pygame.init()
    # Set the height and width of the screen
    size = [750, 500]
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption("My Game")
    # Loop until the user clicks the close button.
    done = False
    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()
    # -------- Main Program Loop -----------
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
        # Set the screen background
        screen.fill(WHITE)
        # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
        recursive_draw(0, 0, 150, 50)
        # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
        # Go ahead and update the screen with what we've drawn.
        pygame.display.flip()
        # Limit to 60 frames per second
        clock.tick(60)
        # Be IDLE friendly. If you forget this line, the program will 'hang'
        # on exit.
    pygame.quit()

Upvotes: 2

Views: 1853

Answers (1)

skrx
skrx

Reputation: 20448

I'd first add a base case so that the function returns when the bottom of the screen is reached. Add the width to x until the right side is reached and when it is there, increment y += height and reset x = 0 to start drawing the next row.

import pygame


BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

def recursive_draw(x, y, width, height):
    """Recursive rectangle function."""
    pygame.draw.rect(screen, BLACK, [x, y, width, height], 1)
    if y >= 500:  # Screen bottom reached.
        return
    # Is the rectangle wide enough to draw again?
    elif x < 750-width:  # Right screen edge not reached.
        x += width
        # Recursively draw again.
        recursive_draw(x, y, width, height)
    else:
        # Increment y and reset x to 0 and start drawing the next row.
        x = 0
        y += height
        recursive_draw(x, y, width, height)


pygame.init()
size = [750, 500]
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
screen.fill(WHITE)
recursive_draw(0, 0, 150, 50)

done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    pygame.display.flip()
    clock.tick(60)


pygame.quit()

It would be easier to use nested for loops to draw the grid:

def draw_grid(x, y, width, height, size):
    for y in range(0, size[1], height):
        for x in range(0, size[0], width):
            pygame.draw.rect(screen, BLACK, [x, y, width, height], 1)

Upvotes: 3

Related Questions