Program blocks are often called as functions or procedures in program languages. Without program blocks it is not advisable to write larger programs, that complete more advanced tasks. In this lecture you will learn how create custom program blocks. You will also learn how to edit, delete previously created blocks, and how to execute the functionality defined within a program block.
Figure 1 - Program about using own block
- Ozeki 10 installed: http://www.ozeki.hu/index.php?owpn=6066
- SNAP: http://www.ozeki.hu/index.php?owpn=6180
- Sniffer: http://www.ozeki.hu/index.php?owpn=6204
Discover how you can create a new block
In this step you will discover how you can create a new block. For that open the SNAP. You should see a button 'Make a block'. Click on it. You will see a pop-up window (Figure 2). In this window you can choose what kind of block you would like to create and you should type the name of the block. Under your block's name you can select the type of the block: 'Command', 'Reporter' or 'Predicate'. You usually use the command block if you would like to create a block that give a command for something. Reporter block for example the 'call [block]' block have a return value. You can use predicate blocks if you would like a block that can return true or false. Finally you can set the new block belongs to all sprites or for this sprite only.
Figure 2 - 'Make a block' window
Discover how you can edit a block
In the previous task you have typed the name of the new block. Click on 'OK' and you will find yourself in a new window called 'Block editor' (Figure 3). You can edit your blocks here. You can see the new block's name and '+' icons the name's left and right side. If you click on one of the '+' icon you can type text or add an object, a number, a list etc. to your block. You can drop blocks into the 'Block editor' like in 'Scripts' field and make programs here. Later you can use your own blocks like the other blocks. You can reach that window by right clicking on the new block and click on 'edit ...'.
Figure 3 - 'Block editor' window
Discover how you can delete a block
In this task you will discover how you can delete a block that you have created. If you right clicking on the block you will see a 'delete block definition ...' option (Figure 4). If you click on it you will see a pop-up window called 'Delete Custom Block'. In this window you can decide that you really want to delete your block and all its instances or not. If you sure about delete click on 'Yes' else click on 'No'.
Figure 4 - How you can delete your block
Discover how you can call a block
You can call a block easily with 'call' block (Figure 5). It is a predicate block from the 'Control' category. It wait a reporter block or a predicate block for it's input. If you drop a block from the categories or your own block for the 'call' block input's parameter it will call the block. You can call your blocks in loops, operators, conditions, etc., too. You can call them if you drop it where you would like to execute the block. You will make your first block in the next task.
Figure 5 - 'Call' block in SNAP
Make your first own block
In this task you will make a block that can drive the motor forward then play a 'beep'. Click on 'Make a block' and type 'go_and_beep' for the block's name. Click on 'OK' then click on the '+' icon the name's left side. You will see a new window called 'Create input name' and click on the arrow. Select 'Object', click on 'OK' then you can change the icon by right clicking on it. Select the 'turtle' icon. Create a new object to the name's right side, too. Select the 'notes' icon for that object. Now, you should drop the necessary blocks into the 'Block editor' (Figure 6). The 'Drive [A+B] [forward]  secs at %' block will dirve your robot forward for a sec and the 'Play  Hz for  ms' block will play a beep with the Brick's speaker. Click on 'OK' and now, you can use your block.
Figure 6 - Your first block
Write a program that use your block
In the previous task you have made your first block. Now, you will use it in a program. You should drop your block into a 'for [i] =  to ' block (Figure 7). The program will drive forward the robot and play a beep with the Brick's speaker for 4 times if you click on the green flag icon. Through the program you can see that you can use easily your own blocks just like the other blocks in SNAP.
Figure 7 - A program using your block
Download the program|
The program is capable to use your block created in this Lecture.
Download Ozeki SNAP code
- My first 'hello robot' program
- Saving and loading my project, writing sequential programs
- Using loops to repeat tasks
- Understanding events
- Understanding conditions (and using sensors)
- Using variables and operators
- Using nested loops and conditions
- Making blocks with input and return value
- Using broadcast events