1

In this project, I'm making a stickman rush game with a joystick. Where when the joystick moves up and down, the stickman also moves up and down. And there are obstacles, that when the stickman touches one of them it prints out "Game Over.."

Now I'm trying to make the button in the joystick (SM) programmed, that whenever it is clicked, everything resets. In other words, everything goes back to the original screen where the stickman and obstacle first started.

Code:

#include <LiquidCrystal.h>


LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

byte customChar[] = {
  B01110,
  B01110,
  B01110,
  B00100,
  B01110,
  B10101,
  B00100,
  B01010
};

byte gameObstical[] = {
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111
};



int vX = A0;
int vY = A1;
int sw = 8;

int xPosition = 0;
int yPosition = 0;
int SW_state = 0;
int mapX = 0;
int mapY = 0;

int screenWidth = 16;
int screenHeight = 2;


void setup() {
  Serial.begin(9600);

  pinMode(vX, INPUT);
  pinMode(vY, INPUT);
  pinMode(sw, INPUT_PULLUP);


  lcd.begin(16, 2);
  lcd.createChar(0, customChar);
  lcd.home();
  lcd.write(byte(0));

  lcd.createChar(1, gameObstical);


}

void loop() {


  int stickManPosition = 0;
  int obsticalPosition = 0;
  int stickManRow = 0;
  int obsticalRow = 0;

  int obsticalPosition2 = 0;
  int obsticalRow2 = 0;

  for (obsticalPosition = 15; obsticalPosition >= 0; obsticalPosition--) {
    lcd.setCursor(obsticalPosition, obsticalRow);
    lcd.write(byte(1));
    lcd.print(" ");
    delay(150);





    xPosition = analogRead(vX);
    yPosition = analogRead(vY);
    SW_state = digitalRead(sw);
    mapX = map(xPosition, 20, 1003, -500, 500);

    Serial.println("-----------");
    Serial.println(mapX);
    Serial.println(SW_state);
    Serial.println("-----------");



    if (mapX > 200) {

      lcd.clear();
      lcd.setCursor(stickManPosition, stickManRow);
      lcd.write(byte(0));

    } else if (mapX < -200) {

      stickManRow = 1;

      lcd.clear();
      lcd.setCursor(stickManPosition, stickManRow);
      lcd.write(byte(0));
    }




    if (stickManPosition == obsticalPosition) {
      if (stickManRow == obsticalRow) {

        lcd.clear();
        lcd.print("Game Over...");
        delay(10000);

      }

      if (obsticalPosition == 12) {

        for (obsticalPosition2 = 11; obsticalPosition2 >= 0; obsticalPosition2--) {
          obsticalRow2 = 1;

          lcd.setCursor(obsticalPosition2, obsticalRow2);
          lcd.write(byte(1));
          lcd.print(" ");
          delay(150);
        }
      }
    }
  }
}

Any ideas? Thanks for reading

3
  • 1
    break up the loop() block into sections ... which section runs is dependent on a state variable, runMode for example ... when arduino is reset, start at runMode = 0 ... if runMode == 0 then display welcome message and switch to runMode = 1 .... if runMode == 1 then check for start button press, if pressed switch to runMode = 2 ... if runMode == 2 then run game ..... if death, then switch to runMode = 3 ........ etc etc – jsotola Mar 31 at 6:29
  • And for more maintainability, separate each phase (initialization, start-up, in-game, game-over, ...) into its own function. – the busybee Mar 31 at 7:21
  • What @jsotola is describing is called a finite state machine. You should learn more about them before continuing with your project. – Majenko Mar 31 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.