Writing a Complete Game in Atari BASIC, Step by Step

Black Atari text screen with SCORE 0 and LIVES 3 at the top, falling star and hash characters, and a V-shaped catcher at the bottom

Most first games stall on structure, not on any single command. Where does the main loop go? How do several objects move at once? Why does the game run at different speeds at different moments? This guide builds a complete, playable game in 44 lines of Atari BASIC. Each part is explained as it is added, so you can reuse the skeleton for your own ideas.

The game is Star Catcher. Stars (*) and rocks (#) fall from the top of the screen, and you steer a catcher along the bottom with the joystick. A star is worth 10 points, and catching a rock costs one of three lives.

What you'll learn: planning a game, a constant-speed main loop, drawing directly into screen memory, moving several objects with arrays, collisions by comparing positions, scoring, sound, game over and restart, and a title screen.

What you need: Atari BASIC and a joystick (or an emulator with a keyboard joystick). The final program was run in atari800, as in the screenshot.

1. Plan before typing

Write down, in a few lines, what the game needs:

  • A screen: GRAPHICS 0 text, 40 × 24. Row 0 is the score line and row 23 the catcher.
  • Objects: the catcher (a position P from 0 to 37) and four falling objects, each with a column X, a row Y and a kind K.
  • A loop that runs once per "tick": read the stick, move the catcher, move every object down a row, check for catches, wait for the next tick.
  • States: title, playing, game over.

A plan like this keeps BASIC programs, which have no named procedures, from turning into a tangle. Give each part its own block of line numbers: 100s for setup, 200s for the main loop, 600s for game over, 700s and 800s for drawing routines, 900s for the title.

2. Drawing fast: screen memory

PRINT, POSITION and LOCATE all go through the operating system's I/O routines, which makes them slow. For things that move every tick, write characters straight into screen memory. Its address is in locations 88 and 89:

120 SC=PEEK(88)+256*PEEK(89):S=0:L=3:P=18

Row r, column c of a GRAPHICS 0 screen is at SC+r*40+c. Screen memory holds internal character codes, not ATASCII (see ATASCII and Keyboard Codes). The ones used here are 10 for *, 3 for #, 0 for a blank, and 60, 63 and 15 for \, _ and /, which make up the catcher:

700 REM DRAW THE CATCHER
710 POKE SC+920+P,60:POKE SC+921+P,63:POKE SC+922+P,15:RETURN

920 is 23×40, the start of the bottom row. PRINT is still fine for things that change rarely, such as the score line:

800 REM SCORE LINE
810 POSITION 2,0:? "SCORE ";S;"   LIVES ";L;" ";:RETURN

The final semicolon matters. Without it the PRINT would move the cursor down a line, and on some rows that would scroll the screen. The trailing space wipes out the old digit when lives go from 10 to 9. Here it's just insurance.

3. Moving the player

220 J=STICK(0):IF J=11 AND P>0 THEN POKE SC+922+P,0:P=P-1:GOSUB 700
230 IF J=7 AND P<37 THEN POKE SC+920+P,0:P=P+1:GOSUB 700

To move left, blank the catcher's rightmost character, decrease P and redraw. Moving right blanks the leftmost character instead. Only one character changes on the screen, so there's no flicker. The limits keep the three-character catcher on the screen.

4. Many objects with arrays

Four falling objects share one piece of code by keeping their state in arrays X(), Y() and K():

130 FOR I=1 TO 4:Y(I)=-I*6:NEXT I
240 FOR I=1 TO 4
250 IF Y(I)>0 THEN POKE SC+Y(I)*40+X(I),0
260 Y(I)=Y(I)+1:IF Y(I)<1 THEN 330
270 IF Y(I)=1 THEN X(I)=INT(RND(0)*40):K(I)=10:IF RND(0)<0.3 THEN K(I)=3
280 IF Y(I)<23 THEN POKE SC+Y(I)*40+X(I),K(I):GOTO 330

Two tricks make this short:

  • Negative rows mean "waiting". Line 130 starts the objects at rows −6, −12, −18 and −24, so they enter one after another instead of in a line. An object below row 1 just counts up (line 260) and isn't drawn.
  • An object is born when it reaches row 1 (line 270). It gets a random column and is a star, unless a 30% chance makes it a rock. The IF ... THEN ...:IF ... chain works because everything after THEN runs only if the condition is true.

Each tick, each object is erased (250), moved down (260) and drawn again (280), until it reaches row 23, the catcher's row.

5. Collisions without collision registers

The game doesn't need LOCATE or hardware collisions. It knows where everything is, so a catch is a comparison:

290 IF X(I)<P OR X(I)>P+2 THEN 320
300 IF K(I)=10 THEN S=S+10:SOUND 0,40,10,8:GOSUB 800
310 IF K(I)=3 THEN L=L-1:SOUND 0,200,8,10:GOSUB 800:GOSUB 700
320 Y(I)=-INT(RND(0)*8)
330 NEXT I

If the object's column is within the catcher's three characters, it counts. A star adds score with a high beep, and a rock costs a life with a low buzz. Either way, line 320 sends the object back above the screen with a random delay, ready to fall again. Line 310 redraws the catcher, in case anything disturbed it.

6. A steady game loop

210 T=PEEK(20)
...
340 SOUND 0,0,0,0:IF L=0 THEN 600
350 IF PEEK(20)=T THEN 350
360 GOTO 210

Location 20 ticks 60 times a second (50 on PAL). Noting it at the top of the loop and waiting at the bottom until it changes stops the game from ever running faster than the display. In BASIC, the loop body takes longer than one frame anyway, so the real benefit appears when you make it simpler or move it to a faster BASIC. Turning the sound off once per tick makes each catch a short click. A longer sound would need a counter.

7. Game over, restart and the title

600 REM GAME OVER
610 POSITION 15,10:? "GAME OVER";
620 POSITION 8,12:? "PRESS FIRE TO PLAY AGAIN";
630 IF STRIG(0)=0 THEN 630
640 IF STRIG(0) THEN 640
650 GOTO 100

Line 630 waits for the fire button to be released first, so a player who was still holding fire when the last life went doesn't skip straight past the message. Then line 640 waits for a fresh press. GOTO 100 starts a new game. Because the setup code resets every variable (S, L, P and the rows), nothing from the old game carries over.

The title uses GRAPHICS 2+16, a full screen of large text. Lower-case letters in a PRINT #6 come out in a different colour in modes 1 and 2:

900 REM TITLE SCREEN
910 GRAPHICS 2+16:SETCOLOR 4,9,2
920 POSITION 3,2:? #6;"STAR CATCHER"
930 POSITION 2,5:? #6;"catch the stars"
940 POSITION 1,6:? #6;"DODGE THE ROCKS"
950 POSITION 2,9:? #6;"PRESS FIRE"
960 IF STRIG(0) THEN 960
970 IF STRIG(0)=0 THEN 970
980 RETURN

8. The complete program

10 REM STAR CATCHER - A SMALL GAME IN ATARI BASIC
20 DIM X(4),Y(4),K(4)
30 GOSUB 900
100 REM NEW GAME
110 GRAPHICS 0:POKE 752,1:SETCOLOR 2,0,0:SETCOLOR 1,0,14
120 SC=PEEK(88)+256*PEEK(89):S=0:L=3:P=18
130 FOR I=1 TO 4:Y(I)=-I*6:NEXT I
140 GOSUB 800:GOSUB 700
200 REM MAIN LOOP
210 T=PEEK(20)
220 J=STICK(0):IF J=11 AND P>0 THEN POKE SC+922+P,0:P=P-1:GOSUB 700
230 IF J=7 AND P<37 THEN POKE SC+920+P,0:P=P+1:GOSUB 700
240 FOR I=1 TO 4
250 IF Y(I)>0 THEN POKE SC+Y(I)*40+X(I),0
260 Y(I)=Y(I)+1:IF Y(I)<1 THEN 330
270 IF Y(I)=1 THEN X(I)=INT(RND(0)*40):K(I)=10:IF RND(0)<0.3 THEN K(I)=3
280 IF Y(I)<23 THEN POKE SC+Y(I)*40+X(I),K(I):GOTO 330
290 IF X(I)<P OR X(I)>P+2 THEN 320
300 IF K(I)=10 THEN S=S+10:SOUND 0,40,10,8:GOSUB 800
310 IF K(I)=3 THEN L=L-1:SOUND 0,200,8,10:GOSUB 800:GOSUB 700
320 Y(I)=-INT(RND(0)*8)
330 NEXT I
340 SOUND 0,0,0,0:IF L=0 THEN 600
350 IF PEEK(20)=T THEN 350
360 GOTO 210
600 REM GAME OVER
610 POSITION 15,10:? "GAME OVER";
620 POSITION 8,12:? "PRESS FIRE TO PLAY AGAIN";
630 IF STRIG(0)=0 THEN 630
640 IF STRIG(0) THEN 640
650 GOTO 100
700 REM DRAW THE CATCHER
710 POKE SC+920+P,60:POKE SC+921+P,63:POKE SC+922+P,15:RETURN
800 REM SCORE LINE
810 POSITION 2,0:? "SCORE ";S;"   LIVES ";L;" ";:RETURN
900 REM TITLE SCREEN
910 GRAPHICS 2+16:SETCOLOR 4,9,2
920 POSITION 3,2:? #6;"STAR CATCHER"
930 POSITION 2,5:? #6;"catch the stars"
940 POSITION 1,6:? #6;"DODGE THE ROCKS"
950 POSITION 2,9:? #6;"PRESS FIRE"
960 IF STRIG(0) THEN 960
970 IF STRIG(0)=0 THEN 970
980 RETURN

9. Where to take it next

  • Difficulty: add a fifth and sixth object as the score rises (DIM the arrays larger), or make rocks more likely.
  • Better graphics: redefine *, # and the catcher as proper shapes with a custom character set (see Redefined Character Sets), or use a player for the catcher so it moves smoothly (see Player/Missile Graphics in Atari BASIC).
  • Speed: BASIC spends most of each tick in the FOR loop. Fewer multiplications help, for example by keeping each object's screen address in an array instead of recomputing SC+Y(I)*40+X(I). See Making Atari BASIC Faster (and Smaller). Turbo-BASIC XL, which runs most Atari BASIC programs unchanged, is another easy speed-up.
  • High score: keep the best score in a variable that the restart code doesn't reset, and show it on the title screen.
  • A menu: let the player choose a starting speed with the joystick (see Designing a Joystick-Driven Menu in Atari BASIC).

Troubleshooting

  • Characters appear as the wrong symbols: you POKEd ATASCII codes into screen memory. Convert to internal codes.
  • Debris left on screen: an object was drawn but not erased before it moved. Check that every path through the loop erases first.
  • The catcher disappears: something POKEd a blank into row 23. Redraw it after any event that might touch that row.
  • The game skips the title or restarts immediately: wait for fire to be released before waiting for a press.
  • ERROR 9: an array index went past its DIM. The arrays must be DIMmed for the number of objects.

Further reading

  • Jeffrey Stanton and Dan Pinal, Atari Graphics and Arcade Game Design (Arrays, Inc.).
  • On this site: Designing a Joystick-Driven Menu in Atari BASIC, Sound Effects Recipes for Atari BASIC, Collision Detection in Depth.

Credits