[SOLVED] total newbie problems

Understanding the language, error messages, etc.

Re: total newbie problems

Postby Montiey » Sat Jan 24, 2015 11:01 pm

Ok, well I got it to work, but now I am even more confused. The only things I added were 3 brackets down at the very bottom.
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;
// the setup routine runs once when Gamebuino starts up
void setup(){
  // initialize the Gamebuino object
  gb.begin();
  //display the main menu:
  gb.titleScreen(F("jasons HEX thingy"));
  gb.titleScreen(F("press A to start"));
}

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

  //first screen
  while(true){
    if(gb.update()){
      //prints Hello World! on the screen
      gb.display.println(F("Hello World!"));
      //me stuffz
      gb.display.println("Jason Harriot 2015");

      if(gb.buttons.pressed(BTN_A)){
        break; //break out of the while loop when A is pressed
      }
    }
  }

  //second screen
  int num = 0;
  int offsetpause = 0;
  while(true){
    if(gb.update()){
      if(gb.buttons.pressed(BTN_A)){
        if(offsetpause = 1){
          offsetpause = 0;
        }
        else{
          if(offsetpause = 0){
          offsetpause = 1;
        }
      if(offsetpause = 1){
        num += 1;
      }
      gb.display.println(num);
      if(gb.buttons.pressed(BTN_B)){
        num = 0;
        gb.popup(F("reset"), 10);
      }
      if(gb.buttons.pressed(BTN_C)){
        break; //break out of the while loop when C is pressed
      }
    }
  }
  //title screen to be able to get back to the game list
  gb.titleScreen(F("super dumb app"));
    }
  }
}

