[Tutorial][Community]1.Level 1-1, hello world!

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!

15 Likes

Great, @adekto! That is a nice way to start people off.

I am taking a stab at some proposed edits and embellishments, will post those later tonight.

1 Like

First section of proposed edit:

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, like NotePad (on Windows) or Text Edit (on Mac). A specialized text editor like the one I will be using (Atom) is even more useful, since it will make programming easier (spotting command syntax errors, sometimes autocompleting certain commands, see the automatic coloring below

[CODE IMAGE]

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.

int main()

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 empyt 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.

no seriously im not using atom XD

I try to interpret it to Chinese :slight_smile:

这是一篇关于C++和Pokitto库的介绍,它让你能够在Pokitto平台上
编写出你自己的游戏来。
从这开始,我会通过一个简单的程序让你熟悉构建游戏程序的基本元素。绝大多数编程教程开始都会讲解“hello world”程序,因为他们能简单地把“Hello World!”显示到你的屏幕上。你会很想要在一个文本编辑器上开发你的游戏,Pokitto的编程环境

(IDE)会很快面世,但你也可以使用一个平常的纯文本编辑器(比如Notepad,还有Textedit)。也有一些特殊的文本编辑器能让你编程时更轻松(比如标注命令语法错误,有些还能自动补全命令,明确标注颜色,在以下这些编辑器中,任何人都可以建立“Monokai”配色方案:Atom, Sublime Test, CodeLite)

https://talk.pokitto.com/uploads/default/original/1X/b10f312a30d1c090df1791387b0b65f05e06542e.png

范例图1

别担心! 让我们看一看上面说的什么,一行一行来

#include “pokitto.h”

第1行是将Pokitto核心库给包含进来。构建了一些需要与Pokitto硬件各个部分对话的代码。因为这些代码是为你准备的,它让事情变得更简单了:它作为你基本构建的块。这个库包含许多方便的函数,其中就包括我们马上要在范例中使用的。

//Creating a pokitto game object in global scope

第2行是一条注释,通过 // 两个斜杠指示,它不是代码,是用来在文档中说明代码是干什么用的。在你的代码中加入这些注释是一种好习惯。不仅能帮助其他人更快的感知程序的每一部分(也能帮助你以后回看)你会看到像这样更多的注释,他们能更好地教你阅读范例代码。

Pokitto::Core game;

第3行 — 上一行的注释已经告诉你了,它使用了一点Pokitto核心库的预制代码:核心对象类型。基于它这个类型的一个新的对

象“game”被创建。我们确实在这儿创建了游戏自己的心脏。如果你想要起一个新的对象名,你当然可以重命名。

https://talk.pokitto.com/uploads/default/original/1X/eaa6adef35395683690cfe048a073ff793592c2f.png

范例图2

第4行创建了一个“main”函数。一个返回类型跟随在函数名前面:当这个函数被调用后一定会返回这个类型的数值,当然如果这个函数没有什么需要返回的类型,也可以将其设置为void。我们的“main”函数前面已经标识了“int”说明这个函数期望返回一个整型。这个main函数是程序所谓的进入点,程序一定是从它这儿开始运行。函数还可以通过在括号中加入参数来影响它们的行为。我们的main()函数没有参数,因此只需要一个空括号即可。在下面的教程中,我们还会讲解它更多的细节。

放在代码行后面的分号(;)说明代码的结束,基于此你的代码才会被转换成计算机能够理解的可执行的机器码。

//start the game object
game.begin();

第5行是另一个注释,第六行我们调用begin函数。这个函数也是Pokitto核心库中game对象类型的一部分。

// main program loop
while (game.isRunning()) {

在第9行我们开始一个while循环。这样的while循环会持续重复它里面的代码,只要给出的条件成立。在这个例子中函数

game.isRunning()会一直返回true值,在我们的代码执行期间。从这行后面的中括号({ )开始,后面是这个循环中的代码段。这后面的什么地方,也一定会有另一个中括号( } )来结束这个代码块。如果没有缩进(利用空格或tab键来调整你写作的格局)这样的代码段会看起来特别费劲,所以像范例一下缩进你的代码就是个好主意了。许多菜鸟级错误就是由于忽略缩进或者在某处忘了放结尾的中括弧。所幸的是专用的代码编辑器会帮助你找出这些错误来!

// if it is time to update the screen
if (game.update() {

第11行有一个if选择语句,它用来测试是否有一个明确的条件符合,如果符合,就会执行它所属块中的代码。(再一次,和上面一样,块需要一对中括号来闭合)

game.display.setCursor(0,0);
game.display.color=1;
game.display.print(“Hello World!”);
}

第12.13.14行都是用来处理显示的,先设置光标在屏幕中的x y坐标位置,用于准备显示文本。然后设置调色板1号颜色,最后显示出字符串(程序员管文本叫字符串)"Hello World"显示在屏幕上!

现在,为了感受一下编写代码,我建议你找一个不错的代码编辑器,把上面的代码打一遍,然后写下你自己的注释,再让它显示出

Hello加上你自己的名字。

开始爽编程吧!!!!

4 Likes