[Need Help] Translating gamebuino code into pokitto code

I am currently trying an idea proposed by MLXXXp on the discord, basically I’m having a hard time following the complicated tutorials for pokitto, and I had a very easy time learning with the gamebuino tutorials, so why not combine them both?

essentially I’m going to be learning with the gamebuino tutorials with all of your help by you all hopefully being nice enough to translate the code into pokitto code.

this will hopefully also make it much easier to make some good easy to understand tutorials in the future, and others struggling with coding for pokitto can follow along with my posts and with all of your help.

Gamebuino tutorial to help translate

I’ll post in 3 parts and hopefully you all can translate in 3 parts so its broken down like the tutorial is, and any explanations would be greatly appreciated, here goes.

part 1 = learning to display text from a variable

#include <Gamebuino-Meta.h>

int counter = 0;  // New line

void setup() {
  gb.begin();

}

void loop() {
  while (!gb.update());
  gb.display.clear();

  gb.display.print(counter);  // Something changed here :)
}

part 2 learning to play a basic sound, if statements and getting button input, also slowly moving a rectangle across the screen everytime a button is pressed and adding 1 each time.

void setup() {
  gb.begin();

}

void loop() {
  while (!gb.update());
  gb.display.clear();

  if (gb.buttons.pressed(BUTTON_UP)) {  // If we press UP
    counter = counter + 1;    // Add 1 to "counter"
    gb.sound.playTick();    // Play a ticking sound
  }  // End of IF

  gb.display.print(counter);
  gb.display.fillRect(counter, 8, 2, 4);  // Draw the rectangle
}

part 3 up down, reset buttons to increment the number, connecting score to the size of the rectangle drawn so it gets bigger, aswell as making coordinates for them, and changing the rectangle colour, and number size

#include <Gamebuino-Meta.h>
int counter = 0;


void setup() {
  gb.begin();
}


void loop() {
  while (!gb.update());
  gb.display.clear();


  if(gb.buttons.pressed(BUTTON_UP)){
    counter = counter + 1;
    gb.sound.playOK();
  }


  if(gb.buttons.pressed(BUTTON_DOWN)){
    counter = counter - 1;
    gb.sound.playCancel();
  }


  if(gb.buttons.pressed(BUTTON_MENU)){
    counter = 0;
    gb.sound.playTick();
  }


  gb.display.setColor(BROWN);
  gb.display.fillRect(0, 0, counter, gb.display.height());


  gb.display.setColor(WHITE);
  gb.display.setFontSize(4);
  gb.display.setCursor(8,8);
  gb.display.print(counter); 
}

added my own explanation there for anyone who needed it and may not understand gamebuino code at all (i know a tiny bit)

so if anyone could translate these with an equivelant or more indepth explanation i’m sure us beginners would greatly appreciate it.

5 Likes

I can start with converting first two examples to Pokitto. Unfortunately I did not have time to do the third one or put any comments yet, but others are welcome to improve and continue :wink:

Part 1

#include <Pokitto.h>

Pokitto::Display display;

int counter = 0;  // New line

void update(){
    
    display.print(counter);
}

Part 2

#include <Pokitto.h>
#include <LibAudio>

Pokitto::Display display;
Pokitto::Buttons buttons;

int counter = 0;

constexpr auto tune = SIMPLE_TUNE(C#/8);

void update() {
 
    if (buttons.pressed(BTN_UP)) {  // If we press UP
        counter = counter + 1;    // Add 1 to "counter"
        Audio::play(tune); // Play a ticking sound
    }  // End of IF

    display.print(counter);
    display.fillRect(counter, 8, 2, 4);  // Draw the rectangle
}

1 Like

So I have officially translated part 3 myself with help from the great people over on discord!

here is is, I will also create a thread later today explaining this code for beginners and I will also be explaining how to turn it into a very basic movement system!

#include <Pokitto.h>


Pokitto::Display display;
Pokitto::Buttons buttons;

int counter = 0;
int moveY = 0;
int moveX = 0;

void setup(){
   
}

void update(){
   if (buttons.pressed(BTN_UP)){
       counter = counter + 1;
   }
   
   if (buttons.pressed(BTN_DOWN)){
       counter = counter - 1;
   }
   
   if (buttons.pressed(BTN_B)){
       counter = 0;
   }
   
   display.setInvisibleColor(0);
   display.setColor(C_BROWN);
   display.fillRect(moveX, moveY, counter, 88);
   display.setColor(C_RED);
   display.print(counter);
   
}
2 Likes

The display / core / buttons and so on do not need to be instantiated to use. All there methods are static.

I would suggest that you follow the lead of most other programmers here and change …

… to …

using PC = Pokitto::Core;
using PD = Pokitto::Display;
using PB = Pokitto::Buttons;

Then instead of display.setInvisibleColor(0) you should use PD::setInvisibleColor(0).

I will update my future code thank you bro

Please do … it just makes it easier for newbies if we adopt consistent code. I see its in all your tutorials, will you update these as well?

Your approach works obviously but actually creates an object for no reason :slight_smile:

I’m actually doing it so each tutorial i do shows my development as a programmer, so my next tutorial I’ll update how I do that, because that way I know i can successfully do it, and luckily my second tutorial dismisses that part anyway so it wont be too annoying for beginners to adopt the new style

I may update it actually when I learn it myself, change can be scary for us beginners.

3 Likes