Uno R4 Matrix bounce game

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:

Video of game


```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);
}
6 Likes

4 Likes

cool game - thanks for sharing this code

1 Like

Fun little game. I was looking for something to try with my new Arduino UNO R4 WiFi and this worked great. Thank you.

1 Like

Looks great - DOES NOT compile on my IDE (latest Arduino) - just stalls with an error message

ooooppps - just realised ; remove ' ' ' cp

:stuck_out_tongue:

2 Likes

This is awesome! Thanks for sharing. Before I saw this, I implemented an almost identical game (one-sided pong) but used an LIS3DH accelerometer breakout board (via QWIIC connector) to control the paddle. It is a good demo to "play" with the R4 LED matrix.

1 Like

KD5VMF/Arduino-UNO-R4-Wifi (github.com)

Some code for this and other boards. I use GPT to get things done and I have done a lot.