Archive for August, 2009

Nestable Sprites for Scratch

Sunday, August 30th, 2009

NestingBYOB 2.0 is here for you to download. I’m surprised and a bit scared about the enthusiastic reception of the my little build-your-own-block experiment for Scratch. I never intended BYOB to be actually used in highschool classrooms and university courses in different parts of the world.

It also feels wonderful! I have been getting excellent suggestions from very bright people, and the idea to gradually distill class-like constructs out of Scratch and turn BYOB into a CS intro environment has got me hooked (and slightly overwhelmed, too).

So I decided to share only those experiments I feel confident about, even if the whole of them doesn’t yet go all the way towards that starry goal. In BYOB 2.0 I resisted the urge to add new blocks, focusing instead on improving the experience to let you create your own ones, and on introducing compositional object relationships to Scratch. Still, please expect to encounter many bugs.

What’s new in BYOB 2.0:

custom blocks (functional recursion)

  • now have all Scratch 1.4 features (esp. string functions)
  • can open / import any Scratch project (also older ones)
  • arguments now take  both numerical and text input (and reporters)
  • double click on a custom reporter block to show its result
  • fixed the block editor’s answer field to improve drag & drop
  • new debugging functions (error blocks are displayed red)
  • fixed escaping out of infinite atomic loops (thanks to Brian Harvey for catching this!)
  • the block editor now is resizable (thanks to Dan Garcia for suggesting this!)

nestable sprites (structural recursion)

  • you can now create composite sprites (made out of subsprites)
  • sprites can be nested infinitely, making them “parts” of more complex simulations
  • subsprites follow their owner’s motion, heading, resizing and graphical effects, and serve as their owner’s extended sensors
  • subsprites can be set to follow their owner’s rotation, or to rotate independently

other

  • you can now share sprites (also nested ones) in a mesh network
  • built-in compiler lets you convert any Scratch/BYOB project into an .exe
  • autoscrolling
  • scrolling by dragging
  • undo (!) - Why didn’t I think of this before…

Elements - blocks of code all the way down to the VM

  • you can now inspect (and awkwardly edit) any non-custom standard Scratch block in my experimental Elements for Smalltalk GUI. This is a taste of Zukunftsmusik…

Smalltalk has helped

BYOB’s new nestable sprites feature was not nearly as difficult to implement as I first expected, because Smalltalk has helped. A central problem was to recursively propagate the owner sprite’s settings to all of its subsprites whenever they are changed. This is about navigating down an ordered tree towards its leaf nodes and basically trivial. What’s tedious about it is that you have to add additional methods for each and every possible modification of any node, which is not exactly “copy & paste” but nonetheless repetitive and boring stuff. Here Smalltalk’s meta-programming capabilities allowed me to write a single simple method instead managing that task for any context:

spread
“private - apply the method calling me to all subsprites
| sel args |
subsprites isEmpty ifTrue: [^self].
sel _ thisContext sender method selector.
args _ OrderedCollection new.
1 to: thisContext sender method numArgs do: [:idx |
    args add: (thisContext sender tempAt: idx) ].
subsprites do: [:eachPart |
    args isEmpty
        ifTrue: [ eachPart perform: sel]
        ifFalse: [ eachPart perform: sel withArguments: args asArray]]

A simple, consistent syntax is worth more than abundant libraries. Gotta love languages for the lazy!

Enjoy!

-Jens Mönig