Course 2 / Lecture 8:

Making blocks

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

Requirements



Task #1:

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



Task #2:

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



Task #3:

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



Task #4:

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



Task #5:

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] [1] secs at [25]%' block will dirve your robot forward for a sec and the 'Play [262] Hz for [500] 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



Task #6:

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] = [1] to [4]' 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

Program code


More information


Next


Copyright © 2000- |Ozeki Ltd | info@ozekirobot.com |
Page: 6198 | 18.221.15.15 | 79.99.42.43 | Login