[SOLVED] total newbie problems

Understanding the language, error messages, etc.

Re: total newbie problems

Postby rodot » Tue Jan 27, 2015 2:44 pm

Your update loops are all messed up, we gave your several example of how you should use it :

Code: Select all
while(1){
   if(gb.update()){
      //your game here
   }
}


See Reference : gb.update.
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Re: total newbie problems

Postby Jamish » Tue Jan 27, 2015 6:56 pm

Code: Select all
void loop(){
  while(gb.update()){
    //code code code blah blah blah
  }
  gb.update();{
  //title screen to be able to get back to the game list
  gb.titleScreen(F("Work in progress"), logo);
  }
}


That's your loop right now. As rodot said, you should never say "while (gb.update())".

Also,
Code: Select all
gb.update();{
  //title screen to be able to get back to the game list
  gb.titleScreen(F("Work in progress"), logo);
  }

That is improper syntax. You are opening some brackets { } without any sort of if/for/while loop. Again, your structure, should you choose to not use my idea of "game state" variables, should be:

Code: Select all
void loop(){
  while(1){
    if (gb.update()) {
        // Your game's first screen
        // Code code code blah blah blah
        if(gb.buttons.pressed(BTN_C)){
          break; //break out of the while loop when C is pressed
        }
    }
  }
 
  // Back to title screen
  gb.titleScreen(F("Work in progress"), logo);
}


EDIT: fixed my code
User avatar
Jamish
 
Posts: 73
Joined: Wed Dec 17, 2014 6:52 pm
Location: California

Re: total newbie problems

Postby Montiey » Sat Jan 31, 2015 8:19 pm

What could be wrong when the IDE says "Expected unqualified-ID before "while"?
Code: Select all
//imports the SPI library (needed to communicate with Gamebuino's screen)
#include <SPI.h>
//imports the Gamebuino library
#include <Gamebuino.h>
//creates a Gamebuino object named gb
Gamebuino gb;

int player_x = LCDWIDTH/2; //set the horizontal position to the middle of the screen
int player_y = LCDHEIGHT/2; //vertical position
int player_vx = 1; //horizontal velocity
int player_vy = 1; //vertical velocity
int player_size = 8; //the size of the player in number of pixels
int item1_size = 6; //the size of the ball in number of pixels
int item1_x = 30; //initial X position of item1
int item1_y = 10; //initial Y position of item1
int item2_size = 8;
int item2_x = 40;
int item2_y = 2;
int score = 0; //$ value
int timer = 0;
int item1_quantity = 0; //the number of item1s collected
int item2_quantity = 0; //the number of item2s collected

static unsigned char PROGMEM logo[]=
{
  46,10,
  B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
  B01111110, B01111000, B11111001, B11100110, B01100001, B10011000,
  B01111110, B11111101, B11111011, B11110110, B01100001, B10011000,
  B00011000, B11001101, B10000011, B00110111, B01100001, B10011000,
  B00011000, B11111101, B11110011, B00110111, B11100001, B10011000,
  B00011000, B11111100, B11111011, B00110111, B11100001, B11111000,
  B11011000, B11001100, B00011011, B00110110, B11100001, B11111000,
  B11111000, B11001101, B11111011, B11110110, B01101101, B10011000,
  B11111000, B11001101, B11110001, B11100110, B01101101, B10011000,
  B00000000, B00000000, B00000000, B00000000, B00000000, B00000000,
};

static unsigned char PROGMEM player[]=
{
  8,8,
  B00111100,
  B01000010,
  B10100101,
  B10000001,
  B10100101,
  B10011001,
  B01000010,
  B00111100,
};

static unsigned char PROGMEM  item2[]=
{
  8,8,
  B01111110,
  B11000011,
  B10100101,
  B10011001,
  B10011001,
  B10100101,
  B11000011,
  B01111110,
};
static unsigned char PROGMEM item1[]=
{
  6,6,
  B011110,
  B100001,
  B101101,
  B101101,
  B100001,
  B011110,
};

