Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

How To Make Snake Game using Arduino, 8X8 LED Metrix and Joystick WTC

 How To Make Snake Game using Arduino, 8X8 LED Metrix and Joystick WTC


To make a snake game using Arduino, 8X8 LED Metrix and Joystick we need to understand our game first as we have to make the snake go infinite  and  To create an infinite snake game where the snake can move in all directions without hitting boundaries, you can implement the "wrap-around" effect. This means when the snake moves off one edge of the LED matrix, it will appear on the opposite edge:

Project Overview

  • Components Needed:
    • Arduino (e.g., Arduino UNO)
    • LED matrix (e.g., 8x8)
    • Joystick
    • laptop
    • Programing Cable
    • Breadboard and jumper wires

Wiring Diagram

  1. LED Matrix:

    • Connect the VCC to 5 volt.
    • Connect the DIN pin to D11.
    • Connect the GND to ground.
    • Connect the CS pin to D10
    • Connect the CLK Pin to D13
  2. Joystick:

    • connect the GND pin to ground and VCC pin to 5 Volt
    • Connect the VRx pin to A0 and VRy pin to A1.

Video Link: - Click Here For Video

How To Make Snake Game using Arduino, 8X8 LED Metrix and Joystick WTC simple Circuit: -



How To Make Snake Game using Arduino, 8X8 LED Metrix and Joystick WTC Arduino Programming: -

Arduino IDE Program

#include <LedControl.h> // Include the LedControl library for MAX7219

// Pin definitions for MAX7219
#define DIN_PIN 11
#define CLK_PIN 13
#define CS_PIN 10

LedControl lc = LedControl(DIN_PIN, CLK_PIN, CS_PIN, 1); // Initialize the LED matrix

// Define joystick pins
const int joyXPin = A0; // X-axis pin
const int joyYPin = A1; // Y-axis pin

// Game variables
const int matrixSize = 8; // Size of the LED matrix
int snakeX[matrixSize * matrixSize]; // Array to store the snake's x positions
int snakeY[matrixSize * matrixSize]; // Array to store the snake's y positions
int snakeLength = 1; // Initial length of the snake
int foodX = 0; // Food x position
int foodY = 0; // Food y position
int direction = 0; // Direction (0: right, 1: down, 2: left, 3: up)
bool gameOver = false; // Game over flag

void setup() {
  lc.shutdown(0, false); // Wake up the MAX7219
  lc.setIntensity(0, 8); // Set brightness level (0 to 15)
  lc.clearDisplay(0); // Clear display
 
  // Initialize snake's starting position
  snakeX[0] = matrixSize / 2;
  snakeY[0] = matrixSize / 2;

  // Generate the first food position
  generateFood();
}

void loop() {
  if (!gameOver) {
    // Read joystick positions
    int xReading = analogRead(joyXPin); // Read X-axis
    int yReading = analogRead(joyYPin); // Read Y-axis

    // Map joystick readings to determine direction
    if (xReading < 300 && direction != 2) {
      direction = 0; // Move right
    } else if (xReading > 700 && direction != 0) {
      direction = 2; // Move left
    } else if (yReading < 300 && direction != 3) {
      direction = 1; // Move down
    } else if (yReading > 700 && direction != 1) {
      direction = 3; // Move up
    }

    // Move the snake
    moveSnake();

    // Check for collision with food
    if (snakeX[0] == foodX && snakeY[0] == foodY) {
      snakeLength++; // Increase the snake length
      generateFood(); // Generate new food
    }

    // Check for collision with itself
    if (isGameOver()) {
      gameOver = true; // Set game over flag
    }

    // Clear the display and redraw the snake and food
    lc.clearDisplay(0);
    drawSnake();
    drawFood();
   
    // Delay to control game speed
    delay(150); // Adjust the speed as needed
  } else {
    // Display "Game Over" on the LED matrix
    displayGameOver();
  }
}

// Function to move the snake
void moveSnake() {
  // Shift the body of the snake
  for (int i = snakeLength; i > 0; i--) {
    snakeX[i] = snakeX[i - 1];
    snakeY[i] = snakeY[i - 1];
  }

  // Update the head of the snake based on the current direction
  switch (direction) {
    case 0: snakeX[0]++; break; // Move right
    case 1: snakeY[0]++; break; // Move down
    case 2: snakeX[0]--; break; // Move left
    case 3: snakeY[0]--; break; // Move up
  }

  // Wrap around logic
  snakeX[0] = (snakeX[0] + matrixSize) % matrixSize; // Wrap around x
  snakeY[0] = (snakeY[0] + matrixSize) % matrixSize; // Wrap around y
}

// Function to check if the game is over
bool isGameOver() {
  // Check for collision with itself
  for (int i = 1; i < snakeLength; i++) {
    if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
      return true;
    }
  }
 
  return false;
}

// Function to generate food at a random position
void generateFood() {
  foodX = random(0, matrixSize);
  foodY = random(0, matrixSize);

  // Ensure food does not spawn on the snake
  for (int i = 0; i < snakeLength; i++) {
    if (foodX == snakeX[i] && foodY == snakeY[i]) {
      generateFood(); // Regenerate if it spawns on the snake
      return;
    }
  }
}

// Function to draw the snake on the LED matrix
void drawSnake() {
  for (int i = 0; i < snakeLength; i++) {
    lc.setLed(0, snakeY[i], snakeX[i], true); // Set each LED according to the snake's position
  }
}

// Function to draw food on the LED matrix
void drawFood() {
  lc.setLed(0, foodY, foodX, true); // Set the LED at the food position
}

// Function to display "Game Over" on the LED matrix
void displayGameOver() {
  lc.clearDisplay(0);
  // Display a simple "GAME OVER" message on the matrix (adjust as needed)
  for (int i = 0; i < 8; i++) {
    lc.setLed(0, 0, i, true); // Display "GAME" on the first row
    lc.setLed(0, 1, i, true); // Display "OVER" on the second row
  }
}


-----------------------------------------------------------------------------------------------------------------------------

Key Changes and Features

  1. Wrap-Around Logic:

    • The head of the snake uses modulo arithmetic to wrap around the edges of the matrix:
      cpp
      snakeX[0] = (snakeX[0] + matrixSize) % matrixSize; // Wrap around x snakeY[0] = (snakeY[0] + matrixSize) % matrixSize; // Wrap around y
    • This ensures that if the snake moves off one edge of the matrix, it reappears on the opposite edge.
  2. Collision Detection:

    • The snake can still collide with itself, but it will not be affected by the boundaries of the matrix.
  3. Gameplay:

    • The gameplay remains the same, where the snake grows longer when it eats food, and it can be controlled with the joystick.

Testing and Customization

  • You can adjust the delay(150); value to control the speed of the snake.
  • The snake will continue to move infinitely in the game without hitting any boundaries.

This implementation should create a fun and engaging snake game experience! Let me know if you have any further questions or modifications!

Circuit Simulation:

You can not use Tinkercad to simulate this project as there is no joystick and led metrix available in tinker cad yet. Let me know if you need more details or help setting up.

Share, Support, and Subscribe!!! 1. PRO KAM EXPLAINED 2. Knowledge KAM 3. PRO KAM Follow us on 1. Facebook 2. Instagram 3. Pinterest 4. Blogspot 5 Twitter If you have any other ideas to make me design, you can describe them in the comment section and if I can, I will make a designing video on it. So I am waiting #prokam #wtc #Arduino #ideas #projects #diy #how #to #ArduinoProjects #UltrasonicSensor #HeightMeasurement #TechDIY #WeTechCreators

Post a Comment

0 Comments

Ad Code

Responsive Advertisement