Raspberry Pi_Eng_23.4.4 Basics of Using Scratch Program

Published Book on Amazon

All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2

출판된 한글판 도서

최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)

Original Book Contents

23.4.4   Basics of Using Program    Concept of Sprite


If a Sprite is defined in Sprite Area, it appears in Stage Area above it and performs various actions. It is like the main character or actor performing on the stage.


In Scratch, a Sprite plays a role to execute actions or command specified by the program and displays the result on the screen when the program is executed. An object created by drawing is used as Sprite. In Scratch, a program is usually written in a manner in which program commands are executed on the basis of the Sprite, and the execution result is also displayed as a change to the Sprite.    Start and Stop of Program


You can use [green flag] button and [red] button at the top right of the screen to run or stop the program. If [green flag] is pressed, commands specified in the script are executed until it goes to the end. If [red] button is pressed, running program is stopped.    Palette and Block 


In Scratch, commands that can be used are classified and provided as Palette format which is a bundle of Blocks. You can use all 8 kinds of Palette. If you click each Palette, Blocks that indicate commands that you can use actually is displayed below it. The following shows the details of the most commonly used [Motion] Palette and [Control] Palette.    Specifying the Detail Characteristics of the Block


In some Blocks, there is a white area inside the Block, which usually means that a programmer can modify it to the desired value. The property values can be changed for various items according to the nature of each Block. The number in [move] Block below is a value that specifies how much to move in one execution, and it can be freely modified to the value desired by the user. In the adjacent [play drum] Block, you can see that there are two characteristic values that can be entered.    Connection with Other Commands


In Scratch, each Block must be associated with other Block. These associations are represented by the connection of Blocks. The task of connecting Blocks in this way is to write a program.


First of all, if you look closely at [move] Block below, there is a groove on the top and a protruding part on the bottom. They perform the function of connecting to other Block. The upper groove means that there must be some other Block in front of this Block. The protrusion in the lower part means that there is another Block after this to connect with each other. The following right picture shows another Block connected before [move] Block.


However, some Blocks do not have groove or protruding part, which means they can not connect to other Block at that point. For example, you can see that the Block below has a protrusion on the bottom and no groove on the top. This means that no other Block can come before this Block, and there must be some Block after this Block.


This time we will see another Block. If you closely look at the Block below, you can see that there is a protrusion at the top inside the Block. This means that some other Block goes inside the Block. The following right picture shows an example that put and connect another Blocks inside [forever] Block. The inclusion of other Blocks in a Block means that all Blocks contained in the Block are executed when the external Block is executed.    Completing Connection with Other Commands


When you work in the Script Area, the related Blocks must be completely connected to each other. If they are separated from each other, it means that they are not completed yet.


If the Blocks in the Script Area are in the state on the left below, the creation is not completed yet. If Blocks are in the state on the right, it means that the connection between Blocks is complete.    Processing Sequence between Blocks


Blocks in the Script Area are basically run from top to bottom. That is, when the Blocks are connected to each other up/down, the above Block is executed first, and then the lower is executed. It does not matter what kind of Blocks are connected to each other. Regardless of whether the connected Blocks are of same type or of different type, they are executed from top to bottom. The next shows examples where Blocks of the same type are connected and Blocks of different types are connected.    Repetitive Processing


If you want to execute specific Block commands repeatedly, use [repeat] Block or [forever] Block. If you include the Blocks you want to repeat inside it when you write a program, all the Blocks inside will be executed repeatedly. Below is an example of running infinitely when [green flag] is pressed.