// the setup routine runs once when Gamebuino starts up
void setup(){
  // initialize the Gamebuino object
  gb.begin();
  //display the main menu:
  gb.titleScreen(F("Work in progress"), logo);
}

// the loop routine runs over and over again forever
void loop(){
  while(1){
    while(1){
      if(gb.update()){
        gb.battery.show = false;
        //item1 system
        if(gb.collideBitmapBitmap( player_x,  player_y,  player, item1_x, item1_y, item1)){
          item1_x = random(0, LCDWIDTH - item1_size);
          item1_y = random(0, LCDHEIGHT - item1_size);
          score += 1;
          item1_quantity += 1;
          gb.sound.playOK();
        }
        //item2 system
        if(gb.collideBitmapBitmap( player_x,  player_y,  player, item2_x, item2_y, item2)){
          item2_x = random(0, LCDWIDTH - item2_size);
          item2_y = random(0, LCDHEIGHT - item2_size);
          score -= 3;
          item2_quantity += 1;
          gb.sound.playCancel();
        }
        gb.display.drawBitmap(item1_x,item1_y,item1);
        gb.display.drawBitmap(item2_x,item2_y,item2);
        //move the  player using the buttons
        if(gb.buttons.repeat(BTN_RIGHT,2)){ //every 2 frames when the right button is held down
           player_x =  player_x +  player_vx; //increase the horizontal position by the  player's velocity
        }
        if(gb.buttons.repeat(BTN_LEFT,2)){
           player_x =  player_x -  player_vx;
        }
         if(gb.buttons.repeat(BTN_DOWN,2)){
           player_y =  player_y +  player_vy;
        }
        if(gb.buttons.repeat(BTN_UP,2)){
           player_y =  player_y -  player_vy;
        }
        //bonus : play a preset sound when A and B are pressed
        if(gb.buttons.pressed(BTN_A)){
          gb.sound.playTick();
           player_vx = 5;
           player_vy = 5;
        }
        if(gb.buttons.pressed(BTN_B)){
          gb.sound.playTick();
           player_vx = 2;
           player_vy = 2;
        }
        //check that the  player is not going out of the screen
        //if the  player is touching the left side of the screen
        if( player_x < 0){
          //bring it back in the screen
           player_x = 0;
        }
        //if the  player is touching the right side
        if(( player_x +  player_size) > LCDWIDTH){
           player_x = LCDWIDTH -  player_size;
        }
        //if the  player is touching the top side
        if( player_y < 0){
           player_y = 0;
        }
        //if the  player is touching the down side
        if(( player_y +  player_size) > LCDHEIGHT){
           player_y = LCDHEIGHT -  player_size;
        }
        //draw the  player on the screen
        gb.display.drawBitmap( player_x, player_y, player);
        gb.display.print("$ = "); gb.display.print(score); //problems with layers and "draw order"!! :P
        // timer system
        timer += 10;
        if(timer >= 200);
        break;
        }
       
        if(gb.buttons.pressed(BTN_C)){
          break; //break out of the while loop when C is pressed
          break; //2x for loop nesting
        }
      }
    }
    while(1){
      if(gb.update());{
        gb.display.println("Cherrys : "); gb.display.print(item1_quantity);
        gb.display.println("Bombs : "); gb.display.println(item2_quantity);
        gb.display.println("$ : "); gb.display.print(score);
        if(gb.buttons.pressed(BTN_A)){
          break;
          break;
        }
      }
    }
  }
  while(1){
    gb.update();{
      //title screen to be able to get back to the game list
      gb.titleScreen(F("Work in progress"), logo);
      if(gb.buttons.pressed(BTN_A)){
        break;
      }
    }
  }
}

I looked it up and got something about the while loop needing to be inside of void loop, but it is.
Also, could the problem be that I have a gb.update inside of every loop? Is this necessary?
User avatar
Montiey
 
Posts: 68
Joined: Sat Jan 17, 2015 5:38 pm

Re: total newbie problems

Postby Myndale » Sat Jan 31, 2015 9:16 pm

You've got one-too-many closing parenthesis, so the compiler thinks you've closed off the functions.

I'll be completely honest with you, there are heaps of problems in this code. I don't think this does what you think it does:

