vovxb
Published © GPL3+

Lcd game console

A console made from an lcd and a joystick

BeginnerFull instructions provided1,182
Lcd game console

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Solderless Breadboard Full Size
Solderless Breadboard Full Size
×1
Analog joystick (Generic)
×1
I2C 16x2 Arduino LCD Display Module
DFRobot I2C 16x2 Arduino LCD Display Module
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1

Software apps and online services

Arduino IDE
Arduino IDE
Arduino Web Editor
Arduino Web Editor

Story

Read more

Schematics

Diagram

Code

sketch.ino

Arduino
#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=2000;
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.init();
  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 = 2000;
           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 (timegame>0)
      {
      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

    }
  }
}

Credits

vovxb
0 projects • 0 followers

Comments