#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int BUTTON_L = 2; //Кнопка "Влево"
const int BUTTON_R = 3; //Кнопка "Вправо"
const int BUTTON_U = 4; //Кнопка "Вверх"
const int BUTTON_D = 5;//Кнопка "Вниз"
const char PLAYER = 1; //Игрок
byte player_image[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b01110,
0b01010,
0b01110,
0b00000
};
const char SPACE = ' '; //Пространство
const char WALL = 0xff; //Стена
const char HP = 'H'; //Здоровье
const char MONSTER_1 = 3;
byte monster_1_image[8] = {
0b00000,
0b00000,
0b01010,
0b00000,
0b01110,
0b10001,
0b00000,
0b00000
};
//Размер игровой карты.
const int MAP_W = 20; //Ширина
const int MAP_H = 4; //Высота
//Игровая карта.
char game_map [MAP_H][MAP_W];
int player_x = 0;
int player_y = 0;
void map_show(int px, int py){
for (int y = 0; y < MAP_H; y++) {
for (int x = 0; x < MAP_W; x++) {
lcd.setCursor(x, y);
if ((y == py) && (x == px)) {
lcd.print (PLAYER);
} else {
lcd.print (game_map [y][x] );
}
}
}
}
void map_generate(){
for (int y= 0; y < MAP_H; y++) {
for (int x= 0; x < MAP_W; x++) {
game_map [y][x] = SPACE;
}
}
game_map[0][5] = WALL;
game_map[1][5] = WALL;
game_map[2][5] = WALL;
game_map[2][4] = WALL;
game_map[1][2] = HP;
game_map[1][0] = MONSTER_1;
}
//Функция для проверки, находится ли на позиции x, y стена.
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;
}
void map_set(int x, int y, char object) {
game_map[y][x] = object;
}
void setup() {
lcd.init();
lcd.backlight(); //подсветка
pinMode(BUTTON_L, INPUT_PULLUP);
pinMode(BUTTON_R, INPUT_PULLUP);
pinMode(BUTTON_U, INPUT_PULLUP);
pinMode(BUTTON_D, INPUT_PULLUP);
//Serial.begin(9600);
lcd.createChar(PLAYER, player_image);
lcd.createChar(MONSTER_1, monster_1_image);
map_generate();
}
void loop() {
map_show(player_x, player_y);
//Это уже не надо:
//lcd.setCursor(player_x, player_y)
//lcd.print(PLAYER);
if (digitalRead(BUTTON_R) == LOW) {
if ((player_x < MAP_W - 1) && (! is_wall(player_x + 1, player_y)) ){
player_x++;
}
}
if (digitalRead(BUTTON_L) == LOW) {
if ((player_x < MAP_W - 1) && (! is_wall(player_x - 1, player_y)) ){
player_x--;
}
}
if (digitalRead(BUTTON_U) == LOW) {
if ((player_y > 0) && (! is_wall(player_x , player_y - 1))) {
player_y--;
}
}
if (digitalRead(BUTTON_D) == LOW) {
if ((player_y < MAP_H - 1) && (! is_wall(player_x , player_y + 1))) {
player_y++;
}
}
}