/*
Forum: https://forum.arduino.cc/t/rpg-game-buttons-not-working-progressing-through-story/1199123
Wokwi: https://wokwi.com/projects/383817515037671425
*/
#include <LiquidCrystal.h>
// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
constexpr byte button1Pin = 8; // Button 1 pin
constexpr byte button2Pin = 9; // Button 2 pin
constexpr byte button3Pin = 10; // Button 3 pin
int state = 0; // Track the story progress
struct buttonType {
byte pin;
byte state = HIGH;
byte lastState = HIGH;
unsigned long lastChange = 0;
void init(byte aPin){
pin = aPin;
pinMode(pin, INPUT_PULLUP);
}
boolean released(){
byte actState = digitalRead(pin);
if (actState != lastState){
lastChange = millis();
lastState = actState;
}
if(state != actState && millis()-lastChange >= 50){
state = actState;
return state;
}
return false;
}
};
buttonType button1, button2, button3;
void setup() {
Serial.begin(9600);
button1.init(button1Pin);
button2.init(button2Pin);
button3.init(button3Pin);
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
showStory("Lost Treasure of", "Eldorado. Begin?");
}
void loop() {
if (button1.released()) {
progressStory(1);
}
if (button2.released()) {
progressStory(2);
}
if (button3.released()) {
progressStory(3);
}
}
void progressStory(int choice) {
if (state == 0 && choice == 1) {
showStory("You're in a jungle", "Which path?");
state = 1;
} else if (state == 1) {
if (choice == 1) {
showStory("Path leads to cave", "Find a map!");
state = 2;
} else if (choice == 2) {
showStory("Climb a tree and", "spot a village");
state = 3;
} else {
showStory("Cross a river", "Discover a clue");
state = 4;
}
} else if (state == 2 && choice == 1) {
showStory("Map shows a path", "to hidden city");
state = 5;
} else if (state == 3 && choice == 1) {
showStory("Village elder gives", "a golden key");
state = 5;
} else if (state == 4 && choice == 1) {
showStory("Clue leads to a", "mysterious cave");
state = 5;
} else if (state == 5) {
if (choice == 1) {
showStory("You find Eldorado", "Treasure is yours!");
state = 6;
} else if (choice == 2) {
showStory("You decide to explore", "the hidden city further");
state = 7;
} else {
showStory("You follow another clue", "What do you find?");
state = 10;
}
} else if (state == 6 && choice == 1) {
showStory("Adventure ends", "Play again?");
state = 0;
} else if (state == 7) {
if (choice == 1) {
showStory("You discover a", "secret chamber");
state = 8;
} else if (choice == 2) {
showStory("You encounter a", "mysterious figure");
state = 9;
}
} else if (state == 8) {
if (choice == 1) {
showStory("Inside the chamber,", "you find ancient artifacts");
state = 9;
} else {
showStory("You leave the chamber", "What's your next move?");
state = 10;
}
} else if (state == 9) {
if (choice == 1) {
showStory("The figure reveals", "a hidden power");
state = 10;
} else {
showStory("You try to escape", "but the figure blocks your way");
state = 11;
}
} else if (state == 10 && choice == 1) {
showStory("Congratulations!", "You are a legendary explorer!");
state = 11;
} else if (state == 11 && choice == 1) {
showStory("The mysterious figure", "becomes your ally");
state = 12;
} else if (state == 12 && choice == 1) {
showStory("Together, you uncover", "even greater mysteries");
state = 13;
} else if (state == 13 && choice == 1) {
showStory("Your legend grows,", "and your adventures continue!");
state = 14;
} else if (state == 14 && choice == 1) {
showStory("Thanks for playing!", "Would you like to play again?");
state = 0;
}
}
void showStory(String line1, String line2) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(line1);
lcd.setCursor(0, 1);
lcd.print(line2);
}