#include <MD_MAX72xx.h>
#include <ezButton.h>
//matrix controls
const byte data_pin = 11;
const byte chip_select_pin = 10;
const byte clock_pin = 13;
const byte max_devices = 4;
// creating matrix object
MD_MAX72XX matrix = MD_MAX72XX(MD_MAX72XX::PAROLA_HW, chip_select_pin, max_devices);
ezButton button(8);
// define the joystick controls
const byte hpin = A1;
const byte vpin = A0;
int snake_x[64];
int snake_y[64];
int snake_length = 4;
int head_x = 0;
int head_y = 0;
int max_x = 31;
int max_y = 7;
int food_x = random(0,8);
int food_y = random(0,8);
int flag = 0;
String prev_dir = "";
String dir = "";
long int current_time = 0, prev_time = 0;
int threshold = 700;
void setup()
{
Serial.begin(9600);
// delay(20);
matrix.begin();
matrix.clear();
button.setDebounceTime(25);
show_message("Welcome");
matrix.setPoint(head_y , head_x , true);
// (right left ,up down, true--> pint visible, false --> no visible)
}
void loop()
{
button.loop();
//returns the number of milliseconds passed since the Arduino board began
//running the current program
current_time = millis(); //
checkDirection();
if (current_time - prev_time >= threshold)
{
prev_time = current_time;
matrix.clear();
moveSprite();
update_snake();
draw_sprite();
}
food_eat_check();
}
void checkDirection()
{
if ((analogRead(hpin) > 512) && (prev_dir != "Right"))
dir = "Left";
else if ((analogRead(hpin) < 512) && (prev_dir != "Left"))
dir = "Right";
else if ((analogRead(vpin) > 512) && (prev_dir != "Down"))
dir = "Up";
else if ((analogRead(vpin) < 512) && (prev_dir != "Up"))
dir = "Down";
prev_dir = dir;
}
void moveSprite()
{
if (dir == "Left" )
{
head_x += 1;
}
else if (dir == "Right" )
{
head_x -= 1;
}
else if (dir == "Up" )
{
head_y -= 1;// has been switched wantedly, only then sprite is moving according tot the joystick
}
else if (dir == "Down" )
{
head_y += 1;// has been switched wantedly, only then sprite is moving according tot the joystick
}
window_check();
if (button.isPressed())
{
Serial.println("The button is pressed");
flag=1;
}
}
void window_check()
{
if (head_x > max_x)
head_x = 0;
else if (head_x < 0)
head_x = max_x;
else if (head_y > max_y)
head_y = 0;
else if (head_y < 0)
head_y = max_y;
}
void draw_sprite()
{
matrix.setPoint(food_y,food_x,true);
for (int i= 0; i <= snake_length; i++ )
matrix.setPoint(snake_y[i],snake_x[i],true);
}
void update_snake()
{
for (int i = snake_length; i >= 0;i--)
{
if (i != 0)
{
snake_x[i] = snake_x[i-1];
snake_y[i] = snake_y[i-1];
}
else
{
snake_x[i] = head_x;
snake_y[i] = head_y;
}
}
}
void food_eat_check()
{
if (head_x == food_x && head_y == food_y)
{
food_x = random(0,8);
food_y = random(0,8);
snake_length++;
threshold -= 50;
if (threshold <100)
threshold = 80;
}
}
void all_animate()
{
for (int i = 0 ; i < 3 ; i++)
{
for (int x = 0; x < 32; x++)
{
for (int y = 0; y < 8; y++)
{
matrix.setPoint(y,x,true);
}
}
delay(500);
matrix.clear();
delay(500);
}
}
void show_message(char messageToShow[])
{
all_animate();
int i = 0,start = 30;
int vals[] = {30,25,20,18,13,8,3};
while (messageToShow[i]!='\0')
{
matrix.setChar(vals[i] , messageToShow[i]);
delay(500);
i++;
}
}