Stopping an Arduino LCD game after a certain level

Hello,

For a school project I am currently trying to get a game working on the I2C LCD screen with a joystick. I am using this project:

https://create.arduino.cc/projecthub/vovxb/lcd-game-console-f8a915?ref=part&ref_id=23573&offset=0

The game works excellent and I can play it without any problems. But, my question is: How can I get the game to stop after 4 or 5 levels of playing and displaying a self-typed text, like for example: "Congratulations! You've made it".
I've already tried a lot but I won't get it happen to work.

I'm using the code from the project of the link that I posted. Every help is very much appreciated! Thanks in advance!!

Welcome to the forum

Please post the sketch that you wrote, using code tags when you do

Hello UKHeliBob,

Thank you! See the sketch below:

#include <Wire.h>
#include <VirtualWire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);

// VARIABLES DECLARATION
int game = 0;
int gameover = 0;
int le = 0;
float ll = 0;
int key = LOW;
int mov=0;
int cc=1;
int screen1[202];
int screen2[202];
int ind = 0;
int randomNumber=0;
int ax=0;
int ay=0;
int timegame=200;
int clef = 0;
int jump = 0;
int path = 42;
int ck = 2;

// CHARS DECLARATION
byte man1[] = {0x04, 0x0E, 0x0E, 0x0E, 0x04, 0x04, 0x1F, 0x1F} ;
byte man2[] = {0x04, 0x1E, 0x05, 0x04, 0x0B, 0x10, 0x1F, 0x1F} ;
byte man3[] = {0x04, 0x0F, 0x14, 0x04, 0x1A, 0x01, 0x1F, 0x1F} ;
byte man4[] = {0x15, 0x0E, 0x04, 0x0E, 0x11, 0x00, 0x00, 0x00} ;
byte man5[] = {0x15, 0x0E, 0x04, 0x0E, 0x11, 0x00, 0x1F, 0x1F} ;
byte man6[] = {0x00, 0x00, 0x04, 0x0E, 0x04, 0x04, 0x1F, 0x1F} ;
byte flat[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x1F};
byte hole[] = {0x0E, 0x1F, 0x15, 0x1B, 0x0E, 0x04, 0x1F, 0x1F} ;
byte m1[] = {0x1F, 0x1F, 0x1F, 0x19, 0x10, 0x00, 0x00, 0x00} ;
byte m2[] = {0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x0F, 0x06, 0x00} ;
byte m3[] = {0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x0C, 0x00, 0x00} ;
byte m4[] = {0x1F, 0x1F, 0x00, 0x0A, 0x00, 0x0A, 0x00, 0x0A} ;
byte m5[] = {0x1F, 0x1F, 0x1F, 0x00, 0x0A, 0x00, 0x0A, 0x00} ;
byte m6[] = {0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00, 0x0A, 0x00} ;
byte keys[8] = {0x00, 0x03, 0x1F, 0x13, 0x00, 0x00, 0x1F, 0x1F} ;
byte dooro[8] = {0x1F, 0x11, 0x11, 0x11, 0x11, 0x11, 0x1F, 0x1F} ;
byte door[8] = {0x1F, 0x1F, 0x1D, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F} ;


 
void setup(){
  lcd.begin();
  lcd.backlight();
  lcd.home();
  lcd.clear();
// CHARACTER ASSOCIATION
  lcd.createChar(1, man1);
  lcd.createChar(2, flat);
  lcd.createChar(3, hole);
  lcd.createChar(8, keys);
  lcd.createChar(7, door);
}

  
void loop(){
  if (game == 0)
  {
    if (gameover == 0)
      {
      // TITLE SCREEN SCREEN
        lcd.home();
        lcd.setCursor(0, 0);
        lcd.print("  key game");
        lcd.setCursor(0, 1);
        lcd.print(" 2020"); 
        key = digitalRead(7);
        if (key == LOW)
            {
            // INIT GAME
            lcd.clear();
            lcd.home();
            le = 0;
            ll = 0;
            gameover = 1;
            cc=2;
            jump = 0;
            path = 16;
            } 
       }
     else
       {
           // NEW SCREEN - START
            randomSeed(analogRead(0));
            game = 1;
            key = LOW;
            le = le + 1;
            ll = ll + 1;
            path = path + 10;
            if (path>202)
            {
            path = 202;
            }
            ind = 0;
            for(int s=0;s<(path + 10);s++)
             {
             screen1[s] = 2;        
             }
               
            for(int s=11;s<(path -2 );s=s+3)
             {
             randomNumber = int(random(2, 4));
             //randomNumber = 3;
             screen1[s] = randomNumber;        
             }
             
            screen1[6]=7;
            screen1[path + 1]=8;

            for(int s=0;s<(path+10);s++)
             {
             randomNumber = int(random(4, 7));
             screen2[s] = randomNumber;        
             }
             
           if ((ll/2) == int(ll/2))
             {
              lcd.createChar(4, m4);
              lcd.createChar(5, m5);
              lcd.createChar(6, m6);
             }
             else
             {
              lcd.createChar(4, m1);
              lcd.createChar(5, m2);
              lcd.createChar(6, m3);
             }
           timegame = 200;
           clef = 0;
           lcd.createChar(7, door);
           cc = 2;
           ck = 2;
     }
  }
  else
  {
    
  // GAME LOOP
      if (jump == 3)
      {
      jump = 0;
      }
      
      lcd.home();
      timegame = timegame - le;
      for(int s=ind;s<(ind+16);s++)
      {
       lcd.setCursor(s-ind, 0);
       lcd.write(byte(screen2[s]));
       lcd.setCursor(s-ind, 1);
       lcd.write(byte(screen1[s]));
      }

      if (timegame < 0)
      {
        timegame = 0;
      }
      lcd.setCursor(16-String(timegame).length(), 0);
      lcd.print(timegame);

if (jump == 0)
{
     lcd.setCursor(7, 1);
     lcd.write(byte(1));
     ck = screen1[ind+7];
     if (mov == 1)
      {
      if (cc==1)
      {
        lcd.createChar(1, man1);
      }
      else
      {
        lcd.createChar(1, man1);
      }
      mov = 0;
      }
      else
      {
      lcd.createChar(1, man1);
      mov = 1;
      }
}

if (jump > 0)
{
       lcd.setCursor(7, 0);
       lcd.write(byte(1));
       lcd.createChar(1, man4);
       if (cc == 2)
       {
       ind = ind + 1;
       }
       else
       {
       ind = ind - 1;
       }
  if (jump == 1)
  {
       jump = 2;
  }
  else
  {
       jump = 3;
  }
}    
      // CHECK LIVE OR DIE - LOOP GAME OR GAME OVER
      if ((ck == 2)&&(timegame>0))  // SET THE VARIABLE TO CHECK
      {
      ay = analogRead(1);   
      ax = analogRead(0);
      if ((ax > 600) && (jump == 0))
        {
            if (ind<(path-7))
            {
            ind = ind + 1;
            cc = 2;
            }
            else
            {
              screen1[path + 1]=2;
              clef = 1;
              lcd.createChar(7, dooro);
            }
        }
      
      if ((ax < 200) && (jump == 0))
      {
            if (ind>0)
            {
               ind = ind - 1;
               cc = 1;
            }
            else
            {
             if (clef==1)
             {
              // CHECK LEVEL CLEAR - START
            lcd.setCursor(0, 1);
            lcd.print("                ");
            lcd.setCursor((int(10-String(le).length())/2),1);
            lcd.print("LEVEL ");
            lcd.print(le+1);
            delay(4000);
            lcd.clear();    
            game = 0;
            gameover = 1;

//  CHECK LEVEL CLEAR - END
              
              }
            }
      }


      if ((ay < 200) && (ind >1) && (ind <(path - 8)) && (jump == 0))
      {
      jump = 1;
      }


      

      delay(200);
      

// GAME LOOP - END

  }
  else
  {

//  GAME OVER - START
    if (le == 2)
      {
      lcd.setCursor(0, 0);
      lcd.print("   GAME  OVER :(   ");
      lcd.setCursor(7, 1);
      lcd.write(byte(1));
      lcd.createChar(1, man6);
      }
      else
      {
      lcd.setCursor(0, 1);
      lcd.print("   TIME  OVER   ");  
      }
      delay(4000);
      lcd.clear();    
      game = 0;
      gameover =0;

//  GAME OVER - END

    }
  }
}

