#include <LiquidCrystal_I2C.h>
#include "graph.h"
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int LCD_H = 4; // высота
const int LCD_W = 20; // ширина
const int MAP_H = 4;
const int MAP_W = 20;
char game_map[MAP_H][MAP_W];
const char player = 239;
const char space = ' ' ;
const char wall = '|' ;
const char hp = '@' ;
const char monster = '$' ;
const char item = 243 ;
int p_x = 0;
int p_y = 0;
const int button_l = 1;
const int button_r = 2;
const int button_up = 3;
const int button_down = 4;
void map_show () {
for (int y = 0; y < LCD_H; y++) {
for (int x = 0; x < LCD_W; x++) {
lcd.setCursor(x, y);
if ((p_x == x) && (p_y == y)) {
lcd.print(player);
}
else {
lcd.print(game_map[y][x]);
}
}
}
}
void map_gen() {
for (int y = 0; y < LCD_H; y++) {
for (int x = 0; x < LCD_W; x++) {
game_map[y][x] = space;
}
}
game_map[0][5] = wall;
game_map[0][10] = hp;
game_map[1][2] = wall;
game_map[1][8] = monster;
game_map[1][15] = item;
}
void setup() {
lcd.init();
lcd.backlight();
pinMode(button_l, INPUT_PULLUP);
pinMode(button_r, INPUT_PULLUP);
pinMode(button_up, INPUT_PULLUP);
pinMode(button_down, INPUT_PULLUP);
map_gen();
}
//функция проверки, находится ли на клетке карты
bool is_wall(int x, int y) {
return game_map[y][x] == wall;
}
bool is_hp(int x, int y) {
return game_map[y][x] == hp;
}
bool is_monster(int x, int y) {
return game_map[y][x] == monster;
}
bool is_item(int x, int y) {
return game_map[y][x] == item;
}
//функция для размещения обьекта на карте
//по координатам х и у
void map_set(int x, int y, char object) {
game_map[x][y] = object;
}
void game_over () {
lcd.setCursor (0, 0);
lcd.print("---OKONCHENO---");
lcd.setCursor (0, 1);
lcd.print("---------------");
delay(2000);
p_x = 0;
p_y = 0;
map_gen();
// lcd.noBacklight();
}
void win() {
lcd.setCursor (0, 0);
lcd.print("----------------");
lcd.setCursor (0, 1);
lcd.print("---URA POBEDA---");
delay(2000);
p_x = 0;
p_y = 0;
map_gen();
// lcd.noBacklight();
}
void loop() {
map_show();
if (digitalRead(button_l) == LOW) {
if ((p_x > 0) && (! is_wall(p_x - 1, p_y))) {
p_x --;
}
}
if (digitalRead(button_r) == LOW) {
if ((p_x < LCD_W - 1) && (! is_wall(p_x + 1, p_y))) {
p_x ++;
}
}
if (digitalRead(button_up) == LOW) {
if ((p_y > 0) && (! is_wall(p_x, p_y - 1))) {
p_y--;
}
}
if (digitalRead(button_down) == LOW) {
if ((p_y < LCD_H - 1) && (! is_wall(p_x, p_y + 1))) {
p_y++;
}
}
if (is_hp(p_x, p_y)) {
map_set(p_x, p_y, space);
}
if (is_monster(p_x, p_y)) {
game_over();
}
if (is_item(p_x, p_y)) {
win();
}
lcd.setCursor(p_x, p_y );
lcd.print(player);
delay(100);
lcd.clear();
}