[VERY BEGINNER FRIENDLY] [Tutorial 0.1] How to create a simple counting program in C++ using the Pokitto!

All explanations are baked into the code so you never forget what something means!
comment below if I got any of my explanations wrong and I’ll be sure to alter the comments.

for those who do not know a comment is either done by // before a single line or with

/* comment here
* here
* here
*/ comment ends here
real code here

the compiler ignores comments so you are free to write any of your own explanations as needed!

any questions ask below!

Almost forgot to credit the people on discord who helped me with the issues I faced during the making of this program, so thank you all!

// here we use this to include the pokitto library
// aka api
#include <Pokitto.h>

// This is making it easier to call the display
// function by renaming it to display
// this lets you use ONLY display instead of having
// to use Pokitto::Display in every bit of code
Pokitto::Display display;
// same explanation as above
Pokitto::Buttons buttons;

// Declare a variable called counter
int counter = 0;
// Declaring the Y coordinate size in this case
int sizeY = 88;

/* We need this here whether you plan to use it or not
* It is ran once when the game begins
*/
void setup(){
    
}

/* Unlike void setup() void update() is ran ever
* single frame, and so is necessary for button
* input or anything else you want to occur on
* screen more than once.
*/

void update(){
    
    /* Here we get the button input for UP
    * an IF statement means something will only
    * occur if this happens
    * so if UP is pressed then do everything
    * inside the {} brackets
    */
    if (buttons.pressed(BTN_UP)){
        // here we add 1 to the counter everytime
        // it is pressed
        counter = counter + 1;
    }
    
    if (buttons.pressed(BTN_DOWN)){
        // here when we press down
        // it subtracts 1 from the counter
        counter = counter - 1;
    }
    
    if (buttons.pressed(BTN_B)){
        // here we completely reset the counter
        counter = 0;
    }
    
    // this code was necessary for fixing a bug
    // which made the brown score box avoid the 
    // numbers aka didn't go on top of the box
    display.setInvisibleColor(0);
    // setting the colour of the thing below it
    display.setColor(C_BROWN);
    /*here we create a filled rectangle, 
    * the rectangle is proceeded by some properties
    * inside brackets
    * counter is in the size of the X coordinates so
    *that it increases the square along the X axis
    *when the counter is increased. aka increase size
    *along that direction
    *The Y axis is declared in a seperate variable
    *so you can choose how tall the rectangle is
    */
    display.fillRect(0, 0, counter, sizeY);
    //Displaying the counter on screen (very important)
    // even though the colour is clear, if you forget to change it it makes the text
   // default to yellow so just had to edit it back in
    display.setColor(C_RED);
    display.print(counter);
    
}

In the next tutorial I will be transforming this code into a movement system!

Part 2!

7 Likes

Well done. Just joined and already contributing a tutorial. A commendable start!

3 Likes

I plan on adding more :wink: keep an eye out

4 Likes