//Copyright (C) 16.03.2024, Kirill ZHivotkov
#include <LedControl.h>
#define MAX_SEG 4
//Инициализируем игровое поле
LedControl matrix = LedControl(8, 10, 9, MAX_SEG); //MAX7219
//Инициализируем игровое поле
//Конфигурация приложения
void setup() {
//Подключаем джойстик
pinMode(A0, INPUT);
pinMode(A1, INPUT);
//Подключаем джойстик
//Включаем каждую из четырех матриц (игровое поле)
for (int index = 0; index < MAX_SEG; index++) {
matrix.shutdown(index, false);
matrix.clearDisplay(index);
matrix.setIntensity(index, 0);
}
//Включаем каждую из четырех матриц (игровое поле)
//Подключаем монитор порта
Serial.begin(9600);
//Подключаем монитор порта
}
//Конфигурация приложения
//Описываем класс "Автомобиль" (игрок)
class Car {
private:
int x = 0;
int y = 0;
int playerIndex = 0;
String moveDir = "";
//Каркас игрока
const int CAR_ROWS = 3;
const int CAR_COLS = 4;
int player[3][4] = {{1, 0, 1, 0},
{1, 1, 1, 1},
{1, 0, 1, 0}};
//Каркас игрока
public:
//Сеттеры
void setPosX(int x) {
this->x = x;
}
void setPosY(int y) {
this->y = y;
}
void setPlayerIndex(int playerIndex) {
if (playerIndex >= -1 && playerIndex <= 3) {
this->playerIndex = playerIndex;
}
}
void setCarDirection(String moveDir) {
if (moveDir == "" || moveDir == "LEFT" || moveDir == "RIGHT") {
this->moveDir = moveDir;
}
}
//Сеттеры
//Геттеры
int getPosX() {
return this->x;
}
int getPosY() {
return this->y;
}
int getPlayerIndex() {
return this->playerIndex;
}
String getCarDirection() {
return this->moveDir;
}
//Геттеры
//Объявляем прототипы методов для данного класса
void playerInit(int, int);
void clearGameField();
void playerMove(int);
void playerJoystick();
//Объявляем прототипы методов для данного класса
//Конструктор по умолчанию
Car() {};
//Конструктор по умолчанию
//Деструктор
~Car() {};
//Деструктор
};
//Описываем класс "Автомобиль" (игрок)
//Метод очистки матрицы для каждого индекса
void Car::clearGameField() {
for (int i = 0; i < MAX_SEG; i++) {
matrix.clearDisplay(i);
}
}
//Метод очистки матрицы для каждого индекса
//Метод установки машики в начальную позицию
void Car::playerInit(int x, int y) {
for (int i = 0; i < CAR_ROWS; i++) {
for (int j = 0; j < CAR_COLS; j++) {
if (player[i][j] == 1) {
matrix.setLed(this->playerIndex, i + y, j + x, 1);
} else if (player[i][j] == 0) {
matrix.setLed(this->playerIndex, i + y, j + x, 0);
}
}
}
}
//Метод установки машики в начальную позицию
//Метод управления игроком (машинкой)
void Car::playerMove(int speed) {
if (this->moveDir == "" || this->moveDir == "RIGHT") {
//Появление игрока
this->playerInit(this->x, this->y);
//Появление игрока
//Переходы между матрицами (движение вправо)
if ((this->x + 1) == 6 && this->playerIndex < CAR_ROWS) {
for (int i = 0; i < CAR_ROWS; i++) {
matrix.setLed(this->playerIndex + 1, i + this->y, 0, player[i][3]);
}
}
else if ((this->x + 1) == 7 && this->playerIndex < CAR_ROWS) {
for (int i = 0; i < CAR_ROWS; i++) {
matrix.setLed(this->playerIndex + 1, i + this->y, 1, player[i][3]);
matrix.setLed(this->playerIndex + 1, i + this->y, 0, player[i][2]);
}
}
else if ((this->x + 1) == 8 && this->playerIndex < CAR_ROWS) {
for (int i = 0; i < CAR_ROWS; i++) {
matrix.setLed(this->playerIndex + 1, i + this->y, 2, player[i][3]);
matrix.setLed(this->playerIndex + 1, i + this->y, 1, player[i][2]);
matrix.setLed(this->playerIndex + 1, i + this->y, 0, player[i][1]);
}
}
else if ((this->x + 1) == 9 && this->playerIndex < CAR_ROWS) {
for (int i = 0; i < CAR_ROWS; i++) {
matrix.setLed(this->playerIndex + 1, i + this->y, 3, player[i][3]);
matrix.setLed(this->playerIndex + 1, i + this->y, 2, player[i][2]);
matrix.setLed(this->playerIndex + 1, i + this->y, 1, player[i][1]);
matrix.setLed(this->playerIndex + 1, i + this->y, 0, player[i][0]);
}
this->setPosX(0);
this->setPlayerIndex(this->playerIndex + 1);
}
//Переходы между матрицами (движение вправо)
}
else if (this->moveDir == "" || this->moveDir == "LEFT") {
//Появление игрока
this->playerInit(this->x, this->y);
//Появление игрока
//Переходы между матрицами (движение влево)
if ((this->x + 1) == 8 && this->playerIndex < CAR_ROWS) {
for (int i = 0; i < CAR_ROWS; i++) {
matrix.setLed(this->playerIndex + 1, i + this->y, 0, player[i][1]);
matrix.setLed(this->playerIndex + 1, i + this->y, 1, player[i][2]);
matrix.setLed(this->playerIndex + 1, i + this->y, 2, player[i][3]);
}
}
else if ((this->x + 1) == 7 && this->playerIndex < CAR_ROWS) {
for (int i = 0; i < CAR_ROWS; i++) {
matrix.setLed(this->playerIndex + 1, i + this->y, 0, player[i][2]);
matrix.setLed(this->playerIndex + 1, i + this->y, 1, player[i][3]);
}
}
else if ((this->x + 1) == 6 && this->playerIndex < CAR_ROWS) {
for (int i = 0; i < CAR_ROWS; i++) {
matrix.setLed(this->playerIndex + 1, i + this->y, 0, player[i][3]);
}
}
//Переходы между матрицами (движение влево)
}
//Задежка времени между перемещениями
delay(speed);
//Задежка времени между перемещениями
}
//Метод управления игроком (машинкой)
//Метод работы со джойстиком
void Car::playerJoystick() {
int a = analogRead(A1);
int b = analogRead(A0);
if (a == 1023 && b == 512) { //Движение влево
if (this->x >= 1 && this->x <= 7) {
this->clearGameField();
this->setPosX(this->x - 1);
this->setCarDirection("LEFT");
} else {
this->clearGameField();
this->setPosX(7);
this->setCarDirection("LEFT");
this->setPlayerIndex(this->playerIndex - 1);
if (this->x == 7 && this->playerIndex == -1) {
this->setPlayerIndex(0);
this->setPosX(0);
}
}
} else if (a == 0 && b == 512) { //Движение вправо
if (this->playerIndex == MAX_SEG - 1) {
if (this->x < MAX_SEG) {
this->clearGameField();
this->setPosX(this->x + 1);
this->setCarDirection("RIGHT");
}
} else {
if (this->x >= 0 && this->x <= 7) {
this->clearGameField();
this->setPosX(this->x + 1);
this->setCarDirection("RIGHT");
}
}
} else if (b == 1023 && a == 512) { //Движение вверх
if (this->y >= 0 && this->y <= 4) {
this->clearGameField();
this->setPosY(this->y + 1);
}
} else if (b == 0 && a == 512) { //Движение вниз
if (this->y >= 1 && this->y <= 5) {
this->clearGameField();
this->setPosY(this->y - 1);
}
}
}
//Метод работы со джойстиком
//Создаем экземпляр класса Car (игрок)
Car* car = new Car();
//Создаем экземпляр класса Car (игрок)
//Выполнение игры в цикле
void loop () {
car->playerMove(50);
car->playerJoystick();
}
//Выполнение игры в цикле