// Define the number of rows and columns for the LED matrix
const int rows = 2;
const int cols = 5;
// Define the pins for the LED array and button
const int ledPins[rows][cols] = {
{2, 3, 4, 5, 6},
{11, 10, 9, 8, 7}
};
const int buttonPin = 12;
// Define a 2D array to represent the LED states (ON/OFF)
bool ledState[rows][cols] = {false};
// Define Dino postition and state
int dinoRow = 0;
int dinoCol = 3;
bool dinoAlive = true;
// Define Cactus
int cacti[cols];
int cactusBuffer = 0;
// Define game mechanics toggles
int gameState = false;
int jumpState = false;
int jumpTimer = 0;
// Setup function to initialize the LED pins
void setup() {
Serial.begin(9600);
// Initialize each pin as an OUTPUT
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
pinMode(ledPins[i][j], OUTPUT);
digitalWrite(ledPins[i][j], LOW); // Ensure all LEDs are initially off
}
}
// Initialize start button pin
pinMode(buttonPin, INPUT);
// initialize cacti
for (int col = 0; col < cols; col++) {
cacti[col] = 0;
}
}
// Function to control LEDs
void turnOnLED(int row, int col) {
if (row >= 0 && row < rows && col >= 0 && col < cols) {
digitalWrite(ledPins[row][col], HIGH);
ledState[row][col] = true;
}
}
// Function to turn off a specific LED
void turnOffLED(int row, int col) {
if (row >= 0 && row < rows && col >= 0 && col < cols) {
digitalWrite(ledPins[row][col], LOW);
ledState[row][col] = false;
}
}
void turnOnLEDS() {
for (int col = cols; col >= 0; col--) {
turnOnLED(0, col);
turnOnLED(1, col);
delay(100);
}
}
void turnOffLEDS() {
for (int col = 0; col < cols; col++) {
turnOffLED(0, col);
turnOffLED(1, col);
}
}
void refreshLEDS() {
turnOffLEDS();
// Draw Dino
turnOnLED(dinoRow, dinoCol);
// Draw Cacti
for (int col = 0; col < cols; col++) {
if (cacti[col] > 0 ) {
turnOnLED(0, col);
}
}
}
void jump() {
if (digitalRead(buttonPin) == HIGH && !jumpState) {
jumpState = true;
jumpTimer = 1;
dinoRow++;
} else if (jumpState) {
if (dinoRow != 0) {
if (jumpTimer > 0) {
jumpTimer--;
} else {
jumpState = false;
dinoRow--;
}
}
}
}
void checkCollision() {
if (dinoRow == 0) {
for (int col = 0; col < cols; col++) {
if (cacti[col] > 0 && col == dinoCol) {
turnOnLEDS();
while (true) {}
}
}
}
}
void animateCacti() {
for (int col = cols - 1; col > 0; col--) {
cacti[col] = cacti[col - 1];
}
cacti[0] = 0;
if (cactusBuffer == 0) {
cacti[0] = 1;
cactusBuffer = random(2, 5);
} else cactusBuffer--;
}
// Example loop function to demonstrate usage
void loop() {
jump();
animateCacti();
checkCollision();
refreshLEDS();
delay(300);
}