(Error: error: ‘buttonClicked’ was not declar/ if (buttonClicked(LEFTBUTTON))
// Snake on 8x8Matrix
//
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_LEDBackpack.h>
// Button pin
const int buttonRightPin = 2;
const int buttonLeftPin = 3;
// Game constants
// buttons
const int RIGHTBUTTON = 0;
const int LEFTBUTTON = 1;
// direction
const int TOP = 0;
const int RIGHT = 1;
const int BOTTOM = 2;
const int LEFT = 3;
// Snake
const int MAX_SNAKE_LENGTH = 10;
// Variables
Adafruit_8x8matrix matrix = Adafruit_8x8matrix(); // Display
int direction = TOP; // direction of movement
int snakeX[MAX_SNAKE_LENGTH]; // X-coordinates of snake
int snakeY[MAX_SNAKE_LENGTH]; // Y-coordinates of snake
int snakeLength = 1; // nr of parts of snake
boolean buttonRead = false; // is button already read in this loop
unsigned long prevTime = 0; // for gamedelay (ms)
unsigned long delayTime = 500; // Game step in ms
int fruitX, fruitY;
unsigned long fruitPrevTime = 0;
unsigned long fruitBlinkTime = 1000/250;
int fruitLed = LED_ON;
void setup(){
Serial.begin(9600);
Serial.println(“Snake is started”);
randomSeed(analogRead(0));
// Init led matrix
matrix.begin(0x70);
// init buttons
int buttonpins = {buttonRightPin, buttonLeftPin};
int Buttons(buttonpins, 2);
// init snake
snakeX[0] = 4;
snakeY[0] = 7;
for(int i=1; i<MAX_SNAKE_LENGTH; i++){
snakeX = snakeY = -1;
* }*
* makeFruit();*
}
void loop(){
* checkButtons();*
* unsigned long currentTime = millis();*
* if(currentTime - prevTime >= delayTime){*
* nextstep();*
* buttonRead = false;*
* prevTime = currentTime;*
* }*
* draw();*
}
void checkButtons(){
* if(!buttonRead){*
* int currentDirection = direction;*
* if (buttonClicked(LEFTBUTTON))*
* {*
* direction–;*
* if(direction < 0){*
* direction = LEFT;*
* }*
* }*
* else if(buttonClicked(RIGHTBUTTON))*
* {*
* direction++;*
* if(direction > 3){*
* direction = TOP;*
* }*
* }*
* buttonRead = (currentDirection != direction);*
* }*
}
void draw(){
* matrix.clear();*
* drawSnake();*
* drawFruit();*
* matrix.writeDisplay();*
}
void drawSnake(){
* for(int i=0; i<snakeLength; i++){*
matrix.drawPixel(snakeX, snakeY*, LED_ON);
_ }
}
void drawFruit(){
if(inPlayField(fruitX, fruitY)){
unsigned long currenttime = millis();
if(currenttime - fruitPrevTime >= fruitBlinkTime){_
fruitLed = (fruitLed == LED_ON) ? LED_OFF : LED_ON;
_ fruitPrevTime = currenttime;
}
matrix.drawPixel(fruitX, fruitY, fruitLed);
}
}
boolean inPlayField(int x, int y){
return (x>=0) && (x<8) && (y>=0) && (y<8);
}
void nextstep(){
for(int i=snakeLength-1; i>0; i–){
snakeX = snakeX[i-1];
snakeY = snakeY[i-1];
}
switch(direction){
case TOP:
snakeY[0] = snakeY[0]-1;
break;
case RIGHT:
snakeX[0] = snakeX[0]+1;
break;
case BOTTOM:
snakeY[0] = snakeY[0]+1;
break;
case LEFT:
snakeX[0]=snakeX[0]-1;
break;
}
if((snakeX[0] == fruitX) && (snakeY[0] == fruitY)){
snakeLength++;_
if(snakeLength < MAX_SNAKE_LENGTH){
_ makeFruit();
}
else {
fruitX = fruitY = -1;
}
}
}
void makeFruit(){
int x, y;
x = random(0, 8);
y = random(0, 8);
while(isPartOfSnake(x, y)){
x = random(0, 8);
y = random(0, 8);
}
fruitX = x;
fruitY = y;
}
boolean isPartOfSnake(int x, int y){
for(int i=0; i<snakeLength-1; i++){
if((x == snakeX) && (y == snakeY)){
return true;
}
}
return false;
}*_