0

Ive been trying to make a recursive rectangles and I wanted to make the rectangles move in the forward direction like each time it recursed, so that it gives a motion as one is going into an endless rectangles. Ive tried making the size bigger each time it recursed but failed as it wont recurse or nothing would show up. Any tips or how to do this would be appreciated. This sample I implemented from pygamearcade. I want to get the feeling as one is going into the rectangles and that can be implemented as the rectangles get bigger each time it goes through recurion. So any tips or how to do it is fine. Thank you

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, WHITE,
                 [x, y, width, height],
                 1)
speed = [10,0]

# Is the rectangle wide enough to draw again?
while (width > 14):
    # Scale down
                    x += width * .1
                    y += height * .1
                    width *= .8
                    height *= .8

    # Recursively draw again
                    recursive_draw(x, y, width, height)

pygame.init()
#rectanglelist = [big()] 
# Set the height and width of the screen
size = [700, 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(BLACK)

    # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
    recursive_draw(0, 0, 700, 500)
    # 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()
Anim3boy
  • 61
  • 1
  • 1
  • 6
  • 1
    What are *recursive rectangles*? It seems you just want to draw a rectangle that grows in size over time, right? – sloth May 27 '15 at 14:04
  • can you post a video of the final output or something? what are you asking for is really unclear, but i can see some NameErrors.. – Liam May 27 '15 at 14:24
  • Yes sloth is right like the rectangles show seem like its moving by growing in size – Anim3boy May 27 '15 at 17:14
  • @Anim3boy: Does it work now? :) – elegent May 29 '15 at 15:56
  • Sorry it does not, only 3 corners gets enlarged – Anim3boy Jun 01 '15 at 17:43
  • @Anim3boy: I updated my answer... :) – elegent Jun 04 '15 at 09:17
  • I have the exact same thing but in black and white, what I need is that each time it goes through recursion the rectangle needs to get bigger as it would give the feeling of one going into the rectangle. Basically a jet trying to go into the rectangles – Anim3boy Jun 04 '15 at 17:16

1 Answers1

0

The problem is that your recursive_draw() function is not really a recursive function, because a recursive function is a function that conditionally calls itself:

Every properly designed recursive function must have at least one base case [A] and must redefine the problem into sub problems that work towards a base case [B].

def countdown(n):
   if n < 1:
      print "Lift Off"   #[A]
   else:
      print n
      countdown(n - 1)   #[B]

What you could do for your code:

The updated function (code from http://www.balloonbuilding.com/ by Paul Vincent Craven):

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(width > 14):
        # Scale down
        x += width * .1
        y += height * .1
        width *= .8
        height *= .8

        # Recursively draw again
        recursive_draw(x, y, width, height)

I hope this helps :)


EDIT:

The updated program:

import pygame

# Colors
BLUE = (55, 155, 255)
WHITE = (255, 255, 255)


def recursive_draw(x, y, width, height):
    """ Recursive rectangle function. """
    pygame.draw.rect(screen, WHITE, (x, y, width, height), 2)

    # Is the rectangle wide enough to draw again?
    if(width > 14):
        # Scale down
        x += width * .1
        y += height * .1
        width *= .8
        height *= .8

        # Recursively draw again
        recursive_draw(x, y, width, height)

speed = [10,0]

pygame.init()
#rectanglelist = [big()] 
# Set the height and width of the screen
size = [700, 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(BLUE)

    # ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
    recursive_draw(0, 0, 700, 500)
    # 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()

Screenshot:

recursive_draw_screenshot

Community
  • 1
  • 1
elegent
  • 3,857
  • 3
  • 23
  • 36