Math Magic
What you'll learn:
- Wrap reusable formulas in `DEF FN` to simplify expressions.
- Combine globals and single-argument functions for game maths.
- Use functions to drive scoring, physics, and procedural patterns.
Lesson 15 – Math Magic
You’ve been writing formulas inline; now collect them into functions. DEF FN
gives each formula a name so your code reads like intent instead of algebra.
[📷 suggested: screenshot showing PRINT lines calling several functions]
The One-Minute Tour
DEF FNNAME(X)=expression
defines a numeric function.- Commodore BASIC V2 supports one argument, one line, one numeric result.
- Use uppercase names without spaces (e.g.,
FNSCORE
).
Basic Function Toolkit
NEW
10 DEF FNSQ(X)=X*X
20 DEF FNDB(N)=N*2
30 PRINT FNSQ(5)
40 PRINT FNDB(7)
Output:
25
14
Functions drop inline: PRINT 20+FNSQ(3)
works just fine.
Percentages, Scaling, and Rounding
NEW
10 DEF FNPERCENT(S)=INT(S*100/MAX)
20 MAX=500
30 SCORE=275
40 PRINT "SCORE:";SCORE;"(";FNPERCENT(SCORE);"%)"
Use globals (MAX
) to supply extra context. Commodore BASIC functions only accept one argument, so globals keep them useful.
Gravity Helper for Your Loop
NEW
10 DEF FNFALL(T)=VY*T-4.9*T*T
20 VY=8
30 FOR T=0 TO 3 STEP .2
40 Y=FNFALL(T)
50 IF Y<0 THEN EXIT
60 PRINT T;Y
70 NEXT T
FNFALL
encapsulates a physics formula so the main loop stays readable. With a screen POKE
you could bring back the trajectory from Lesson 10.
Pattern Functions for Generators
NEW
10 DEF FNCHESS(X)= (X AND 1)=0
20 FOR R=0 TO 7
30 FOR C=0 TO 7
40 IF FNCHESS(R+C) THEN PRINT "#"; ELSE PRINT " ";
50 NEXT C
60 PRINT
70 NEXT R
FNCHESS
returns true/false, letting you alternate characters elegantly inside nested loops.
Random Helpers
NEW
10 DEF FNROLL(N)=INT(RND(1)*N)+1
20 FOR I=1 TO 5
30 PRINT "D6:";FNROLL(6)," D20:";FNROLL(20)
40 NEXT I
Random dice, percentages, scaling—they all benefit from a named function you can drop into scoring logic.
Combining Globals with Dummy Arguments
Need more than one input? Store extra values globally, use a dummy parameter to satisfy BASIC’s single-argument rule.
NEW
10 X1=4:Y1=5:X2=18:Y2=9
20 DEF FNDIST(Q)=SQR((X2-X1)^2+(Y2-Y1)^2)
30 PRINT "DIST:";INT(FNDIST(0))
Not elegant, but effective—and common practice in 1980s code.
Experiment Section
- Add
DEF FNFX(S)=INT(SPEED*S)
to convert player speed to animation frames. - Use
DEF FNHP(V)=MINHP+INT((MAXHP-MINHP)*V)
to scale hit points. - Integrate Lesson 14’s generators: call a function inside nested loops to pick tiles.
- Combine with Lesson 13 logic:
IF FNHIT(NX,NY)=1 THEN ...
wraps collision math in a friendly wrapper. - Precompute tables: fill an array with
FOR I=0 TO 15:LOOKUP(I)=FNFX(I/15)
.
[📷 suggested: screenshot showing a HUD that uses function output]
Concept Expansion
Functions are the maths sibling of subroutines: they only return a value, but that value can express physics, scoring, or procedural rules concisely. Later you’ll embed similar logic in machine code, but the interfaces will look familiar.
Game Integration
- Scoring:
SCORE=SCORE+FNCOMBO(HITS)
to reward streaks. - Projectile motion: call
FNFALL
to update bullet arcs each frame. - Damage curves:
HP=HP-FNDAMAGE(WEAPON)
to centralise balancing. - Enemy AI:
ANGLE=FNTRACK(PX,PY,EX,EY)
returns chase direction (store positions in globals first).
From the Vault
- Paradroid — read design diaries referencing control formulas.
- Commodore 64 — understand how floating-point arithmetic behaves on the 6510.
Quick Reference
DEF FNSQ(X)=X*X : REM define
PRINT FNSQ(5) : REM call
DEF FNROLL(N)=INT(RND(1)*N)+1 : REM random range helper
- Functions must appear before you call them.
- Names must be
FN
followed by letters (no spaces). - Use globals for extra context; reset them before each call if needed.
What You’ve Learnt
- Defined reusable formulas with
DEF FN
. - Balanced the single-argument limitation using globals.
- Dropped functions into loops, scoring, and physics for cleaner code.
- Built a toolbox of random, pattern, and physics helpers ready for the Week 2 mini-game.
Next lesson: Mini-Game: Maze Craze — stitch the whole week together into a procedural maze run.