Counting on You
What you'll learn:
- Store numbers in variables and update them with arithmetic.
- Track scores, lives, and other game stats over time.
- Use string variables to remember player names or messages.
Lesson 4 – Counting on You
Every game tracks something: score, lives, fuel, gold pieces, goat encounters. Variables are the boxes where you stash those numbers (and strings) so the computer remembers them between lines. Time to start keeping score like a 1984 arcade machine.
[📷 suggested: screenshot of a simple scoreboard showing NAME and SCORE]
The One-Minute Tour
- Variables are names that hold values—numbers or strings.
- Assign with
=
:SCORE = 0
,NAME$ = "SAM"
. - Update by reading the current value:
SCORE = SCORE + 10
. - Numbers use plain names (
TOTAL
), strings end with$
(NAME$
).
Example Program
NEW
10 PRINT "WELCOME, PILOT"
20 INPUT "WHAT IS YOUR NAME"; NAME$
30 SCORE = 0
40 PRINT "HELLO "; NAME$; "!"
50 SCORE = SCORE + 10
60 PRINT "YOUR SCORE IS "; SCORE
Sample output:
WELCOME, PILOT
WHAT IS YOUR NAME?SAM
HELLO SAM!
YOUR SCORE IS 10
READY.
Line 20 stores the player’s name in NAME$
.
Line 30 starts SCORE
at zero.
Line 50 adds 10 more points by reading the old value and writing it back.
Tip: BASIC automatically treats new variable names as zero (or empty). Explicitly setting them, like
SCORE = 0
, keeps your intent clear.
Experiment Section
- Start
SCORE
at 100 instead of 0 and watch the totals shift. - Add a penalty:
SCORE = SCORE - 5
before line 60. - Print a bonus message when the score reaches 50:
IF SCORE >= 50 THEN PRINT "LEVEL UP!"
. - Introduce lives:
LIVES = 3
andLIVES = LIVES - 1
after a “danger” message. - Mix in another player by adding
INPUT "RIVAL"; RIVAL$
and tracking two scores side-by-side.
[🎥 suggested: clip showing score increments and a bonus message appearing]
Concept Expansion
Variables remember state so your program doesn’t have to start from scratch each line. Later you’ll use arrays (DIM
) to store groups of related values and DATA
statements to preload level layouts. Assembly lessons connect this idea to actual memory addresses—today we’re enjoying BASIC’s friendlier labels.
Game Integration
- Scoreboard:
SCORE = SCORE + POINTS
every time the player succeeds. - Lives:
IF HIT THEN LIVES = LIVES - 1
andIF LIVES = 0 THEN GOTO GAME_OVER
. - Timers:
TICKS = TICKS + 1
inside your main loop, paired withIF TICKS = 60
for once-a-second events. - Inventory:
HAS_KEY = 1
to unlock doors later in the adventure.
From the Vault
- Commodore 64 — revisit how the interpreter stores variable tables in memory so your score persists between lines.
Quick Reference
REM Variable essentials
SCORE = 0
LIVES = 3
NAME$ = "ARTHUR"
SCORE = SCORE + 10
IF LIVES <= 0 THEN PRINT "GAME OVER"
- Numeric variables hold whole numbers or decimals (BASIC stores them as floating-point).
- String variables must end with
$
. - Spaces are optional:
SCORE=SCORE+1
works, but spacing improves readability. - Variables keep their values until you change them or type
RUN
again (which clears memory).
What You’ve Learnt
- Variables act as named storage boxes so programs can remember state.
- Updating a variable reads the current value before writing the new one.
- Numbers and strings live in different namespaces (
SCORE
vsSCORE$
). - You can already build scoreboards, life counters, and basic inventories.
Next lesson: Random Encounters — unleash RND
to add surprises and procedural twists.