Code: Select all
if(gb.buttons.pressed(BTN_C)){
          break; //break out of the while loop when C is pressed
          break; //2x for loop nesting
        }


The first break will break out of that block of code and the second break will never be executed. Code like this will also cause problems:

Code: Select all
  if(timer >= 200);
        break;


The semicolon at the end of the first line means the break will always be executed regardless.

It might help to find some online resources about programming in C, the DIY Robotics site also has a good article on some of the more common compiler issues you may encounter with Arduino.
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am

Re: total newbie problems

Postby Montiey » Sat Jan 31, 2015 9:25 pm

Ah. Thanks. I'll bet I missed a lot of problems because it wouldn't compile and so I couldn't even see that it didn't work.
User avatar
Montiey
 
Posts: 68
Joined: Sat Jan 17, 2015 5:38 pm

Re: total newbie problems

Postby Myndale » Sat Jan 31, 2015 9:38 pm

This is why it's good to split your code up into smaller functions, when problems do appear it's much easier to find the exact part of the code that's causing it. You also avoid the problem of keeping track of your opening/closing braces.
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am

Re: total newbie problems

Postby Montiey » Sun Feb 08, 2015 2:26 am

I am quite annoyed with this..
I've looked over this many times, but unfortunately I couldn't find anything wrong with it.
Code: Select all
//imports the SPI library (needed to communicate with Gamebuino's screen)
#include <SPI.h>
//imports the Gamebuino library
#include <Gamebuino.h>
//creates a Gamebuino object named gb
Gamebuino gb;

static unsigned char PROGMEM logo[]=
{
  38,12,
  B00111100, B00110000, B11000011, B11000000, B111100,
  B00111100, B00110000, B11000011, B11000000, B111100,
  B11000011, B00110000, B11001100, B00110011, B000011,
  B11000011, B00110000, B11001100, B00110011, B000011,
  B11000011, B00110000, B11001100, B00000011, B000000,
  B11000011, B00110000, B11001100, B00000011, B000000,
  B11111100, B00110000, B11001100, B11000011, B001100,
  B11111100, B00110000, B11001100, B11000011, B001100,
  B11000011, B00110000, B11001100, B00110011, B000011,
  B11000011, B00110000, B11001100, B00110011, B000011,
  B00111100, B00001111, B00000011, B11000000, B111100,
  B00111100, B00001111, B00000011, B11000000, B111100,
};

static unsigned char PROGMEM player[]=
{
  8,8,
  B00111100,
  B01000010,
  B10100101,
  B10000001,
  B10100101,
  B10011001,
  B01000010,
  B00111100,
};

static unsigned char PROGMEM  item2[]=
{
  8,8,
  B01111110,
  B11000011,
  B10100101,
  B10011001,
  B10011001,
  B10100101,
  B11000011,
  B01111110,
};
static unsigned char PROGMEM item1[]=
{
  6,6,
  B011110,
  B100001,
  B101101,
  B101101,
  B100001,
  B011110,
};

int score = 0; // score
int item1_quantity = 0;
int item2_quantity = 0;
int player_x = LCDWIDTH/2; //set the horizontal position to the middle of the screen
int player_y = LCDHEIGHT/2; //vertical position
int player_vx = 1; //horizontal velocity
int player_vy = 1; //vertical velocity
int player_size = 8; //the size of the player in number of pixels
int item1_size = 6; //the size of the ball in number of pixels
int item1_x = 30; //initial X position of item1
int item1_y = 10; //initial Y position of item1
int item2_size = 8;
int item2_x = 40;
int item2_y = 2;

// the setup routine runs once when Gamebuino starts up
void setup(){
  // initialize the Gamebuino object
  gb.begin();
  //display the main menu:
  gb.titleScreen(F("Work In Progress"), logo);
}