Thanks in advance!

Your code seems to test whether the level is 2 and, if so, display a message

      if (le == 2)
      {
        lcd.setCursor(0, 0);
        lcd.print("   GAME  OVER :(   ");
        lcd.setCursor(7, 1);
        lcd.write(byte(1));
        lcd.createChar(1, man6);
      }

Do yo see the message on the LCD ?

So that's where I modded the code myself to see if I could get the mesage to show up. The original code of that if-statement was this:

//  GAME OVER - START
   if (timegame>0)
     {
     lcd.setCursor(0, 0);
     lcd.print("   GAME  OVER :(   ");
     lcd.setCursor(7, 1);
     lcd.write(byte(1));
     lcd.createChar(1, man6);

From this I modded it to if (le ==2)....
Unfortunately I don't get any message on the LCD, so that didn't work.
Hope you can help me out! Thanks!

The idea sounds OK but I can't test your code at the moment

I would start by giving the variable sensible names. Presumably le is level, so why not name it that and what is ll and why is it a float and what are ck, ax, ay and cc ? Give the pins names too. digitalRead(7); tells me nothing about what is going on. Is the button on pin 7 the start button, maybe ? You test whether pin 7 is LOW but what keeps it in a known state when the button is not pressed? Name pins A0 and A1 too for the same reason

None of these changes will make the code behave differently but it will be so much easier to read and follow. You might know now what is going on but come back later and it will be more difficult

Now to debugging

Print the value of relevant variable, such as the level, at important points in the sketch, such as before you test whether it equals 2. Is it possible for it never to equal 2 for some reason such as not being incremented or even being incremented twice between your tests ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.