Skip to content
Game 2 Unit 5 of 8 1 hr learning time

Cut a Wire

Read the keyboard instantly with INKEY$, convert the keypress with VAL, and check the player's choice — defused or boom.

63% of Bomb Defusal

The countdown ticks. The border turns red. The pitch climbs. But there’s nothing the player can do about it — the bomb always reaches zero. This unit changes that. Press 1, 2, 3, or 4 to cut a wire. Cut the right one and the bomb is defused. Cut the wrong one and it explodes. The game is playable after this unit.

INKEY$ — Instant Input

In Lucky Number, INPUT waited forever for the player to type a number and press ENTER. That’s fine for a guessing game, but a ticking bomb can’t wait. INKEY$ checks the keyboard right now, this instant, and returns whatever key is currently held down. If no key is pressed, it returns an empty string.

Type NEW, then:

  10 PRINT "Press 1, 2, 3 or 4"
  20 LET k$=INKEY$
  30 IF k$="" THEN GO TO 20
  40 PRINT "You pressed: ";k$

Type RUN. “Press 1, 2, 3 or 4” appears. Nothing happens until you press a key — line 30 loops back to 20 while k$ is empty. The moment you press a key, line 40 prints what you pressed.

The loop on lines 20-30 is the pattern for instant input: keep checking until a key appears. No ENTER needed. No cursor. The game reacts the moment the player’s finger hits a key.

Choosing a Wire

This short program picks a random wire and lets the player cut one:

  10 LET w=INT (RND*4)+1
  20 PRINT "Cut a wire (1-4):"
  30 LET k$=INKEY$
  40 IF k$<"1" OR k$>"4" THEN GO TO 30
  50 LET g=VAL k$
  60 IF g=w THEN PRINT "DEFUSED!": STOP
  70 PRINT "BOOM! It was wire ";w

Type RUN. Press 1, 2, 3, or 4.

DEFUSED! — the player cut the correct wire

If you’re lucky, “DEFUSED!” appears. If not, “BOOM! It was wire 3” (or whichever wire was correct). Run it again — the correct wire changes every time because RND picks a new random number.

How It Works

Line 10 picks the correct wire: LET w=INT(RND*4)+1. This gives a random whole number from 1 to 4. You saw INT(RND*100)+1 in Lucky Number — the pattern is the same, just with a smaller range.

Line 40 checks the keypress: IF k$<"1" OR k$>"4" THEN GO TO 30. This is a string comparison — it rejects anything that isn’t “1”, “2”, “3”, or “4”. The OR means “if either condition is true, go back and wait.” Letters, symbols, and numbers outside 1-4 are all ignored.

Line 50 converts the key to a number: LET g=VAL k$. INKEY$ returns a string — the character “2”, not the number 2. VAL converts that string to a number so you can compare it with w. Without VAL, you’d be comparing a string to a number, and the Spectrum would give an error.

Line 60 checks: IF g=w THEN PRINT "DEFUSED!": STOP. If the player chose the right wire, the game ends with success. Line 70 only runs if the wire was wrong — it prints BOOM and reveals the answer.

OR for Multiple Checks

The OR on line 40 lets you combine conditions. k$<"1" OR k$>"4" means “the key is below 1 or above 4.” Any key outside that range is rejected. You could also write it as four separate checks — IF k$="1" OR k$="2" OR k$="3" OR k$="4" THEN GO TO 50 — but the range check is shorter.

In the full game, the INKEY$ check sits inside the countdown loop. Each tick, the program checks for a keypress. If a valid key appears, it jumps out of the loop to check the wire. If no key appears, the countdown continues.

Try This

  • Run the program ten times. Keep track of how many you defuse. With four wires and one correct answer, you should get about 25% right — pure luck.
  • Remove the OR k$>"4" from line 40. Now any key from 1 upward is accepted. Press 5 — it’s always wrong because w is never 5. The game doesn’t crash, but it’s not fair either.
  • Change RND*4 to RND*2 on line 10. Now only wires 1 and 2 are ever correct. The odds shift — but the player doesn’t know that.

What You’ve Learnt

  • INKEY$ — returns the currently pressed key as a string, or "" if nothing is pressed
  • The polling loopLET k$=INKEY$: IF k$="" THEN GO TO checks the keyboard every frame
  • VAL — converts a string like “3” to the number 3
  • OR for combining checksk$<"1" OR k$>"4" rejects keys outside the valid range
  • RND for random choicesINT(RND*4)+1 picks 1, 2, 3, or 4 at random