Writing a Paddle Game in Atari BASIC: Squash, Step by Step
Paddle controllers give the Atari something a joystick can't: absolute position. Turn the knob a little and the bat moves a little; spin it and the bat crosses the screen at once. That is why bat-and-ball games felt so good on paddles. This guide builds Paddle Squash, a one-player wall game, in 48 lines of Atari BASIC. You keep the ball in play against three walls, every return scores a point, and you have five balls.
What you'll learn: how paddle readings work and how to map them onto the screen, smoothing out jitter, using a player for the bat and another for the ball, bouncing at different angles depending on where the ball hits the bat, and waiting for the paddle button.
What you need: Atari BASIC and a paddle pair in port 1, or an emulator that maps the mouse to a paddle. The program was tested on Atari BASIC revision C in atari800, started with -mouse pad: moving the mouse turns paddle 0 and the mouse button is its trigger. In Altirra, add a paddle controller to an input map and bind it to the mouse or an analog stick. For the BASIC functions themselves, see Reading Joysticks, Paddles and Fire Buttons.
1. How a paddle reading works
Each paddle is a potentiometer. POKEY measures it by timing how long a capacitor takes to charge through the knob's resistance, counting scan lines as it goes, up to a maximum of 228. The OS starts a new measurement in every vertical blank and copies the results to locations 624–631, which is what PADDLE(0) to PADDLE(7) return. So:
- A reading is 0 to 228 and is refreshed 60 times a second (50 on PAL). Reading it more often than that just returns the same number.
- The value rises as you turn the knob anti-clockwise. To make the bat move right when the knob turns right, subtract the reading from 228.
- On a standard CX30 paddle, only about half the knob's rotation changes the reading. Turned further anti-clockwise, it simply stays at 228.
- The two paddles of a pair share one port: paddles 0 and 1 are port 1.
PTRIG(0)reads paddle 0's button, 0 when pressed.
Readings also wobble by a count or so even when the knob is still. A game that copies them straight to the screen gets a bat that shivers.
2. The court and the players
1000 REM SET UP SCREEN AND PLAYERS
1010 A=PEEK(106)-8:POKE 106,A
1020 GRAPHICS 0:POKE 752,1:SETCOLOR 2,0,0:SETCOLOR 4,0,0
1030 PM=A*256:POKE 54279,A:POKE 559,46:POKE 53277,3
1040 FOR I=PM+512 TO PM+767:POKE I,0:NEXT I
1050 POKE 53256,1:POKE 704,26:POKE 705,14
1060 POKE PM+612,255:POKE PM+613,255
1070 B=PM+640:K=128/228:OX=0
1080 FOR I=1 TO 38:POSITION I,1:? CHR$(160);:NEXT I
1090 FOR I=2 TO 22:POSITION 1,I:? CHR$(160);:POSITION 38,I:? CHR$(160);:NEXT I
1100 RETURN
Line 1010 lowers RAMTOP (location 106) by 8 pages, 2K, to make room for player/missile memory above BASIC. Line 1020 then reopens the screen below the new top, which is why GRAPHICS must come after the POKE. With a 48K machine and BASIC, RAMTOP starts at 160, so the new value 152 is a multiple of 4: exactly the 1K boundary that double-line players need. Line 1030 points PMBASE (54279) at it, turns on player and missile DMA in double-line mode with the playfield (559 = 46), and enables player display in GRACTL (53277).
Only players 0 and 1 are used, so line 1040 clears just their 256 bytes (offsets 512–767). A POKE loop is slow in BASIC, but this one runs only once.
- The bat is player 0. POKE 53256,1 makes it double width, so the 8-bit shape
255becomes a 16-colour-clock bar. Two rows of 255 at offsets 612 and 613 (offset 100 within the player) put it near the bottom. It never moves vertically, so this is the only time it is drawn. - The ball is player 1.
Bholds the start of its 128-byte strip. Its shape is two rows of192(binary 11000000), a small square two colour clocks wide. - The walls are inverse spaces,
CHR$(160), printed along row 1 and down columns 1 and 38. The trailing semicolons stop PRINT from moving to a new line, which would scroll the screen.
Those character positions set the limits used later. Column c starts at horizontal position 48 + 4c, so the inside of the court runs from 56 to 199. Row r starts at player byte 16 + 4r, so the space below the top wall starts at byte 24.
3. From knob to bat
210 P=PADDLE(0):X=56+INT((228-P)*K)
220 IF ABS(X-OX)>1 THEN POKE 53248,X:OX=X
K is 128/228, computed once in line 1070 so the loop multiplies rather than divides. 228-P turns the reading round, so clockwise means right. Scaling by K maps the full 0–228 range onto 0–128, and adding 56 places the bat between the left wall and position 184, where its right edge meets the right wall. The arithmetic guarantees the bat can never leave the court, with no IF tests needed.
Line 220 is the jitter filter. The bat only moves when the new position differs from the old one, OX, by more than one. A one-count wobble is ignored, while real movement still reaches the screen. Horizontal movement of a player is just one POKE to its position register (53248 for player 0), so it is instant even in BASIC.
4. Moving the ball without flicker
230 NX=BX+DX:NY=BY+DY
240 IF NX<56 OR NX>198 THEN DX=-DX:NX=BX+DX:SOUND 0,80,10,6
250 IF NY<24 THEN DY=-DY:NY=BY+DY:SOUND 0,80,10,6
260 POKE B+BY,0:POKE B+BY+1,0:BX=NX:BY=NY
270 POKE B+BY,192:POKE B+BY+1,192:POKE 53249,BX
BX,BY is the ball's position and DX,DY its speed. The program works out the new position first, bouncing off the side walls (reverse DX) and the top wall (reverse DY), each with a click. Only then does it erase the old ball and draw the new one, in two consecutive lines.
The order matters. The first version of this program erased the ball at the start of the loop and redrew it at the end, with the bounce tests in between. Because Atari BASIC takes a noticeable time per line, the ball spent much of each pass invisible, and in the first test screenshot it was missing altogether. Doing the calculations first keeps the gap between erase and redraw as short as possible.
5. Hitting the ball, and angles
280 IF BY>=98 THEN GOSUB 500:IF L=0 THEN 600
...
500 REM BALL AT THE BAT
510 IF BX+1<OX OR BX>OX+15 THEN 550
520 DY=-DY:DX=INT((BX-OX-7)/3):IF DX=0 THEN DX=1
530 S=S+1:SOUND 0,40,10,10:GOSUB 800:RETURN
When the ball reaches byte 98, just above the bat at 100, line 510 checks whether any part of it overlaps the bat's 16 colour clocks, from OX to OX+15. This compares positions directly rather than using the collision registers. In BASIC, you know exactly where everything is, and you avoid the timing traps of hardware collisions.
Line 520 is what makes it a game rather than a screensaver. The ball goes back up, and its sideways speed depends on where it hit: BX-OX-7 runs from about −8 at the left end of the bat to +8 at the right, and dividing by 3 gives a DX between −3 and 2. A ball that hits the edge flies off at a sharp angle, while a central hit sends it up nearly straight. A DX of 0 is changed to 1, so the ball never gets stuck bouncing vertically for ever. That is the control a paddle player expects: you aim with the bat.
Notice the :IF L=0 THEN 600 after the GOSUB on line 280. When a subroutine returns, BASIC carries on with the rest of the line, so the test runs straight after each bat check.
6. Missing, serving and game over
550 POKE B+BY,0:POKE B+BY+1,0:L=L-1:SOUND 0,200,8,12:GOSUB 800
560 FOR D=1 TO 200:NEXT D:SOUND 0,0,0,0
570 IF L>0 THEN GOSUB 900
580 RETURN
...
900 REM SERVE A NEW BALL
910 BX=60+INT(RND(0)*130):BY=24:DY=2:DX=1:IF RND(0)<0.5 THEN DX=-1
920 RETURN
A miss erases the ball, costs a ball, buzzes and pauses briefly. A new ball is served from the top at a random column, heading left or right at random. When the balls run out, the game-over code waits for the paddle button with PTRIG(0). As always, it waits first for the button to be released (line 640) and then pressed (650), so a button still held from play can't skip the message.
7. The complete program
10 REM PADDLE SQUASH - ATARI BASIC
20 GOSUB 1000:HI=0
100 REM NEW GAME
110 S=0:L=5:GOSUB 800
120 GOSUB 900
200 REM MAIN LOOP
210 P=PADDLE(0):X=56+INT((228-P)*K)
220 IF ABS(X-OX)>1 THEN POKE 53248,X:OX=X
230 NX=BX+DX:NY=BY+DY
240 IF NX<56 OR NX>198 THEN DX=-DX:NX=BX+DX:SOUND 0,80,10,6
250 IF NY<24 THEN DY=-DY:NY=BY+DY:SOUND 0,80,10,6
260 POKE B+BY,0:POKE B+BY+1,0:BX=NX:BY=NY
270 POKE B+BY,192:POKE B+BY+1,192:POKE 53249,BX
280 IF BY>=98 THEN GOSUB 500:IF L=0 THEN 600
290 SOUND 0,0,0,0:GOTO 210
500 REM BALL AT THE BAT
510 IF BX+1<OX OR BX>OX+15 THEN 550
520 DY=-DY:DX=INT((BX-OX-7)/3):IF DX=0 THEN DX=1
530 S=S+1:SOUND 0,40,10,10:GOSUB 800:RETURN
550 POKE B+BY,0:POKE B+BY+1,0:L=L-1:SOUND 0,200,8,12:GOSUB 800
560 FOR D=1 TO 200:NEXT D:SOUND 0,0,0,0
570 IF L>0 THEN GOSUB 900
580 RETURN
600 REM GAME OVER
610 IF S>HI THEN HI=S
620 GOSUB 800:POSITION 13,10:? "G A M E O V E R"
630 POSITION 9,12:? "PRESS THE PADDLE BUTTON"
640 IF PTRIG(0)=0 THEN 640
650 IF PTRIG(0)=1 THEN 650
660 POSITION 9,10:? " "
670 POSITION 9,12:? " "
680 GOTO 100
800 REM SCORE LINE
810 POSITION 2,0:? "SCORE ";S;" BALLS ";L;" BEST ";HI;" ";:RETURN
900 REM SERVE A NEW BALL
910 BX=60+INT(RND(0)*130):BY=24:DY=2:DX=1:IF RND(0)<0.5 THEN DX=-1
920 RETURN
1000 REM SET UP SCREEN AND PLAYERS
1010 A=PEEK(106)-8:POKE 106,A
1020 GRAPHICS 0:POKE 752,1:SETCOLOR 2,0,0:SETCOLOR 4,0,0
1030 PM=A*256:POKE 54279,A:POKE 559,46:POKE 53277,3
1040 FOR I=PM+512 TO PM+767:POKE I,0:NEXT I
1050 POKE 53256,1:POKE 704,26:POKE 705,14
1060 POKE PM+612,255:POKE PM+613,255
1070 B=PM+640:K=128/228:OX=0
1080 FOR I=1 TO 38:POSITION I,1:? CHR$(160);:NEXT I
1090 FOR I=2 TO 22:POSITION 1,I:? CHR$(160);:POSITION 38,I:? CHR$(160);:NEXT I
1100 RETURN
Type it in, or ENTER it from a text file, and RUN. On a real Atari, plug a paddle pair into port 1 and use the left paddle of the pair (paddle 0).
8. Where to take it next
- Two players: add a second bat as player 2 at the top of the court, controlled by
PADDLE(1), the other paddle of the same pair, and turn it into Pong. Remove the top wall and check the ball against the top bat. - Bricks: print a few rows of characters across the top. When the ball reaches a brick row,
LOCATEthe character at the ball's position and, if it is a brick, erase it and bounce. That is the core of a Breakout clone. - Speed: BASIC limits how fast the ball can move smoothly. Increase DY to 3 or 4 after every ten points for difficulty, or move the loop to Turbo-BASIC XL or FastBasic for much smoother motion. The paddle code carries over unchanged.
- A better bat: a three-row shape, a different colour while the ball is near, or a quadruple-width bat (POKE 53256,3) for an easy mode.
Troubleshooting
- The bat doesn't move: check that the paddles are in port 1 and that you are turning paddle 0, not paddle 1. In an emulator, make sure mouse-to-paddle mode is enabled.
- The bat moves the wrong way: you used
PADDLE(0)directly instead of228-PADDLE(0). - The bat shakes when the knob is still: increase the jitter threshold in line 220, or average two readings.
- Garbage instead of the ball or bat: PMBASE is not on a 1K boundary, or the player memory wasn't cleared. Make sure GRAPHICS comes after the RAMTOP change.
- Memory shrinks every time you RUN: each run lowers RAMTOP by another 2K. Turn the computer off and on between long test sessions, or remember the original value and only lower it once.
Further reading
- Ian Chadwick, Mapping the Atari: locations 106 (RAMTOP), 559, 624–631 (PADDL0–7), 636–643 (PTRIG0–7), 704–705, 53248–53256 and 54279.
- Avery Lee, Altirra Hardware Reference Manual, chapters on the POKEY paddle scan and on paddle controllers.
- On this site: Reading Joysticks, Paddles and Fire Buttons, Player/Missile Graphics in Atari BASIC, Writing a Complete Game in Atari BASIC, Step by Step, Sound Effects Recipes for Atari BASIC.
Credits
- Picture: Paddle Squash running in atari800, with the mouse standing in for a paddle. — Program authors; screenshot by the site
- Mapping the Atari — 106 RAMTOP, 559 SDMCTL, 624-631 PADDL0-7, 636-643 PTRIG0-7, 704 PCOLR0, 53248 HPOSP0, 53256 SIZEP0, 53277 GRACTL, 54279 PMBASE — AtariArchives.org
- Altirra Hardware Reference Manual (Avery Lee) — 5.9 Paddle scan; 7.2 Paddle — Avery Lee (Altirra)
- Altirra help — input maps, paddle controllers and analog mappings — Screenshot from Avery Lee's Altirra page, virtualdub.org
- De Re Atari — Chapter 4, Player-Missile Graphics (memory layout) — Chris Crawford (Section 4 of De Re Atari)