// the loop routine runs over and over again forever
void loop(){

  while(1){
    if(gb.update()){
      //item1 system
      if(gb.collideBitmapBitmap( player_x,  player_y,  player, item1_x, item1_y, item1)){
        item1_x = random(0, LCDWIDTH - item1_size);
        item1_y = random(0, LCDHEIGHT - item1_size);
        score += 1;
        item1_quantity += 1;
        gb.sound.playOK();
      }
      //item2 system
      if(gb.collideBitmapBitmap( player_x,  player_y,  player, item2_x, item2_y, item2)){
        item2_x = random(0, LCDWIDTH - item2_size);
        item2_y = random(0, LCDHEIGHT - item2_size);
        score -= 3;
        item2_quantity += 1;
        gb.sound.playCancel();
      }
      //move the  player using the buttons
      if(gb.buttons.repeat(BTN_RIGHT,2)){ //every 2 frames when the right button is held down
         player_x =  player_x +  player_vx; //increase the horizontal position by the  player's velocity
      }
      if(gb.buttons.repeat(BTN_LEFT,2)){
         player_x =  player_x -  player_vx;
      }
       if(gb.buttons.repeat(BTN_DOWN,2)){
         player_y =  player_y +  player_vy;
      }
      if(gb.buttons.repeat(BTN_UP,2)){
         player_y =  player_y -  player_vy;
      }
      //bonus : play a preset sound when A and B are pressed
      if(gb.buttons.pressed(BTN_A)){
        gb.sound.playTick();
         player_vx = 5;
         player_vy = 5;
      }
      if(gb.buttons.pressed(BTN_B)){
        gb.sound.playTick();
         player_vx = 2;
         player_vy = 2;
      }
      //check that the  player is not going out of the screen
      //if the  player is touching the left side of the screen
      if( player_x < 0){
        //bring it back in the screen
         player_x = 0;
      }
      //if the  player is touching the right side
      if(( player_x +  player_size) > LCDWIDTH){
         player_x = LCDWIDTH -  player_size;
      }
      //if the  player is touching the top side
      if( player_y < 0){
         player_y = 0;
      }
      //if the  player is touching the down side
      if(( player_y +  player_size) > LCDHEIGHT){
         player_y = LCDHEIGHT -  player_size;
      }
      //draw the  player on the screen
      gb.display.drawBitmap(player_x, player_y, player);
      gb.display.drawBitmap(item1_x,item1_y,item1);
      gb.display.drawBitmap(item2_x,item2_y,item2);
      gb.display.print("$ = "); gb.display.print(score);
      // timer and endgame system removed for bug testing :(
    }
  }
  while(1){
    if(gb.update());{
      //title screen to be able to get back to the game list
      gb.titleScreen(F("Work in progress"), logo);
      if(gb.buttons.pressed(BTN_A)){
        break;
      }
    }
  }
}

Im guessing that there is some mistake that I keep making after you all so helpfully fix it, ruining things until I bother you to fix it again.. :lol:
Did I use to many variables? What kind of serious memory limitations does the gamebuino have?
I suspect it's probably my loops again. Do I only need a "while(1){" on the very outer loop? What about "if(gb.update()){" ?
Is "While(1){" the same thing as "While(true){" ?
User avatar
Montiey
 
Posts: 68
Joined: Sat Jan 17, 2015 5:38 pm

Re: total newbie problems

Postby Jamish » Sun Feb 08, 2015 9:35 pm

Code: Select all
if(gb.update());{


That should be
Code: Select all
if(gb.update()){


Otherwise, what's the problem? It runs fine for me
User avatar
Jamish
 
Posts: 73
Joined: Wed Dec 17, 2014 6:52 pm
Location: California

Re: total newbie problems

Postby Montiey » Sun Feb 08, 2015 11:17 pm

I'm still getting a blank screen. Are you sure its not the other way around?
Did you mean it compiles fine? Did you test on on a GB or emulator at all?
User avatar
Montiey
 
Posts: 68
Joined: Sat Jan 17, 2015 5:38 pm

Re: total newbie problems

Postby Jamish » Sun Feb 08, 2015 11:25 pm

Odd. I ran it in Simbuino and could move around and pick up both items
User avatar
Jamish
 
Posts: 73
Joined: Wed Dec 17, 2014 6:52 pm
Location: California

PreviousNext

Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 10 guests