/*
Forum: https://forum.arduino.cc/t/hilfe-bei-meinem-dino-spiel/1195564
Wokwi: https://wokwi.com/projects/382918177173098497
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C-Display-Adresse und Zeilenzahl
const byte ROWS = 4; //three rows
const byte COLS = 3; //four columns
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
int dinoX = 1; // X-wert des ">"
int dinoY = 1; // Y-wert des ">"
int obstacleX = 15; //X-wert des Hindernisses "#"
int obstacleY = 1; //Y-wert des Hindernisses "#"
int gameStarted = 0; //wird später zum starten auf 1 gesetzt
void setup() { //LCD Display initialisieren und "Startmenü" anzeigen
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Press 0 to start");
lcd.setCursor(0, 1);
lcd.print("FAKE DINO RUNNER");
pinMode(2, INPUT); //pins für das keypad
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(6, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
}
//LOOP
void loop() {
char key = keypad.getKey(); // Wenn "O" auf dem keypad gedrückt wird, soll das Unterprogramm startGame() ausgeführt werden
if(key == '0'){
delay(1000);
startGame();
gameStarted == 1;
}
moveGame();
updateGame();
}
void moveGame() { // automatische Bewegung des ">" (Dinos) nach rechts Richtung Hinderniss "#"
if(gameStarted == 1){
for(dinoX=1;dinoX<16;dinoX++){
Serial.println(dinoX);
lcd.clear();
lcd.setCursor(dinoX, dinoY);
lcd.print(">");
delay(250);
}
}
}
void updateGame() {
char key = keypad.getKey(); // mit der Taste "2" soll man später springen können, aktuell unrelevant
if (key == '2') {
dinoY = 0;
}
if (dinoX == obstacleX && dinoY == obstacleY) { // wenn der "Dino" im Hinderniss ist, hat man verloren
endGame();
}
}
void endGame() { // GAME-OVER screen
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("GAME OVER");
lcd.setCursor(0, 1);
lcd.print("Press 0 to restart");
obstacleX = 15;
dinoY = 1;
}
void startGame() { //">" und "#", Dino und Hinderniss werden auf das Display geprintet
lcd.clear();
lcd.setCursor(dinoX, dinoY);
lcd.print(">");
lcd.setCursor(obstacleX, obstacleY);
lcd.print("#");
}