9. Next, make the Cube sprite
change costumes, so the color
changes each time it comes
down the conveyor belt:
Go to the Looks menu
(purple) and drag a “switch
costume to [costume
name]” block above the
“go to” block
(Figure
Q
)
.
Go to the Operators menu
(green). You will see
blocks that look like math
operations. Drag the “pick
random [1] to [10]” block
over the name area on the
switch costume” block.
Count the number of
costumes you have,
and type that number
over the 10.
10. From the Events (tan) menu, drag a “When [flag] clicked” block on the
top of the stack. Click on the stack to make sure everything is working!
11. Now create a new stack of code telling the cube to change course when
it receives a message from the robot arm saying that a green cube has
been detected. The cube needs to fly through the air in the direction of
the Green Cubes sign.To begin:
Go to the Events menu (tan) and look for “when I receive
[message1].” Drag it into an open area in the workspace
(Figure
R
)
.
Click on “message1” and create a new message. Give it a name like
CubeSort.
On the Stage, move the cube to the right edge near the Green Cubes
sign
(Figure
S
)
.
Q
R
96
Making Simple Robots, 2nd Edition
Make_Simple_Robots_interior_FIN.indd 96Make_Simple_Robots_interior_FIN.indd 96 4/26/22 1:55 PM4/26/22 1:55 PM
Go to the Motion menu and drag a “glide [1] sec to x: [] y:[]” block.
The numbers should be the current position of the Cube sprite, at
the right edge of the stage near the Green Cubes sign.
12. Before moving on, test out the program:
Click the green flag to run the program.
To save time, instead of waiting for a green cube
to appear at random, open the Cube sprite’s Paint
Editor and click on a green costume.
When the green cube touches the robot arm, it should
glide up to the point near the Green Cubes sign.
When it reaches the edge of the stage, it should instantly
go back to the original starting position and start again.
S
Chapter 3: Robots That Think 97
Make_Simple_Robots_interior_FIN.indd 97Make_Simple_Robots_interior_FIN.indd 97 4/26/22 1:55 PM4/26/22 1:55 PM
13. To finish off the program, add a
few costumes to the Arm sprite
to make it look like it is tossing
the cube in the direction of the
Green Cubes sign (even though
the cube is really moving by
itself). To animate the Arm:
Right click on the first
costume and duplicate it.
On the second costume,
check that any separate
parts of the arm are grouped
together
(Figure
U
).
Click on the arm to make the box
with the curved handle appear.
Grab the handle and swing it up
a little to rotate the arm. You only
need to move it a little from the first position.
Repeat two or three more times, ending with the
arm in position to let the cube fly
(Figure
V
)
.
U
V
98
Making Simple Robots, 2nd Edition
Make_Simple_Robots_interior_FIN.indd 98Make_Simple_Robots_interior_FIN.indd 98 4/26/22 1:55 PM4/26/22 1:55 PM
Now let’s code the Arm sprite. Its algorithm looks like this:
Test each cube as it goes by to see if it is green.
If the cube is green:
Broadcast a message to the Cube sprite.
Wait for the cube to start flying away from the conveyor belt.
Switch costumes to show the arm moving with the cube.
Move back to the starting position.
14. To make the Arm sprite
broadcast a signal to the Cube
sprite:
Go to the Control
menu (orange). Drag a
“forever” block onto the
workspace
(Figure
W
)
.
Drag an “if-then” block
inside the “forever”
block. (You don’t need
the “else” for this one.)
Go to the Sensing menu
(light blue) and drag a
pointy-ended “touching
[color]?” block into the
space on the “if-then” block.
Change the color to green.
From the Events menu, drag a “broadcast []” block inside
the “if-then” block. to “CubeSort” as the message.
15. To animate the robot arm:
Go to the Control menu and drag a “repeat [10]” block into the
workspace. Don’t connect it to other blocks yet
(Figure
X
)
.
Go to “Looks” and find the “next costume” block. Drag it inside
the “repeat” block. Set the number of repeats to the number of
costumes for your Arm sprite.
W
X
Chapter 3: Robots That Think 99
Make_Simple_Robots_interior_FIN.indd 99Make_Simple_Robots_interior_FIN.indd 99 4/26/22 1:55 PM4/26/22 1:55 PM
Click on the repeat block
to watch the arm move.
It should end up at the
starting position. If not,
adjust the number of
repeats. If you want to
make it change positions a
little slower, go to Control
and find a “wait [1] second”
block. To set the wait to half
a second, type 0.5 over the
number 1. To make it go a
little faster, set it to 0.2.
Drag the “repeat”stack of
blocks inside the if-then
block, below the broadcast
block
(Figure
Y
)
.
Top the stack with a “when [green flag]
clicked” block from the Events menu.
16. You’re done! Test out the whole program by
clicking on the green flag
(Figure
Z
)
.
Y
Z
100
Making Simple Robots, 2nd Edition
Make_Simple_Robots_interior_FIN.indd 100Make_Simple_Robots_interior_FIN.indd 100 4/26/22 1:55 PM4/26/22 1:55 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.130.13