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

Boundaries

Stop the player from walking off the edge of the screen.

13% of Treasure Hunt

If you press Q enough times in the previous program, the player walks off the top of the screen and the Spectrum crashes with an error. The screen has rows 0-21 and columns 0-31. Move outside that range and PRINT AT fails.

AND for Boundaries

You already know AND from Quiz Master — it combines two conditions. Here it prevents movement when the player is at an edge.

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" AND r > 1 THEN LET r = r - 1
90 IF k$ = "a" AND r < 20 THEN LET r = r + 1
100 IF k$ = "o" AND c > 0 THEN LET c = c - 1
110 IF k$ = "p" AND c < 31 THEN LET c = c + 1
120 PRINT AT r, c; "O"
130 GO TO 50

What Changed

Compare line 80 in the previous unit:

80 IF k$ = "q" THEN LET r = r - 1

With the new version:

80 IF k$ = "q" AND r > 1 THEN LET r = r - 1

The movement only happens if both conditions are true — the key is pressed and the player isn’t at the edge. If r is already 1 (top row, leaving row 0 for a HUD later), pressing Q does nothing.

The Boundaries

DirectionConditionWhy
Upr > 1Row 0 reserved for HUD
Downr < 20Row 21 reserved for border
Leftc > 0Column 0 is the left edge
Rightc < 31Column 31 is the right edge

Try This

Tighter boundaries. Change the limits to r > 3 and r < 18. The player is now confined to a smaller area in the centre of the screen.

Wrap around. Instead of stopping at the edge, make the player appear on the opposite side: IF k$ = "p" AND c >= 31 THEN LET c = 0.

What You’ve Learnt

  • AND for guards — combine a key check with a boundary check
  • Screen limits — rows 0-21, columns 0-31
  • Reserving rows — leave row 0 free for a HUD