I've seen some of the matrix display demos using bouncing balls, and thought it would be fun to create a bounce / pong game using a potentiometer as a controller. It's written so it speeds up as the game progresses. It's very basic just now but I plan to add a score at the end of each play, and to have a high score displayed too. Here is a short video of the game in action:
```cpp
#include "Arduino_LED_Matrix.h"
ArduinoLEDMatrix matrix;
void setup() {
Serial.begin(115200);
matrix.begin();
}
uint8_t frame[8][12] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
uint8_t lose[8][12] = {
{ 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 }
};
int y = 4; // starting y position
int x = 4; // starting x position
int b = 1; // starting y direction
int a = 1; // starting x direction
int anticipate = 5; // variable used to anticipate whether ball hits bat
int speed = 300; // initial delay which decreases each time ball hit
void loop() {
int bat = (analogRead(A0) / 128);
frame[bat][11] = 1;
frame[bat + 1][11] = 1;
frame[y][x] = 1;
matrix.renderBitmap(frame, 8, 12);
delay(speed);
frame[bat][11] = 0;
frame[bat + 1][11] = 0;
frame[y][x] = 0;
matrix.renderBitmap(frame, 8, 12);
delay(1);
if (y == 7) {
b = -b;
}
if (y == 0) {
b = -b;
}
if (x == 10) { // when ball reaches line in front of bat, checks if bat hit
anticipate = (y + b);
speed = (speed-20); // reduces delay each time bat hit
if ((anticipate == bat) or (anticipate == (bat + 1))) {
a = -a;
}
else { // game over grid
matrix.renderBitmap(lose, 8, 12);
delay(2000);
x = 4;
y = 4;
a = -1;
b = -1;
speed = 300;
}
}
if (x == 0) {
a = -a;
}
y = (y + b); // adjusts ball position
x = (x + a);
}