PDF

blockstblockst0.4.0

Render Scratch, Blockly, MakeCode and Open Roberta blocks for educational documents.

Running Scratch programs#

blockst includes an execution engine that runs Scratch programs visually — for demonstrating program flow and pen drawing. It runs Scratch text only.

scratch-run Module#

#import "@preview/blockst:0.4.0": scratch-run, set-scratch-run

#scratch-run.stage("...", scale: 2)
#scratch-run.grid("...", grid: true)

scratch-run.stage() — Stage Canvas#

Renders pen drawing on a stage canvas, like the Scratch stage.

#scratch-run.stage(
  program,
  language: "en",      // locale for block text
  size: auto,          // (width, height) in pixels (default 480×360)
  scale: auto,         // drawing scale multiplier
  start: auto,         // (x: 0, y: 0, angle: 90)
  pen: auto,           // (down: false, color: ..., size: ...)
  background: auto,    // background color (e.g. white, black)
  cursor: auto,        // show/hide turtle cursor (default: true)
  border: auto,        // show/hide stage border (default: true)
)

scratch-run.grid() — Coordinate Grid#

Renders the drawing on a Cartesian coordinate grid with optional axes and grid lines.

#scratch-run.grid(
  program,
  language: "en",      // locale for block text
  x: auto,             // view bounds (tuple, e.g. (-10, 10))
  y: auto,             // view bounds (tuple)
  step: auto,          // grid step size
  scale: auto,         // drawing scale multiplier
  start: auto,         // (x: 0, y: 0, angle: 90)
  pen: auto,           // (down: false, color: ..., size: ...)
  background: auto,    // background color
  axes: auto,          // show axis lines (default: false)
  grid: auto,          // show grid lines (default: false)
  grid-style: auto,    // grid line stroke (default: 0.5pt + gray)
  cursor: auto,        // show/hide turtle cursor (default: true)
  fit: auto,           // auto-fit view to drawing bounds
)

set-scratch-run() — Run Global Defaults#

#set-scratch-run(
  scale: none,         // default scale for all runs
  start: none,         // (x: 0, y: 0, angle: 90)
  pen: none,           // (down: false, color: ..., size: ...)
  background: none,    // default background color
  cursor: none,        // show/hide turtle cursor (default: true)
  stage: none,         // (size: (480, 360), border: true)
  grid: none,          // (visible: false, axes: false, step: auto, style: auto)
)

Supported Execution Commands#

CategoryCommands
Motionmove, turn-right, turn-left, set-direction, go-to, set-x, set-y, change-x, change-y
Penpen-down, pen-up, set-pen-color, set-pen-size, change-pen-size, erase-all, stamp, set-pen-param, change-pen-param
Variablesset-variable, change-variable, variable
Operatorsplus, minus, multiply, divide, modulo, random, round, greater, less, equals, op-and, op-or, op-not
Controlrepeat, repeat-until, if-then, if-else, wait
Lookssay, think
Shapessquare, triangle, circle, star, spiral
Germangehe, drehe-rechts, drehe-links, setze-richtung, gehe-zu, stift-ein, stift-aus, setze-stiftfarbe-auf, setze-stiftdicke, setze-variable, aendere-variable, groesser, kleiner, gleich, und, oder, nicht, mal, geteilt, zufallszahl

Complete Example#

#let square-program = "
go to x: (-45) y: (45)
pen down
set pen [color v] to (0)
set pen size to (45)
repeat (4)
  move (90) steps
  turn cw (90) degrees
  change pen [color v] by (25)
end"

#set-scratch-run(
  stage: (size: (300, 240)),
  start: (x: 0, y: 0, angle: 90),
)

#grid(
  columns: (auto, auto),
  gutter: 6mm,
  [#scratch(square-program)],
  [#scratch-run.stage(square-program, scale: 2)],
)