This is a simplified version of "Osu! Catch the Beat" game which is played on an 8 to 8 Led matrix.

This is a simplified version of "Osu! Catch the Beat" game which is played on an 8 to 8 Led matrix. The game consists of catching the falling balls.

The hunter moves left and right by turning the potentiometer. You have a total of 10 Lives, and you lose one for each missed ball. For every ball, you catch you get one life. Every fourth point won the speed of the balls increases by one level. The game ends when we spend all our lives and it is manifested by 5 times switching "on" and "off" of all 64 LEDs. So the goal is to stay in the game for as long as possible.

I am currently working on a modification of the code, and at the end of the game as many LEDs will be lit as we have scored points, and when I finish it, I will present it as an upgrade.

- The device is simple to build and contains only a few components:

- Arduino Nano microcontroller

- homemade 8 to 8 matrix

- active Buzzer

- and one Potentiometer

The initial speed of the game is set in the code, by changing the value in line 56

"speed="

The number of lives in line 54 can also be changed:

"vidas="

Finally, the device is mounted in a suitable box made of PVC board coated with self-adhesive wallpaper.

Untitled Sketch_schem.jpg
        int row[] = {12, 11, 10, 9, 8, 7, 6, 5};
int col[] = {4, 3, 2, A0, A1, A2, A3, A4};
int joystick = A6; //it is a simple potentiometer
int lastUpdate;
int puntos = 0;
int vidas = 10; //number of lives
int speakerPin = 13;
int SPEED = 0;
bool updatedAsteroid = false, updatedPlayer = true;

typedef struct coordenada {
  int x; //column coordinate
  int y; //row coordinate
}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(); //clean the led matrix
    if(lastUpdate == 1) {
      movePlayer();
    }
    if(lastUpdate == 0) {
      asteroidFall();
      //Serial.print ("The asteroid changes position !! \ n");
    }
    lastUpdate = Update(); //update the array of LEDs according to movePlayer () and asteroidFall ()
    delay(SPEED);
  }
  puntuacion(); //calculates the score and increases one life per point, when the player runs out of lives it goes to gameover () to restart the game
  if (asteroid.y == 8 || asteroid.y > 8) {
    initasteroid(); //initialize a new asteroid when the previous one has already dropped
  }
}

void initGame() {
  vidas = 10;
  puntos = 0; //points
  SPEED = 25;
  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; //so that asteroid Fall () starts at 0 as soon as it is executed
  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("Player Updated !! \ n");
    return 0; //returns that the player has been updated
  } else {
    digitalWrite(col[asteroid.x], LOW);
    digitalWrite(row[asteroid.y], HIGH);
    updatedAsteroid = true;
    updatedPlayer = false;
    //Serial.print ("Asteroid Updated !! \ n");
    return 1; //returns that the asteroid has been updated
  }
}

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--; //SPEED up the game (new level)
        puntos = 0; //We restart the punctuation when we go to the next level
      }
      Serial.print("Points: ");
      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() {
  //animation when you lose the game
  for(int y = 0; y < 5; y++)
  {
    //Serial.print("Animacion: ");
    //Serial.print(y);
    //Serial.print("\n");
    //loop that turns off all LEDs in the matrix
    digitalWrite(speakerPin, HIGH);
    for(int i = 0; i < 8; i++)
    {
      digitalWrite(col[i], HIGH);
      digitalWrite(row[i], LOW);
    }
    delay(500);
    //loop that turns on all the LEDs of the matrix
    for(int i = 0; i < 8; i++)
    {
      digitalWrite(col[i], LOW);
      digitalWrite(row[i], HIGH);
    }
    digitalWrite(speakerPin, LOW);
    delay(500);
  }
  delay(3000);
  initGame(); //initialize a new game
  return;
}
    
Mirko Pavleski
Electronics , Arduino , Physics

Categories