Skip to content
Game 7 Unit 2 of 6 1 hr learning time

Drive It with the Stick

Open a loop that reads the joystick on every pass, decode the direction, and move the rover — erase the old block, draw the new one. PEEK(56320), an AND to pick the bit, two POKEs. The first time the machine answers you in real time.

33% of Rover

Now the rover comes alive. Open a loop that, on every pass, reads the joystick and moves the rover to match. Hold the stick and it rolls; let go and it stops. This is the first time in the volume the machine answers you the instant you act.

10 POKE 53281,0
20 PRINT CHR$(147)
30 R=12:C=20
40 POKE 1024+R*40+C,160:POKE 55296+R*40+C,7
50 J=PEEK(56320)
60 NR=R:NC=C
70 IF (J AND 1)=0 THEN NR=R-1
80 IF (J AND 2)=0 THEN NR=R+1
90 IF (J AND 4)=0 THEN NC=C-1
100 IF (J AND 8)=0 THEN NC=C+1
110 IF NR=R AND NC=C THEN 50
120 POKE 1024+R*40+C,32
130 R=NR:C=NC
140 POKE 1024+R*40+C,160:POKE 55296+R*40+C,7
150 GOTO 50
A C64 screen, black background, the rover block driven up and to the right of centre.
The rover, driven: pushed right and up with the stick, it travelled there a cell at a time. Hold a direction and it keeps going; release and it stops where it is.

The loop is lines 50–150, and it does three things each pass. Read the stick: line 50, J = PEEK(56320), reads control port 2, where the joystick lives. Decode the direction: the bits are active-low — holding a direction clears its bit — so you test with AND. Lines 70–100 read up (J AND 1), down (2), left (4) and right (8); a result of 0 means that direction is held, so the new row NR or column NC shifts by one. Move: if the position changed, line 120 erases the old cell (POKE a space), then lines 130–140 update R/C and draw the rover at its new spot. That's erase-move-draw — clear where it was, draw where it is.

Then GOTO 50 runs it all again, dozens of times a second. The rover follows the stick because the loop never stops asking what the stick is doing. The bit-testing looks fiddly, but it's the C64's real-time input in three lines — the same PEEK(56320) every action game on the machine leans on.

Try this

  • Change the feel. The rover moves one cell per pass, as fast as the loop runs. Add a tiny delay — FOR D=1 TO 20:NEXT — inside the loop to slow it to a steady crawl.
  • One axis only. Comment out the left/right tests (lines 90–100) and the rover only goes up and down. Proof each direction is its own independent bit.

What's next

The rover drives anywhere — including straight off the screen. In Unit 3 walls give the world edges, and a look-ahead PEEK stops the rover dead when it meets one.