In this project, and we'll create a rectangular pulsating effect on the display.

The pulsating rectangle will consist of 4 individual rectangles nested within each other. The smallest rectangle will simply be a 2x2 square of LEDs at the center of the display. The largest rectangle will sit at the outer edge of the display:

    FFFFFFFF
    FBBBBBBF
    FB7777BF
    FB7337BF
    FB7337BF
    FB7777BF
    FBBBBBBF
    FFFFFFFF

By slowly changing the color intensity of the rectangles over time, we can generate a pulsating effect. Each rectangle will have a slightly different color than the one next to it.

Your breadboard should still have the circuit we wired in the project. As a reminder, here is what it should look like:

The code for this project will do the following:

- Initialize a list of color intensities for the four rectangles we plan to draw (with each one successively brighter)

- In a loop, for each of the four rectangles, draw the rectangle with the assigned color intensity

- Each time through the loop, increase the intensity of each rectangle (and reset to intensity 0 if it goes above the maximum threshold of 15)

Because each of the rectangles is increasing in brightness in a fixed pattern, it will appear that the rectangles are animated on the LED Matrix. Here is what the code should look like:

from rstem.led_matrix import FrameBuffer import time # Initialize the game environment fb = FrameBuffer() colors = [0, 2, 4, 6] while True: fb.erase() fb.rect((0,0), (8,8), color=colors[0]) colors[0] = colors[0] + 2 if colors[0] > 15: colors[0] = 0 fb.rect((1,1), (6,6), color=colors[1]) colors[1] = colors[1] + 2 if colors[1] > 15: colors[1] = 0 fb.rect((2,2), (4,4), color=colors[2]) colors[2] = colors[2] + 2 if colors[2] > 15: colors[2] = 0 fb.rect((3,3), (2,2), color=colors[3]) colors[3] = colors[3] + 2 if colors[3] > 15: colors[3] = 0 fb.show() time.sleep(.5)

Give it a try. You should see a series of pulsating rectangles.

1. The four rectangles are individually drawn on the screen with very similar code. That is, the code on lines 10-13 is repeated almost identically 4 times, once for each rectangle. Can you instead modify this code to use a `for` loop that creates these four rectangles?

2. Can you add a button that, for each time it is pressed, increases the speed that the rectangles pulsate?

3. Can you create a similar pulsating effect with 8 vertical lines across the screen?

4. Can you create a similar pulsating effect with diagonal lines? Note: when using diagonal lines, there are 15 lines, of different lengths.