Page 1 of 1

[SOLVED]C_Controls example error

PostPosted: Fri Jan 13, 2017 1:36 am
by IPaulasaurus
I've been doing the examples, and just got to the controls one. it compiles perfectly and everything, but when I try to run it on my gamebuino no matter what button I hit, the ball goes completely dow the screen, and then the next button I hit goes to the left corner. When the ball is there, I can move it one pixel to the right and back again. I checked my code, and everything seems in order.
Code: Select all
#include <SPI.h>
#include <Gamebuino.h>
Gamebuino gb;
int ball_x = LCDWIDTH/2;
int ball_y = LCDHEIGHT/2;
int ball_vx = 1;
int ball_vy = 1;
int ball_size = 6;

void setup() {
  // put your setup code here, to run once:
  gb.begin();
  gb.titleScreen(F("Ball Control"));
}

void loop() {
  // put your main code here, to run repeatedly:
  if(gb.update());{
    if(gb.buttons.pressed(BTN_RIGHT)){
      ball_x = ball_x = ball_vx;
      gb.sound.playTick();
    }
  if(gb.buttons.pressed(BTN_LEFT)){
    ball_x = ball_x - ball_vx;
    gb.sound.playTick();
    }
  if(gb.buttons.pressed(BTN_DOWN)){
    ball_y = ball_y + ball_vy;
    gb.sound.playTick();
    }
  if(gb.buttons.pressed(BTN_UP)){
    ball_y = ball_y + ball_vy;
    gb.sound.playTick();
    }
  if(gb.buttons.pressed(BTN_A)){
    gb.sound.playOK();
    }
  if(gb.buttons.pressed(BTN_B)){
    gb.sound.playCancel();
    }
  if(gb.buttons.pressed(BTN_C)){
    gb.titleScreen(F("Ball Control"));
    }
  if(ball_x < 0){
    ball_x = 0;
    }
  if((ball_x + ball_size) > LCDWIDTH){
    ball_x = LCDWIDTH - ball_size;
    }
  if(ball_y < 0){
    ball_y = 0;
    }
  if((ball_y + ball_size) > LCDHEIGHT){
    ball_y = LCDHEIGHT - ball_size;
    }
  gb.display.fillRect(ball_x, ball_y, ball_size, ball_size);
  }
}

also, I was fiddling around with the code and changed the
Code: Select all
if(gb.buttons.repeat(BTN_RIGHT,2)){
      ball_x = ball_x + ball_vx;
      gb.sound.playTick();
    }
    if(gb.buttons.repeat(BTN_LEFT,2)){
      ball_x = ball_x - ball_vx;
      gb.sound.playTick();
    }
    if(gb.buttons.repeat(BTN_DOWN,2)){
      ball_y = ball_y + ball_vy;
      gb.sound.playTick();
    }
    if(gb.buttons.repeat(BTN_UP,2)){
      ball_y = ball_y - ball_vy;
      gb.sound.playTick();
    }

to
Code: Select all
if(gb.buttons.pressed(BTN_RIGHT)){
      ball_x = ball_x = ball_vx;
      gb.sound.playTick();
    }
  if(gb.buttons.pressed(BTN_LEFT)){
    ball_x = ball_x - ball_vx;
    gb.sound.playTick();
    }
  if(gb.buttons.pressed(BTN_DOWN)){
    ball_y = ball_y + ball_vy;
    gb.sound.playTick();
    }
  if(gb.buttons.pressed(BTN_UP)){
    ball_y = ball_y + ball_vy;
    gb.sound.playTick();
    }

but that didnt change anything
I know it's just an example, but if i did something wrong I would like to know so that I do not do it again when I am trying to make a real game

Re: C_Controls example error

PostPosted: Fri Jan 13, 2017 8:10 am
by Sorunome
You use if(gb.update();{ which should be if(gb.update(){

Other than that I haven't taken a closer look at your code yet ;)

Re: C_Controls example error

PostPosted: Fri Jan 13, 2017 8:45 pm
by IPaulasaurus
Nevermind... I realized that alot of my + = and - were mixed up. I fixed it

Re: [SOLVED]C_Controls example error

PostPosted: Sun Jan 15, 2017 5:29 pm
by Sorunome
By the way, you have things like this in your code:
Code: Select all
ball_x = ball_x - ball_vx;


Which can be simplified to this:
Code: Select all
ball_x -= ball_vx;


In general with any math operator you can do instead of this:
Code: Select all
var = var + var2;


this:
Code: Select all
var += var2;


It's less to type! ^.^