Robot Can Draw
In this lesson you will learn how to teach a robot to draw.
The PENDOWN command tells the robot to lower the pencil. When moving forward or backward the robot will start drawing.
The command PENUP tells the robot to raise the pencil.
The command COLOR "<code>" changes the color of the pencil.
You can find the color codes here.
Example No. 1. Robot Draws a Square
PENDOWN
COLOR "#F39C12"
REPEAT 4 [
FORWARD 130
RIGHT
]
Example No. 2. Robot Draws a Triangle and a Circle
HERE TRIANGLE :size [
PENDOWN
REPEAT 3 [
FORWARD :size
LEFT 120
]
PENUP
]
HERE CIRCLE :radius [
PENDOWN
:x = 2 * 3.14 * :radius / 360
REPEAT 360 [
FORWARD :x
LEFT 1
]
PENUP
]
TRIANGLE 120
FORWARD 60
CIRCLE 34
Example no. 3. Robot Is an Artist
HERE ORNAMENT :step :angle :size [
PENDOWN
COLOR choose @["GREEN", "RED", "BLUE", "ORANGE"]
IF :step > 100 [STOP]
FORWARD :step * :size
RIGHT :angle
ORNAMENT :step + 2 :angle :size
]
ORNAMENT 1 93 3
Click “Solve” and try these commands!
Lesson 1. Robot Moves Back and Forth
The first command you will learn is FORWARD. It tells the robot to move forward.
You can also specify how many cells the robot should move.
For example, type FORWARD 3 and your robot will move forward three cells.
Sometimes you may have to go back. The BACKWARD command is for moving backwards.
For example, type BACKWARD 2 and your robot will go back two cells.
Example No. 1
FORWARD 3
Example No. 2
BACKWARD 2
Click “Solve” and try these commands!
Lesson 2. Robot Knows How to Turn
Now you will learn to turn the robot left and right. LEFT and RIGHT commands are used for this.
You can also specify the angle at which the robot should turn. For example, LEFT 180. Allowed angles: 90, 180, 270.
Example No. 1
FORWARD 3
RIGHT
FORWARD
Example No. 2
FORWARD
RIGHT
FORWARD
LEFT 180
Time to try the new commands!
Lesson 3. Robot Is Powered by Energy
Every action of the robot consumes energy. When the robot runs out of energy, it stops.
Note. The energy level of the robot depends on the game level.
Example No. 1. Robot Stopped
FORWARD 3
RIGHT
FORWARD
RIGHT
FORWARD
0
Example No. 2. Well Done
FORWARD
RIGHT
FORWARD
3
Lesson 4. Robot Repeats Commands
Familiarize yourself with the command REPEAT X [<command sequence>]. X is a number telling the robot how many times to repeat the sequence of commands.
Command sequence is enclosed in square brackets.
Example no. 1. Robot Moves in Circle
REPEAT 4 [
FORWARD 2
LEFT
]
Example no. 2. Robot Drives Away and Returns
REPEAT 2 [
FORWARD 4
RIGHT 180
]
Lesson 5. Create New Commands
Instead of long sequences of commands, new commands can be created to make programming easier.
To create a new command, use the following syntax: HERE <new_command_name> [<command sequence>].
Also, a new command can be enhanced by adding parameters - freely variable values.
The parameters allow you to adapt to different situations.
For example, the FORWARD command parameter specifies how many times the robot should move forward.
To create a new command with a parameter, use the following syntax: HERE <new_command_name> :<parameter1_name> :<parameter2_name> etc. [<command sequence>].
Example No. 1. New Command CLIMB
HERE CLIMB [
FORWARD
RIGHT
FORWARD
LEFT
]
REPEAT 4 [ CLIMB ]
Example No. 2. New Command CLIMB With Parameter
HERE CLIMB :count [
REPEAT :count [
FORWARD
RIGHT
FORWARD
LEFT
]
]
CLIMB 4
Lesson 6. Robot Memory and Sensors
Memory
The robot has a memory that is like a chest of drawers. Drawers can store information. To save specific data, you first need to name the drawer and then put the information in it. For example:
:year = 2023
:color = "yellow"
The name of the drawer is the address of the information. It is written as :<name>
In the third lesson, you learned that a robot is powered by energy. Anytime checking the address :ENERGY
you will see how much energy the robot has left.
Sensors
Also, the robot has special sensors, similar to eyes, located on the front, back, left and right.
Using sensors, the robot is able to recognize objects.
Sensor data is stored in the following addresses: :FRONT, :BACK, :LEFT and :RIGHT.
The codes of the objects recognized by the robot are stored in the following addresses:
:EMPTY = 0
:WALL = 1
:STAR = 2
:CHARGER = 3
Example No. 1. Star on the Right
The object :STAR will be written in the robot’s memory with the address :RIGHT
:RIGHT = :STAR
Example No. 2. Charger in the Front
The object :CHARGER will be written in the robot’s memory with the address :FRONT
:FRONT = :CHARGER
Note. The charger increases the energy level of the robot by 10 units.
Lesson 7. Robot Makes Decisions
Until now, you decided what the robot should do. However, the robot can also make decisions on its own.
If the robot needs to choose between two actions use IF and ELSE commands.
If the robot needs to choose between more than two actions use the WHEN command.
Also, the robot can repeat commands only under certain conditions. When you want the robot to repeat a command under specific conditions use WHILE.
Example No. 1. Robot Chooses One of Two Actions
For example, this code teaches robot to go left to avoid a wall.
IF :FRONT IS :WALL [
LEFT
FORWARD
]
ELSE [
FORWARD
]
Example No. 2. Robot Chooses From More Than Two Actions
For example, this code teaches robot to avoid obstacles.
REPEAT 3 [
WHEN [
:FRONT IS :EMPTY [ FORWARD ]
:RIGHT IS :EMPTY [ RIGHT FORWARD ]
:LEFT IS :EMPTY [ LEFT FORWARD ]
:BACK IS :EMPTY [ BACKWARD ]
ELSE [ "TOASTED!" ]
]
]
Example no. 3. Robot Repeats Action Under Certain Conditions
This code tells the robot to move forward until it runs out of energy and until there is a wall on the left.
WHILE :ENERGY > 0 [
WHILE :LEFT IS :WALL [
FORWARD
]
LEFT
FORWARD
]
0
Lesson 8. The Maze
The robot found itself in a maze. In this lesson you will teach the robot how to escape from it. It’s not difficult at all if you know the One Hand Rule. It’s like this - put one hand to the wall of the maze and don’t take it away. If you walk along the wall without taking your hand away you will surely find a way out!
The One Hand Rule can be written like this:
WHILE :ENERGY > 0 [
WHEN [
CAN_GO_FORWARD [ FORWARD ]
CAN_GO_RIGHT [ RIGHT FORWARD ]
ELSE [ LEFT ]
]
]
However, you will need to create two new commands CAN_GO_FORWARD and CAN_GO_RIGHT yourself. Good luck!