#include <LedControl.h> // Library for controlling LED matrix
#define JOYSTICK_X A0 // Joystick X pin connected to A0
#define LED_MATRIX_DATA 6 // Data pin connected to the LED matrix
#define LED_MATRIX_CLK 7 // CLK pin connected to the LED matrix
#define LED_MATRIX_CS 8 // CS pin connected to the LED matrix
#define BUTTON_PIN 2 // Button pin connected to digital pin 2
LedControl lc = LedControl(LED_MATRIX_DATA, LED_MATRIX_CLK, LED_MATRIX_CS, 1); // Set up the LED matrix
int carPosition = 3; // Initial position of the car
const int numObstacles = 3; // Number of obstacles (cars)
int obstacleRows[numObstacles]; // Rows of the obstacles
int obstacleColumns[numObstacles]; // Columns of the obstacles
bool gameRunning = true; // Game state
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(JOYSTICK_X, INPUT); // Set joystick pin as input
pinMode(BUTTON_PIN, INPUT_PULLUP); // Set button pin as input with pull-up resistor
lc.shutdown(0, false); // Wake up the MAX72XX
lc.setIntensity(0, 8); // Set brightness level
lc.clearDisplay(0); // Clear the display
randomSeed(analogRead(0)); // Initialize random seed
for (int i = 0; i < numObstacles; i++) {
obstacleRows[i] = random(0, 3); // Initialize obstacle rows randomly at the top
obstacleColumns[i] = random(0, 8); // Initialize obstacle columns randomly
}
}
void loop() {
if (gameRunning) {
int joystickValue = analogRead(JOYSTICK_X); // Read joystick input
if (joystickValue < 100) { // If joystick moved left
if (carPosition > 0) {
carPosition--; // Move the car left
}
} else if (joystickValue > 900) { // If joystick moved right
if (carPosition < 7) {
carPosition++; // Move the car right
}
}
moveObstacles(); // Move obstacles
displayGame(); // Update LED matrix display
delay(200); // Adjust the speed of the game
} else {
gameOver(); // Game over state
}
}
void displayGame() {
lc.clearDisplay(0); // Clear the display
for (int i = 0; i < numObstacles; i++) {
lc.setLed(0, obstacleRows[i], obstacleColumns[i], false); // Clear previous obstacle
lc.setLed(0, obstacleRows[i] + 1, obstacleColumns[i], true); // Display the obstacle car
}
lc.setLed(0, 7, carPosition, true); // Display the car
delay(50); // Pause briefly to display
for (int i = 0; i < numObstacles; i++) {
lc.setLed(0, obstacleRows[i], obstacleColumns[i], true); // Display obstacle at new position
}
// Check for collision
for (int i = 0; i < numObstacles; i++) {
if (obstacleRows[i] + 1 == 7 && obstacleColumns[i] == carPosition) {
gameRunning = false; // Set game state to false (game over)
break; // Exit loop if collision detected
}
}
}
void moveObstacles() {
for (int i = 0; i < numObstacles; i++) {
obstacleRows[i]++; // Move obstacle down
if (obstacleRows[i] > 7) { // If obstacle reached bottom
obstacleRows[i] = 0; // Reset obstacle to the top
obstacleColumns[i] = random(0, 8); // Randomize obstacle position horizontally
}
}
}
void gameOver() {
Serial.println("Game Over! Press the button to restart.");
while (digitalRead(BUTTON_PIN) == HIGH) {
// Wait for button press to restart the game
}
delay(100); // Debouncing delay
gameRunning = true; // Restart the game
carPosition = 3; // Reset car position
for (int i = 0; i < numObstacles; i++) {
obstacleRows[i] = random(0, 3); // Reset obstacle rows
obstacleColumns[i] = random(0, 8); // Reset obstacle columns
}
}