#include <LiquidCrystal_I2C.h>
// Параметры дисплея
const byte LCD_ADDRESS = 0x27;
const byte LCD_H = 4; // Высота (Height)
const byte LCD_W = 20; // Ширина (Width)
// Объект для взаимодействия с дисплеем
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_W, LCD_H);
const byte BUTTON_R = 23; // Кнопка "Вправо" (Right)
const byte BUTTON_L = 25; // Left
const byte BUTTON_U = 27; // Up
const byte BUTTON_D = 29; // Down
// Игрок
const char PLAYER = '@';
const char SPACE = ' ';
const char HP = '+'; // Аптечка
const char WALL = '#'; // Стена
// Позиция игрока
int player_x = 0;
int player_y = 0;
// Размер игровой карты:
const byte MAP_H = 4;
const byte MAP_W = 20;
// Игровая карта:
char game_map[MAP_H][MAP_W];
void generate_map() {
for (int y = 0; y < MAP_H; y++) {
for (int x = 0; x < MAP_W; x++) {
game_map[y][x] = SPACE;
}
}
game_map[0][2] = WALL;
game_map[2][6] = HP;
}
void show_map() {
for (int y = 0; y < MAP_H; y++) {
for (int x = 0; x < MAP_W; x++) {
lcd.setCursor(x, y);
if ((y == player_y) && (x == player_x)) {
lcd.print( PLAYER );
} else {
lcd.print( game_map[y][x] );
}
}
}
}
void setup() {
lcd.init();
lcd.backlight();
pinMode(BUTTON_R, INPUT_PULLUP);
pinMode(BUTTON_L, INPUT_PULLUP);
pinMode(BUTTON_U, INPUT_PULLUP);
pinMode(BUTTON_D, INPUT_PULLUP);
generate_map();
//Serial.begin(9600);
}
char map_get(int x, int y) {
return game_map[y][x];
}
boolean is_hp(int x, int y) {
return map_get(x, y) == HP;
}
boolean is_wall(int x, int y) {
return map_get(x, y) == WALL;
}
void map_set(int x, int y, char object) {
game_map[y][x] = object;
}
void map_clear(int x, int y) {
map_set(x, y, SPACE);
}
void loop() {
if (digitalRead(BUTTON_R) == LOW) {
if ((player_x < MAP_H - 1) && (! is_wall(player_x + 1, player_y))) {
player_x++;
}
}
if (digitalRead(BUTTON_L) == LOW) {
if (! is_wall(player_x - 1, player_y)) {
player_x--;
}
}
if (digitalRead(BUTTON_U) == LOW) {
player_y--;
}
if (digitalRead(BUTTON_D) == LOW) {
player_y++;
}
if (is_hp(player_x, player_y)) {
map_clear(player_x, player_y);
}
show_map();
delay(100);
}