[SOLVED]C_Controls example error

Understanding the language, error messages, etc.

[SOLVED]C_Controls example error

Postby IPaulasaurus » Fri Jan 13, 2017 1:36 am

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
Last edited by IPaulasaurus on Fri Jan 13, 2017 8:45 pm, edited 1 time in total.
User avatar
IPaulasaurus
 
Posts: 22
Joined: Sun Dec 25, 2016 3:31 pm
Location: Deeeeeeeeeeeeeeeeeep Space

Re: C_Controls example error

Postby Sorunome » Fri Jan 13, 2017 8:10 am

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 ;)
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm

Re: C_Controls example error

Postby IPaulasaurus » Fri Jan 13, 2017 8:45 pm

Nevermind... I realized that alot of my + = and - were mixed up. I fixed it
User avatar
IPaulasaurus
 
Posts: 22
Joined: Sun Dec 25, 2016 3:31 pm
Location: Deeeeeeeeeeeeeeeeeep Space

Re: [SOLVED]C_Controls example error

Postby Sorunome » Sun Jan 15, 2017 5:29 pm

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! ^.^
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm


Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 13 guests

cron