Arduino Catch The Beat [8x8 LED Matrix]

Arduino Catch The Beat [8x8 LED Matrix]

This project consist on a simple catch the beat game on Arduino. Using a trimmer potentiometer and a 8x8 Matrix LED table.

  • 2,742 views
  • 3 comments
  • 3 respects

Components and supplies

Apps and online services

About this project

This project is inspired in OSU!Catch the beat and this game consist in catching the falling balls, you have 10 lives, you lose one for every ball you miss and you recover two for every ball you catch getting one point. Every four point the speed increases and you tun out of lives the game ends.

Arduino catch the beat gameplay

OSU!Catch the beat gameplay

How to do this project:

Step 1: Check Arduino connection.

Step 2: Copy and paste the code in new sketch of Arduino IDE.

Step 3: Conect the led matrix, buzzer and potentiometer to Arduino.

Step 4: Enjoy the game ^^

Code

Untitled fileC/C++
Copy and paste this code to do this project
int row[] = {2, 7, A5, 5, 13, A4, 12, A2};
int col[] = {6, 11, 10, 3, A3, 4, 8, 9};
int joystick = A0; //es un simple potenciometro
int lastUpdate;
int puntos = 0;
int vidas = 10;
int speakerPin = A1;
int speed = 16;
bool updatedAsteroid = false, updatedPlayer = true;

typedef struct coordenada {
  int x; //cordenada de la columna
  int y; //cordenada de la fila
}COORD;

COORD asteroid, jugador;

void setup() {
  // put your setup code here, to run once:
	for (int i = 0; i < 8; i++)
	{
		pinMode(row[i], OUTPUT);
		pinMode(col[i], OUTPUT);
		digitalWrite(col[i], HIGH);
		digitalWrite(row[i], LOW);
	}
  pinMode(speakerPin, OUTPUT);
  initGame();
  Serial.begin(9600);
}

void loop() {
	// put your main code here, to run repeatedly:
  for(int i = 0; i < 2; i++)
  {
    clearScreen(); //limpia la matriz de leds
    if(lastUpdate == 1) {
      movePlayer();
    }
    if(lastUpdate == 0) {
      asteroidFall();
      //Serial.print("El asteroide cambia de posicion!!\n");
    }
    lastUpdate = Update(); //actualiza la matriz de leds segun movePlayer() y asteroidFall()
    delay(speed);
  }
  puntuacion(); //calcula la puntuacion y aumenta una vida por punto, cuando el jugador se queda sin vidas se pasa a gameover() para reiniciar la partida
  if (asteroid.y == 8 || asteroid.y > 8) {
		initasteroid(); //inizializa un nuevo asteroid cuando el anterior ya ha caido
	}
}

void initGame() {
  vidas = 10;
  puntos = 0;
  speed = 16;
  initplayer();
  initasteroid();
  return;
}

void initplayer() {
	jugador.x = 3;
	jugador.y = 7;
	digitalWrite(col[jugador.y], HIGH);
	digitalWrite(row[jugador.x], LOW);
	return;
}

void initasteroid() {
	asteroid.x = random(0, 7);
	asteroid.y = -1; //para que asteroidFall() comienze en 0 nada mas ejecutarse
	digitalWrite(col[asteroid.x], LOW);
	digitalWrite(row[asteroid.y], HIGH);
	return;
}

void movePlayer() {
	int value = analogRead(joystick);
	value = map(value, 0, 1023, 0, 7);
	value = constrain(value, 0, 7);
  //Serial.print("joystickX: ");
  //Serial.print(jugador.x);
  //Serial.print("\n");
  if(value >= 0 && value <= 7) {
    jugador.x = value;
  }
  return;
}

void asteroidFall() {
	asteroid.y++;
  //Serial.print("AsteroidY: ");
  //Serial.print(asteroid.y);
  //Serial.print("\n");
	return;
}

void clearScreen() {
	for (int led = 0; led < 8; led++)
	{
		digitalWrite(col[led], HIGH);
		digitalWrite(row[led], LOW);
	}
  return;
}

int Update() {
  if(updatedAsteroid == true && updatedPlayer == false) {
    digitalWrite(col[jugador.x], LOW);
    digitalWrite(row[jugador.y], HIGH);
    updatedAsteroid = false;
    updatedPlayer = true;
    //Serial.print("Jugador Actualizado!!\n");
    return 0; //retorna que se ha actualizado el jugador
  } else {
    digitalWrite(col[asteroid.x], LOW);
    digitalWrite(row[asteroid.y], HIGH);
    updatedAsteroid = true;
    updatedPlayer = false;
    //Serial.print("Asteroid Actualizado!!\n");
    return 1; //retorna que se ha actualizado el asteroide
  }
}

void puntuacion() {
  if(asteroid.y == jugador.y && asteroid.x == jugador.x) {
      digitalWrite(speakerPin, HIGH);
      puntos++;
      delay(100);
      digitalWrite(speakerPin, LOW);
      vidas += 2;
      if(speed > 9 && puntos >= 4) {
        speed--; //aumenta la velocidad del juego (nivel nuevo)
        puntos = 0; //reiniciamos la puntucion al pasar al siguiente nivel
      }
      Serial.print("Puntos: ");
      Serial.print(puntos);
      Serial.print("\n");
  } 
  if(asteroid.y == jugador.y && asteroid.x != jugador.x) {
      vidas--;
      Serial.print("Vidas: ");
      Serial.print(vidas);
      Serial.print("\n");
      if(vidas == 0) {
        gameover();
      }
  }
  return;
}

void gameover() {
  //animacion cuando pierdes la partida
  for(int y = 0; y < 5; y++)
  {
    //Serial.print("Animacion: ");
    //Serial.print(y);
    //Serial.print("\n");
    //bucle que apaga todos los leds de la matriz
    digitalWrite(speakerPin, HIGH);
    for(int i = 0; i < 8; i++)
    {
      digitalWrite(col[i], HIGH);
      digitalWrite(row[i], LOW);
    }
    delay(500);
    //bucle que enciende todos los leds de la matriz
    for(int i = 0; i < 8; i++)
    {
      digitalWrite(col[i], LOW);
      digitalWrite(row[i], HIGH);
    }
    digitalWrite(speakerPin, LOW);
    delay(500);
  }
  delay(1000);
  initGame(); //inicializa una nueva partida
  return;
}

Schematics

Arduino CTB Scheme
Asteroid sketch bb zsdoiqeugf

Comments

Similar projects you might like

Programming 8x8 LED Matrix

by SAnwandter1

  • 113,953 views
  • 26 comments
  • 105 respects

8x8 Matrix LED Snake Game (Smartphone Motion)

Project tutorial by hmkim

  • 4,577 views
  • 2 comments
  • 17 respects

Bluetooth control led with lcd led status display real time.

Project tutorial by Youssef Sabaa

  • 17,304 views
  • 4 comments
  • 34 respects

LED Matrix Wave Moves With Hand

by Mostafa Abcreno

  • 4,053 views
  • 1 comment
  • 20 respects
Add projectSign up / Login