What makes no sense to me, is that the set of brackets above it close all the lines in the code, but the 3 at the bottom seem to do nothing but are irremovably required in the verification of the file.
It verifies fine, but it is completely broken when I run it on the Gamebuino.
Help! :(
User avatar
Montiey
 
Posts: 68
Joined: Sat Jan 17, 2015 5:38 pm

Re: total newbie problems

Postby Jamish » Sun Jan 25, 2015 12:25 am

Oops. Submitted too soon. Gimme a sec!

Okay. Here were some problems you had:
When evaluating a condition in an "if" statement
Code: Select all
if (offsetpause == 1) {

you must be sure to use "==" and not "=". In programming, those are two very different operators. A single equals sign is an "assignment". It says, whatever the value on the right is, store that inside the left. So saying "offsetpause = 0" will set offsetpause to 0. I'm sure you knew that part. But when you say "offsetpause == 1", that statement will either say "true" if offsetpause equals 1, or "false" otherwise. By saying
Code: Select all
if (offsetpause = 1) {

what you're actually doing is assigning 1 to offsetpause, and then the if statement evaluates to true (because the assignment was successful).


Next problem. You aren't matching your brackets how you think you are. Your if-else structure isn't quite right.
Code: Select all
else{
          if(offsetpause = 0){
          offsetpause = 1;
        }


You're opening a new bracket inside the else, when I think you meant to write "else if (offsetpause == 0) {"

Rodot's way of using the while loops and multiple gb.update() calls will work, but I prefer only having one. In my opinion, you should instead use a variable, like "int state" and set it to 0. Then, in place of your "while" loops, you just have "if (state == 0)" for the first screen and "if (state == 1)" for the next screen, and so on. That way you have a lot of flexibility, because at any time you could set the state to whatever you want to show a different screen rather than going through the while loops in order.

Try this out:

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 state;
int num;
int offsetpause;

// the setup routine runs once when Gamebuino starts up
void setup() {
  restart_game();
  // initialize the Gamebuino object
  gb.begin();
  //display the main menu:
  gb.titleScreen(F("jasons HEX thingy"));
  gb.titleScreen(F("press A to start"));
}

void restart_game() {
  state = 0;
  num = 0;
  offsetpause = 0;
}

// the loop routine runs over and over again forever
void loop() {
  if (gb.update()) {
    //first screen
    if (state == 0) {
      //prints Hello World! on the screen
      gb.display.println(F("Hello World!"));
      //me stuffz
      gb.display.println("Jason Harriot 2015");
 
      if (gb.buttons.pressed(BTN_A)){
        state = 1;
      }
    }
 
    //second screen
    if (state == 1) {
      if (gb.buttons.pressed(BTN_A)) {
        if (offsetpause == 1) {
          offsetpause = 0;
          num += 1;
        } else if (offsetpause == 0) {
          offsetpause = 1;
        }
      }

      gb.display.println(num);
       
      if (gb.buttons.pressed(BTN_B)) {
        num = 0;
        gb.popup(F("reset"), 10);
      }
    }
     
    if (gb.buttons.pressed(BTN_C)) {
      // Go back to the title screen if C is pressed in the 2nd screen
      restart_game(); // Be sure to reset the game variables
      gb.titleScreen(F("super dumb app"));
    }
  }
}



You can see I moved all the variables that are needed to the top of the file. I set their initial value in a function called "restart_game()", which I called in Setup (which is called once on power-on). I called restart_game() later (if you press C) in order to reset the game. Notice how the code that check if I press "c" is outside of the "first screen" and "second screen" if statements? That way, no matter which screen I am on, the C button will always take me back to the start.
User avatar
Jamish
 
Posts: 73
Joined: Wed Dec 17, 2014 6:52 pm
Location: California

Re: total newbie problems

Postby Montiey » Sun Jan 25, 2015 2:49 pm

Ok. Sorry to keep everyone busy with dumb questions, but does this throw "B000... was not declared in this scope." at me?
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[]=
{
  45,10,
  B0000000000000000000000000000000000000000000000,
  B0111111001111000111110011110011001100001100110,
  B0111111011111101111110111111011001100001100110,
  B0001100011001101100000110011011101100001100110,
  B0001100011111101111100110011011111100001100110,
  B0001100011111100111110110011011111100001111110,
  B0101100011001100000110110011011011100001111110,
  B0111100011001101111110111111011001101101100110,
  B0111100011001101111100011110011001101101100110,
  B0000000000000000000000000000000000000000000000,
};
// 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(){
  //second screen
  int num = 0;
  int dat = 0;
  while(true){
    if(gb.update()){
      gb.battery.show = false;
      if(gb.buttons.pressed(BTN_A)){
        dat = 1;
      }
      if(gb.buttons.pressed(BTN_B)){
        dat = 0;
      }
      if(gb.buttons.pressed(BTN_C)){
        break; //break out of the while loop when C is pressed
      }
      if(dat == 1){
        num += 1;
      }
      gb.display.println(num);
       
    }
  }
  //title screen to be able to get back to the game list
  gb.titleScreen(F("Work in progress"), logo);
}
User avatar
Montiey
 
Posts: 68
Joined: Sat Jan 17, 2015 5:38 pm

Re: total newbie problems

Postby Jamish » Sun Jan 25, 2015 3:50 pm

Typing a number with a B means you're typing a byte. A byte is 8 bits, and a bit is either 0 or 1. That means it cannot be more than 8 digits. So it has to be B00000000, for example. You should add a comma and a B every 8 digits in your bitmap. Or use the bitmap encoder tool and encode an image you made in an image editor.
User avatar
Jamish
 
Posts: 73
Joined: Wed Dec 17, 2014 6:52 pm
Location: California

Re: total newbie problems

Postby Montiey » Sun Jan 25, 2015 3:59 pm

Never mind replying.. Fixed it when I figured out that each horizontal section contains exactly 8 bits. This works perfectly:
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[]=
{
  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 bug[]=
{
  8,8,
  B00000000,
  B00100100,
  B00011000,
  B01111110,
  B00111100,
  B01111110,
  B00111100,
  B00000000,
};

// 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(){
  //second screen
  int num = 0;
  int dat = 0;
  while(true){
    if(gb.update()){
      gb.battery.show = false;
      if(gb.buttons.pressed(BTN_A)){
        dat = 1;
      }
      if(gb.buttons.pressed(BTN_B)){
        dat = 0;
      }
      if(gb.buttons.pressed(BTN_C)){
        break; //break out of the while loop when C is pressed
      }
      if(dat == 1){
        num += 1;
      }
      gb.display.println(num);
       
    }
  }
  //title screen to be able to get back to the game list
  gb.titleScreen(F("Work in progress"), logo);
}


Does anyone have some simple code that moves a sprite with the up/down/left/right controls (thats what the BUG sprite in this code was going to be for)? I am starting to feel comfortable enough to do something thats not text-based. :P
User avatar
Montiey
 
Posts: 68
Joined: Sat Jan 17, 2015 5:38 pm

Re: total newbie problems

Postby rodot » Sun Jan 25, 2015 4:08 pm

You should take a look at Gamebuino/examples/basics/controls. It moves a rectangle, but you only have to change the drawRect function to a drawBitmap.
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Re: total newbie problems

Postby Montiey » Sun Jan 25, 2015 9:26 pm

Here is my latest problem:
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 bug_x = LCDWIDTH/2; //set the horizontal position to the middle of the screen
int bug_y = LCDHEIGHT/2; //vertical position
int bug_vx = 1; //horizontal velocity
int bug_vy = 1; //vertical velocity
int bug_size = 8; //the size of the ball in number of pixels

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 bug[]=
{
  8,8,
  B00000000,
  B00100100,
  B00011000,
  B01111110,
  B00111100,
  B01111110,
  B00111100,
  B00000000,
};

// 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(){
    gb.battery.show = false;
     //move the bug using the buttons
    if(gb.buttons.repeat(BTN_RIGHT,2)){ //every 2 frames when the right button is held down
      bug_x = bug_x + bug_vx; //increase the horizontal position by the bug's velocity
      gb.sound.playTick(); //play a preset "tick" sound every time the button is pressed
    }
    if(gb.buttons.repeat(BTN_LEFT,2)){
      bug_x = bug_x - bug_vx;
      gb.sound.playTick();
    }
    if(gb.buttons.repeat(BTN_DOWN,2)){
      bug_y = bug_y + bug_vy;
      gb.sound.playTick();
    }
    if(gb.buttons.repeat(BTN_UP,2)){
      bug_y = bug_y - bug_vy;
      gb.sound.playTick();
    }
    //bonus : play a preset sound when A and B are pressed
    if(gb.buttons.pressed(BTN_A)){
      gb.sound.playOK();
    }
    if(gb.buttons.pressed(BTN_B)){
      gb.sound.playCancel();
    }
   
    //check that the bug is not going out of the screen
    //if the bug is touching the left side of the screen
    if(bug_x < 0){
      //bring it back in the screen
      bug_x = 0;
    }
    //if the bug is touching the right side
    if((bug_x + bug_size) > LCDWIDTH){
      bug_x = LCDWIDTH - bug_size;
    }
    //if the bug is touching the top side
    if(bug_y < 0){
      bug_y = 0;
    }
    //if the bug is touching the down side
    if((bug_y + bug_size) > LCDHEIGHT){
      bug_y = LCDHEIGHT - bug_size;
    }
   
    //draw the bug on the screen
    gb.display.drawChar(bug_x, bug_y, bug, 1);
    int bug_x = LCDWIDTH/2; //set the horizontal position to the middle of the screen
  //first screen
  while(true){
    if(gb.update()){
      if(gb.buttons.pressed(BTN_C)){
        break; //break out of the while loop when C is pressed
      }
    }
  }
  //title screen to be able to get back to the game list
  gb.titleScreen(F("Work in progress"), logo);
}


It simply won't verify. Supposedly I have a syntax error where the "Bug" is displayed using "gb.display.drawChar" What is wrong with that line? It seems like I have followed the syntax correctly from the reference page, but apparently not.
User avatar
Montiey
 
Posts: 68
Joined: Sat Jan 17, 2015 5:38 pm

Re: total newbie problems

Postby rodot » Sun Jan 25, 2015 9:43 pm

That's because you are trying to draw a bitmap using the drawChar function instead of drawBitmap ;)
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Re: total newbie problems

Postby Montiey » Mon Jan 26, 2015 1:30 am

That is the most logical statement in the entire universe. Fixed that and worked on the code a bit more, but now I am running into looping problems..

Here is my latest code:
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 bug_x = LCDWIDTH/2; //set the horizontal position to the middle of the screen
int bug_y = LCDHEIGHT/2; //vertical position
int bug_vx = 1; //horizontal velocity
int bug_vy = 1; //vertical velocity
int bug_size = 8; //the size of the ball in number of pixels

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 bug[]=
{
  8,8,
  B01111110,
  B11000011,
  B10100101,
  B10011001,
  B10011001,
  B10100101,
  B11000011,
  B01111110,
};

// 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(true){
    gb.update();
      gb.battery.show = false;
       //move the bug using the buttons
      if(gb.buttons.repeat(BTN_RIGHT,2)){ //every 2 frames when the right button is held down
        bug_x = bug_x + bug_vx; //increase the horizontal position by the bug's velocity
        gb.sound.playTick(); //play a preset "tick" sound every time the button is pressed
      }
      if(gb.buttons.repeat(BTN_LEFT,2)){
        bug_x = bug_x - bug_vx;
        gb.sound.playTick();
      }
       if(gb.buttons.repeat(BTN_DOWN,2)){
        bug_y = bug_y + bug_vy;
        gb.sound.playTick();
      }
      if(gb.buttons.repeat(BTN_UP,2)){
        bug_y = bug_y - bug_vy;
        gb.sound.playTick();
      }
      //bonus : play a preset sound when A and B are pressed
      if(gb.buttons.pressed(BTN_A)){
        gb.sound.playOK();
      }
      if(gb.buttons.pressed(BTN_B)){
        gb.sound.playCancel();
      }
     
      //check that the bug is not going out of the screen
      //if the bug is touching the left side of the screen
      if(bug_x < 0){
        //bring it back in the screen
        bug_x = 0;
      }
      //if the bug is touching the right side
      if((bug_x + bug_size) > LCDWIDTH){
        bug_x = LCDWIDTH - bug_size;
      }
      //if the bug is touching the top side
      if(bug_y < 0){
        bug_y = 0;
      }
      //if the bug is touching the down side
      if((bug_y + bug_size) > LCDHEIGHT){
        bug_y = LCDHEIGHT - bug_size;
      }
     
      //draw the bug on the screen
      gb.display.drawBitmap(bug_x,bug_y,bug);
      while(true){
        if(gb.update()){
          if(gb.buttons.pressed(BTN_C)){
            break; //break out of the while loop when C is pressed
            break;
          }
        }
      }
    }
  }
  //title screen to be able to get back to the game list
  gb.titleScreen(F("Work in progress"), logo);
}

