Snake_Game.ino
Arduino (C++) · 205 lines
//Snake_Game.ino
#include "WS_QMI8658.h"
#include "WS_Matrix.h"
// Declare Accel globally
extern IMUdata Accel;
// Game state variables
#define MAX_LENGTH 64 // Max length of the snake
uint8_t snakeX[MAX_LENGTH], snakeY[MAX_LENGTH]; // Snake's body coordinates
uint8_t snakeLength = 1; // Initial length of the snake
uint8_t foodX, foodY; // Food coordinates
uint8_t direction = 1; // 1 = Right, 2 = Left, 3 = Up, 4 = Down
// For controlling with the gyro
uint8_t X_EN = 0, Y_EN = 0, Time_X_A = 0, Time_X_B = 0, Time_Y_A = 0, Time_Y_B = 0;
// Movement timing
unsigned long lastMoveTime = 0;
unsigned long moveDelay = 500; // Initial speed (milliseconds)
// Dodge mechanism timing
unsigned long lastCollisionTime = 0;
unsigned long dodgeDelay = 1500; // Dodge time in ms
// Food state
bool foodActive = true; // Flag to indicate if food is currently visible
unsigned long foodRespawnMoves = 0; // Number of moves until food respawns
unsigned long moveCounter = 0; // Counter for snake moves
void setup() {
QMI8658_Init(); // Initialize QMI8658 sensor
Matrix_Init(); // Initialize the LED Matrix
// Initialize the snake's starting position
snakeX[0] = 4;
snakeY[0] = 4;
// Spawn the first food item
spawnFood();
}
void loop() {
QMI8658_Loop(); // Update sensor data
// Only move the snake if enough time has passed
if (millis() - lastMoveTime >= moveDelay) {
lastMoveTime = millis(); // Update the timer
// Control the snake direction with the gyro data
controlDirection();
// Move the snake and check for collisions
moveSnake();
// Check if snake ate the food
if (foodActive && snakeX[0] == foodX && snakeY[0] == foodY) {
snakeLength++;
foodActive = false; // Food disappears
moveCounter = 0; // Reset move counter
foodRespawnMoves = random(2, 8); // Random moves before food respawns
}
// Handle food respawn logic
if (!foodActive && moveCounter >= foodRespawnMoves) {
spawnFood(); // Respawn the food
foodActive = true; // Make the food visible again
}
// Update the game state and display on the matrix
updateMatrix();
// Increment move counter
moveCounter++;
}
}
void controlDirection() {
// Modify thresholds for quicker response
float threshold = 0.1; // Lower the threshold for faster response
// Prevent reverse movement
if (Accel.x > threshold && direction != 2) { // Right (only if not moving left)
direction = 1;
}
else if (Accel.x < -threshold && direction != 1) { // Left (only if not moving right)
direction = 2;
}
else if (Accel.y > threshold && direction != 4) { // Up (only if not moving down)
direction = 3;
}
else if (Accel.y < -threshold && direction != 3) { // Down (only if not moving up)
direction = 4;
}
}
void moveSnake() {
// Move the snake body
for (uint8_t i = snakeLength; i > 0; i--) {
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}
// Move the snake head in the current direction
switch (direction) {
case 1: // Right
snakeX[0]++;
break;
case 2: // Left
snakeX[0]--;
break;
case 3: // Up
snakeY[0]--;
break;
case 4: // Down
snakeY[0]++;
break;
}
// Check for collision with walls or snake's body
if (millis() - lastCollisionTime > dodgeDelay) {
if (snakeX[0] < 0 || snakeX[0] >= 8 || snakeY[0] < 0 || snakeY[0] >= 8 || checkSelfCollision()) {
gameOver(); // Trigger game over after dodge period
}
}
}
bool checkSelfCollision() {
for (uint8_t i = 1; i < snakeLength; i++) {
if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
return true; // Collision with self
}
}
return false;
}
void spawnFood() {
uint8_t attempts = 0;
bool validPosition = false;
while (attempts < 100) { // Limit attempts to prevent infinite loops
foodX = random(0, 8);
foodY = random(0, 8);
validPosition = true;
// Check if the food position overlaps with the snake
for (uint8_t i = 0; i < snakeLength; i++) {
if (foodX == snakeX[i] && foodY == snakeY[i]) {
validPosition = false;
break;
}
}
if (validPosition) {
return; // Exit once valid food position is found
}
attempts++;
}
gameOver(); // Reset game if no valid position found
}
void updateMatrix() {
// Clear the matrix
memset(Matrix_Data, 0, sizeof(Matrix_Data));
// Draw the snake
for (uint8_t i = 0; i < snakeLength; i++) {
Matrix_Data[snakeX[i]][snakeY[i]] = 1; // Snake = 1
}
// Draw the food (only if active)
if (foodActive) {
Matrix_Data[foodX][foodY] = 2; // Food = 2
}
// Update the LED matrix
RGB_Matrix();
}
void gameOver() {
// Display all pixels with the food color (2) for 100ms
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
Matrix_Data[i][j] = 2; // Set all pixels to the food color (2)
}
}
RGB_Matrix(); // Update the LED matrix to reflect the food color
delay(100); // Keep the food color for 100ms
// Reset the game state
snakeLength = 1;
snakeX[0] = random(1, 7);
snakeY[0] = random(1, 7);
// Spawn a new food after reset
spawnFood();
foodActive = true; // Make sure the food is active after reset
printf("Game Over! Restarting...\n");
lastCollisionTime = millis(); // Reset the dodge timer
}