In this project, we're going to expand on what we did in the previous concept, , where we created a simple "game" that moves a dot across the LED Matrix display.

For this project, we're going to modify the game loop to test the location of the dot. When it reaches the edge of the display, we will reverse direction of the dot so that it never falls off the screen.

Here's a reminder of what the code should look like so far:

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

In the previous code, to move the dot to the right on the LED Matrix display, we increment (increase) x by 1. This works great...until we get to the edge of the screen and we don't want to go any further. At that point, we're going to want the dot to move in the OPPOSITE direction (in this case, to the left). To do that, instead of incrementing x by 1, we're going to want to decrement (decrease) x by 1.

In other words, to go to the right, we increase x by 1. To go to the left, we decrease x by 1 — this is actually the same as increasing x by -1.

We can do this by creating a variable (let's call it direction), setting that variable to either 1 or -1 (depending on the direction we want the dot to go) and then increasing x by that variable. When direction is set to 1, the ball will move right; when direction is set to -1, the ball will move left.

And, if we change the direction every time we get to the edge of the display (that is, when x gets to either 0 or 7), the dot will reverse direction.

This requires the following changes to the code (marked with arrows):

Give it a try. You should now see the dot bouncing back and forth on the display.

  1. Can you make the ball bounce vertically instead of horizontally?

  2. Can you make the ball bounce diagonally from the lower left corner (0,0) to the upper right corner (7,7)?

  3. Can you have the ball start bouncing back forth slowly (as it does now), but continually get faster each time it hits a wall?

  4. In the example, the ball bounces all the way from the left side of the display to the right side. Can you confine the ball to only bounce on the left half of the screen?