#include <LiquidCrystal_I2C.h>
#include <LiquidCrystal.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
#define VERT_PIN A1
#define HORZ_PIN A2
#define SEL_PIN 4
int bombchecker[20][4];
int x = 0;
int y = 0;
int xa = 0;
int ya = 0;
int score = 0;
int xbomb;
int ybomb;
int bombscore = score;
int stat = 1;
int ab;
int xab = 0;
int yab = 0;
int scoreab = 0;
int statab = 0;
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
LiquidCrystal lcd2(12, 11, 10, 9, 8, 7);
void setup()
{
randomSeed(analogRead(0));
lcd.init();
lcd.backlight();
lcd2.begin(16, 2);
pinMode(VERT_PIN, INPUT);
pinMode(HORZ_PIN, INPUT);
pinMode(SEL_PIN, INPUT_PULLUP);
xa = random (0, 20);
ya = random (0, 4);
lcd.setCursor(xa, ya);
lcd.print(char(229));
lcd.setCursor(x, y);
lcd.print(char(252));
lcd2.print("Score:0");
lcd2.setCursor(0, 1);
lcd2.print("Ability:0");
}
void loop()
{
int vert = analogRead(A1);
int horz = analogRead(A2);
bool selPressed = digitalRead(SEL_PIN) == LOW;
// Передвижение
if (vert == 0) // Вниз
{
movedown();
dead();
}
else if (vert == 1023) // Вверх
{
moveup();
dead();
}
else if (horz == 0) // Направо
{
moveright();
dead();
}
else if (horz == 1023) // Налево
{
moveleft();
dead();
}
// Яблочко
if (x==xa and y==ya)
{
appleget();
lcd2.setCursor(6, 0); // Счётчик
lcd2.print(score);
}
// Способность
if (selPressed == true and scoreab>=1)
{
ability();
delay (200);
}
// Счетчик способности
if (x==xab and y==yab and statab==1)
{
abilityget();
lcd2.setCursor(8, 1); // Счётчик
lcd2.print(scoreab);
}
// Мины
if (bombscore >= 1 and stat == 1)
{
stat = 0;
memset(bombchecker, 0, sizeof(bombchecker[0][0]) * 20 * 4);
int q = bombscore;
lcdclear();
for (int i = 0; i < q; i++)
{
bombspawn();
bombchecker[xbomb][ybomb] = 1;
bombscore++;
}
}
}
// ------------------Движения Бибки:------------------
// Вниз
void movedown()
{
lcd.setCursor(x, y);
lcd.print(char(32));
lcd.setCursor(x, y+1);
lcd.print(char(252));
y = y+1;
if (y == 4)
{
y = 0;
}
delay(200);
}
// Вверх
void moveup()
{
lcd.setCursor(x, y);
lcd.print(char(32));
lcd.setCursor(x, y-1);
lcd.print(char(252));
y = y-1;
if (y == (-1))
{
y = 3;
}
delay(200);
}
// Направо
void moveright()
{
lcd.setCursor(x, y);
lcd.print(char(32));
lcd.setCursor(x+1, y);
lcd.print(char(252));
x = x+1;
if (x == 20)
{
int i = 0;
while (i<4)
{
x = 0;
lcd.setCursor(x, i);
lcd.print(char(32));
lcd.setCursor(16, 0);
lcd.print(char(32));
i++;
}
lcd.setCursor(x, y);
lcd.print(char(252));
lcd.setCursor(xa, ya);
lcd.print(char(229));
}
delay(200);
}
// Налево
void moveleft()
{
lcd.setCursor(x, y);
lcd.print(char(32));
lcd.setCursor(x-1, y);
lcd.print(char(252));
x = x-1;
if (x == (-1))
{
int i = 0;
while (i<4)
{
x = 19;
lcd.setCursor(x, i);
lcd.print(char(32));
lcd.setCursor(0, 0);
lcd.print(char(32));
i++;
}
lcd.setCursor(x, y);
lcd.print(char(252));
}
delay(200);
}
// Использование cпособности
void ability()
{
scoreab-- ;
lcd2.setCursor(8, 1); // Счётчик теперь работает адекватно, если что тут был Слава
lcd2.print(scoreab);
if (scoreab<10)
{lcd2.setCursor(9, 1); // Счётчик
lcd2.print(char(32));}
else{
lcd2.setCursor(8, 1); // Счётчик
lcd2.print(scoreab);
}
lcd.setCursor(x+1, y);
lcd.print(char(32));
bombchecker[x+1][y] = 0;
lcd.setCursor(x-1, y);
lcd.print(char(32));
bombchecker[x-1][y] = 0;
lcd.setCursor(x, y+1);
lcd.print(char(32));
bombchecker[x][y+1] = 0;
lcd.setCursor(x, y-1);
lcd.print(char(32));
bombchecker[x][y-1] = 0;
lcd.setCursor(xa, ya);
lcd.print(char(229));
if (statab == 1){ // теперь нельзя удалить ни обилку, ни яблоко
lcd.setCursor(xab, yab); // тут был Слава
lcd.print(char(244));
}
}
//------------------------------------
// ------------------Яблоко:------------------
// Поднятие яблока
void appleget()
{
xa = random (1, 18);
ya = random (0, 4);
while (xa==x and ya==y)
{
xa = random (1, 18);
ya = random (0, 4);
}
lcd.setCursor(xa, ya);
lcd.print(char(229));
lcd.setCursor(x, y);
score++;
bombscore = score;
xab = random (1, 18);
yab = random (0, 4);
while ((xab==x and yab==y) or (xab==xa and yab==ya))
{
xab = random (1, 18);
yab = random (0, 4);
}
lcd.setCursor(xab, yab);
lcd.print(char(244));
lcd.setCursor(x, y);
stat = 1;
statab = 1;
}
// Поднятие способности
void abilityget()
{ statab = 0;
scoreab++;
}
// Мины
void bombspawn()
{
xbomb = random (1, 18);
ybomb = random (0, 4);
while (xbomb == x and ybomb == y)
{
xbomb = random (1, 18);
ybomb = random (0, 4);
}
while (xbomb == xa and ybomb == ya)
{
xbomb = random (1, 18);
ybomb = random (0, 4);
}
lcd.setCursor(xbomb, ybomb);
lcd.print("*");
}
// Чистка
void lcdclear()
{
int xcl = 0;
while (xcl != 20)
{
lcd.setCursor(xcl, 0);
lcd.print(char(32));
lcd.setCursor(xcl, 1);
lcd.print(char(32));
lcd.setCursor(xcl, 2);
lcd.print(char(32));
lcd.setCursor(xcl, 3);
lcd.print(char(32));
xcl++;
}
lcd.setCursor(x, y);
lcd.print(char(252));
lcd.setCursor(xa, ya);
lcd.print(char(229));
lcd.setCursor(xab, yab);
lcd.print(char(244));
}
// Press F
void dead()
{
if (bombchecker[x][y] == 1)
{
while (true)
{
// передвигаем надпись вправо
for (int i = 0; i < 13; i++) {
//выводим полосы
lcd.setCursor(0, 0);
lcd.print("--------------------");
lcd.setCursor(0, 3);
lcd.print("--------------------");
lcd.setCursor(9, 2);
lcd.print("LOH");
lcd.setCursor(i, 1);
lcd.print("YOU DEAD");
delay(200);
lcd.clear(); // очищаем экран
}
// передвигаем надпись влево
for (int i = 12; i > 0; i--) {
// выводим полосы
lcd.setCursor(0, 0);
lcd.print("--------------------");
lcd.setCursor(0, 3);
lcd.print("--------------------");
lcd.setCursor(9, 2);
lcd.print("LOH");
lcd.setCursor(i, 1);
lcd.print("YOU DEAD");
delay(200);
lcd.clear(); // очищаем экран
}
}
}
}
//------------------------------------