This is an introduction to the C++ programming language and the pokitto library, which will allow you to make your own games on the Pokitto platform.
To start with, I am going to go over this simple program, to familiarize you with the basic elements of a game program like the ones you will be building. Most programming tutorials start with such so-called ‘hello world’ programs, since them simply draw the words “Hello World!” on your screen.
You will usually want to develop your game program code in a text editor, pokitto will have a IDE for this soon but you can also use a regular plain text editor (Notepad, Textedit). there are also specialized text editors for this they make programming easier (spotting command syntax errors, sometimes autocompleting certain commands, see the automatic coloring below, for anyone who wants this color setup its called Monokai in most of these editors: Atom, Sublime Text, CodeLite)
Don’t be intimidated! Let’s take a look at what that means, line by line:
#include "pokitto.h"
This first line includes the core library of the pokitto. This is a library of core functions, ready made bits of code that are needed to talk to all the parts of the pokitto hardware. Because these bits of code are all ready-made for you, it makes things easier: think of it as your base building blocks. The library contains some convenient functions that we are going to use in this example.
//Creating a pokitto game object in global scope
Line 2 is a comment, indicated by the two forward slashes (//
), it is not code at all and is meant to document what the code is doing. It is good practice to put such comments in your code: not only will it help others makes sense of what each part much does faster (and also help yourself when you revisit your code later). You will see comments like these all over and they are great to learn to read code examples.
Pokitto::Core game;
Line 3 - as the comment above tells you - uses a bit of pre-made code from the pokitto Core library: the Core object type. It is used to create a new object of that type called ‘game’. We are actualy creating the heart of the game itself here. You could rename it if you want but it is sort of a best practice to call this game.
Line 4 creates a function called ‘main’. A function starts with a return type: this is the type of value it will return when called, if the function has nothing to return the type is set to void. Our main()
function is preceded by int
to indicate that the function is expected to return an integer. The main function is the program’s so-called entry point, where the main game functional code starts. Functions can have parameters added to them that will affect their behaviour and these parameters are assigned between parentheses ()
. Our main()
function has no parameters, and thus just the two empty parenthese with nothing between them. We will cover this in more detail in the next tutorial.
The semicolon at the end of the line is how every line of code ends, so the software that converts your code into an executable, the actual machine code instructions that your computer can understand.
//start the game object
game.begin();
Line 5 is another comment and on line 6 we call the function ‘begin’. This function is also a part of of the Game object type in the pokitto Core library.
// main program loop
while (game.isRunning()) {
In line 9 we start a ‘while’ loop. Such while loops will continue to repeat the code inside them as long as a given condition is met, in this case as long as the function game.isRunning()
function will keep returning the value true
during our execution of the code. The curly braces at the end of the line are the start of the code section that is contained within the while loop. Somewhere later on there will have to be a closing curly brace to close the block. Without indentation, such code sections would be harder to spot, so indenting your code as in this example is a good idea. A lot of beginner errors are due to forgetting the indentation or the closing braces somewhere. Luckily dedicated code editors help you spot those errors!
// if it is time to update the screen
if (game.update()) {
Line 11 has an if
statement, which is a test to see if a certain condition is met. If it is, then it will execute its block of code (again, that following block is indented and enclosed by curly braces).
game.display.setCursor(0,0);
game.display.color=1;
game.display.print("Hello World!");
}
}
Lines 12, 13 and 14 all deal with the display, setting the cursor to the desired x and y position on the display, ready to start printing text, then setting the color
to index 1
of the color palette and finally printing the string of letters (a string is what programmers call text) “Hello World!” on the screen!
Now, in order to get a feel for writing code, I would suggest that you get a good code editor, type over this code into it, but then write your own comments and let it print out “hello” plus your own name. Happy coding!