Skip to content
Game 7 Unit 1 of 16 1 hr learning time

The Game Loop

Build the core loop: poll INKEY$, move a character, and repeat.

6% of Treasure Hunt

Every game you’ve built so far has used INPUT — the program stops and waits for you to type something. Real-time games can’t do that. They need to keep running whether or not a key is pressed.

INKEY$

INKEY$ checks the keyboard without stopping. If a key is held down, it returns that character. If nothing is pressed, it returns an empty string "". The program keeps running either way.

This is the foundation of every real-time game on the Spectrum.

The Program

10 CLS
20 LET r = 10
30 LET c = 15
40 PRINT AT r, c; "O"
50 LET k$ = INKEY$
60 IF k$ = "" THEN GO TO 50
70 PRINT AT r, c; " "
80 IF k$ = "q" THEN LET r = r - 1
90 IF k$ = "a" THEN LET r = r + 1
100 IF k$ = "o" THEN LET c = c - 1
110 IF k$ = "p" THEN LET c = c + 1
120 PRINT AT r, c; "O"
130 GO TO 50

Player character on screen

How It Works

Lines 20-30 set the player’s starting position — row 10, column 15 (roughly centre screen).

Line 40 draws the player as “O” at that position.

Lines 50-60 are the heart of the game loop. INKEY$ checks for a keypress. If nothing is pressed (""), it loops back to check again. This is polling — checking repeatedly until something happens.

Line 70 erases the old position by printing a space over it.

Lines 80-110 check which key was pressed and adjust the position:

  • Q — up (row decreases)
  • A — down (row increases)
  • O — left (column decreases)
  • P — right (column increases)

Line 120 draws the player at the new position.

Line 130 jumps back to the polling loop.

The Pattern

Every real-time game follows this structure:

  1. Wait for inputINKEY$ polling
  2. Update state — change position based on key
  3. Draw — erase old, draw new
  4. RepeatGO TO back to step 1

This is called a game loop. You’ll use this pattern for every game from now on.

Try This

Speed test. Hold down a key. The character moves one cell per keypress. Try pressing keys quickly versus holding them — INKEY$ reads the current state, so holding a key makes the character move on every loop iteration.

Different character. Change the “O” on lines 40 and 120 to something else — try ”@”, ”#”, or ”*”.

What You’ve Learnt

  • INKEY$ — reads the keyboard without stopping the program
  • Polling loop — check, act, repeat
  • Erase and redraw — print a space to clear the old position
  • Q/A/O/P — the standard Spectrum movement keys