0

it´s a school project, but I'm having some problems to create a menu selection. In the LCD, all the character are overwriting, I'm a beginer, so i dont have any idea of what i was doing wrong (ps.: sorry about my english, i'm a brazilian. Here goes the code: //A portable "atari" videogame project, including 3 games

#include <LiquidCrystal.h>
//define os pinos a ser utilizados no arduino uno,etc.
#define btcima 9
#define btbaixo 8
#define bttiro 10
#define menu 11

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

int vel=100;
 int pxnave,pynave,pxaste,pyaste,pxtiro,pytiro;//px=posição x, py=posição y, etc
bool game1,game2,game3,vtiro;//vtiro=variavel de tiro
int pontos;
int maxpontos=15;
int vtela;
byte nave[8]={B00000,B01100,B00110,B11111,B00110,B01100,B00000,B00000};
byte asteroide[8]={B01110,B10111,B11111,B11101,B10111,B01110,B00000,B00000};
byte explosao[8]={B10101,B00010,B11000,B10101,B01010,B10110,B01101,B10011};
byte tiro3[8]={B00000,B00000,B11110,B11111,B11111,B11110,B00000,B00000};

int      line[4] = {0,1,2,3},
         line_bk[4],
         index,
         menu_number = 0;

boolean  menu_flag  = 0,
         enter_flag = 0,
         sub_menu = 0;

void readButts();
void list_menu();
void menu_select();



void setup()
{
  //--configuração do menu--
 pinMode(menu,  INPUT_PULLUP);
  pinMode(bttiro, INPUT_PULLUP);



 lcd.begin (16,4);
 //lcd.setBacklight(HIGH);--apenas para lcd i2c
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print(">");

pxnave=pynave=pyaste=pontos=pytiro=0;
pxtiro=-1;
pxaste=12;
vtela=0;
lcd.createChar(1,nave);
lcd.createChar(2,asteroide);
lcd.createChar(4,explosao);
lcd.createChar(6,tiro3);
//lcd.begin(16,2);
//lcd.clear();
game1=-false;
game2=false;
game3=false;
vtiro=false;
}


void loop(){

  if(!sub_menu)
  {
   lcd.clear();
    lcd.setCursor(1,line[0]);
    lcd.print("1) NAVE: THE GaMe       ");
    lcd.setCursor(1,line[1]);
    lcd.print("2) PING PONG       ");
    lcd.setCursor(1,line[2]);
    lcd.print("3) lula & dilma      ");
    lcd.setCursor(1,line[3]); 
    lcd.print("4) Creditos     "); 

  }


  readButts();

  if(game1){
    lcd.clear();
    vtela=0;

  if (digitalRead(btcima)==1){
    pynave=0;

  }
  if(digitalRead(btbaixo)==1){
    pynave=1;
  }
  if(digitalRead(bttiro)==1){
    pxtiro=1;
    vtiro=true;
pytiro=pynave;    
  }
  pxaste-=1;
  desenhaNave(pxnave, pynave);
  desenhaAsteroide(pxaste, pyaste);
  if(vtiro){
    desenhaTiro3(pxtiro, pytiro);
    pxtiro+=1;
  }

  if(pxaste<0){
  pxaste=12;
  pyaste=random(0,2);
  }
  if(pxtiro>16){
    vtiro=false;
    pxtiro-=1;
  }
  if (((pxtiro==pxaste+1)&&(pytiro==pyaste))||((pxtiro==pxaste)&&(pytiro==pyaste))){
  vtiro=false;
  pxtiro=-1;
  desenhaExplosaoAsteroide(pxaste, pyaste);
  pyaste=random(0,2);
  pxaste=12;
  pontos+=5;
  if((pontos>=maxpontos)){
    game1=0;
    vtela=7;//rever
  }
}
if((pxnave==pxaste)&&(pynave==pyaste)){
game1=0;
desenhaExplosaoNave(pxnave, pynave);
vtela=6;
}

  delay(vel);

  }else{
    tela(vtela);
    if(digitalRead(bttiro)==1){
      reset1();
    }
  }
}

  void readButts()
{

   if(!digitalRead(menu))  menu_flag  = 0x01;
   if(!digitalRead(bttiro)) enter_flag = 0x01;

   if(digitalRead(menu) && menu_flag)
   {
       menu_flag = 0x00;
       list_menu();
       menu_number+=1;
       if(menu_number > 5) menu_number = 1;

   } 

   if(digitalRead(bttiro) && enter_flag)
   {
      enter_flag = 0x00; 
      sub_menu = !sub_menu;
      menu_select();

   } 


} 


void list_menu()
{
  for(int i=4; i>-1; i--) 
  {
     index = i-1;
     line_bk[i] = line[i];

     if(index < 1) line[i] = line_bk[i+4];

     else line[i] = line[i-1];

  }

} 


void menu_select()
{

    switch(menu_number)
    {
       case 1: 
        vtela==0;
        game1= true;
        game2=false;
        game3=false;

               break;
       case 2: 
            vtela==4;
              game1= false;
        game2=true;
        game3=false; 
               break;
       case 3: 
           vtela==5;
           game1= false;
        game2=false;
        game3=true; 
               break;
       case 4: 
               vtela==8;
               game1= false;
        game2=false;
        game3=false;
               break;
       //case 5: ---pensar--- 


    } //end switch


}
  void desenhaNave(int px, int py){
  lcd.setCursor(px,py);
  lcd.write(1);
}
void desenhaAsteroide(int px, int py){
  lcd.setCursor(px,py);
  lcd.write(2);
}

void desenhaTiro3(int px, int py){
  lcd.setCursor(px,py);
  lcd.write(6);
}
void desenhaExplosaoNave(int px, int py){
 lcd.clear();
  lcd.setCursor(px,py);
  lcd.write(4);
  delay(1000);
  lcd.clear();
}
void desenhaExplosaoAsteroide(int px, int py){
  lcd.setCursor(px,py);
  lcd.write(4);
  delay(1000);
  lcd.clear();
}
void reset1(){
  pontos=0;
  game1=true;
  vtela=0;
}
void reset2(){
  pontos=0;
  game2=true;
  vtela=0;
}
void reset3(){
  pontos=0;
  game3=true;
  vtela=0;
}


void tela(int cond){//0=jogo da nave 1=ganhou 2= perdeu 3= menu de seleção 4=jogo pingpong 5=jogo plataforma

if(cond<1){
   lcd.setCursor(3,0);
     lcd.print("Nave: THE GAmE");
     lcd.setCursor(0,1);
lcd.print("PRESSIONE START");
}
if(cond==6){
  lcd.setCursor(9,0);
  lcd.print("pts:");
  lcd.setCursor(13,0);
  lcd.print (pontos);
  lcd.setCursor(1,0);
  lcd.print("Perdeu");
lcd.setCursor(0,1);
lcd.print("PRESSIONE START");
}
if(cond==7){
  lcd.setCursor(9,0);
  lcd.print("pts:");
  lcd.setCursor(13,0);
  lcd.print (pontos);
  lcd.setCursor(1,0);
  lcd.print("Ganhou");
lcd.setCursor(0,1);
lcd.print("PRESSIONE START");
}
if (cond==3){
  sub_menu=true;
  lcd.clear();
}

if(cond==4)
{
  lcd.setCursor(3,0);
  lcd.print ("Ping Pong");
  lcd.setCursor(0,1);
  lcd.print ("PRESSIONE START");

}
if (cond==5)
{
  lcd.setCursor(3,0);
  lcd.print("Lula e Dilma");
  lcd.setCursor (0,1);
  lcd.print ("PRESSIONE START");
}
if (cond==8)
{
  lcd.setCursor(3,0);
  lcd.print("Equipe LULA Livre");
  lcd.setCursor(0,1);
  lcd.print("Obrigado por Jogar");
}
}`enter code here`
  • Just for inspiration, you may want to take a look at Arduboy and the open source libraries its games are built on. – Edgar Bonet Aug 10 at 20:48
0

Probably since you set the cursor at a position in the middle of a line, only the new text is overwritten over the old text. But if there are some characters from the previous text, those will stay.

So the easiest way is to either clear the screen (if you can do the updating of the screen from a single place), or rewrite the entire line (and probably having to add spaces before/after the text you want to write to overwrite the old text).

Some other tips:

  • Write comments and variable names in English (I know it's not your mother language, but you can learn two languages at once, C and English).
  • Align your code (make sure each { and } is aligned nicely; this will make life much easier and prevent mistakes.
  • game1, game2 and game3 are booleans where only one can be true. For this the enum (enumeration) is invented. So make an enum enum Game { game1, game2, game3 }; as a type, and declare a variable Game currentGame = game1;
  • You use a switch statement for menu_number, but why not for cond?
  • You use a lot of text, all text also takes up precious SRAM, where you only have 2,048 bytes of. If for each string you add F in front (thus e.g. F("MENU"); this will be stored only in Flash.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.