#include <LiquidCrystal_I2C.h>
#include <LedControl.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int SW1_pin = 53; // Set pins for joysticsk, LCD, MATRIX LED
const int X1_pin = 0;
const int Y1_pin = 1;
int X1_map = 0;
int Y1_map = 0;
LedControl ledControls[] = {
LedControl(2, 4, 3, 4),
LedControl(5, 7, 6, 4),
LedControl(8, 10, 9, 4),
LedControl(11, 13, 12, 4),
};
void setup() {
Serial.begin(115200);
pinMode(SW1_pin, INPUT);
digitalWrite(SW1_pin, HIGH);
lcd.init();
lcd.clear();
lcd.backlight();
LCD_Welcome(); // We are welcomed with arduino uno game by Jakub Almasi TUKE-FEI 2024/25
delay(2000);
}
void loop(){
bool mode = 0;
int game = 0;
while(1){ // if in game its choosed back it will start again with choosing mode
mode = LCD_MODE(); // We will choose mode singleplayer or multiplayer
if(mode == 0){
game = LCD_GAMES_S(mode); // We are choosing game to play
}
if(mode == 1){
game = LCD_GAMES_M(mode); // We are choosing game to play
}
if(game != 1){
break;
}
}
Serial.print("Mode is: ");
Serial.print(mode);
Serial.println();
Serial.print("Game is: ");
Serial.print(game);
Serial.println();
while(1){
if(mode == 0 && game == 2){
snake(2);
}
}
delay(10000);
}
void LCD_Welcome(){
lcd.setCursor(0,0);
lcd.print("WELCOME");
lcd.setCursor(0,1);
lcd.print("CHOOSE MODE");
}
bool LCD_MODE(){ //choose mode singleplayer or multiplayer, mode in return
delay(100);
bool change_1 = 0;
bool what_mode = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" * SINGLEPLAYER ");
lcd.setCursor(0,1);
lcd.print(" MULTIPLAYER ");
while(digitalRead(SW1_pin) == 1){
X1_map = map(analogRead(X1_pin), 0, 1023, -50, 50);
if(X1_map > 40 || X1_map < -40 || change_1 == 1){
change_1 = 1;
if(X1_map < 10 && X1_map > -10){
if(what_mode == 0){
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" * ");
what_mode = 1;
change_1 = 0;
}else{
lcd.setCursor(0,0);
lcd.print(" * ");
lcd.setCursor(0,1);
lcd.print(" ");
what_mode = 0;
change_1 = 0;
}
}
}
}
while(digitalRead(SW1_pin) == 0){
}
delay(20);
return what_mode;
}
int LCD_GAMES_S(bool mode){
delay(100);
bool change_1 = 1;
int what_mode = 2;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" * SNAKE ");
lcd.setCursor(0,1);
lcd.print(" TETRIS ");
while(digitalRead(SW1_pin) == 1){
X1_map = map(analogRead(X1_pin), 0, 1023, -50, 50);
if(X1_map > 40 && change_1 == 1){
what_mode++;
if(what_mode == 3){
what_mode = 0;
}
change_1 = 0;
}
if(X1_map < -40 && change_1 == 1){
what_mode--;
if(what_mode == -1){
what_mode = 2;
}
change_1 = 0;
}
if(X1_map < 10 && X1_map > -10 && change_1 == 0){
if(what_mode == 0){
lcd.setCursor(0,0);
lcd.print(" SNAKE ");
lcd.setCursor(0,1);
lcd.print(" * TETRIS ");
}else if(what_mode == 1){
lcd.setCursor(0,0);
lcd.print(" TETRIS ");
lcd.setCursor(0,1);
lcd.print(" * BACK ");
}
else if(what_mode == 2){
lcd.setCursor(0,0);
lcd.print(" * SNAKE ");
lcd.setCursor(0,1);
lcd.print(" TETRIS ");
}
change_1 = 1;
}
}
delay(20);
return what_mode;
}
int LCD_GAMES_M(bool mode){
delay(100);
bool change_1 = 1;
int what_mode = 2;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" * METEORS ");
lcd.setCursor(0,1);
lcd.print(" PING-PONG ");
while(digitalRead(SW1_pin) == 1){
X1_map = map(analogRead(X1_pin), 0, 1023, -50, 50);
if(X1_map > 40 && change_1 == 1){
what_mode++;
if(what_mode == 3){
what_mode = 0;
}
change_1 = 0;
}
if(X1_map < -40 && change_1 == 1){
what_mode--;
if(what_mode == -1){
what_mode = 2;
}
change_1 = 0;
}
if(X1_map < 10 && X1_map > -10 && change_1 == 0){
if(what_mode == 0){
lcd.setCursor(0,0);
lcd.print(" METEORS ");
lcd.setCursor(0,1);
lcd.print(" * PING-PONG ");
}else if(what_mode == 1){
lcd.setCursor(0,0);
lcd.print(" PING-PONG ");
lcd.setCursor(0,1);
lcd.print(" * BACK ");
}
else if(what_mode == 2){
lcd.setCursor(0,0);
lcd.print(" * METEORS ");
lcd.setCursor(0,1);
lcd.print(" PING-PONG ");
}
change_1 = 1;
}
}
delay(20);
return what_mode;
}
void snake(int dificulty){
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("TIME IS:");
lcd.setCursor(0,1);
lcd.print("SCORE IS:");
bool matrix[32][32];
for(int i=0; i<32; i++){
for(int j=0; j<32; j++){
matrix[i][j] = 0;
}
}
int delay_time = 0;
int board = 3;
int square = 3;
int row = 7;
int column = 31;
int last_joystick_1_position = 0;
int tmp = 0;
int tmp_column = 0;
int tmp_row = 0;
int history[3][4];
int apple[4];
bool apple_eaten = 1;
int score = 0;
int milisec = 0;
int tmp_sec = 0;
int divider = 0;
int seconds = 0;
bool only_start = 0;
int time = 0;
for(int i=0; i<3; i++){
for(int j=0; j<4; j++){
history[i][j] = 0;
}
}
for(int j=0; j<4; j++){
apple[j] = 0;
}
if(dificulty == 0){
time = 70;
}else if(dificulty == 1){
time = 60;
}else if(dificulty == 2){
time = 50;
}
while(1){
milisec = millis();
tmp_sec = milisec/1000;
if(tmp_sec < 0){
tmp_sec = tmp_sec * (-1);
}
if(tmp_sec % 2 == 1 && divider == 0){
divider = 1;
seconds++;
Serial.println(tmp_sec);
Serial.println(divider);
Serial.println(seconds);
lcd.setCursor(8,0);
lcd.print(seconds);
}
if(tmp_sec % 2 == 0 && divider == 1){
divider = 0;
seconds++;
Serial.println(tmp_sec);
Serial.println(divider);
Serial.println(seconds);
lcd.setCursor(8,0);
lcd.print(seconds);
}
if(seconds == 60){
//Serial.print("Your score is: ");
//Serial.print(score);
break;
}
if(apple_eaten == 1){
if(milisec < 0){
milisec = milisec * (-1);
}
if(milisec == 0)
{
milisec++;
}
delay_time = (milisec % 40);
delay(delay_time);
apple[0] = (milisec % 4); // board
//Serial.print("board: ");
//Serial.print(apple[0]);
milisec = milisec - seconds - score;
if(milisec <= 0){
milisec = milisec - 1;
milisec = milisec * (-1);
}
apple[1] = (milisec % 4); // square
//Serial.print(" square: ");
//Serial.print(apple[1]);
milisec = milisec - seconds - score + 19;
if(milisec <= 0){
milisec = milisec - 1;
milisec = milisec * (-1);
}
apple[2] = (milisec % 7); // row
//Serial.print(" row: ");
//Serial.print(apple[2]);
milisec = milisec - seconds - score + 27;
if(milisec <= 0){
milisec = milisec - 1;
milisec = milisec * (-1);
}
apple[3] = (milisec % 7); // column
//Serial.print(" column: ");
//Serial.println(apple[3]);
ledControls[apple[0]].setLed(apple[1], apple[2], apple[3], true); // ktora doska - stvorec - riadok - stlpec
apple_eaten = 0;
delay(time - delay_time);
}else{
ledControls[apple[0]].setLed(apple[1], apple[2], apple[3], true);
delay(time);
}
tmp = Joystick_1(last_joystick_1_position);
last_joystick_1_position = tmp;
if(last_joystick_1_position == 0){
tmp_row++;
if(tmp_row == 32){
tmp_row = 0;
}
}else if(last_joystick_1_position == 1){
tmp_row--;
if(tmp_row == -1){
tmp_row = 31;
}
}else if(last_joystick_1_position == 2){
tmp_column--;
if(tmp_column == -1){
tmp_column = 31;
}
}else if(last_joystick_1_position == 3){
tmp_column++;
if(tmp_column == 32){
tmp_column = 0;
}
}
if(tmp_column<8){
square = 0;
column = tmp_column;
}else if(tmp_column>=8 && tmp_column<16){
square = 1;
column = tmp_column - 8;
}else if(tmp_column>=16 && tmp_column<24){
square = 2;
column = tmp_column - 16;
}else if(tmp_column>=24 && tmp_column<32){
square = 3;
column = tmp_column - 24;
}
if(tmp_row<8){
board = 0;
row = tmp_row;
}else if(tmp_row>=8 && tmp_row<16){
board = 1;
row = tmp_row - 8;
}else if(tmp_row>=16 && tmp_row<24){
board = 2;
row = tmp_row - 16;
}else if(tmp_row>=24 && tmp_row<32){
board = 3;
row = tmp_row - 24;
}
history[0][0] = history[1][0];
history[0][1] = history[1][1];
history[0][2] = history[1][2];
history[0][3] = history[1][3];
history[1][0] = history[2][0];
history[1][1] = history[2][1];
history[1][2] = history[2][2];
history[1][3] = history[2][3];
history[2][0] = history[3][0];
history[2][1] = history[3][1];
history[2][2] = history[3][2];
history[2][3] = history[3][3];
history[3][0] = board;
history[3][1] = square;
history[3][2] = row;
history[3][3] = column;
if(history[3][0] == apple[0]){
if(history[3][1] == apple[1]){
if(history[3][2] == apple[2]){
if(history[3][3] == apple[3]){
apple_eaten = 1;
score = score + 1;
//Serial.print("Score is: ");
//Serial.println(score);
lcd.setCursor(9,1);
lcd.print(score);
}
}
}
}
ledControls[history[3][0]].setLed(history[3][1], history[3][2], history[3][3], true); // ktora doska - stvorec - riadok - stlpec
ledControls[history[0][0]].setLed(history[0][1], history[0][2], history[0][3], false); // ktora doska - stvorec - riadok - stlpec
}
}
int Joystick_1(int last_joystick_1_position){
int state = last_joystick_1_position;
int x_map = map(analogRead(X1_pin), 0, 1023, -50, 50);
int y_map = map(analogRead(Y1_pin), 0, 1023, -50, 50);
delay(20);
if(x_map >= 40){
return 1;
}
if(x_map <= -40){
return 0;
}
if(y_map >= 40){
return 3;
}
if(y_map <= -40){
return 2;
}
return state;
}
void tetris(){
}
void meteors(){
}
void ping_pong(){
}