PX-BASIC

The PX-42F calculator's BASIC, based on Tiny BASIC, with the calculator's own 34-digit decimal numbers.

1. What it is

PX-BASIC turns the PX-42F into a small computer you program in BASIC, the language of the first home computers. It follows Palo Alto Tiny BASIC, from 1976: numbered lines, PRINT, INPUT, IF, GOTO, GOSUB, FOR. To that it adds the calculator itself:

A program typed and run: a table of square roots
A program and its run.
A spiral drawn with LINE
Graphics (SPIRAL).
Sine and cosine plotted with PSET
Plotting (SINE).

2. Getting started

Installing

  1. Make sure the calculator's firmware is the PX-42F-Plus with app API version 3 or later.
  2. On the calculator, open Settings → USB Drive → Connect to Computer.
  3. Copy BASIC.bin into BIN/APPS on the drive named PX-42F. From the project folder, make install does it.
  4. Optionally, copy the example programs (.BAS files, in the project's sim/drive/BIN/APPS/BASIC/) into BIN/APPS/BASIC on the drive.
  5. Eject the drive and press EXIT on the calculator.

Starting and leaving

Start it from Settings → Games & Apps → Apps → BASIC. It shows how much room there is for a program, then the prompt, >.

To leave, press ON at the prompt. The status bar asks LEAVE BASIC? ENTER=YES: ENTER (or ON again) goes back to the list of apps, and any other key stays in BASIC. Held for 2 seconds, ON turns the calculator off without asking, and the Sleep Timer does too.

The status bar asking LEAVE BASIC? ENTER=YES
ON at the prompt asks first.

The program you were working on is saved when you leave, as AUTOSAVE.BAS, and comes back the next time you start BASIC.

Your first program

BASIC keeps lines that start with a number, in number order, as the program. A line without a number runs at once. Type these three lines, pressing ENTER after each. Use the keyword map (SHIFT ▼, below) for FOR, PRINT and NEXT, and SHIFT before a key for one digit or symbol, or SHIFT ENTER (ALPHA) twice for NUM:

10 FOR I=1 TO 5
20 PRINT I,SQR(I)
30 NEXT

Then type RUN (or choose it from the keyword map): it prints each number and its square root. LIST shows the program again.

3. Typing

The status bar

The top right of the screen says what the keys do. The prompt, Edit mode and the GTO prompt all show the mode there:

It saysMeaning
[A]ABC: the keys type capital letters. Every line typed at the prompt starts here.
[a]abc: the keys type small letters, for text in quotes. BASIC reads its words and variables as capitals.
[#]NUM: the keys type digits and symbols. A program's INPUT starts in NUM: it wants a number.
SHIFT (next to it)SHIFT is on: ENTER goes to the next mode (ALPHA); another key types from NUM once, or from ABC if you're in NUM.
RUNA program is running. R/S stops it.
ABC mode: IJ typed at the prompt
ABC: the second row's keys typed I and J.
abc mode after SHIFT ENTER: ij typed
SHIFT ENTER (ALPHA) goes to abc.
NUM mode after SHIFT ENTER again
Again: NUM. Once more is ABC.

ABC and abc: letters

The letters are where the calculator's ALPHA keyboard has them, as on the DM42: A to F on the top row, G to L on the second, M N O next to ENTER, then P to Z, row by row. = is after Z, and the bottom row types : . ? and a space.

NUM: digits and symbols

The digits and + − × ÷ are where they're printed (* and / for times and divide), with . and a space on the bottom row. The top rows type BASIC's symbols. E, on the calculator's E key, writes a number's exponent: 2E-3.

The other keys

KeyDoes
SHIFT ENTERALPHA, as printed below ENTER: the next mode, ABC, abc, NUM and round again.
SHIFTWith any other key, types from NUM once, or from ABC if you're in NUM: SHIFT 7 types 7 in ABC, SHIFT Σ+ types A in NUM.
▲At the prompt: the last command, again the one before that, back through the last 8. After the oldest it stays there and plays the error sound.
▼On an empty line: List mode (section 4). On a command ▲ brought back: one newer, then an empty line.
▲ ▼ in a lineOnce you've changed a line at the prompt, or in Edit mode: move the cursor left and right. ▼ at the end of the line completes the keyword being typed, like TAB on the terminal, and lists the words when several fit: PR becomes PRINT .
SHIFT ▼The keyword map (below).
SHIFT ▲List mode, from any line at the prompt. What you've typed is still there when you come back.
←Deletes the last character. SHIFT ← deletes the whole line.
ENTEREnters the line.
R/SWhile a program runs, or waits in INPUT: stops it. Typing: ? in ABC, a space in NUM.
SHIFT R/SAt the prompt: RUN. It runs the program at once, in place of whatever is typed on the line.
ONWhile a program runs or waits in INPUT: stops it, as R/S does. At the prompt: asks before leaving BASIC. Held 2 seconds: turns the calculator off.

The keyword map

Typing RETURN letter by letter takes six keys. SHIFT ▼ shows a map of BASIC's words laid over the keys: press a key to type its word, with the space or ( after it. The words are in eight pages by kind, and in alphabetical order on each page, reading along the rows. ▼ shows the next page and ▲ the one before (the page number is in the top right corner); the pages go round, so ▼ on the last page shows the first, and ▲ on the first shows the last. ON (shown as BACK), or a key with no word on it (an empty box), goes back without typing anything.

PageWords
1. StatementsDIM END FOR GOSUB GOTO IF INPUT LET NEXT PRINT REM RETURN STEP STOP THEN TO
2. FunctionsABS( ACS( ASN( ATN( COS( EXP( INT( LOG( LOG10( PI RND( SGN( SIN( SQR( TAN(
3. Strings$ ASC( CHR$( INKEY$ INSTR( LEFT$( LEN( MID$( RIGHT$( STR$( VAL(
4. Input / Output? AT INKEY$ INPUT KEY() PRINT
5. GraphicsBOX CIRCLE CLS ELLIPSE LINE PEN POLYGON PSET
6. SoundMIDI() PLAYMIDI PLAYWAV STOPMIDI STOPWAV TONE
7. SystemCMDS DEG DIGITS ERASE EXIT FILES HELP LIST LOAD MEM NEW RAD RUN SAVE SIZE VER WAIT
8. Operators@( AND MOD NOT OR

SHIFT R/S runs the program without the map.

The keyword map, statements
Page 1: statements.
The keyword map, functions
Page 2: functions.
The keyword map, strings
Page 3: strings.
The keyword map, system
Page 7: system.

Typing from a computer

With the calculator connected by USB, open its console port ("PX-42F Console") in any terminal program; the speed setting doesn't matter. A Mac lists it with ls /dev/cu.usbmodem* (the first of two); screen /dev/cu.usbmodem2101 opens it. Then:

4. Writing a program

CommandDoes
RUNRuns the program from its first line, with every variable set to 0. SHIFT R/S does the same without typing it; so does the R/S key's place in the keyword map, which types RUN for ENTER.
LISTShows the whole program. LIST 100: from line 100 on. LIST 100,200: lines 100 to 200. R/S stops a long listing.
NEWErases the program and the variables.
GOTO 100Typed at the prompt: runs from line 100 without clearing the variables.

Editing the program

You don't have to retype a line to change it. On the calculator, ▼ at an empty prompt or SHIFT ▲ at any prompt opens List mode. It works in two steps, on one screen:

  1. List mode. The program is shown a line to a row, the current line highlighted, and the header says LIST. It opens on the line you last entered, or on the first line. ▲ and ▼ move the highlight, and the list scrolls; SHIFT with them moves a screenful. The list goes round: ▲ on the first line goes to the last, and ▼ on the last goes to the first. To go to a line, press SHIFT GTO (SHIFT XEQ), its number and ENTER. With no line of that number you get the nearest one, and of two as near the earlier one. ← deletes a digit, and EXIT cancels. SHIFT ← deletes the highlighted line: the header asks DELETE 20? ENTER=YES, ENTER deletes it and any other key keeps it. ENTER edits the highlighted line. EXIT (the ON key) goes back to the prompt.
  2. Edit mode. The line is edited where it is, in the list: all of its text, as many rows as it needs, with the rest of the program still shown around it. The header says EDIT and what the keys do, with the mode at the right. ▲ moves the cursor left and ▼ right (the cursor is the white block). What you type goes in at the cursor; ← deletes the character before it; SHIFT ← clears the line; SHIFT ▼ is the keyword map; SHIFT ENTER goes to the next mode (the header then shows [a], or [#]).
    • ENTER accepts the change: the line replaces the old one.
    • EXIT cancels: the line stays as it was.
    Either way you're back in List mode, on the same line, to move to another one.
The program as a list, line 20 highlighted
1. List mode: ▼ opened it on line 10, ▼ again moved the highlight to line 20.
Line 20 being edited in place, the header saying EDIT
2. ENTER: line 20 is edited where it is, the rest still shown.
The cursor moved to SQR
3. ▲ six times moved the cursor to SQR.
2* typed before SQR
4. 2 * went in at the cursor.
Back in the list after ENTER
5. ENTER accepted it: List mode again, on line 20.
The run, with the square roots doubled
6. EXIT went back to the prompt; RUN shows the change.

At the prompt, ▲ brings back the last commands, for running them again, as the terminal’s up arrow does. To change one first, press ← or type something: from then on the arrows move the cursor. At a program's INPUT, ▲ and ▼ only move in what you've typed.

A program asking for a radius and printing the area
A program with INPUT. ^ shows as ↑, the HP-42S's own character.
R/S stops the program; LIST shows it
R/S stopped it (BREAK IN 10); LIST shows it.

5. Numbers

Every number is a decimal of up to 34 significant digits, from ±10−6143 to ±106144, the same numbers Plus42 computes with. Because they're decimal, the sums that go wrong in most computer languages come out right: 0.1+0.2 is exactly 0.3. A fraction with no exact decimal still rounds, as on any calculator: 1/3 is 0.333…3, 34 threes.

How PRINT shows them

Writing them

12, -3.5, .25, 2E-3, 6.02214076E23. An E is the exponent only with digits after it, so 2*E is two times the variable E.

Variables and the array

6. Expressions and functions

Operators, the first done firstMeaning
( )grouping
^power, right to left: 2^3^2 is 29. On the screen it shows as ↑.
-minus: -2^2 is −4
* / MODtimes, divided by, and the remainder. a MOD b has the sign of b, as the HP-42S's MOD: -7 MOD 3 is 2.
+ -plus, minus
= <> < > <= >=comparisons: 1 if true, 0 if false
NOT1 if 0, else 0
AND1 if both are true (not 0)
OR1 if either is true
FunctionGives
SIN(x) COS(x) TAN(x)trigonometry, in radians, or in degrees after DEG
ASN(x) ACS(x) ATN(x)arcsine, arccosine, arctangent, in the same unit
SQR(x)square root
EXP(x), LOG(x), LOG10(x)ex, the natural logarithm, the base-10 logarithm
ABS(x), SGN(x)without its sign; −1, 0 or 1
INT(x)the whole number at or below x: INT(-2.5) is −3
RND(n)a random whole number from 1 to n. RND(0): a random fraction from 0 to 1.
PIπ to 34 digits
KEY()a key pressed while the program runs (section 8)
MIDI()1 while a song or a WAV plays, otherwise 0
SIZEbytes left for the program

Arrays: DIM

DIM A(n) makes a list of numbers, A(0) to A(n). DIM M(r,c) makes a table: M(0,0) to M(r,c), so DIM M(2,3) has 3 rows of 4. DIM S$(n) makes a list of strings, S$(0) to S$(n). Several can be made in one statement: DIM A(9), M(4,4), N$(20).

10 DIM M(2,2), N$(2)
20 FOR I=0 TO 2:FOR J=0 TO 2
30 M(I,J)=I*3+J
40 NEXT J:NEXT I
50 N$(0)="ONE":N$(1)="TWO"
60 PRINT M(1,2);" ";N$(1)

Strings

Text is kept in string variables, A$ to Z$ (the name, then a $). They are separate from the numbers: A and A$ are two variables. Type the $ on its key in NUM (the one STO has above it, row 2 right of =), or from the third page of the keyword map. All the strings share one pool of 4,096 characters; one string can be up to 255 characters long, and a variable that was never set is empty. RUN and NEW empty them all.

10 A$="HELLO"
20 B$=A$+", WORLD"
30 PRINT B$;" HAS ";LEN(B$);" LETTERS"
40 IF LEFT$(B$,1)="H" THEN PRINT "STARTS WITH H"
50 INPUT "YOUR NAME";N$
60 PRINT "HI ";N$
FunctionGives
LEN(s$)the number of characters
LEFT$(s$,n) RIGHT$(s$,n)the first or last n characters (all of them if there are fewer)
MID$(s$,start,n)n characters from start, counting from 1; without n, to the end. start 0 is HOW?
CHR$(n) ASC(s$)the character with code 1 to 255, and the code of a string's first character
STR$(x) VAL(s$)a number as text, as PRINT shows it, and text as a number (0 if it isn't one)
INSTR(a$,b$)where b$ first appears in a$, counting from 1; 0 if it doesn't
INKEY$the next key pressed while the program runs, as a one-character string, or an empty string if there was none. Digit and operator keys give what they type in NUM; the other keys give their letter; ENTER, ←, ▲ and ▼ give CHR$(13), CHR$(8), CHR$(30) and CHR$(31). KEY() gives the key's place instead.

A string can be given wherever a name is wanted: LOAD A$, SAVE "X"+A$, PLAYMIDI A$, PLAYWAV "TONES/TONE"+STR$(N).

7. Statements

StatementDoesExample
PRINT or ?Shows numbers and "text". ; between items keeps them together, , moves to the next column of 8. A ; or , at the end keeps the line open for the next PRINT.PRINT "X=";X,"Y=";Y
DIMMakes number arrays (DIM A(10), DIM M(5,5)) and string arrays (DIM N$(20)); see Arrays.DIM M(2,3), N$(9)
INPUTAsks for numbers, or for a line of text into a string variable (INPUT A$). Without a prompt it shows the variable's name: R? . Something that isn't a number gets HOW? and the question again.INPUT "RADIUS? ",R
LETGives a variable a value. LET can be left out.X=X+1, @(I)=0
IF … THENIf the condition is true (not 0), does the rest of the line; if false, skips it, including statements after :. THEN 100 goes to line 100.IF X>9 THEN PRINT "BIG":X=0
GOTOGoes to a line. The number can be an expression.GOTO 100+10*K
GOSUB, RETURNGoes to a line, and RETURN comes back after the GOSUB.GOSUB 500
FOR … TO … STEP, NEXTRepeats the lines up to NEXT, the variable going from the first value to the last. STEP can be a fraction or negative; without it, 1.FOR A=0 TO 1 STEP 0.1
END, STOPEnds the program.
REMA note: the rest of the line is ignored.REM DRAW THE AXES
DEG, RADAngles in degrees, or radians (the default). RUN starts in radians, so a program that wants degrees says DEG itself.DEG:PRINT SIN(30)
DIGITSDigits PRINT shows, 1 to 34; 12 at first, and again at each RUN.DIGITS 34
CLS, AT, PSET, LINE, BOX, CIRCLE, ELLIPSE, POLYGON, PEN, TONE, PLAYMIDI, STOPMIDI, PLAYWAV, STOPWAV, WAITThe screen, sound and time: section 8.
SAVE, LOAD, FILES, ERASEFiles: section 9.
VERShows the PX-BASIC version.VER
MEMHow much is left of the program space and of the string pool. (SIZE is the program space alone, as a number.)MEM
HELP (or CMDS)Shows the keyword map (below) to read, without typing anything: every word BASIC knows, in the same eight pages. ▲ and ▼ turn the pages, round and round; ON or any key with a word on it closes it and brings back the prompt.HELP
EXITLeaves BASIC for the calculator's menu, as ON does, but without asking. The program is kept for next time.EXIT

8. The screen, sound and keys

Text

The text fills 9 lines of 31 characters under the status bar, and scrolls up when it reaches the bottom.

Graphics

The screen is 250 × 122 pixels, with x from 0 (left) to 249 and y from 0 (top) to 121. A colour of 1 is black (the default), 0 white, which erases.

StatementDraws
PSET x,y or PSET x,y,cone pixel
LINE x1,y1,x2,y2 (,c,t)a line, t pixels thick (1 if left out)
BOX x,y,w,h (,c,t)a rectangle w wide, h high, from its top-left corner
CIRCLE x,y,r (,c,t)a circle around x,y
ELLIPSE x,y,rx,ry (,c,t)the same, rx across and ry up and down from the centre
POLYGON n,X,Y (,c,t)the n corners (1 to 64) in the lists X() and Y(), closed for you; where it crosses itself, a filled one has holes. DIM X(2),Y(2) holds 3 corners.
t is the thickness: 0 or left out is filled (a line: 1), 1 one pixel, n n pixels. An outline grows inward, keeping the outer size; a line or a polygon's edge is centred, with round ends. To give t, give c first: CIRCLE 125,61,40,1,1.
PEN t or PEN t,cdraws nothing: the shapes after it use this t and c when they leave those out, and PSET the colour. PEN 2:CIRCLE 125,61,40 is an outline 2 pixels thick; PEN 0,0 erases, filled. What a shape gives itself still counts. RUN and NEW forget it.

The shapes are the calculator's own, in its firmware, and Plus42 draws with the same ones: PEN, BOX, ELLIPSE and POLYGON there, with DLINE for LINE. Plus42 counts from 1,1, so a position there is one more than here, and its pen starts at 1, an outline. Here, until a PEN, a shape that leaves out t is filled.

Drawing and text share the screen. While a program runs the status bar isn't redrawn, so the program can use the whole screen; it comes back at the prompt.

Sound and time

Keys: KEY()

While a program runs, the keys pressed (except R/S and ON, which stop it) are kept for KEY(). Each call gives the next one, in the order pressed, as its row and column from 1, or 0 when there's none left:

10 REM MOVE A BOX WITH 4 AND 6, R/S STOPS
20 CLS:X=120
30 BOX X,55,8,8
40 K=KEY()
50 IF K=0 THEN WAIT 20:GOTO 40
60 BOX X,55,8,8,0
70 IF K=52 THEN X=X-8
80 IF K=54 THEN X=X+8
90 GOTO 30

Line 50 waits for a key, asleep, 20 ms at a time. Line 60 erases the box (colour 0) before it moves. A single pixel (PSET) is hard to see on the calculator's screen: for something that moves, draw a BOX.

9. Saving programs

Programs are saved on the calculator's drive as plain text files, NAME.BAS, in the folder /BIN/APPS/BASIC/. A name has up to 20 letters, digits, _ or -. The quotes can be left out: LOAD RINGS3D is LOAD "RINGS3D". Inside quotes a name can also have spaces: SAVE "My Ring"; without quotes a name ends at the first space. The SAVE, LOAD and ERASE keys of the keyword map type the word and a space, no quote. Lower case is the same name in capitals: "Ring_3d" is saved and listed as RING_3D.

CommandDoes
SAVE "NAME"Saves the program, replacing a saved one of that name.
LOAD "NAME"Loads a saved program, in place of the one in memory.
FILESLists the saved programs.
ERASE "NAME"Deletes a saved program.

On a computer. In USB Drive mode the files are in BIN/APPS/BASIC. A .BAS file is the program as LIST shows it, so you can read and write programs in any text editor. Lines without a number are skipped when loading.

AUTOSAVE. When you leave BASIC, the program in memory is saved as AUTOSAVE.BAS, and loaded again when BASIC starts. It's the program as it is at that moment, whatever you saved or loaded under other names meanwhile: after LOAD "GAME" and leaving, BASIC starts with GAME. It's written when you leave by ON (and ENTER to confirm), when you hold ON to turn off, and when the Sleep Timer or an empty battery turns the calculator off. It isn't written if the calculator crashes or is reset, so SAVE a program you can't lose. FILES doesn't list it.

10. Errors

The three messages of the original Tiny BASIC, plus BREAK. In a program each also says the line: HOW? IN 120.

MessageMeansFor example
WHAT?It can't be read.a misspelling (PRIMT), a missing ) or =
HOW?It can't be done.division by zero, SQR(-1), GOTO a line that isn't there, RETURN without GOSUB, @(5000), LOAD a file that isn't there
SORRYThere's no room.the program is full (16 KB), or GOSUBs and FORs nest more than 32 deep
BREAKR/S, ON or Ctrl-C stopped it.
HOW?, WHAT? and HOW? IN 10 on the screen
An error at the prompt, and one in a program.

11. Examples

These come with the project, in sim/drive/BIN/APPS/BASIC/. Copy them to BIN/APPS/BASIC on the calculator's drive, then LOAD "PRIMES" and RUN.

HELLO

10 REM THE FIRST PROGRAM
20 CLS
30 PRINT "HELLO FROM THE PX-42F!"
40 FOR I=1 TO 5
50 PRINT I;" SQUARED IS ";I*I
60 NEXT I
70 TONE 5

PRIMES

10 REM PRIMES BELOW 100
20 FOR N=2 TO 100
30 FOR D=2 TO SQR(N)
40 IF N MOD D=0 THEN 70
50 NEXT D
60 PRINT N;" ";
70 NEXT N
80 PRINT
The primes below 100
PRIMES.

GUESS

10 REM GUESS THE NUMBER, 1 TO 100
20 N=RND(100):T=0
30 INPUT "GUESS? ",G
40 T=T+1
50 IF G<N THEN PRINT "HIGHER":GOTO 30
60 IF G>N THEN PRINT "LOWER":GOTO 30
70 PRINT "YES, IN ";T;" GUESSES"
80 TONE 9
A game of guessing a number
GUESS.

SPIRAL

10 REM A SPIRAL OF LINES: R/S STOPS
20 CLS:DEG
30 X=125:Y=61
40 FOR A=0 TO 1440 STEP 15
50 R=A/24
60 U=125+R*COS(A):V=61+R*SIN(A)
70 LINE X,Y,U,V
80 X=U:Y=V
90 NEXT A
100 WAIT 3000

JUKEBOX

Plays /MIDI/Canon.mid (put a song of that name there, or change the name in line 50) and stops when you press a key.

10 REM PLAYS A SONG FROM /MIDI
20 CLS
30 PRINT "PLAYING CANON"
40 PRINT "ANY KEY STOPS IT"
50 PLAYMIDI "Canon"
60 WAIT 100
70 IF KEY()<>0 THEN STOPMIDI
80 IF MIDI() THEN GOTO 60
90 PRINT "DONE"

SINE

10 REM SIN AND COS, PLOTTED
20 CLS:RAD
30 LINE 0,61,249,61:LINE 125,0,125,121
40 FOR X=0 TO 249
50 T=(X-125)/20
60 PSET X,61-40*SIN(T)
70 PSET X,61-40*COS(T)
80 NEXT X
90 WAIT 3000

SHAPES

Each kind of shape, a page at a time, with PEN; a key goes on. Line 650 draws over the ball with a white pen.

10 REM SHAPES: WHAT THE CALCULATOR DRAWS. A KEY GOES ON
20 REM PEN T,C SETS THE THICKNESS AND COLOUR OF WHAT FOLLOWS
30 DEG:DIM X(5),Y(5)
100 REM ----- THE WHOLE SCREEN -----
110 CLS:PEN 1
120 BOX 0,0,250,122:LINE 0,0,249,121:LINE 0,121,249,0
130 GOSUB 900
200 REM ----- A STAR OF LINES, THEN EACH THICKNESS -----
210 CLS:PEN 1
220 FOR A=0 TO 345 STEP 15
230 LINE 69,61,INT(69.5+56*COS(A)),INT(61.5+56*SIN(A))
240 NEXT A
250 LINE 139,19,239,19
260 PEN 2:LINE 139,39,239,39
270 PEN 4:LINE 139,61,239,61
280 PEN 8:LINE 139,83,239,83
290 PEN 16:LINE 149,105,229,105
295 GOSUB 900
300 REM ----- BOXES: OUTLINES, FILLED, WHITE INSIDE -----
310 CLS
320 PEN 1:BOX 9,9,70,45
330 PEN 2:BOX 89,9,70,45
340 PEN 4:BOX 169,9,70,45
350 PEN 0:BOX 9,64,110,50:BOX 129,64,110,50
360 PEN 0,0:BOX 34,77,60,24
370 PEN 3,0:BOX 141,72,86,34
380 GOSUB 900
400 REM ----- CIRCLES AND ELLIPSES -----
410 CLS
420 PEN 1:CIRCLE 41,31,28
430 PEN 3:CIRCLE 124,31,28
440 PEN 7:CIRCLE 207,31,28
450 PEN 0:ELLIPSE 67,91,60,26
460 PEN 0,0:ELLIPSE 67,91,30,12
470 PEN 2:ELLIPSE 189,91,50,20
480 GOSUB 900
500 REM ----- POLYGONS: A FILLED STAR, WITH ITS HOLE; A HEXAGON -----
510 CLS
520 PEN 0:N=5:S=144:C=66:GOSUB 800
530 PEN 2:N=6:S=60:C=186:GOSUB 800
540 GOSUB 900
600 REM ----- A WHITE PEN ERASES: A BALL HOPS ACROSS -----
610 CLS
620 FOR D=19 TO 229 STEP 30
630 PEN 0:CIRCLE D,61,14
640 WAIT 300
650 PEN 0,0:CIRCLE D,61,14
660 NEXT D
670 END
800 REM N CORNERS, S DEGREES APART, AROUND C,62
810 A=-90
820 FOR I=0 TO N-1
830 X(I)=INT(C-0.5+50*COS(A)):Y(I)=INT(61.5+50*SIN(A))
840 A=A+S
850 NEXT I
860 POLYGON N,X,Y
870 RETURN
900 REM WAIT FOR A KEY
910 IF INKEY$="" THEN WAIT 20:GOTO 910
920 RETURN

CATCHER

A game that shows the graphics and the sound. Stars and rocks fall; 4 and 6 (or ▲ ▼) move the basket. A star caught plays a tone without stopping the game (PLAYWAV "TONES/TONE"+STR$(C)), one higher for each star in a row; a rock costs a life, with sparks drawn by LINE and the error sound (TONE 10). Every 10 stars the game speeds up, with a jingle. Three rocks and it's over; ENTER plays again, R/S stops.

The title: a burst of lines around STAR CATCHER
CATCHER's title.
The game: the score, falling stars and rocks, the basket
CATCHER, playing.
10 REM STAR CATCHER: CATCH THE STARS, DODGE THE ROCKS
20 REM 4 AND 6 (OR THE ARROWS) MOVE THE BASKET, R/S QUITS
30 REM GRAPHICS: LINE BOX PSET AT. SOUND: TONE AND PLAYWAV
40 DIM X(3),Y(3),T(3)
50 H=0
100 REM ----- TITLE: A BURST OF LINES, A FANFARE -----
110 CLS:RAD
120 FOR A=0 TO 6.2 STEP 0.2
130 LINE 121,66,121+55*COS(A),66+38*SIN(A)
140 NEXT A
150 BOX 66,58,110,17,0
160 LINE 66,58,175,58:LINE 66,74,175,74
170 AT 9,4:PRINT "STAR CATCHER";
180 AT 4,8:PRINT "ENTER: PLAY  R/S: QUIT";
190 TONE 3:TONE 5:TONE 7:TONE 9
200 REM TWINKLE IN THE CORNERS UNTIL ENTER
210 N=RND(60)-1:IF RND(2)=1 THEN N=N+190
220 PSET N,RND(80)+13,RND(2)-1
230 K$=INKEY$:IF K$<>CHR$(13) THEN WAIT 20:GOTO 210
300 REM ----- A NEW GAME -----
310 CLS:S=0:L=3:V=2:C=0:P=113:G=10
320 FOR I=0 TO 3:T(I)=0:NEXT I
330 FOR I=1 TO 40:PSET RND(250)-1,RND(80)+26:NEXT I
340 LINE 0,121,249,121
350 GOSUB 900:GOSUB 800
400 REM ----- EACH FRAME: THE KEYS, THEN EVERYTHING FALLS -----
410 K$=INKEY$:D=0
420 IF K$="4" OR K$=CHR$(30) THEN D=-12
430 IF K$="6" OR K$=CHR$(31) THEN D=12
440 IF D THEN BOX P,112,24,8,0:P=P+D:IF P<0 THEN P=0
450 IF P>226 THEN P=226
460 IF D THEN GOSUB 800
500 FOR I=0 TO 3
510 IF T(I)=0 THEN GOSUB 600:GOTO 590
520 BOX X(I)-3,Y(I)-3,7,7,0
530 Y(I)=Y(I)+V
540 IF Y(I)>=110 THEN GOSUB 700:GOTO 590
550 IF T(I)=1 THEN LINE X(I)-3,Y(I),X(I)+3,Y(I):LINE X(I),Y(I)-3,X(I),Y(I)+3
560 IF T(I)=2 THEN BOX X(I)-3,Y(I)-3,7,7,1:PSET X(I)-1,Y(I)-1,0
590 NEXT I
595 IF L=0 THEN 1000
598 WAIT 20
599 GOTO 410
600 REM ----- MAYBE A NEW STAR OR ROCK IN SLOT I -----
610 IF RND(12)>1 THEN RETURN
620 X(I)=RND(234)+7:Y(I)=29:T(I)=1
630 IF RND(10)<=2+INT(S/15) THEN T(I)=2
640 RETURN
700 REM ----- SLOT I REACHED THE BASKET'S LINE -----
710 Z=T(I):T(I)=0
720 IF X(I)<P-2 OR X(I)>P+25 THEN 770
730 IF Z=2 THEN 780
740 S=S+1:C=C+1:IF C>9 THEN C=9
750 PLAYWAV "TONES/TONE"+STR$(C)
760 IF S=G THEN GOSUB 850
765 GOSUB 900:GOSUB 800:RETURN
770 IF Z=1 THEN C=0
775 GOSUB 800:RETURN
780 L=L-1:C=0:GOSUB 950:GOSUB 900:GOSUB 800:RETURN
800 REM ----- THE BASKET -----
810 BOX P,112,24,8,0
820 LINE P,112,P+3,119:LINE P+23,112,P+20,119:LINE P+3,119,P+20,119
830 LINE P+1,115,P+22,115
840 RETURN
850 REM ----- NEXT LEVEL: FASTER, AND A JINGLE -----
860 V=V+1:G=G+10
870 AT 11,4:PRINT "LEVEL ";V-1;
880 TONE 5:TONE 7:TONE 9
890 BOX 0,61,250,12,0:RETURN
900 REM ----- SCORE, BEST, LIVES -----
910 AT 0,0:PRINT "SCORE ";S;"  ";
920 AT 13,0:PRINT "BEST ";H;"  ";
930 FOR J=0 TO 2:BOX 218+J*10,15,8,8,1:IF J>=L THEN BOX 219+J*10,16,6,6,0
940 NEXT J
945 LINE 0,25,249,25:RETURN
950 REM ----- A ROCK HIT: SPARKS AND THE ERROR SOUND -----
960 FOR A=0 TO 3.2 STEP 0.4
970 LINE X(I),111,X(I)+12*COS(A),111-12*SIN(A)
980 NEXT A
985 TONE 10
990 BOX X(I)-13,98,27,14,0:RETURN
1000 REM ----- GAME OVER: A FALLING SCALE -----
1010 IF S>H THEN H=S
1020 BOX 50,47,150,52,0
1030 LINE 50,47,199,47:LINE 199,47,199,98:LINE 199,98,50,98:LINE 50,98,50,47
1040 AT 11,3:PRINT "GAME OVER";
1050 AT 11,5:PRINT "SCORE ";S;
1060 FOR N=9 TO 0 STEP -3:TONE N:NEXT N
1070 IF INKEY$<>"" THEN 1070
1080 AT 10,6:PRINT "ENTER: AGAIN";
1090 K$=INKEY$:IF K$<>CHR$(13) THEN WAIT 20:GOTO 1090
1100 GOTO 300

12. Limits

Limit
Program16,384 bytes: each line takes its text plus 3
A line80 characters
Line numbers1 to 32767
Variables26, A to Z, and the array @(0) to @(1023)
DIM arraysone or two indices, indices from 0; 1,024 number elements in all, and 512 string elements (one index)
Numbers34 significant digits, exponents to ±6144
GOSUB and FOR32 deep each
Textstrings A$ to Z$: 255 characters each, 4,096 in all

13. Quick reference

CommandsRUN LIST NEW SAVE LOAD FILES ERASE VER MEM HELP CMDS EXIT
StatementsDIM PRINT ? INPUT LET IF THEN GOTO GOSUB RETURN FOR TO STEP NEXT END STOP REM
The calculatorCLS AT PSET LINE BOX CIRCLE ELLIPSE POLYGON PEN TONE PLAYMIDI STOPMIDI PLAYWAV STOPWAV WAIT DEG RAD DIGITS
FunctionsSIN COS TAN ASN ACS ATN SQR EXP LOG LOG10 ABS SGN INT RND PI KEY MIDI SIZE LEN ASC VAL INSTR LEFT$ RIGHT$ MID$ CHR$ STR$ INKEY$
Operators^ * / MOD + - = <> < > <= >= NOT AND OR
KeysSHIFT ENTER ABC, abc, NUM · SHIFT from NUM once · ▲ last commands · ▼ List mode · ▲ ▼ in a changed line move the cursor · ▼ at the end complete · SHIFT ▼ keywords · SHIFT ▲ List mode · in List mode SHIFT GTO a line number, SHIFT ← delete the line (asks) · ← delete · R/S stop · SHIFT R/S RUN · ON stop, or leave (asks)