Top-level code runs once, at power-on. f(), if there is one, runs 30 times a second. Every variable is global, except a function's parameters (extra ones you don't pass start at 0, so they work as locals). There are numbers, strings and arrays: a=[1,2], a[len(a)]=3. Statements: if/else, while, for(;;), break, continue, return, and name(a,b){…} to define a function.
cls(c) pset(x,y,c) pget(x,y) | clear, set and read a pixel |
rect(x,y,w,h,c) box(…) | filled and outlined rectangles |
circ(x,y,r,c) ring(…) line(x0,y0,x1,y1,c) | circles and lines |
text(s,x,y,c) cam(x,y) | 3×5 letters; move the camera |
btn(i) btnp(i) | held, just pressed: 0 ← 1 → 2 ↑ 3 ↓ 4 A 5 B |
rnd(n) flr ceil abs sgn min max mid sqrt | dice (seeded by a block) and maths |
sin(t) cos(t) atan2(y,x) | angles as turns: 0.25 is a right angle |
len(a) arr(n,v) del(a,i) | array length, a new array, take one out |
sub(s,i,n) ord(s) chr(n) num(s) | a piece of text; a letter's number and back; a number read from text |
t() score(n) over() | the frame count; set the score; end the game |
beep(note,frames,wave) | a sound: note 0–96, wave 0–3 |
A plain face. f() can say words as well as draw dots. A cartridge that only says words is shown as a page of real text, with fields to type in and buttons to press, and reads the same from a command line.
head(s) say(a,b,…) | a heading; a line of words |
row(a,b,…) | a row of a table: rows that follow each other line up |
field(label) | a place to type: it gives back what's typed there |
button(label) | a button: it gives back 1 on the frame after it's pressed |
name(n) | a person's name, from the number who() gives |
l=[]
m(s){if(len(s))l[len(l)]=s}
f(i,s){head("A list")
for(i=0;i<len(l);i++)say(l[i])
s=field("Add a line")
if(button("Add")&&len(s))send(s)}
Building on other cartridges. Any cartridge on the network can be a part of yours. Open one, press Code, and copy its line:
use d="3fa91c20b7e4"
f(){cls(1)d.big(42,10,40,7)}
Everything in it is then there under that name: d.big() calls its function, d.x is its variable. Its top-level code runs before yours, and its names never meet yours. It can be built from others in turn, and a whole program can run inside another (d.f()). A record can't change, so nothing you build on can shift under you.
Seeing other programs. A shared program can be looked at by any cartridge, as it stands for everyone:
see g="81c0de55aa07"
f(){cls(0)for(i=0;i<1024;i++)if(g.a[i])
pset(i%32,flr(i/32),g.a[i])
if(btnp(4))g.send(5,5)}
g.a is then that program's own variable, and g.fn() its function over what it holds. It can be read, not changed: g.send() asks to send it a message, like anyone else. That's a whole other screen for the same program. A shared program can see others too: in each block it sees them as they stood at the end of the block before, so its own state comes out the same for everyone.
Shared programs. Give a cartridge m() or b() and it's one program for everyone. Its top-level code runs once, in the block it's published in. From then on m(a,b) runs for every message anyone sends it, and b() once for every block, in the same order on every machine, so its variables come out the same everywhere. f() still draws them, but it only looks: whatever it changes is put back at the next block. The one way to change the program for everyone is send(), which asks whoever is at the console to send a message.
send(a,b,…) | ask to send a message: up to 8 numbers and strings |
who() me() | in m(), who sent it (at power-on, who published); in f(), who's looking |
blk() t() | the block's number; in m() and b(), t() counts blocks since it began |
typed() | the line typed under the screen (Enter presses A) |
n=0
m(){n+=1}
f(){cls(1)text(n,60,60,7)
if(btnp(4))send()}
In m() and b() there's no screen and there are no buttons, rnd() gives the same dice on every machine, and each call gets so many steps. A program with a busy b() takes everyone longer to work out, for ever, so let it rest when nothing's happening.
Every cartridge on the shelf is a record on Compute Substrate: its code, written into the network's shared memory, signed by whoever wrote it. There's no app store and no server. Your browser reads the records, checks each one against the network's proof-of-work, and runs the code itself, in a tiny machine that gives the same answer in every browser.
A score isn't a number someone claims. It's the buttons you pressed on every frame, plus a block whose hash seeded the game's dice. Anyone's browser plays the cartridge again with those buttons and that seed, and the score counts only if it comes out exactly the same.
A cartridge marked SHARED is one program for everyone. The network keeps its code and every message anyone has sent it, and nothing else: it never runs anything. Your machine runs the program through all of those messages, in order, and arrives at exactly what everyone else's machine arrives at. That's why the garden you see is the garden everyone sees, though no computer anywhere is running it for you. The seal under the console is a picture of what your machine worked out: the same seal, the same world.
What a cartridge does is settled by the machine it runs on, and the machine is one small file, known by its fingerprint and kept in the network's memory with everything else. This page checks that fingerprint before it runs a line of it. Whoever holds this page holds a screen, not the computer: anyone can make another screen that runs the very same machine, and a screen that ran programs some other way would show different seals from the rest.
Every cartridge on the network can be used by every other, by one line. So what's published here adds up: a function someone wrote is a part for whatever comes after, and it can never be changed or taken away from under what was built on it. A shared program can be seen as it stands by any other, so a new program can stand on what an old one has become. Under Scores, each cartridge shows what it's built from, what it sees, and what has been built on it.
And all of it is in one place, as the code itself. There's nothing to verify and no index to ask: the search on the shelf goes through every cartridge's code in your own browser.
Fork any cartridge and publish your version: it remembers where it came from, so the shelf knows who made the first snake and who made it fast.
Writing takes one post per record (512 bytes). Most cartridges fit on one; bigger ones run on to more pages. A message to a shared program is one post, and so is a score. Reading and playing are free, forever.