Scratch (and other) programs by Kevin and Abe Karplus

(Last Update: 11:43 PDT 8 May 2008 )


Scratch is a beginner's programming language for programming simple sprite-based animations and computer games. It is available free from MIT: We worked with the 7Nov06 beta-release version on a Mac OS X system.

We have converted our program to the 3 Jan 07 beta, and thus, with no further changes, to version 1.0 and 1.1 and 1.2.1. The differences are fairly minor and the old programs will still run under the new system, but certain of the old commands have been deprecated, so we replaced them with their newer equivalents. One useful new feature was a "go to" that moved a sprite to the location of some other sprite.

Scratch 1.1 has added a repeat-until block, which cleans up the code quite a bit in a few places.

Scratch 1.2.1 has added trig functions and read-only access to the state variables of other sprites.

In addition to the tutorials on the scratch web site, there is a good beginner's tutorial at

11 Jan 2008. I have not been maintaining this web page for a while. For more recent projects by Abe and me, check out our pages on the MIT website:

19 Feb 2007. Abe has started learning a more complicated language (Alice) that is also available for free (at This is a 3D animation/video game program that uses object-oriented programs (very similar to Java, but stripped down). The entry barrier is much higher than with scratch, and Alice does not include tools for creating your own characters, but the 3D stuff is nice.

15 April 2007. Abe and I have started learning Starlogo (free from, a version of logo intended for handling large numbers of turtles running identical programs. We had wanted to do "boids" simulations, but neither Alice nor scratch makes it easy to have large numbers of identically programmed sprites or objects.

5 July 2007. We may try to learn Processing (from, which is supposedly closer to Java than Alice is, but still a good visual environment.

Tech Club

We have also started a Tech Club at Abe's school for 4th, 5th, and 6th graders.

Kevin's scratch programs

Sliding Slug
This is an exercise in automatic scaling for perspective, and is otherwise pretty boring.

Caveat! The cyberslug is the logo of the School of Engineering at UCSC, and should *not* be used for non-SoE purposes (like this one).

Electric piano
This is a small, one-sprite music maker. The sprite is of a 3-octave piano keyboard. Clicking on the keyboard plays the corresponding note, for both black and white keys. Holding down the mouse causes repeated notes. The idea is that this sprite can easily be incorporated into other, more sophisticated animations (dance routines, cat on the keyboard, fancier instruments with organ stops, ...).

Here is the keyboard I created for the electric piano, at 140 pixels per octave. Feel free to use it in your own projects. three octaves of a piano keyboard, set up to have 140
	pixels per octave

Polyphonic piano
This program looks the same as the simpler electric piano above, but it can play more than one note at a time. It is currently implemented to play up to 4 notes, but is easily extended up to whatever limitation the underlying scratch interpreter has. Adding more simultaneous notes just consists of copying a note sprite and changing what turn it takes, and modifying "num at once" in the keyboard sprite.
Polyphonic piano with arrows
This program is very similar to the polyphonic piano above, but provides fading arrows that indicate what keys have been played. It also uses separate variables to indicate where the key was pressed, making it a bit easier to incorporate into a random music player.
Composing piano
Too lazy to click the mouse on your piano? This one has an extra "player" sprite that composes music for you. It is not very exciting music---just random noodling around on the C-major chord with a 4/4 beat, but it shows how to write a tiny composer that is no worse than the loops in a lot of crude games. It uses the polyphonic piano, but you could easily remove most of the visual parts and have just the sound.
Composing without a keyboard
Want a minimal music composing program to import into another project? This program is a stripped-down version of the composing piano (with the improvements from the Karplus name animation). The keyboard has been thrown away, as have all the x and y variables. The composer now works with notes of a scale, which it then converts to pitches on a major scale (C major by default, but transposing is trivial by changing the base note). There are only 5 sprites, all of which are always hidden.
Composing with only one sprite
Scratch 1.1 allowed me to simplify the above composer and put all the scripts in a single sprite. This makes it much easier to incorporate the sprite into a new project.
This is a crude implementation of the classic Simon game, done as an exercise in using the display as a fake array, since Scratch (rather painfully) lacks arrays.
Binary counter
This program is a tutorial for counting on your fingers in binary.
Printing numbers
Scratch provides two mechanisms for showing the numeric values of variables: the "scoreboard" display and the "say" command. This program provides another mechanism: using a digit sprite and the "stamp" command to print values of variables on the background. Currently, the program is limited to integer-valued variables.
RPN calculator
This reverse-Polish notation calculator is a very simple calculator implementation. It has a stack of depth 4, just like an HP calculator, and has the standard 4 operations (+,-,*./). Output is limited by the use of scoreboards to one decimal point, but input and calculation are not so limited. Some day I'll set up a better display for the calculator using sprites, allowing higher precision.
Moiré patterns
Moiré patterns are formed when 2 (or more) repeating designs are superimposed with slight differences in size or angle. This program is a playground for experimenting with simple Moir´ patterns.
One-sprite, one-script bubblewrap
Andres Monroy-Hernandez wrote a script for popping bubbles in bubble wrap. This is an improved version of the program, using only one script and one sprite.

Abe's scratch programs

This is a (not fully tested) basic one-player fighting game. Each fighter is one sprite. The player sprite has two costumes (one for crouching).
Ghost Chase
Ghost Chase is a game in which you need to use arrow keys to avoid the ghosts for 30 seconds to win. If you get tagged too many times by the ghosts, you lose and turn into a ghost of yourself.
Dancer is an exercise in scripting a dance and learning how to use message passing and multiple threads. It has no interaction with the user. This version has a more complex dance than the first two, and the routine is (mostly) constructed of 4-beat phrases. There is now an exit as well as an entrance.
Painter is a drawing program.
Pirate Flag
Pirate flag is an exercise in animation with sound effects, and is Abe's first attempt at using the sound recorder in scratch.
BouncingBall is Abe's first attempt at a realistic simulation, which he wrote as part of his 5th grade science fair project. It is a discrete-time simulation and uses the stamp tool to show the position of the ball at each time step. The scale (in pixels per cm) can be adjusted with a slider, as can the coefficient of restitution for the ball. The background grid and axis labeling is done with the pen and stamp tools, since scratch does not provide an easy way for printing numbers on the screen.

This simulation has some problems with handling the overshoot when the ball hits the ground during a time step. The correct handling of that case is difficult in any simulation where the time step is fixed.

The simulation works better downloaded than in the java applet.

Here is the report wrote for Science Fair 2007.

Jointly written programs (scratch and starlogo)

Shoot Kevin
This was Abe's first game, for which he had some programming assistance from Kevin, as he learned the language. The version currently on the web is not his first version, as Abe has since refined the program. For example, the first version used several costumes to do the rotating head, but the new version just uses a rotatable sprite. Also, the gliding and rotating were mixed together in the original program, but are now done by separate scripts in the head sprite.

The alien spaceship is stolen from the web ( and used without permission.

Tennis game
Abe started this 2-player tennis game, but needed some help with the programming and converting the initial stick figures into photos. (Abe is the left-handed player who serves first.)
Karplus name animation
This over-4-minute animation is our most complex project yet, involving 16 sprites, 8 global variables, and 57 messages. The number of messages could be greatly reduced if there were a "send to " command, as there are 7 flavors of many messages, one for each sprite.

The animation includes an improved version of the composing_piano, to play music for the scramble segment and the credits.

Treasure Hunt for 2007 March 13 Tech Club
This is not a real program, but 8 snippets of code that were distributed at Tech Club for a cooperative treasure hunt. The (0,0) point was marked on the floor, and each kid had a snippet of code to execute. The treasure hunt went well, with no serious mistakes in interpreting the code. (The "treasure" was a couple of chocolate bars that were shared out among the participants.)

See the Tech News, 13 Mar 2007 for other activities that day.

Architecture Stamps
Architecture stamping program based on Fun with Architecture rubber stamp set by David Eisen, published by the Metropolitan Museum of Art.

Kevin did most of the programming and costume editing, but Abe helped out some with both. There are currently 3 versions:

Architecture Stamps v1
The initial minimal version
Architecture Stamps v2
adding an animated opening sequence
Architecture Stamps v3
adding a line tool and tool size changing.
This is the classic game of MasterMind, where the player tries to deduce the colors of 4 pegs. There are 6^4 = 1296 possible ways for the computer to have constructed the right answer. You have 10 guesses, and get feedback after each one on the number of pegs in the right holes and the number of pegs of the right color in the wrong holes.

Abe helped me some with the programming and design of the game.

Chemotaxis applet
Abe and I wrote a chemotaxis simulation of bacteria in a food gradient. The basic simulation (with no eating, movement cost, or reproduction) causes the bacteria to swarm in a ring around the food source, around the optimum concentration. With eating and movement cost, the bacteria quickly deplete the supply of food. The reproduction model is pretty crude---the bacterium copies itself when it has a high enough stored energy and each copy has half the original food.

We probably need to add some documentation to the interface.

The starlogo source code for the simulation is at swimtumble.slogo

the scratch cat
Scratch home page
sketch of Kevin Karplus by Abe
Kevin Karplus's home page
Karplus name logo
Kevin and Abe's scratch page
Spring Hill Tech Club logo
Spring Hill Tech Clubs's gallery

Questions about page content should be directed to

Kevin Karplus
Biomolecular Engineering
University of California, Santa Cruz
Santa Cruz, CA 95064
318 Physical Sciences Building