#include "LedControl.h"
#define SEL_pin 2
#define X_pin A0 //
#define Y_pin A1 //
#define DIN 11
#define CS 10
#define CLK 13
#define BUZZER 7
#define screenWidth 8
#define screenHeight 8
int snakeX, snakeY, foodX, foodY, score = 0, snakeSize = 3;
char direction;
int tailX[100], tailY[100];
bool isGameOver = false;
LedControl lc = LedControl(DIN, CS, CLK, 0);
void setup() {
setupPins();
setupLedBoard();
setupSnakePosition();
setupFoodPosition();
}
void setupSnakePosition() {
byte row = 4;
byte column = 4;
}
void setupFoodPosition() {
foodX = rand() % screenWidth;
foodY = rand() % screenHeight;
}
void setupLedBoard() {
lc.shutdown(0, false);
lc.setIntensity(0, 8);
lc.clearDisplay(0);
}
void setupPins() {
pinMode(SEL_pin, INPUT);
digitalWrite(SEL_pin, HIGH);
}
void loop() {
if (isGameOver) {
playGameOverSong();
showGameOverScreen();
} else {
startGame();
}
}
//canción cuando pierdes
void playGameOverSong() {
tone (BUZZER, 1000, 1000);
delay(100);
tone(BUZZER, 2000, 1000);
delay(100);
tone(BUZZER, 3000, 1000);
delay(100);
tone(BUZZER, 4000, 1000);
delay(100);
tone(BUZZER, 5000, 2000);
}
//sonido cuando come algo
void playFoodEatenSong() {
tone(BUZZER, 500, 100);
}
//comienzo de juego
void startGame() {
manageGameOver();
setJoystickDirection();
changeSnakeDirection();
manageSnakeOutOfBounds();
manageEatenFood();
manageSnakeTailCoordinates();
drawSnake();
delay(300);
}
void manageGameOver() {
for (int i = 1; i < snakeSize; i++) {
if (tailX[i] == snakeX && tailY[i] == snakeY) {
isGameOver = true;
}
}
}
void manageSnakeOutOfBounds(){
if (snakeX >= screenWidth) {
snakeX = 0;
} else if (snakeX < 0) {
snakeX = screenWidth -1;
}
if (snakeY >= screenHeight) {
snakeY = 0;
} else if (snakeY < 0) {
snakeY = screenHeight -1;
}
}
void manageSnakeTailCoordinates() {
int previousX, previouesY, prevX, prevY;
previousX = tailX[0];
previouesY = tailY[0];
tailX[0] = snakeX;
tailY[0] = snakeY;
for (int i = 1; i < snakeSize; i++) {
prevX = tailX[i];
prevY = tailY[i];
tailX[i] = previousX;
tailY[i] = previouesY;
previousX = prevX;
previouesY = prevY;
}
}
void manageEatenFood() {
if (snakeX == foodX && snakeY == foodY) {
playFoodEatenSong();
score++;
snakeSize++;
setupFoodPosition();
}
}
void setJoystickDirection() {
if (analogRead(X_pin) > 1000){
direction = 'u';
} else if (analogRead(X_pin) < 100) {
direction = 'd';
} else if (analogRead(Y_pin) > 1000) {
direction = 'l';
} else if (analogRead(Y_pin) < 100) {
direction = 'r';
}
}
void changeSnakeDirection() {
switch (direction) {
case 'l':
snakeX--;
break;
case 'r':
snakeX++;
break;
case 'u':
snakeY--;
break;
case 'd':
snakeY++;
break;
}
}
void showGameOverScreen() {
for (int i = 0; i < screenHeight; i++) {
for (int j = 0; j < screenWidth; j++) {
showLed(j, i);
delay(50);
}
}
resetVariables();
}
void resetVariables() {
setupSnakePosition();
setupFoodPosition();
direction = ' ';
isGameOver = false;
score = 0;
snakeSize = 1;
}
void showLed(int row, int column) {
lc.setLed(0, row, column, true);
}
void hideLed(int row, int column) {
lc.setLed(0, row, column, false);
}
void drawSnake() {
for (int i = 0; i < screenHeight; i++) {
for (int j = 0; j < screenWidth; j++) {
if (i == snakeY && j == snakeX) {
showLed(snakeX, snakeY);
} else if (i == foodY && j == foodX) {
showLed(foodX, foodY);
} else {
bool isShown = false;
for (int k = 0; k < snakeSize; k++) {
if (tailX[k] == j && tailY[k] == i) {
showLed(j, i);
isShown = true;
}
}
if (!isShown) {
hideLed(j, i);
}
}
}
}
}