#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
const int HEIGHHT = 64;
const int WIDHT = 128;
int vert = A0;
int horz = A1;
int button = 2;
int xShift = -1; //для двежения по вертекали
int snakeXOld = -1;
int yShift = 0; //для двежения в горезонтали
int snakeYOld = -1;
bool headSnake[8][8] =
{
{0,0,0,0,1,1,1,0},
{0,0,1,1,0,1,1,1},
{1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1},
{0,0,1,1,0,1,1,1},
{0,0,0,0,1,1,1,0},
{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0}
};
bool apple[8][8] =
{
{0,0,0,0,1,0,0,0},
{0,0,0,1,0,0,0,0},
{0,1,1,0,1,1,0,0},
{1,0,0,0,0,0,1,0},
{1,0,0,0,0,0,1,0},
{1,0,0,0,0,0,1,0},
{0,1,0,0,0,1,0,0},
{0,0,1,1,1,0,0,0}
};
const int CAPACITY = 15;
int bodyX[CAPACITY];
int bodyY[CAPACITY];
int countBody = 0;
int appleX = -1;
int appleY = -1;
int screanGame = 0;
int scoreApple = 0;
#define OLED_RESET 7
Adafruit_SSD1306 display(WIDHT, HEIGHHT, &Wire, OLED_RESET);
void setup()
{
display.begin(SSD1306_SWITCHCAPVCC, 0X3C);
display.clearDisplay();
display.setTextColor(WHITE);
Serial.begin(9600);
pinMode(vert, INPUT);
pinMode(horz, INPUT);
pinMode(button, INPUT);
randomSeed(analogRead(A2));
initSnake();
}
void loop()
{
while(screanGame == 0)
{
display.setCursor(40,10);
display.print("Snake");
display.setCursor(20,30);
display.print("Press button");
display.display();
if(digitalRead(button) == 1)
{
display.clearDisplay();
screanGame = 1;
}
}
while (screanGame == 1)
{
DrawPicture(apple, appleX, appleY);
ViewSnake();
MoveSnake(xShift, yShift);
if(analogRead(horz) == 0) //для двежения влево
{
xShift = 1;
yShift = 0;
}
if(analogRead(vert) == 0) //для двежения в вниз
{
xShift = 0;
yShift = 1;
}
if(analogRead(horz) == 1023) //для двежения в право
{
xShift = -1;
yShift = 0;
}
if(analogRead(vert) == 1023) //для двежения вверх
{
xShift = 0;
yShift = -1;
}
delay(100);
if(bodyX[0] == appleX && bodyY[0] == appleY)
{
EatApple();
scoreApple++;
}
if(IsCrashed())
{
display.clearDisplay();
screanGame = 2;
initSnake();
xShift = -1;
yShift = 0;
}
}
while(screanGame == 2)
{
display.setCursor(20,10);
display.print("End game");
display.setCursor(10,30);
display.print("Score:");
display.setCursor(50,30);
display.print(scoreApple);
display.display();
delay(5000);
display.clearDisplay();
screanGame = 0;
scoreApple = 0;
}
}
void ClearObjakt(int x, int y)
{
const int size = 8;
display.fillRect(x * size, y * size, size, size, SSD1306_BLACK);
display.display();
}
void DrawPicture(bool dataPicture[8][8], int x, int y)
{
const int SIZE = 8;
for(int i = 0; i < SIZE; i++)
{
for(int j = 0; j < SIZE; j++)
{
if(dataPicture[i][j] == 1)
{
display.drawPixel(j + (x * SIZE), i + (y * SIZE), SSD1306_WHITE);
}
}
}
display.display();
}
void ViewSnake()
{
for(int i=0; i < countBody; i++)
{
DrawPicture(headSnake, bodyX[i], bodyY[i]);
}
ClearObjakt(snakeXOld, snakeYOld);
}
void MoveSnake(int directionX, int directionY)
{
snakeXOld = bodyX[countBody - 1];
snakeYOld = bodyY[countBody - 1];
for(int i = countBody - 2; i >= 0; i--)
{
bodyX[i + 1] = bodyX[i];
bodyY[i + 1] = bodyY[i];
}
bodyX[0] += directionX;
bodyY[0] += directionY;
if(bodyX[0] > 15)
{
bodyX[0] = 0;
}
if(bodyX[0] < 0)
{
bodyX[0] = 15;
}
if(bodyY[0] > 7)
{
bodyY[0] = 0;
}
if(bodyY[0] < 0)
{
bodyY[0] = 7;
}
}
void AddBody()
{
bodyX[countBody] = bodyX[countBody] - 1;
bodyY[countBody] = bodyY[countBody] - 1;
countBody++;
}
void EatApple()
{
ClearObjakt(appleX, appleY);
AddBody();
appleX = random(0,15);
appleY = random(0,7);
}
bool IsCrashed()
{
bool isLose = false;
for(int i = 4; i < countBody; i++)
{
if(bodyX[0] == bodyX[i] && bodyY[0] == bodyY[i])
{
isLose = true;
break;
}
}
return isLose;
}
void initSnake()
{
bodyX[0] = 4;
bodyY[0] = 4;
bodyX[1] = 5;
bodyY[1] = 4;
bodyX[2] = 6;
bodyY[2] = 4;
bodyX[3] = 7;
bodyY[3] = 4;
bodyX[4] = 8;
bodyY[4] = 4;
countBody = 5;
appleX = random(0,15);
appleY = random(0,7);
}