I'm sorry for wasting your time with dumb problems... :P
User avatar
Montiey
 
Posts: 68
Joined: Sat Jan 17, 2015 5:38 pm

Re: total newbie problems

Postby Montiey » Tue Jan 27, 2015 2:37 pm

I finally got the program to run, but now it doesn't leave the tittle screen!
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 bug_x = LCDWIDTH/2; //set the horizontal position to the middle of the screen
int bug_y = LCDHEIGHT/2; //vertical position
int bug_vx = 1; //horizontal velocity
int bug_vy = 1; //vertical velocity
int bug_size = 8; //the size of the ball in number of pixels

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 bug[]=
{
  8,8,
  B01111110,
  B11000011,
  B10100101,
  B10011001,
  B10011001,
  B10100101,
  B11000011,
  B01111110,
};

// 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(gb.update()){
    gb.battery.show = false;
     //move the bug using the buttons
    if(gb.buttons.repeat(BTN_RIGHT,2)){ //every 2 frames when the right button is held down
      bug_x = bug_x + bug_vx; //increase the horizontal position by the bug's velocity
      gb.sound.playTick(); //play a preset "tick" sound every time the button is pressed
    }
    if(gb.buttons.repeat(BTN_LEFT,2)){
      bug_x = bug_x - bug_vx;
      gb.sound.playTick();
    }
     if(gb.buttons.repeat(BTN_DOWN,2)){
      bug_y = bug_y + bug_vy;
      gb.sound.playTick();
    }
    if(gb.buttons.repeat(BTN_UP,2)){
      bug_y = bug_y - bug_vy;
      gb.sound.playTick();
    }
    //bonus : play a preset sound when A and B are pressed
    if(gb.buttons.pressed(BTN_A)){
      gb.sound.playOK();
      bug_vx = 6;
      bug_vy = 6;
    }
    if(gb.buttons.pressed(BTN_B)){
      gb.sound.playCancel();
      bug_vx = 2;
      bug_vy = 2;
    }
    //check that the bug is not going out of the screen
    //if the bug is touching the left side of the screen
    if(bug_x < 0){
      //bring it back in the screen
      bug_x = 0;
    }
    //if the bug is touching the right side
    if((bug_x + bug_size) > LCDWIDTH){
      bug_x = LCDWIDTH - bug_size;
    }
    //if the bug is touching the top side
    if(bug_y < 0){
      bug_y = 0;
    }
    //if the bug is touching the down side
    if((bug_y + bug_size) > LCDHEIGHT){
      bug_y = LCDHEIGHT - bug_size;
    }
    //draw the bug on the screen
    gb.display.drawBitmap(bug_x,bug_y,bug);
    if(gb.buttons.pressed(BTN_C)){
      break; //break out of the while loop when C is pressed
    }
  }
gb.update();{
  //title screen to be able to get back to the game list
  gb.titleScreen(F("Work in progress"), logo);
}
}

I'm not sure what went wrong, but I probably did something with the loops up at the top that broke it.
User avatar
Montiey
 
Posts: 68
Joined: Sat Jan 17, 2015 5:38 pm

PreviousNext

Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 30 guests

cron