Designing a Joystick-Driven Menu in Atari BASIC
A game that is played with the joystick shouldn't send the player to the keyboard to choose a level or start again. Al Baker made this point in his "Programming Hints" column, reprinted in COMPUTE!'s First Book of Atari. When several players sit away from the computer, a yes/no question answered with the joystick is far more convenient than one answered from the keyboard. This guide builds the standard joystick menu step by step, then a numeric selector with auto-repeat, and shows the small details that make menus feel solid rather than twitchy.
What you'll learn: reading STICK and STRIG for menus, highlighting with inverse video, why you must wait for the stick to return to centre, adding audible feedback, auto-repeat with the system clock, and keyboard fallbacks.
What you need: Atari BASIC and a joystick in port 1. In an emulator, map the joystick to keys. Both programs here were run in atari800 with its keypad joystick. Reading Joysticks, Paddles and Fire Buttons explains the STICK values in detail.
1. The values you need
STICK(0) returns 15 when the stick is centred, 14 for up, 13 for down, 11 for left and 7 for right. The diagonals are 10, 6, 9 and 5. STRIG(0) is 0 while the button is held and 1 otherwise, which trips people up because 0 usually means "no". Both come from shadow registers updated 60 times a second, so a BASIC loop can't miss a press that lasts more than a frame or two.
2. A complete menu
10 REM JOYSTICK MENU
20 DIM M$(60),T$(12),I$(12):N=5:W=12
30 M$="NEW GAME LOAD GAME OPTIONS HIGH SCORES QUIT "
40 GRAPHICS 0:POKE 752,1:SETCOLOR 2,9,2
50 POSITION 12,3:? "MAIN MENU"
60 FOR I=1 TO N:GOSUB 300:NEXT I
70 S=1:I=S:GOSUB 400
100 REM WAIT FOR STICK OR TRIGGER
110 J=STICK(0):IF J=14 AND S>1 THEN I=S:GOSUB 300:S=S-1:I=S:GOSUB 400:GOSUB 500
120 IF J=13 AND S<N THEN I=S:GOSUB 300:S=S+1:I=S:GOSUB 400:GOSUB 500
130 IF STRIG(0)=0 THEN 200
140 GOTO 110
200 SOUND 0,60,10,8:FOR D=1 TO 10:NEXT D:SOUND 0,0,0,0
210 POSITION 8,19:? "YOU CHOSE: ";M$((S-1)*W+1,S*W)
220 IF STRIG(0)=0 THEN 220
230 GOTO 110
300 REM DRAW ITEM I NORMALLY
310 POSITION 12,5+I*2:? M$((I-1)*W+1,I*W);:RETURN
400 REM DRAW ITEM I IN INVERSE
410 T$=M$((I-1)*W+1,I*W)
420 FOR K=1 TO W:T$(K,K)=CHR$(ASC(T$(K,K))+128):NEXT K
430 POSITION 12,5+I*2:? T$;:RETURN
500 REM CLICK, THEN WAIT FOR THE STICK TO CENTRE
510 SOUND 0,20,10,6:SOUND 0,0,0,0
520 IF STICK(0)<>15 THEN 520
530 RETURN
Walking through it:
- The items live in one string (line 30), each padded to exactly 12 characters. Atari BASIC has no string arrays, so item I is the substring
M$((I-1)*W+1,I*W). Padding every item to the same width also means the highlight bar is the same length for each, and a short item fully overwrites a long one. - Two drawing routines. Line 300 prints an item normally, and line 400 prints it in inverse video by adding 128 to each character code (bit 7 is the inverse bit). Moving the selection means redrawing the old item normally and the new one inverted. Nothing else on the screen changes, so there is no flicker.
- Limits.
S>1andS<Nstop the bar at the top and bottom. To wrap around instead, set S to N when it would go below 1, and to 1 when it would pass N. - Wait for centre (line 520). This is the most important line. Without it, one push of the stick would race through several items, because the loop runs many times while the stick is held. Waiting until STICK returns 15 makes each push move exactly one step, however long it is held.
- Feedback. A short click on every move (line 510) and a different tone on selection (line 200) tell the player the program heard them, which matters on a real TV where the bar may be hard to see.
- Wait for fire to be released (line 220) before going round again, or one press would select the same item repeatedly.
POKE 752,1 hides the cursor, so no stray block appears after each PRINT. It has to come after GRAPHICS 0, which turns the cursor back on.
In our test, two pushes down and a press of fire left OPTIONS highlighted and printed YOU CHOSE: OPTIONS, as in the screenshot.
3. Variations
- Left/right choices. Baker's yes/no routine used the same pattern horizontally: YES on the left and NO on the right, the default shown in inverse, left and right to move, fire to accept. Use 11 and 7 instead of 14 and 13.
- Several players. Pass the player number in a variable and read
STICK(P)andSTRIG(P). Baker's routines took the player as a parameter, so whoever was asked could answer from their own joystick. - Keyboard and console keys too. Accept RETURN (
PEEK(764)=12) or START (PEEK(53279)=6) as alternatives to fire, so that people without a joystick, or with the stick in the wrong port, aren't stuck. Clear 764 withPOKE 764,255after reading it. - Arrow-shaped cursor. Instead of inverting the whole item, print a
>(or a redefined character) in the column to the left and erase it from the old row.
4. Numbers: a selector with auto-repeat
For values such as a difficulty level, the stick's left and right should change the number. A single push should step by one, and holding the stick should start repeating after a short delay, just like the keyboard:
10 REM JOYSTICK NUMBER SELECTOR WITH AUTO-REPEAT
20 GRAPHICS 0:POKE 752,1:V=5:R=20
30 POSITION 8,5:? "LEVEL (LEFT/RIGHT): ";V;" "
40 J=STICK(0):IF J=15 THEN R=20:GOTO 90
50 IF J=11 AND V>1 THEN V=V-1
60 IF J=7 AND V<99 THEN V=V+1
70 POSITION 8,5:? "LEVEL (LEFT/RIGHT): ";V;" "
80 T=PEEK(20):GOSUB 200:R=4
90 IF STRIG(0) THEN 40
100 POSITION 8,8:? "STARTING AT LEVEL ";V:END
200 D=PEEK(20)-T:IF D<0 THEN D=D+256
210 IF D<R AND STICK(0)<>15 THEN 200
220 RETURN
The delay comes from the system clock, not an empty FOR loop. Location 20 counts sixtieths of a second (fiftieths on PAL) and wraps from 255 to 0, which line 200 allows for. After the first step the program waits up to 20 jiffies (a third of a second). After that each step waits only 4, so holding the stick runs the number up quickly. Letting go resets the delay (line 40), and the wait also ends early if the stick is released, so taps feel immediate. The extra space after the number in lines 30 and 70 erases the last digit when the value drops from 10 to 9. Three taps right and one left took the value from 5 to 7 in our test.
FOR loops are a poor clock in Atari BASIC: their speed depends on where the loop sits in the program, and John Victor warned about exactly this when timing narration. The clock gives the same feel in any program.
5. Making it feel right
- Respond on the press, not the release, but guard against repeats: move once, then wait for centre or for the repeat timer.
- Show the default. Highlight the most likely choice when the menu appears, as Baker did with YES.
- Keep the highlight where the player left it when they return to a menu, by not resetting S.
- Debounce fire separately from the stick. A player often pushes the stick and fire at the same moment.
- Attract mode. The OS only cancels attract-mode colour cycling on a key press. A menu that waits a long time for the joystick should
POKE 77,0now and then, or the colours will start drifting after nine minutes.
Troubleshooting
- The bar jumps several items per push: the wait-for-centre step is missing.
- Selecting fires twice: wait for STRIG to return to 1 after a selection.
- Nothing happens on fire: remember that
STRIG(0)=0means pressed. - Highlight leaves junk behind: pad all items to the same width, and redraw the old item before the new one.
- Joystick works in one port only: you read STICK(0). Use STICK(1) for port 2, or both.
Further reading
- Al Baker, "Programming Hints: Apple and Atari", COMPUTE!'s First Book of Atari (1981): joystick yes/no and number-input routines.
- On this site: Reading Joysticks, Paddles and Fire Buttons, Reading the Keyboard, Console Keys and HELP, Keeping Time: RTCLOK, System Timers and POKEY Hardware Timers.
Credits
- Picture: The example menu after moving the joystick down twice and pressing fire. — Program authors; screenshot by the site
- Al Baker, "Al Baker's Programming Hints: Apple and Atari", COMPUTE!'s First Book of Atari (joystick values, two-entry menu select, number select) — Al Baker; AtariArchives.org
- John Victor, "Adding a Voice Track to Atari Programs" (FOR-loop timing is not linear; use RTCLOK) — John Victor; AtariArchives.org
- Atari Operating System User's Manual — attract mode (ATRACT, keyboard-only reset, 9 minutes) — ksquiggle
- Mapping the Atari — 20 RTCLOK, 77 ATRACT, 752 CRSINH, 764 CH, 53279 CONSOL — AtariArchives.org
