Graphical Programming & Robots
Graphical Programming & Robots
Learning Target
1
Introduction to
Programming
- Smart Assistant
Figure 1.1
2
In Figure 1.1, the yellow block and the purple block mean instructions, and the
girl Kelly is a set sprite. We can randomly set this sprite. It can understand and
execute the instructions from the yellow block and the purple block in the
program.
After we select a sprite and write a program by drawing blocks, we can click
the green flag button to run the program. Then, the purple block will instruct
Kelly to show "Hello!" in the bubble form.
Let's find it
Find and double-click DobotBlock on the computer, and now, we
come to the interface; watch carefully, and do you find anything
new?
There are Menu, Stage, Device, Sprite and Stage List, Block Area, Coding
Area and Tabs on the DobotBlock interface, see Figure 1.2.
Figure 1.2
Let's do it
Freely click icons on the interface, read and share them with
others.
3
(1) Menu
We can see File, Edit, Help and Tutorial Center on the DobotBlock interface;
each of them has different menu items for different functions. For example,
there are New, Save, Load from your computer, Save to your computer and
Recent under File, see Figure 1.3.
Figure 1.3
(2) Stage
We can move or interact sprites (or devices) on the Stage, see Figure 1.4.
Do you still remember Kelly? She said hello on the Stage. Yes, we can move or
interact sprites (or devices) on her Stage, see Figure 1.4.
Figure 1.4
4
We can see 6 icons on top of the Stage. Boys and girls, let's guess which is
Start and which is Stop on the left? What do the three stage modes mean on
the right?
Let's read it
Stage Icons
By default, a new item includes a device. For the device list, see Figure 1.5.
5
Figure 1.5
Let's read it
By default, a new item includes a sprite. For the sprite list, see Figure 1.6.
6
Figure 1.6
In the sprite control module, we can show or hide this sprite on the stage, or
modify its name, or change its position by modifying X and Y values, or set its
size and direction. For example, Figure 1.7 shows the sprite control module for
the sprite "John (1)".
Figure 1.7
7
Let's do it
Click the Sprite List in the software, and try adding a sprite "John
(1)".
Modify the name of the sprite name to "John", and set its size to
60, change the X and Y values to 0, and the direction to 0. Watch
and describe the change in the sprite on the stage.
As shown Figure 1.8, the Block Area is on the left, and the Coding Area is on
the right.
Figure 1.8
Block Area: This area provides blocks necessary for programming, and we can
search for our desired blocks by class and color.
Coding Area: This area is for programming; we can drag blocks to this area to
program.
From the block area in DobotBlock, we see different types of blocks and the
Add Extension button, and different blocks in different modules. A module
includes blocks with the same color, and different modules include blocks with
different colors. We can add different extension modules by clicking Add
Extension, for example, AI extension, music and pen modules.
8
As we use the sprite, the block area is divided into nine modules. Of them, the
Motion module includes blue blocks, and the Looks module includes purple
blocks, see Figure 1.9.
Figure 1.9
To create a program, first drag our desired blocks with a mouse to the coding
area from the block area, and according to needs, then connect and combine
these blocks just as we play puzzles. These connected and combined blocks
are called scripts.
Let's do it
After adding the sprite "John", find the "when the green flag
clicked" block in the Events module and the "say 'Hello!' for 2
seconds" in the Looks module, and connect and combine these
two blocks, see Figure 1.10.
Figure 1.10
9
In DobotBlock, we can process a sprite or backdrop picture. For example, after
we select a sprite, we can change the color and size and so on by clicking the
costume tabs. As shown in Figure 1.11, we can delete some costumes of the
sprite "John" in the costume tabs, and modify the name of the costume.
Figure 1.11
10
Let's read it
Sounds
Besides Code and Costumes, the sprite can play sound. This function
makes the sprite lifelike. In Sounds, we can manage the sound played
by the sprite. For example, we can edit the name and effect and so on
of sound, see Figure 1.12.
Figure 1.12
Research Laboratory
1. Task Release
(1) Task 1
In DobotBlock, we can upload a written program (Sing a song), and run it.
(2) Task 2
Processing Workshop
11
Upload a
Program
Task 1
Step 1: Enable the programming software, click File in the menu, and
Load from your computer, see Figure 1.13.
Figure 1.13
Step 2: Select the file "Sing a song.sb3", and finally click Open to
upload the program.
12
Run a
Program
Task 1
Step 1: Click the Sprite List, and click Green Flag on the stage to run
the program, see Figure 1.14.
Figure 1.14
Step 2: Click Stop on the stage to stop the running program, see Figure
1.15.
Figure 1.15
Knowledge Base
Boys and girls, since we have known DobotBlock software and tried writing
and running a program, could you tell me what is programming?
13
Let's read it
What is programming?
Programming is also known as program design. It is a program process
to solve a specific problem and also a major step for software
development. Program design generally uses a program design
language as a tool, and gives a program of this language. Program
design includes analysis, design, coding, testing, and debugging.
A program, a series of computer instructions arranged in order, is
equivalent to the orderly task document of a computer. A computer
instruction means an instruction or command that has a computer to
execute a task.
Innovation Park
Modify the program "Sing a song": Ask the sprite to sing the song, then run
the program, and watch the change in the sprite on the stage.
Self-Assessment Room
Content Result
I've known the DobotBlock software and its
interface ☆☆☆☆☆
14
Chapter 2 Motion
Control
- Smart Porter
John has recently opened a supermarket. He uses a smart robot as a helper.
In this supermarket, the robot handles articles by motion. But how does the
robot move and handle cargoes?
In this chapter, we will learn the Motion module through DobotBlock. The
module can control the motion mode, position, direction and so on of a sprite or
device. To control the motion of a sprite and a robot, we must have a command
of some mathematical knowledge and logic. Then, how do we control the
motion of a sprite and a robot. Boys and girls, let's explore this question!
Learning Target
15
2.1. Sprite Motion
John and Kelly found Mr. Lee, and expressed their idea and requirements. Mr.
Lee enabled DobotBlock in his computer, and asked John and Kelly to find
Motion in the block area, see Figure 2.1. John and Kelly found many blocks in
Motion of the sprite.
Figure 2.1
16
idea
John and Kelly find that these blocks can not only move the position of a robot,
but also enable it to rotate. This is so fun!
idea
Let's do it
Figure 2.2
Boys and girls, we have finished running the program. Now, do you have any
question?
After running the program, John and Kelly find a big problem, and ask Mr. Lee,
"Mr. Lee, this robot turns right only. It cannot reach the designated position, nor
turn around. What shall we do?"
17
(2) Robot moving forward or backward
Mr. Lee asks John and Kelly to continuously watch the blocks in the motion
module, and to think about the function of the "go to x: (-10) y: (0)" block, see
Figure 2.3. What does this block do?
Figure 2.3
John and Kelly find two values users can enter in the "go to x: (-10) y: (0)"
block. They understand the y value is 0. But they do not know why the x value
is -10. This is a negative number in math. Boys and girls, do you know
negative numbers? Let's guess what motions this block enables a robot to
complete in this program.
18
Let's do it
with and run the program, and watch how the robot
moves, see Figure 2.4.
Figure 2.4
Boys and girls, do you see other negative numbers in life? Let's get to know
them.
19
As shown in Figure 2.5, the part on the left of zero along the number axis refers
to negative numbers, while the part on the right of zero refers to positive
numbers. A positive number is greater than zero, a negative number is smaller
than zero, and a positive number is greater than a negative number.
Figure 2.5
Positive numbers and negative ones are applied to not only math but also life.
Boys and girls, as it is very cold, particularly in the case of icing, we often hear
"centigrade degrees below zero" from weather forecast. For example, in Figure
2.6, it is "44 centigrade degrees below zero" in Antarctica, which is also written
as "-44℃".
Figure 2.6
20
idea
Figure 2.7
In a plane Cartesian coordinate system, we can set the coordinate of any point
in the plane. The coordinate of any point in the plane contains two value: one
indicates the lateral coordinate or x-coordinate; the other one indicates the
longitudinal coordinate or y-coordinate.
The coordinate of any point in the plane is written in a pair of brackets, where
the x-coordinate is placed before the y-coordinate, and both are separated by
a comma. As shown in Figure 2.8, the coordinate of point A is (3, 2), and that of
point B is (-4, -2).
21
Figure 2.8
Boys and girls, can do you find the coordinate of any point in the coordinate
system? Let's interact and play two rounds of the "report the coordinate by
point" game!
Now, you have known the plane Cartesian coordinate system. But do you
know who invented it?
Let's read it
The plane Cartesian coordinate system (or 2D coordinate system) in
math is a great invention by French mathematician Rene Descartes.
Boys and girls, do you know what plane figure the stage of DobotBlock
software has?
Yes, it is a rectangular area, with the center being the origin of the coordinate
system. Can you read the stage scope? See Figure 2.10.
22
idea
Let's describe it
What do you learn about from Figure 2.10? In this plane Cartesian
coordinate system, how do we indicate the coordinate of any point?
Figure 2.10
The motion of the sprite is actually the motion of its coordinate point, during
which the point moves to another position from one position. The position of
the sprite depends on the x-coordinate and the y-coordinate of its center. As
shown in Figure 2.11, the central position of the robot has a coordinate (0, 0),
and the sprite robot stands at the center of the stage. If we move the central
position of the robot to the coordinate (100, 100), the sprite robot will move to
the top right corner of the stage, see Figure 2.12.
23
idea
Let's try it
To move a robot to the stage center through a program, we just need to
change the values of the x-coordinate and y-coordinate in the "go to x:
(-10) y; (0)" block to 0, and click the green flag button, see Figure 2.13.
Figure 2.13
Boys and girls, do you find that the "go to x: (-10) y; (0)" block is to
accurately move the sprite to a specific position on the stage? Yes, it is
a practical and convenient block.
Let's try it
How do we move a robot to the top right corner of the stage through a
program? Similarly, we just need to change the values of the
x-coordinate and y-coordinate in the "go to x: (-10) y; (0)" block to 100,
and click the green flag button, see Figure 2.14.
Figure 2.14
24
Let's do it
Use the "go to x: (-10) y; (0)" block to modify the x-coordinate and
y-coordinate values:
Control a robot to move to the top right corner of the stage.
Control a robot to move to the position (145, -18) of the stage.
Control a robot to move to any set position of the stage.
Then, how do we choose the rotation direction of a sprite? First, click Motion,
and find the "point in direction (90)" block, see Figure 2.15.
Figure 2.15
Drag the "point in direction (90)" block to the Coding Area, and click the value
area of this block. Then, a disk like a dial pops up, see Figure 2.16.
25
Figure 2.16
Of course, besides the above four cases, we can choose many other directions
of sprite rotation, see Figure 2.17. We can choose any direction from a
360-degree circle. See Figure 2.18 for the detailed angle corresponding to
each direction.
Figure 2.17
Figure 2.18
Boys and girls, have you learned sprite rotation? Let's perform a challenging
task.
26
Research Laboratory
1. Task Release
Program the Robot to Draw a Square: Program a robot to draw a square on
the stage, for example, draw a square with a 7 cm side.
2. Task Analysis
Boys and girls, if you want a robot to draw a square, what should you
consider?
From the Control module, you need to find the module that controls the waiting
time every time the robot finishes drawing a stroke;
Carefully watch the following figure. We find this figure has four equal sides
and four right angles. It is called a square, see Figure 2.19.
Figure 2.19
Obviously, a robot takes four steps to draw a square, see Figure 2.20:
27
Figure 2.20
You usually draw a square with a ruler and a pen. How does a robot do so in
the programming software? From the task analysis we can find that this job
uses the Motion module and the Pen module.
Task
Sprite Block
Description
A robot draws
four lines to
form a
square, with
each line
being 100
long.
Processing Workshop
28
Add a Sprite and
Module
Step 1: Select the default sprite "Sprite 1", see Figure 2.21.
Figure 2.21
29
Add a Sprite and
Module
After we add a sprite, we often need to adapt its size to the stage, or
change its name to facilitate use. In this case, we also need to modify
the name and size of the sprite, see Figure 2.22.
Figure 2.22
Step 2: Add a Pen module. Click the Add Extension button to add the
Pen module, see Figure 2.23.
Figure 2.23
30
Write a Sprite
Script
Select the sprite robot in the sprite column. As per the script plan, drag
a block, and program the sprite robot in the coding area to draw a
square.
Step 1: Select the sprite robot, write a program shown in Figure 2.24,
and draw the first line towards the right.
Figure 2.24
Step 2: Select the sprite robot, write a program shown in Figure 2.25,
and draw the second line downward.
Figure 2.25
31
Write a Sprite
Script
Step 3: Select the sprite robot, write a program shown in Figure 2.26,
and draw the third line towards the left.
Figure 2.26
Step 4: Select the sprite robot, write a program shown in Figure 2.27,
and draw the fourth line upward.
Figure 2.27
32
Write a Sprite
Script
Step 5: Combine the above programs to allow the robot to draw a
square. The relations between the coordinates of the four angles of the
square and the coordinates set in the program are shown in Figure
2.28.
Figure 2.28
Note: The initial position of the robot may be any point on the stage. In
Figure 2.29, when we move the position of the robot, the coordinate
point on the "go to x: (-10) y: (0)" block will change accordingly.
Figure 2.29
33
Innovation Park
Figure 2.30
Self-Assessment Room
Content Result
34
2.2. Robot Motion
- Cargo Handling
The coordinate system offers two axes, namely, x-axis (lateral axis) and y-axis
(longitudinal axis). Two values (x, y) can set the coordinate of a point.
35
idea
Figure 2.31
This coordinate system has three axes, namely, x-axis (lateral axis), y-axis
(longitudinal axis), and z-axis (vertical axis), which are also called coordinate
axes. The coordinate axes and the origin constitute a space Cartesian
coordinate system 0-xyz. We can determine any point in space as 0, through
which to draw three mutually perpendicular axes (Ox, Oy and Oz). These axes
take 0 as the origin, and offer the same length unit.
In this coordinate system, three values can set the coordinate (x, y, z) of a
point, with 0 point being the coordinate origin. For example, how should we
denote the top position of Kelly through a space Cartesian coordinate system?
As shown in Figure 2.32, point B' in her top position has the x-coordinate value
as 1, the y-coordinate value as 1 and the z-coordinate value as 1. Thus, the
coordinate of point B' is (1, 1, 1).
Figure 2.32
36
When we learn about the space coordinate of a point of a robot, we can
accurately control the position of such a point. For example, to move right such
a point 6 mm, forward 7 mm, and upward 5 mm, we just change the parameter
of its coordinate. The space Cartesian coordinate system of a robot is shown in
Figure 2.33.
Figure 2.33
Tip: No R axis exists without an end kit carrying a steering engine on the robot.
For example, the robot can rotate around R axis as it uses the suction cup and
the soft gripper.
37
idea
Figure 2.34
Do you know how to move a robot to point B from point A along the
door-shaped route?
Let's think about this question this way: First raise the robotic arm to
a certain height from point A, then move it horizontally to the
position above point B, and finally lower it to the position of point B.
Figure 2.35
Research Laboratory
1. Task Release
Create a "Porter" Game: Program a robot to simulate a porter to carry a block.
Boys and girls, think back how a porter loads cargoes. Generally, what should
a porter do before he carries cargoes to a truck from the storage zone? See
Figure 2.36 for this process.
38
Figure 2.36
In this task we should use a robot to simulate a porter to carry cargoes twice. In
this course we will use a block to simulate a cargo, and carry two stacked
blocks to the loading zone from the storage zone. We will also stack those
blocks placed in the Loading Zone. For the arrangement of materials, see
Figure 2.37.
Figure 2.37
2. Task Analysis
To complete this task, you need to consider:
Program a robot to carry blocks, and have a robot carry a block to the
designated position with a suction cup;
39
Figure 2.38
3. Script Plan
A robot carries a
block to the loading
zone from the
storage zone.
Processing Workshop
Click Add Device, choose Magician Lite from the device library, and
delete the default device Magician, see Figure 2.39.
Figure 2.39
40
Add and Connect a
Device
Figure 2.40
Write a Device
Script
Select a robot. As per the script plan, drag a block, and program the
Robot device in the coding area to carry a block.
Step 1: Select the Robot device, and write a program shown in Figure
2.41.
Figure 2.41
41
Write a Device
Script
Each block is fixed in height, and about 10 mm in concave height. As
shown in Figure 2.42, after we determine the suction position of the first
block, we can reduce the coordinate of z axis by 10 mm to determine
the suction position of the second block.
Figure 2.42
Step 2: Write a program for "carrying the second block". Select the
Robot device, and program the blocks shown in Figure 2.43.
Figure 2.43
42
Write a Device
Script
Step 3: Splice and combine the program “carrying the first block” and
“carrying the second block”, see Figure 2.44.
Figure 2.44
43
Let's read it
Move the end tool of a robot. In this case, the coordinate value on the
control panel changes accordingly. We can quickly get the coordinate of
the end tool of the robot by right-clicking Fill coordinates, see Figure
2.45.
Figure 2.45
Innovation Park
Blocks in the course are stacked. If we disperse them, what should a robot
do to carry them? Write a program to carry these blocks to the loading
zone from the storage zone. For the arrangement of blocks in the storage
zone and the loading zone, see Figure 2.46.
Figure 2.46
44
Self-Assessment Room
Content Result
I've known the space Cartesian coordinate ☆☆☆☆☆
system
I've completed the task of a "porter" ☆☆☆☆☆
45
Chapter 3: Data and Operation
Learning Target
46
3.1. Program Data (1)
Cargo delivery
robot? Is it so
magical? Let me look
at it.
You got it!
idea
3.1.1 Variables
Today, we are going to learn variables. A varying value is called a variable.
Delivery addresses change with directions and distances of customers' homes.
We can indicate a distance with a variable. Thus, it will be OK if the robot
knows a distance variable.
Let's do it
Introduce the backdrop into the stage
47
3.1.2 Create a Variable
In DobotBlock, click Make a Variable in the Variables module. Enter a new
variable name, click OK, and now we have created a variable, see Figure 3.1.
Figure 3.1
Let's do it
Create a "distance" variable.
idea
Figure 3.2
Let's do it
Set the initial value of the "Distance" variable to 0.
Try programming the cargo delivery by a robot.
48
(2) Modify the value of the variable
After a robot delivers a cargo, the device must return to the supermarket. In
DobotBlock, we can modify the value of the variable in the "increase (Distance)
by (1)" block and the "set (Distance) to (0)" block. Now, we can rely on the
"increase (Distance) by (1)" block to increase or decrease the current value of
the "Distance" variable.
Let's do it
Write a program to allow the robot to return to the supermarket.
Tip: If the increased distance during cargo delivery is positive, the increased
distance during return will be negative.
Knowledge Base
49
Let's read it
Boolean type originates from Boolean algebra. This type of data
contains only two values: true and false. Boolean type of data usually
acts as the result of a conditional judgment. For example, this
expression, 5 > 3, is valid, so Boolean value returned by this expression
is true. This expression, 5 < 3, is not valid, so Boolean value returned by
this expression is false.
Research Laboratory
1. Task Release
John receives three orders today. Now, he must set his robot to deliver
cargoes. The delivery destinations are Gary's home, Daisy's, and Ella's
respectively. Every time the robot finishes delivering a cargo, it must return to
the supermarket to pick up another cargo and deliver it to the next destination.
Boys and girls, now let's design this program together.
2. Task Analysis
(1) Sprite interface
A robot picks up a cargo from John's supermarket and starts out. It determines
the first destination as Gary's home, sets the "Distance" variable as a distance
from Gary's home, moves the "Distance" toward Gary's home and reaches that
destination. Then, it jumps back to the supermarket to pick up another cargo,
and deliver it to the next destination.
John's home delivery service falls into three parts, see Figure 3.3.
Figure 3.3
50
(2) Device interface
The initial position of the end of the robot is set in John's supermarket. The
robot grips the cargo with its end gripper, and uses the "move along a
door-shaped route to X()Y()Z()R()" block to carry the cargo to Gary's home,
and release it. Then, the robot uses such block to jump back to the
supermarket, and grip and carry another cargo to the next destination. See
Figure 3.4 for the device arrangement position.
Gary’s
Home
John’s
Ella’s Home supermarket Daisy’s
Home
Robot
Figure 3.4
Home delivery service by the robot falls into three parts, see Figure 3.5.
51
1. Grip the cargo Release the cargo
and jump to and jump back to
Gary’s home the supermarket
Figure 3.5
1. Deliver the
cargo to Gary's
home
2. Deliver the
cargo to Daisy's
home
3. Deliver the
cargo to Ella's
home
52
4. Device Script Plan
Task
Device Block
Description
1. Deliver
the cargo to
Gary's
home
2. Deliver
the cargo to
Daisy's
home
3. Deliver
the cargo to
Ella's home
53
Processing Workshop
Upload a Stage
Backdrop
Step 1: Add the backdrop. Click Upload Backdrop in the Stage List, and
find the file "John's supermarket.png", and upload the file to the stage,
see Figure 3.6.
Figure 3.6
Create a Variable
and List
Click Make a Variable in the Variables module, and fill in the variable
name "Distance", see Figure 3.7.
Figure 3.7
54
Write a Sprite
Script
Figure 3.9
There is a similarity between the program to handle the cargo to
Gary's home and the one to handle the cargo to Daisy's and Ella's
homes. Boys and girls, please write the program for the later delivery
yourselves.
55
Innovation Park
To modify the value of a variable, we can not only "set (Distance) to 0", but
also use "increase (Distance) by (1)". In the task of home delivery, can we
implement the function of "increase (Distance) by (1)" or both "set
(Distance) to 0" and "increase (Distance) by (1)"? Please consider this
question. And perform and complete this task yourselves.
Self-Assessment Room
Content Result
56
3.2. Program Data (2)
- Store Opening
My supermarket is launching a
lucky draw for store opening
ceremony. Only if you press the
draw button, the chosen prize
Why is this place so appears in the screen. Come try
bustling with a sea of your luck. Maybe, you will win a
people? grand prize!
John wants you to think about two questions. How does the draw system
randomly offer awards? How does the draw system store so many prizes?
1. List
A variable can store a single value. But if you desire to store a series of values,
the variable may fall short of your expectation. For example, there are 20
prizes. Supposing you intend to store the names of 20 prizes, the program will
have to use 20 variables. This is a complicated job. But we can rely on a list to
solve this problem.
A list is a container storing multiple variables. You can store and get the value
of each variable in the container.
To create a list, we first name it, and then access each variable in the list based
on the variable position.
Click Make a List in the Variables module, see Figure 3.10. Fill in the list name.
Here, we name the list "Prize List", see Figure 3.11.
57
Figure 3.10
Figure 3.11
After we create a list, the list is initially empty. So its length is 0. We can click
the "+" sign in Figure 3.12 to add a new variable, and change the size of this
interface by dragging the equality sign with a mouse.
Figure 3.12
If the Prize List in the list contains four different prizes, we can create four
variables by clicking the "+" sign in the lower left corner four times. Each
variable is an element of the list. In the editing brackets, enter the names of the
prizes, namely, candy, toy, school bag, and pencil, see Figure 3.13.
58
Figure 3.13
Let's do it
Create Prize List to store the names of the prizes.
Research Laboratory
1. Task Release
Create a Lucky Draw System: John's supermarket launches a special offer for
opening. Please press the luck button, and you may draw a lucky prize.
2. Task Analysis
The draw system provides the Start sprite and the Stop sprite. Start functions
to start the draw. After you click Start, the prize name will quickly roll on the
stage. Stop functions to stop the draw. After you click Stop, the name of the
chosen prize will appear on the stage. The chosen prize will not appear in the
next draw.
The draw system falls functionally into three tasks, see Figure 3.14.
3. Make sure the chosen prize will not appear in the next draw
Figure 3.14
59
3. Script Plan
Stop draw
60
Processing Workshop
Step 1: Add a sprite. Click Upload Sprite in the Sprite List, find the files
“Start” and “Stop”, upload the files to the Sprite List, and delete the
default sprite "Sprite 1", see Figure 3.15.
Figure 3.15
Figure 3.16
61
Create a Variable
and List
Click Make a Variable in the Variables module, and fill in the variable
name "Prize", see Figure 3.17.
Figure 3.17
Create a Variable
and List
Step 2: Create Prize List. Click Make a List in the Variables module, fill
in the variable name "Prize", and edit the elements of the list "Prize
List", see Figure 3.18.
Figure 3.18
62
Write a Sprite
Script
Figure 3.19
When the sprite Start is clicked, we can start the draw. Delete the
chosen prize, and allow the prizes to appear randomly, see Figure 3.20.
Write a Sprite
Script
Figure 3.21
63
Let's read it
To allow the draw system to show the prizes randomly, we use the "pick
random (0) to (10)" block. This block randomly generates a number
every time. Of the numbers generated, the minimum value is 0, and the
maximum value is 10. These two numbers form the range of randomly
picked values. Table 3.2 gives the use case of the random number
block.
Table 3.2
{0, 1}
Note: "pick random (0) to (1)" differs from "pick random (0) to (1.0)". Although the
maximum value 1 is equal to 1.0, 1 is the integer type of data, 1.0 the
floating-point type of data. Thus, the random numbers you obtain from both are
accordingly the integer type of data and the floating-point type of data.
64
Innovation Park
The draw system in the preceding case offers seven types of prizes.
Generally, however, we will arrange multiple draw links in the evening
party, with different quantities and names of prizes. Four types of prizes
may be available to the draw link at the opening of the evening party. Two
types of prizes may be available to the draw link in the middle of the
evening party. One type of prize may be available to the draw link at the
end of the evening party. Then, how does the program learn about the
quantities and names of prizes at each link?
We can add a link to ask the user for the prize name. We take a special
value (e.g., 0) as the end of the list, and include the prize name answered
each time in the list, see Figure 3.22.
Figure 3.22
Self-assessment Room
Content Result
65
3.3. Data Operation (1)
It’s summer vacation. We are having a holiday activity. The theme for
Why are there so the first week is math. Kelly, come answer
many lovely kids in questions, and let's see who will finish them
the supermarket? faster.
John wants you to think about a question. If we want to use a computer to help
us with math problems, what shall we do?
Figure 3.23
We can use "say (Hello!)" block to show the results of the four operations, see
Figure 3.24.
66
Figure 3.24
Let's do it
idea
Figure 3.25
67
Through a remainder operation we can get a remainder after the division
between two numbers. The remainder of an exact division expression is 0. For
example, the reminder of 11 divided by 2 is 1, and the remainder of 10 divided
by 2 is 0, see Figure 3.26.
Figure 3.26
Let's do it
Program the remainder calculation according to Figure 3.26.
The host formally starts to raise the second round of questions,
asking students to calculate the remainders of the following
formulas. Let's look at who answers these questions most quickly.
Calculate the remainder of 568 divided by 15, that of 236 divided by
6, that of 333 divided by 12, and that of 789327 divided by 23
idea
In math, we often use the inequality operation signs like greater than sign (>),
less than sign (<) and equality sign (=) to compare numbers. In DobotBlock,
we often use comparison operators like greater than sign (>), less than sign (<)
and equality sign (=) to compare the relations between numbers, between
68
variables, between expressions or between characters. Comparison operators
are in the Operators module, see Figure 3.27.
Figure 3.27
Let's read it
The block of comparison operators is hexagonal. The hexagonal block in
DebotBlock is also called Boolean expression. The result returned by
Boolean expression only contains two items: true and false. The returned
result is called Boolean result.
We can use the "say (Hello!)" block to show the comparison result of 3 and 9
by a comparison operator, see Figure 3.28.
Figure 3.28
Let's do it
Program the comparison operation according to Figure 3.28.
69
3.3.3. Logical Operators
idea
For the three logical operators and their meanings, see Figure 3.3.
Table 3.3
Operator Meaning
As we know, the expressions "3 < 5" and "5 > 3" are true, whereas "5 < 3" and
"3 > 5" are false. The two ones are connected with logical operators. For the
program and its running result, see Figure 3.29.
70
Figure 3.29
Let's do it
Only when your answers to two inequalities are both true can you qualify for
the next round of contest. Based on the program in Figure 3.29, decide
which logical operator you should choose to achieve the above purpose.
In math, we often compare numbers with greater than or equality sign (≥), less
than or equality sign (≤). In DobotBlock, however, there are not these two signs.
In this case, how do we implement the functions of such two signs?
For this, we can use logical operators. For example, to express the range of x
≥ 7, we can use "or" or "not", see Figure 3.30.
71
Figure 3.30
Let's do it
Try using logical operators to denote x ≤ 7 and 5 ≤ x ≤ 7.
Let's read it
To perform more mathematic operations, DobotBlock offers rounding
off, random numbers, absolute values and among other mathematical
functions. For example, from the "absolute value ()" block in the
Operators module, we can draw down a menu, and find mathematical
functions like "sqrt", "trigonometric functions", "log" and "exponent",
see Figure 3.31.
Figure 3.31
72
Research Laboratory
1. Task Release
Make a Simple Calculator: We can design a simple calculator with DobotBlock
to perform the basic operations of addition, subtraction, multiplication and
division.
2. Task Analysis
The simple calculator can perform three tasks, see Figure 3.32.
Figure 3.32
3. Script Plan
73
Sprite Task Description Block
2. Operate
3. Say the
operation result
Processing Workshop
Add a
Sprite
In the Sprite List, add the sprite "Kelly (1)", and delete the default sprite
"Sprite 1", see Figure 3.33.
Figure 3.33
74
Create a
Variable
In the Variables module, click Make a Variable and we can create four
variables, and name them The first number, The second number,
Arithmetic operator and Operation result, see Figure 3.34.
Figure 3.34
75
Write a Sprite
Script
Step 1: Ask and wait for entering the number and mathematical
operators.
Find the "ask (What is your name?) and wait" block in the Sensing
module, and enter the inquiry content "What is the first number?" in
such module, see Figure 3.35.
Figure 3.35
Set the value of the variable "The first number" to answer, see Figure
3.36.
Figure 3.36
Similarly, ask the arithmetic operator and the second number, see
Figure 3.37.
Figure 3.37
Step 2: Operate.
Decide operators, and then perform related operations. For example,
decide whether the variable "Arithmetic operator" is "+". If yes,
perform the addition operation on the first number and the second
number, see Figure 3.38.
Figure 3.38
76
Write a Sprite
Script
Figure 3.39
Step 3: Say the operation result. Find the "say (Hello!)" block in the
Looks module, and allow the sprite to say the operation result, see
Figure 3.40.
Figure 3.40
Step 4: Integrate the programs. Repeat the operation and say its result.
For the program of the simple calculator, see Figure 3.41.
77
Write a Sprite
Script
Figure 3.41
78
Running Result
of the Program
We can click the green flag button to run the program, enter the formula
for calculation, and check the calculator for accuracy. For example,
when we enter the formula 5*6, the operation result is shown in Figure
3.42.
Figure 3.42
Self-Assessment Room
Content Result
79
3.4. Data Operation (2)
It sounds fun.
Can’t wait to play
it!
String Processing
A string is a limited sequence consisting of zero or multiple characters. We
look on an English word as a sequence of multiple letters. To shuffle the letters
of an English word, we can operate a string. In DobotBlock, characters in a
string are stored in order. We can process characters by using the
80
Figure 3.43
Let's do it
Process the string "Apple" with a string processing block, and ask
Kelly to say the third character and end character of the string.
Research Laboratory
1. Task Release
Create an Interesting English game: Randomly choose a word from the
wordlist, shuffle the letters of the word, and guess the correct word according
to the output unrecognizable "word". For the game rule, see Figure 3.44.
Figure 3.44
2. Task Analysis
According to the game rule, we divide the game into creating a wordlist,
choosing a word randomly, shuffling the letters, and guessing the word.
81
We shuffle the letters through the following steps:
(1) Randomly choose any letter in a word, and put the letter in the first place of
the unrecognizable word.
3. Script Plan
1. Create a
wordlist.
2. Choose a word
randomly.
3. Shuffle the
letters: Choose
any letter from a
word, put the
letter in a null
character string in
order according
to the random
position, and
delete the used
letter.
82
Sprite Task Description Block
4. Guess the
word: Show the
shuffled word,
ask the answer,
and fill in it. If the
answer is true the
game ends, or
you can continue
to guess the
word.
Processing Workshop
Add a Sprite
In the Sprite List, add the sprite "Kelly (1)", and delete the default sprite
"Sprite 1".
83
Create a
List
Create a list, name it "wordlist", and add words in the list, see Figure
3.45.
Figure 3.45
84
Write a Sprite
Script
Figure 3.46
Step 2: Shuffle the letters. Choose any letter from a word, put the letter
in a null character string in order according to the random position, and
delete the placed letter, see Figure 3.47.
Figure 3.47
Step 3: Guess the word. Show the shuffled word, ask the answer, and
fill in it. If the answer is true the game ends, or you can continue to
guess the word, see Figure 3.48.
Figure 3.48
85
Write a Sprite
Script
Step 4: Integrate the program of the interesting English game, see Figure
3.49.
Figure 3.49
Self-Assessment Room
Content Result
86
Chapter 4 Program
Structure
Learning objectives
87
Section 1 Sequential Structure
and Loop Structure
Mr. Lee draws a flow chart to help John understand the overall expansion
process, see Figure 4.1.
88
Figure 4.1
1. Flow chart
Flow chart is to show the process of a thing in life with specific graphic symbols.
Generally, we denote the start or end of a thing with an ellipse, and the specific
step with a rectangle, such as foundation treatment, building construction,
indoor water and electricity decoration as well as lighting and air conditioning
equipment installation in the expansion. Also, we represent the flowline by a
straight line with an arrow, to show the direction of the work process. See Table
4.1 for the basic symbols and functions of flow chart.
Table 4.1
89
Basic Symbols of Flow Chart
Judge according to a
Judgment
certain condition
Remarks: the above are common symbols in flow chart, and we will also learn some
others in the later study.
We can also use flow chart during program design to represent the sequence
for executing instructions.
2. Sequential structure
Mr. Lee directs John to start from Step 1 and end at the last Step in turn, just
like that in Figure 4.2; such a program structure is namely the sequential
structure. So in program design, sequential structure is namely the sequence
executing instructions from the first one to the last, see Figure 4.2.
Then Mr. Lee and John come to the construction process. Mr. Lee divides the
whole process generally into four steps, and each of the steps can be divided
90
into a lot of miniterms. For example, the building construction can further be
divided into wall masonry, installation of doors, windows and lintels, concreting
for stairs and floorslabs and roofing..., see Figure 4.3.
Figure 4.3
To make it easier to understand, Mr. Lee writes a program with DobotBlock for
John to move bricks by the robotic arm.
91
Let's do it
Figure 4.4
3. Loop structure
It is inefficient and takes much time when moving only one brick at a time. Is
there any way to carry continuously? Mr. Lee suggests John use the loop
structure in program structure, and thus he can repeat a function in the
program.
In DobotBlock, blocks that can realize loop are "repeat", "repeat (10)" and
"repeat until ()", see Figure 4.5.
Figure 4.5
(1) "repeat"
92
Figure 4.6
Clicking "repeat" during programming, we will keep repeating the block inside
until clicking Stop on the Stage, see Figure 4.7 for the flow chart of repeat.
Figure 4.7
Let's do it
Run the program "Graphical Programming and Robots" \ Chapter 4\
Section 1 \ Motion of Robots (2).sb3", make robot repeat the motion
below: move from the initial position to the first point and then to the
second point, and finally return to the initial position.
93
(2) "repeat (10)"
It is an instruction to repeat for (10) times. The number 10 in the ellipse frame
is namely the repeating times 10, and the specific block to be repeated is in the
groove of repeat (10), see Figure 4.8.
Figure 4.8
Let's do it
Run the program "Graphical Programming and Robots" \ Chapter 4 \
Section 1 \ Motion of Robots (3).sb3", make robot repeat the motion
below for 5 times: move from the initial position to the first point and
then to the second point, and finally return to the initial position.
When using "repeat until ()", we will repeat the block inside until the repeat
condition is met, see Figure 4.9.
94
Figure 4.9
When using "repeat until ()", the repeat condition is at the hexagon position of
repeat until… (). Both Boolean data and expressions can be the condition. It is
to be decided according to the actual condition.
For example, when we need to carry 5 bricks, set the initial value of the
number of bricks moved to 0; the number will increase by 1 each time after we
carry 1 brick, until the number is 5, then exit the repeat. See Figure 4.10 for the
moving process and flow chart.
95
Figure 4.10
Let's do it
Run the program "Graphical Programming and Robots" \ Chapter 4 \
Section 1 \ Moving 5 bricks.sb3", make robot repeat to carry 5 bricks.
Research Office
1. Task release
Carry a list of blocks: carry a list of blocks (4 blocks) in Area A to Area B and
place blocks in Area B into a list. See Figure 4.11 for the schematic diagram of
the robot and block placement.
Figure 4.11
96
2. Task analysis
We carry them from back to front in the task, see Figure 4.12. Similarly, we
place the blocks in Area B from back to front.
Figure 4.12
When carrying blocks by robot, the number of blocks in Area A and Area B will
change, see Table 4.2.
Table 4.2
Carrying Changes
Before carrying
97
After carrying the second
block
See Figure 4.13 for the change to the coordination of blocks when carrying
them from back to front.
Figure 4.13
All blocks are placed neatly in a list, so the coordination of Y is unchanged, but
the coordination of X increases. It is because that both the length and width of
the grid for placing blocks are 20 mm in the block box, so the coordination of X
98
will increase by 20 each time after carrying a block.
Read the flow chart of "carrying a list of blocks" according to the task analysis,
see Figure 4.14.
Figure 4.14
3. Script planning
Task
Device Block
description
1. Initial
settings
99
Task
Device Block
description
2. Value of
initial variable
"i"
3. The robot
carries a list
of blocks in
Area A to
Area B
Processing zone
Step 1. Add the device "Magician Lite" in device list at the lower left of
DobotBlock interface and delete the default device "Magician".
Step 2. Connect robot and computer with a Type-C cable, and click
Connect key in the control area of DobotBlock to connect device.
Create variable
Click "Variable" module and "Set up a variable", fill new variable "i" in the
pop-up interface. We can use variable "i" to record the times for carrying
blocks, see Figure 4.15.
Figure 4.15
100
Write device
script
Step 1. Initial setting. Suck the block with a suction cup when carrying it with
robot, see Figure 4.16.
Figure 4.16
Figure 4.17
Step 3. Set up judgment conditions. When we use robot to carry a list of
blocks (4 blocks), when i=1, the robot will carry the first block; when i=2, it will
carry the second block, when i=3, it will carry the third block, and when i=4, it
will carry the fourth block. So, when i=5, it will stop repeat. When using "repeat
until ()", we need to set the program of setting judgment conditions, see Figure
4.18.
Figure 4.18
101
Write device
script
Step 4. The robot will carry blocks one by one from back to front. It will
move to Area A to suck the block, and then move to Area B to place it. The
value of variable "i" will increase by 1 each time after carrying a block, see
Figure 4.19 for the programming.
Figure 4.19
Lets' think about it: what does 20 mean in the expression 260+20*i for coordination
of X?
Step 5. Integrate program. The robot carries a list of blocks from Area A to
Area B, see Figure 4.20 for the programming.
Figure 4.20
102
Innovation park
Carrying two lists of blocks: carry two lists of blocks (4/list) in Area A
to Area B and also place the blocks in Area B into two lists. See
Figure 4.21 for the schematic diagram of robot and block placement.
Figure 4.21
Self-Assessment Room
Content Result
103
Section 2 Branch
Structure (1)
104
Figure 4.22
Figure 4.23
Figure 4.24
In "if..., then..." block, the judgment condition is at the hexagon position; it can
include an or several instruction (s). If the condition is met, then run all blocks
inside. While if not, then run blocks behind "if..., then...".
105
For example, when judging if it is a floor tile, if the side length is 80 cm, then
the program can judge that it is the floor tile; see Figure 4.25 for some
programs for judgment of floor tiles.
Figure 4.25
Let's do it
Run "Graphical Programming and Robots" \ Chapter 4 \ Section 2\
Judgment of Floor Tiles.sb3", enter the side length of a floor tile to check
if it is floor tile.
There is only a judgment condition in the dual branch structure to come into
two different branches. We can choose any of the branches based on
judgment and execute corresponding instructions. As shown in Figure 4.26, if
the condition is met, then execute instruction A; while if not, then execute
instruction B.
106
Figure 4.26
Figure 4.27
Mr. Lee suggests John to solve his new question in the way in Figure 4.28.
107
Figure 4.28
Research Office
1. Task release
Ticket checking by ticket inspectors (I): the ticket inspector will ask for height
before the student takes the bus. If the height is < or = 1.2 m, then the student
can take the bus for free. If > 1.2 m, the ticket inspector will ask the student to
buy a ticket.
2. Task analysis
Fill out the flow chart "Ticket Checking by Ticket Inspectors (I)" in Figure 4.29
according to task analysis above.
108
Figure 4.29
3. Script planning
1. Ticket inspector
asks for height.
109
Processing zone
Add
sprites
Step 1. Add sprite "City Bus". Click Sprite, the key Add Sprite on the
interface, and then click sprite "City Bus" on the pop-up interface, delete
the default sprite "Sprite 1".
Step 2. Add sprite "Abby" and set its size as 60. Also, adjust the position
of "Abby" on Stage, see Figure 4.30.
Figure 4.30
110
Write sprite
script
Step 1. Ticket inspector asks for height. Program to ask for student's
height in the Coding Area of sprite "Abby", see Figure 4.31.
Figure 4.31
Step 2. Judge if the student needs to buy a ticket based on the answer,
see Figure 4.32.
Figure 4.32
Step 3. Integrate program. See Figure 4.33 for the program for ticket
checking by ticket inspectors (I).
Figure 4.33
Innovation park
111
Modify the program "Ticket checking by ticket inspectors (I)", try to
substitute "if..., then...else..." with "if..., then…" to finish the task "Ticket
checking by ticket inspectors (I)".
Self-Assessment Room
Content Result
112
Section 3 Branch
Structure (2)
Figure 4.34
113
Now John goes to the construction site for acceptance of its shopping center.
According to the construction process, the overall expansion can be thought
ended only when all of the conditions for foundation, building construction,
decoration and device installation are met, see Figure 4.35.
Figure 4.35
Figure 4.36
114
See Figure 4.37 for part of the processes accepted by John on construction
site.
Figure 4.37
idea
Let's do it
Program performing acceptance on construction site by other ways
of block connection.
115
Research Office
1. Task release
Ticket checking by ticket inspectors (2): the ticket inspector will ask for height
before the student takes the bus. If the height is < or = 1.2m, then the student
can take the bus for free. If > 1.2m and < or = 1.5m, then ticket inspector will
ask the student to buy a half-price ticket. If > 1.5m, then ticket inspector will ask
the student to buy a full-price ticket.
2. Task analysis
The video about ticket checking by ticket inspectors shows asking for height,
entering the height, judging the height and deciding to buy ticket or not. Fill out
the flow chart of "Ticket checking by ticket inspectors (2)", see Figure 4.38.
Figure 4.38
116
3. Script planning
1. Ticket inspector
asks for height.
Processing zone
Add sprites
Write sprite
script
Step 1. Ticket inspector asks for height. Program to ask for student's
height in the Coding Area of sprite "Abby", see Figure 4.39.
Figure 4.39
117
Write sprite
script
Step 2. Judge if the student needs to buy a ticket based on the answer,
see Figure 4.40.
Figure 4.40
Step 3. Integrate program. See Figure 4.41 for the program for ticket
checking by ticket inspectors (2).
Figure 4.41
Innovation Park
118
Modify the program "Ticket checking by ticket inspectors (2)", try to use
"if..., then…" and "if..., then...else..." to finish the task "Ticket checking by
ticket inspectors (2)".
Self-Assessment Room
Content Result
119
Chapter 5 Message
and Sensing
Mr. Lee told John that in our daily life, humans can communicate with each
other or with animals by language, body actions and even facial expression.
We can regard the specific contents communicated by above ways as a
message, and after receiving the message, the other side will give
corresponding response.
In this chapter, let's learn about message instruction and sensing instruction.
Learning objectives
120
Section 1 Message
Instruction
1. Message
Message is an independent communication content delivered from the sender
to a or more object (s). And the process delivering messages is also called
message broadcasting. After broadcasting the message, the receiver will give
different responses.
121
2. Broadcasting and receiving of message
(1) Broadcasting of message
Figure 5.1
Figure 5.2
122
"Broadcast message 1" is very similar to "Broadcast message 1 and wait", but
the former will run program next immediately after broadcasting. The latter will,
after the broadcasting, run program next until running of all scripts receiving
the message.
For instance in the magic show elephant changing color, the program running
sequence is different when different message broadcasting blocks are used,
see Figure 5.3.
Figure 5.3
Figure 5.4
123
Figure 5.5
Taking the magic show elephant changing color as the example again, after
broadcasting "Change color", the sprite elephant will receive the message and
then change its color for 10 times, see Figure 5.6.
Figure 5.6
Let's do it
Run the program "Magic Show", observe the change to sprite on the
stage.
After running the program, substitute the "Broadcast message 1"
with "Broadcast message 1 and wait" and then run the program
again.
Research Office
1. Task release
Circus show: monkey makes opening introduction and then drums. After the
show, monkey introduces the next show, namely dinosaur's dance.
124
2. Task analysis
There are three sprites in the circus show: monkey, drums conga and dinosaur.
See Figure 5.7 for the action analysis of each of them, and see Figure 5.8 for
the process of the circus show.
Figure 5.7
Figure 5.8
125
3. Script planning
1. Monkey makes
opening introduction.
2. Broadcast "Begin to
drum" and then begin to
drum.
1. Receive message
"Begin to drum" and then
change the costumes and
start sound.
2. Receive message
"Begin to dance" and then
hide.
2. Receive message
"Begin to dance" and then
show.
126
Processing zone
Step 1. Add stage backdrop, see Figure 5.9 for the steps.
Figure 5.9
Figure 5.10
127
Add backdrop and
sprite
Figure 5.12
128
Write sprite
script
Figure 5.14
Introduce the next show, and then hide, see Figure 5.15.
Figure 5.15
129
Write sprite
script
Integrate code. After the opening introduction by monkey, the program
broadcasts, and monkey begins to drum. After the performance, monkey
introduces the next show and then hide, see Figure 5.16.
Figure 5.16
Step 2. Write script of Drums Conga.
Receive message "Begin to drum" and then change the costumes and
start sound, see Figure 5.17.
Figure 5.17
130
Write sprite
script
After receiving the message "Begin to dance", the "Drums Conga" then
hides, see Figure 5.18.
Figure 5.18
Step 3. Write script of dinosaur.
Click Sprite and sprite "Dinosaur5", and then begin to write script of
dinosaur.
Dinosaur hides before receiving message "Begin to dance". After
receiving the message "Begin to dance", Dinosaur then shows and
begins to dance, see Figure 5.19.
Figure 5.19
Innovation park
Modify the video of "circus show": there are two shows in the original
program. Now we need to add another one "poetry reading"; choose sprite
"Starfish" to read poems on its own decision.
131
Self-Assessment Room
Content Result
132
Section 2 Sensing
Instruction
-- Maze Escape
The dog is so
awesome that no
matter where you put Indeed, the dog senses the
him in the maze, it can environment by "touching” and
always get out! then gives response; it is
important for sprite interaction in
program design!
1. Setting Dog!
After start of the performance, we need to set Dog at a corner of the maze.
When setting the sprite, we need to move mouse to the position of the sprite,
mouse down the left key and drag the sprite to the designated place, and then
release the left key of mouse.
We can find "Mouse down?” from Sensing module in software block area, see
Figure 5.20.
Figure 5.20
Choose the position in maze, mouse down, and then the Dog will be set at the
position mouse-down; see Figure 5.21 for some programs.
133
Figure 5.21
Figure 5.22
134
Figure 5.23
We can find "Touching color ()?" block from Sensing module in software block
area. Click the elliptical position on right of the block, and then we can choose
the color, saturation and brightness, see Figure 5.24.
Figure 5.24
If Dog attempts to get through black wall, he will wait for 1s and then move
back for 2 steps, see Figure 5.25.
Figure 5.25
135
4. Arriving Victory Gate
When controlling Dog to touch the exit, the Dog arrives at the Victory Gate. We
can find "Touching (mouse-pointer)?" from the sensing block in software block
area, click the white inverted triangle symbol, then we can see two basic
options "mouse-pointer" and "edge". When adding new sprite on stage, the
block will add the option of new sprite automatically, see Figure 5.26.
Figure 5.26
In the example above, some program for arriving at Victory Gate is shown in
Figure 5.27.
Figure 5.27
Let's do it
Run the program "Dog Maze.sb3".
136
Research Office
1. Task release
Shark eating fish: there are different kinds of fishes in the vast sea, and when a
shark meets a fish, the shark will eat the fish. Count the number of fish eaten
by shark within given time.
2. Task analysis
We can divide the game into three parts: number of fish eaten, game time and
sprite control. See Figure 5.28 for task analysis of shark eating fish.
2. Shark
meets with
fish
(2) Number of fish eaten
increases
2. Fish
(2) Fish moves in stage, and will
hide when meeting a shark.
Figure 5.28
137
Figure 5.29
138
3. Script planning
1. Count number
of fish eaten.
3. Control the
motion direction
of shark with
Direction key.
1. Show at
random position
of stage every 2s.
139
Sprite Task description Block
2. Fish moves
around stage.
Fish hides when
meeting a shark.
140
Processing zone
Figure 5.30
141
Create
variable
Click "Variable" module and "Set up a variable", enter variable name
"Number of fish eaten" and click OK. Create variable "Time" with the
same method, see Figure 5.31 for the two variables.
Figure 5.31
Figure 5.32
142
Write sprite
script
Set the time. Create variable "Time" to count the game time. It is only
60s for each game. When the game time is used out, Shark will say the
total number of fish eaten, and then stop all scripts, see Figure 5.33.
Figure 5.33
Control the motion direction of shark. Shark can move up and down and
left and right. We can control the motion direction of shark with Direction
key, see Figure 5.34.
Figure 5.34
143
Write sprite script
Step 2. Click sprite "Fish", and then begin to write script of fish.
To make fish show on the stage all the time, we set it to show every 2s,
see Figure 5.35.
Figure 5.35
Fish moves on stage and hides when meeting with a shark, see Figure
5.36.
Figure 5.36
144
Innovation Park
Modify the game shark eating fish: add an effect that shark will get large
each time after eating a fish; the more shark eats, the larger it will be.
Boys and girls, lets' challenge to see who has the largest shark.
Self-Assessment Room
Content Result
145
Chapter 6
Comprehensive Project
In the field of medicine circulation, the auto medicine vending system is quick,
convenient and intelligent, allowing people to buy the medicines they need at
any time of the day. So in this chapter, let's comprehensively apply what we’ve
learned about programming to build an auto medicine vending system.
Learning objectives
146
Auto Medicine Vending
System
147
Let's discuss it
Boys and girls, have you ever seen an auto vending system?
Figure 6.1 shows an auto vending system in our life. Have a
look about it, and let’s think what menu interfaces will you need
to design on the auto vending system? What interactive
functions will you need to realize through the program? How to
control the robot to take the corresponding medicines
automatically?
Figure 6.1
Research Office
1. Task release
Design and complete an auto medicine vending system with software
interaction and robot control.
Requirements: For the software interaction part, users can click a button to
enter the pharmacy, choose medicines, and delete those chose by mistake.
The program will show the Total Price and Medicine List on Purchase Interface
during purchasing, and then come to the End Interface after purchase. There
are Start Interface, Purchase Interface and End Interface on the software
interaction interface, see Figure 6.2.
148
Figure 6.2
As for the robot control part, the robot takes medicine according to the
"Medicine List", and then puts them in the Laydown Area. See Figure 6.3 for
position of device.
Figure 6.3
2. Task analysis
There are six steps to build an auto medicine vending system, and see Figure
6.4 for the overall task flow.
Figure 6.4
149
Figure 6.5
Step 2. Program "Start Interface". Analyze tasks of each sprite in the "Start
Interface" and program for each one. See Figure 6.6 for the work flow chart of
"Start Interface".
Figure 6.6
Let's do it
Refer to the work flow chart of "Start Interface", and analyze and
draw the work flow chart of "Purchase Interface", "End Interface"
and "Auto Medicine Taking".
150
3. Script planning
Sprite and
Task description Block
device
Enter the
pharmacy
1. Initialize Start
Interface.
2. Switch to
Purchase
Interface and
hide, show
variables and a
list, and then
broadcast "Enter
the pharmacy”.
Choose
1. Hide Start
medicine
Interface.
2. When I receive
"Enter the
pharmacy", show
at the designated
place.
151
Sprite and
Task description Block
device
3. When clicked,
broadcast
"Choose
medicine".
4. When I receive
"Pay the bill",
hide.
Delete
1. Hide Start
medicine Interface
2. Show at the
designated place
after receiving the
message "Enter
the pharmacy"
3. When clicked,
delete the last
item of Medicine
List.
152
Sprite and
Task description Block
device
4. When I receive
"Pay the bill",
hide.
Cold Medicine
1. Hide Start
Interface
Headache
Medicine
2. Show at the
designated place
after receiving the
Anti-fever
message "Enter
Medicine
the pharmacy"
Anti-Inflammatory
Medicine 3. When I receive
"Choose
medicine", click
the sprite
medicine, the
sprite medicine
will change color
effect. Change
the Total Price
according to the
medicine price
and add the
medicine to
Medicine List.
4. When I receive
"Pay the bill",
hide, and stop
calculating the
price.
153
Sprite and
Task description Block
device
2. Show at the
designated place
after receiving the
message "Enter
the pharmacy"
3. When clicked,
broadcast "Pay
the bill", switch
backdrop to End
Interface, and
hide sprites,
variables and list.
1. Initialization
2. Identify the
name of the first
item in "Medicine
List", take the
corresponding
medicine and put
it in Laydown
Area.
154
Processing zone
Design interface
backdrop
Figure 6.7
Add sprite
Figure 6.8
155
Write sprite script
Figure 6.9
Figure 6.10
156
Write sprite script
Figure 6.11
157
Write sprite script
We can program for other medicine sprites with the same ideas.
However, it is important to note that different medicines have different
prices. After choosing a medicine, the variable "Total Price” will
increase by the amount of the medicine, and also the program will add
an element the same as the medicine name to "Medicine List". So let's
program for other medicines referring to the "Cold Medicine" program.
Step 3. Program for all sprites on "End Interface".
Switch the stage backdrop to the backdrop of "End Interface", hide all
sprites, stop other scripts of sprite medicine, and hide variables and list.
The program for sprite "Cold Medicine" on "Purchase Interface" is
shown in Figure 6.12. Now boys and girls, please program for rest
sprites on "End Interface" according to task analysis and script
planning.
Figure 6.12
Step 4. Program for Auto Medicine Taking.
The key in the program of "Auto Medicine Taking" is to identify each
item in the "Medicine List", and then take the medicine according to the
"Medicine List", see Figure 6.13 for the program. Boys and girls, lets'
debug and improve the program.
158
Write sprite script
Figure 6.13
159
Capacity
promotion
Optimization
No.
Scheme
160
Self-Assessment
Content Result
161