Step 7: The Program
The program has an array that stores the location of the ball and paddles as bytes. Each row of bytes is easily displayed because there are 8 bits in a byte and 8 LEDs per row. The library for the LED driver ICs has a function to display rows of LEDs as binary bytes, so this makes things really easy. The "pong simulator" function I wrote will bit shift the ball around the screen correctly, carry the ball over to a new matrix when needed, and deflect the ball off of the edge of the screen when needed. The ball randomly starts off at a row near the middle of the screen, at a random position and direction.
The analog values from the controllers gets mapped to a value to control where the paddles are located on the screen.
The hardest thing for me was getting the correct reaction when the ball meets the paddle. I wrote a few pages in a notebook coming up with the different scenarios and what I call "special bits" where the ball would need a different bounce off the paddle than normal. This might include bouncing off the wall or carrying over to another matrix. Some stuff I came up with I later found to be wrong or found that my other code would actually take care of it for me. There might still be a bug or two that I have to work out, but for right now I am pretty satisfied with its performance.
There are a few different modes I programmed in. There are two 2 player modes. The difference between them is the paddle width. There is one where the paddle is 3 LEDs wide, and another where the paddle is 2 LEDs wide (this one is obviously harder to play). The game keeps track of score and the first person to 5 points wins. The last mode is a practice, 1 player mode. The player can play against a "wall" of LEDs to practice. The modes are selected at the beginning of the program, when the game is turned on or restarted, by using one of the paddles. You have 3 seconds from after turning the game on or restarting it to choose what mode to play and then the game will begin. A display bar will show you what mode you are currently selecting during the 3 seconds.
Program explanation video:
How the ball bounces off the paddle: