My Blocks
My Blocks are blocks you define yourself (other languages call them "functions"): package a repeated block sequence into one reusable, parameterized block. The first thing that separates mature teams from beginners.
Why
A competition program without My Blocks: every mission copies the same "init + straight + turn" stacks; changing one speed means editing ten places. With My Blocks: init, straight [50] cm, turn right [90] combine like LEGO; logic changes happen once, at the definition.
Creating one
- Find My Blocks at the bottom of the palette, click "Make a Block".
- Name it, e.g.
straight. - Click "Add an input" for parameters: number/text input, boolean, or label text. Add a number input
cm. - Confirm - a pink define hat block appears; hang the implementation under it.
The three My Blocks every team needs
1. init
define init
set movement motors [A+E]
set movement speed [50] %
set 1 motor rotation to [17.5] cm distance moved
reset yaw angle [0]2. straight (with a distance parameter)
define straight (cm)
move [forward] for (cm) cmStart with this simple version; after gyro driving, upgrade the implementation to gyro-corrected driving - no call site changes. That's the point of encapsulation.
3. gyro turn (with an angle parameter)
define turn (angle)
reset yaw angle [0]
if <(angle) > [0]> then
start moving [steering 100]
wait until <yaw angle > ((angle) - [2])>
else
start moving [steering -100]
wait until <yaw angle < ((angle) + [2])>
stop movingPositive = right turn, negative = left. One block for every turn in every program.
In use
Mission programs become readable at a glance:
when program starts
init
straight [40]
turn [90]
straight [25]
run motor [C] [clockwise] for [180] degrees ← attachment action
turn [-45]
straight [-30] ← negative = backward, if your implementation supports itDesign principles
- One My Block does one thing: straight drives straight; it does not also raise the arm.
- Parameterize what varies: distance, angle, speed become inputs; constants (port letters) stay inside the definition.
- One shared library per team: everyone's missions call the same
straight, so behavior is consistent.
My Blocks have no return value
A Word Blocks custom block cannot compute a value and hand it back. For that, use variables as a workaround - or move to Python for real functions.
Exercises
- Extract the init section of all your previous practice programs into an
initMy Block. - Build
square (side_cm): internally callstraightandturnfour times each - My Blocks can call other My Blocks.
Next lesson: Line Following.