Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Snake Game with Matrix Display and push button with Arduino

Introduction

This project is a Snake Game implemented on an 8×8 LED matrix using the MAX7219 driver module. It uses push buttons to control the snake’s direction and grow its length by “eating” food. The snake moves around the grid, avoiding collisions with walls and itself. When food is consumed, a new food position is randomly generated. If the snake collides with itself or the wall, the game ends, displaying a “Game Over” screen. This project demonstrates interactive gameplay with visual feedback and is a fun way to explore embedded programming with Arduino.

Required Components

  • Arduino UNO
  • 8×8 LED Matrix Display Module
  • tandalone Tactile Push Button (4)
  • 10kΩ resistor (4)
  • Jumper wires
  • Breadboard

Circuit Diagram / Wiring

  • MAX7219 (LED Matrix) Connections:
    • VCC (LED Module) → 5V (Arduino)
    • GND (LED Module) → GND (Arduino)
    • DIN (LED Module) → Pin 12 (Arduino)
    • CS (LED Module) → Pin 11 (Arduino)
    • CLK (LED Module) → Pin 10 (Arduino)
  • Push Buttons:
    • Up Button → Pin 3 on Arduino (with a 10kΩ resistor to 5V)
    • Down Button → Pin 4 on Arduino (with a 10kΩ resistor to 5V)
    • Left Button → Pin 5 on Arduino (with a 10kΩ resistor to 5V)
    • Right Button → Pin 2 on Arduino (with a 10kΩ resistor to 5V)

Arduino Code / Programming

Make sure you have the required libraries installed:

  • LedControl library for Matrix Display
#include <LedControl.h>  // MAX7219 control library
 
// Define MAX7219 pins
#define DATA_IN 12
#define CLK 10
#define CS 11
 
 
LedControl lc = LedControl(DIN_PIN, CLK_PIN, CS_PIN, 1);
 
// Push buttons
#define UP_BTN 3
#define DOWN_BTN 4
#define LEFT_BTN 5
#define RIGHT_BTN 2
 
// Game variables
int snake[64][2]; // Snake body (maximum length 64)
int snakeLength = 2; // Starting length of the snake
int direction = 1;   // 0=UP, 1=RIGHT, 2=DOWN, 3=LEFT
int foodX, foodY;
bool isGameOver = false;
 
void setup() {
  // Initialize MAX7219
  lc.shutdown(0, false);
  lc.setIntensity(0, 8);  // Set brightness (0-15)
  lc.clearDisplay(0);
 
  // Initialize buttons
  pinMode(UP_BTN, INPUT_PULLUP);
  pinMode(DOWN_BTN, INPUT_PULLUP);
  pinMode(LEFT_BTN, INPUT_PULLUP);
  pinMode(RIGHT_BTN, INPUT_PULLUP);
 
  // Initialize Serial Monitor
  Serial.begin(9600);
  Serial.println("Game Started! Use buttons to control the snake.");
   
  // Initialize snake position
  snake[0][0] = 3; snake[0][1] = 3; // Head
  snake[1][0] = 3; snake[1][1] = 2; // Body
 
  generateFood(); // Generate the first food
}
 
void loop() {
  if (isGameOver) {
    gameOver();
    return;
  }
 
  // Read buttons for direction
  if (!digitalRead(UP_BTN) && direction != 2) direction = 0;
  if (!digitalRead(RIGHT_BTN) && direction != 3) direction = 1;
  if (!digitalRead(DOWN_BTN) && direction != 0) direction = 2;
  if (!digitalRead(LEFT_BTN) && direction != 1) direction = 3;
 
  moveSnake();
 
  if (checkCollision()) {
    isGameOver = true;
    return;
  }
 
  if (checkFood()) {
    snakeLength++;
    Serial.print("Food eaten! New length: ");
    Serial.println(snakeLength);
    generateFood();
  }
 
  displaySnake();
  delay(300); // Adjust speed of the game
}
 
void moveSnake() {
  // Move body
  for (int i = snakeLength - 1; i > 0; i--) {
    snake[i][0] = snake[i - 1][0];
    snake[i][1] = snake[i - 1][1];
  }
 
  // Move head
  if (direction == 0) snake[0][0]--; // UP
  if (direction == 1) snake[0][1]++; // RIGHT
  if (direction == 2) snake[0][0]++; // DOWN
  if (direction == 3) snake[0][1]--; // LEFT
 
  // Print direction to Serial Monitor
  Serial.print("Snake moved: ");
  if (direction == 0) Serial.println("UP");
  if (direction == 1) Serial.println("RIGHT");
  if (direction == 2) Serial.println("DOWN");
  if (direction == 3) Serial.println("LEFT");
}
 
bool checkCollision() {
  // Check wall collision
  if (snake[0][0] < 0 || snake[0][0] >= 8 || snake[0][1] < 0 || snake[0][1] >= 8) {
    Serial.println("Collision with wall! Game Over.");
    return true;
  }
  // Check self-collision
  for (int i = 1; i < snakeLength; i++) {
    if (snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1]) {
      Serial.println("Collision with self! Game Over.");
      return true;
    }
  }
  return false;
}
 
bool checkFood() {
  return snake[0][0] == foodX && snake[0][1] == foodY;
}
 
void generateFood() {
  do {
    foodX = random(0, 8);
    foodY = random(0, 8);
  } while (isFoodOnSnake());
 
  Serial.print("New food generated at: (");
  Serial.print(foodX);
  Serial.print(", ");
  Serial.print(foodY);
  Serial.println(")");
}
 
bool isFoodOnSnake() {
  for (int i = 0; i < snakeLength; i++) {
    if (foodX == snake[i][0] && foodY == snake[i][1]) {
      return true;
    }
  }
  return false;
}
 
void displaySnake() {
  lc.clearDisplay(0);
 
  // Display snake
  for (int i = 0; i < snakeLength; i++) {
    lc.setLed(0, snake[i][0], snake[i][1], true);
  }
 
  // Display food
  lc.setLed(0, foodX, foodY, true);
}
 
void gameOver() {
  lc.clearDisplay(0);
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      lc.setLed(0, i, j, true);
    }
  }
  Serial.println("Game Over! Restarting...");
  delay(3000);
  setup(); // Restart the game
}

Explanation

  • Implements a Snake game on an 8×8 LED matrix using MAX7219 and Arduino.
  • Four push buttons control the snake’s movement (UP, DOWN, LEFT, RIGHT).
  • Snake grows by eating randomly generated food and avoids collisions with walls or itself.
  • LED matrix visually updates the game state, and the Serial Monitor logs events like direction, food eaten, and Game Over.

    Leave a Reply

    Your email address will not be published.

    Need Help?