Chapter 4: Math for Cool Kids

Okay, I'm going to let you in on a secret. Come closer. Closer...

Math in games is actually fun.

I know, I know! You've probably had teachers drone on about algebra and geometry, making you wonder when you'd EVER use this stuff in real life. Well, guess what? Games use math constantly, and once you see it in action, it stops being boring homework and starts being MAGIC.

We're not going to derive formulas or prove theorems. We're going to make things move, bounce, spin, and explode. THAT'S game math.

Oh, and along the way, we're going to learn how to talk to the player using INPUT. Because what good is math if you can't let someone type in numbers and see cool stuff happen?


Talking to the Player: INPUT

Before we dive into math, let's learn how to ask the player for information. The INPUT statement pauses your program and waits for the player to type something.

INPUT "What is your name"; name$
PRINT "Hello, "; name$; "!"

PAUSE

Let's break it down:

  • INPUT tells Crash BASIC to wait for the player to type something
  • "What is your name" is the prompt—what the player sees
  • name$ is the variable where the answer gets stored (remember, $ means it's text!)

You can also ask for numbers:

INPUT "How old are you"; age
PRINT "In 10 years, you'll be "; age + 10; " years old!"

PAUSE

See that? We asked for a number, stored it in age, and then did MATH with it! This is where things get interesting.


The Basic Stuff: Addition, Subtraction, Multiplication, Division

You already know these, but let's see them in game terms:

INPUT "Starting health"; health
INPUT "Damage taken"; damage

health = health - damage
PRINT "Remaining health: "; health

PAUSE

Run it! Type in 100 for health and 25 for damage. The math happens instantly: 100 - 25 = 75. That's how every RPG calculates damage!

Here are the basic operators:

Operator Meaning Example
+ Addition score = score + 100
- Subtraction health = health - damage
* Multiplication total = price * quantity
/ Division average = sum / count
^ Power (exponent) squared = x ^ 2
MOD Remainder isEven = x MOD 2

Let's build a damage calculator:

PRINT "=== DAMAGE CALCULATOR ==="
INPUT "Base damage"; baseDamage
INPUT "Critical hit multiplier"; critMultiplier
INPUT "Enemy armor"; armor

finalDamage = (baseDamage * critMultiplier) - armor
IF finalDamage < 0 THEN finalDamage = 0

PRINT "Final damage dealt: "; finalDamage

PAUSE

That's REAL game math! Base damage times crit multiplier, minus armor, but never less than zero.


MOD: The Secret Weapon

MOD gives you the remainder after division. "Big deal," you might think. But MOD is secretly EVERYWHERE in games!

INPUT "Enter a number"; n

IF n MOD 2 = 0 THEN
    PRINT n; " is EVEN"
ELSE
    PRINT n; " is ODD"
END IF

PAUSE

If the remainder when dividing by 2 is 0, the number is even. Simple!

But here's where MOD gets REALLY useful—wrapping things around:

SCREEN 13
x = 0

DO
    BEGIN FRAME
    CLS
    CIRCLE (x, 100), 10, 14
    END FRAME

    x = x + 3
    x = x MOD 320    ' Wrap around when we hit the edge!

    DELAY 16
LOOP

When x reaches 320, x MOD 320 becomes 0, and the circle wraps back to the left side. No IF statement needed! This is how Pac-Man tunnels work.


Random Numbers: RND

Games would be boring if everything was predictable. Enter RND—your chaos generator!

PRINT "Rolling dice..."
PRINT

FOR i = 1 TO 5
    roll = INT(RND * 6) + 1
    PRINT "Roll "; i; ": "; roll
NEXT i

PAUSE

Let's decode INT(RND * 6) + 1:

  • RND gives a random decimal between 0 and 0.999...
  • RND * 6 scales it to 0 to 5.999...
  • INT(...) chops off the decimal, giving 0, 1, 2, 3, 4, or 5
  • + 1 shifts it to 1, 2, 3, 4, 5, or 6

The formula: INT(RND * range) + minimum

Want a random number from 50 to 100?

  • Range is 51 (because 100 - 50 + 1 = 51 possibilities)
  • Minimum is 50
  • Formula: INT(RND * 51) + 50
INPUT "Minimum value"; minVal
INPUT "Maximum value"; maxVal

range = maxVal - minVal + 1
result = INT(RND * range) + minVal

PRINT "Random number: "; result

PAUSE

Distance: How Far Apart Are Things?

"Is the player close enough to the treasure chest to open it?"
"Did the bullet hit the enemy?"
"How far away is the exit?"

All of these need DISTANCE calculations. Here's the magic formula:

' Pythagorean theorem: distance = √((x2-x1)² + (y2-y1)²)

INPUT "Player X"; playerX
INPUT "Player Y"; playerY
INPUT "Treasure X"; treasureX
INPUT "Treasure Y"; treasureY

dx = treasureX - playerX
dy = treasureY - playerY
distance = SQR(dx * dx + dy * dy)

PRINT "Distance to treasure: "; distance

IF distance < 20 THEN
    PRINT "You can open the chest!"
ELSE
    PRINT "Get closer!"
END IF

PAUSE

SQR is the square root function. The formula SQR(dx*dx + dy*dy) is the Pythagorean theorem in action—the same thing you learned in geometry class, but now it's helping you make games!

Let's see it visually:

SCREEN 13

playerX = 160
playerY = 100
treasureX = 200
treasureY = 80

DO
    k$ = INKEY$
    IF k$ = "a" THEN playerX = playerX - 3
    IF k$ = "d" THEN playerX = playerX + 3
    IF k$ = "w" THEN playerY = playerY - 3
    IF k$ = "s" THEN playerY = playerY + 3

    ' Calculate distance
    dx = treasureX - playerX
    dy = treasureY - playerY
    distance = SQR(dx * dx + dy * dy)

    BEGIN FRAME
    CLS

    ' Draw treasure (changes color when close!)
    IF distance < 20 THEN
        CIRCLE (treasureX, treasureY), 8, 14  ' Yellow when close
    ELSE
        CIRCLE (treasureX, treasureY), 8, 6   ' Brown when far
    END IF

    ' Draw player
    CIRCLE (playerX, playerY), 5, 15

    LOCATE 1, 1
    PRINT "Distance: "; INT(distance)
    END FRAME

    DELAY 16
LOOP

Move with WASD and watch the treasure change color as you get close!


Angles and Trigonometry: Spinning and Orbiting

Okay, this is where math gets REALLY cool. Want to make things spin? Orbit around a point? Shoot in any direction? You need SIN and COS.

Don't panic! You don't need to understand the math deeply—just know how to use the formula.

The magic circle formula:

x = centerX + COS(angle) * radius
y = centerY + SIN(angle) * radius

This gives you a point that's radius pixels away from the center, at the specified angle.

SCREEN 13

centerX = 160
centerY = 100
radius = 50
angle = 0

DO
    ' Calculate position on the circle
    x = centerX + COS(angle) * radius
    y = centerY + SIN(angle) * radius

    BEGIN FRAME
    CLS

    ' Draw the center and the orbiting dot
    PSET (centerX, centerY), 7        ' Gray center
    CIRCLE (x, y), 5, 14              ' Yellow orbiting ball

    ' Draw a line connecting them
    LINE (centerX, centerY)-(x, y), 8
    END FRAME

    angle = angle + 0.05              ' Increase angle to move around circle

    DELAY 16
LOOP

A ball orbiting a center point! This is how planets orbit, how shoulder joints rotate, how radar sweeps work—TONS of game effects use this.

Want multiple things orbiting at different speeds?

SCREEN 13

centerX = 160
centerY = 100
angle1 = 0
angle2 = 0
angle3 = 0

DO
    ' Three moons at different distances and speeds
    x1 = centerX + COS(angle1) * 30
    y1 = centerY + SIN(angle1) * 30

    x2 = centerX + COS(angle2) * 50
    y2 = centerY + SIN(angle2) * 50

    x3 = centerX + COS(angle3) * 70
    y3 = centerY + SIN(angle3) * 70

    BEGIN FRAME
    CLS

    ' Draw planet and moons
    CIRCLE (centerX, centerY), 10, 4  ' Red planet
    CIRCLE (x1, y1), 3, 15            ' Fast inner moon
    CIRCLE (x2, y2), 4, 14            ' Medium moon
    CIRCLE (x3, y3), 5, 11            ' Slow outer moon
    END FRAME

    angle1 = angle1 + 0.1   ' Fast
    angle2 = angle2 + 0.05  ' Medium
    angle3 = angle3 + 0.02  ' Slow

    DELAY 16
LOOP

Three moons, each orbiting at its own speed! Change the numbers and watch what happens.


Shooting in a Direction

Want to shoot a bullet toward where the player is aiming? You need ATN (arctangent) to calculate the angle, then SIN and COS to move in that direction.

SCREEN 13

' Turret position
turretX = 160
turretY = 180

' Bullet
bulletX = turretX
bulletY = turretY
bulletActive = FALSE
bulletAngle = 0
bulletSpeed = 5

DO
    k$ = INKEY$

    ' Fire bullet toward mouse/target position
    IF k$ = " " AND NOT bulletActive THEN
        bulletX = turretX
        bulletY = turretY
        ' Aim at a random spot for demo
        targetX = INT(RND * 320)
        targetY = INT(RND * 100)

        ' Calculate angle to target
        dx = targetX - turretX
        dy = targetY - turretY
        bulletAngle = ATN(dy / (dx + 0.001))  ' +0.001 prevents division by zero
        IF dx < 0 THEN bulletAngle = bulletAngle + 3.14159

        bulletActive = TRUE
    END IF

    ' Move bullet
    IF bulletActive THEN
        bulletX = bulletX + COS(bulletAngle) * bulletSpeed
        bulletY = bulletY + SIN(bulletAngle) * bulletSpeed

        ' Deactivate if off screen
        IF bulletX < 0 OR bulletX > 320 OR bulletY < 0 OR bulletY > 200 THEN
            bulletActive = FALSE
        END IF
    END IF

    BEGIN FRAME
    CLS

    ' Draw turret
    CIRCLE (turretX, turretY), 8, 7

    ' Draw bullet
    IF bulletActive THEN
        CIRCLE (bulletX, bulletY), 3, 14
    END IF

    LOCATE 1, 1
    PRINT "SPACE to fire!"
    END FRAME

    DELAY 16
LOOP

Press SPACE to fire bullets at random targets. The math figures out which direction to go!


Bouncing: Reflecting Off Walls

Want a ball to bounce off walls? It's simpler than you think:

SCREEN 13

x = 160
y = 100
dx = 3    ' Velocity in X direction
dy = 2    ' Velocity in Y direction

DO
    ' Move the ball
    x = x + dx
    y = y + dy

    ' Bounce off walls
    IF x <= 10 OR x >= 310 THEN dx = -dx
    IF y <= 10 OR y >= 190 THEN dy = -dy

    BEGIN FRAME
    CLS
    CIRCLE (x, y), 10, 14
    END FRAME

    DELAY 16
LOOP

When we hit a wall, we just flip the velocity! dx = -dx means "go the opposite direction horizontally." That's it—that's bouncing!


Putting It All Together: A Math Playground

Let's make a little playground where you can experiment with all these concepts:

SCREEN 13

PRINT "=== MATH PLAYGROUND ==="
PRINT
PRINT "1. Distance Calculator"
PRINT "2. Random Number Generator"
PRINT "3. Bouncing Ball"
PRINT "4. Orbiting Moons"
PRINT
INPUT "Choose (1-4)"; choice

IF choice = 1 THEN
    INPUT "X1"; x1
    INPUT "Y1"; y1
    INPUT "X2"; x2
    INPUT "Y2"; y2
    dx = x2 - x1
    dy = y2 - y1
    distance = SQR(dx * dx + dy * dy)
    PRINT "Distance: "; distance
    PAUSE

ELSEIF choice = 2 THEN
    INPUT "Minimum"; minVal
    INPUT "Maximum"; maxVal
    PRINT "Generating 10 random numbers:"
    FOR i = 1 TO 10
        result = INT(RND * (maxVal - minVal + 1)) + minVal
        PRINT result; " ";
    NEXT i
    PRINT
    PAUSE

ELSEIF choice = 3 THEN
    x = 160: y = 100: dx = 4: dy = 3
    DO WHILE INKEY$ = ""
        x = x + dx: y = y + dy
        IF x <= 10 OR x >= 310 THEN dx = -dx
        IF y <= 10 OR y >= 190 THEN dy = -dy
        BEGIN FRAME
        CLS
        CIRCLE (x, y), 10, 14
        END FRAME
        DELAY 16
    LOOP

ELSEIF choice = 4 THEN
    angle = 0
    DO WHILE INKEY$ = ""
        BEGIN FRAME
        CLS
        CIRCLE (160, 100), 15, 4
        FOR i = 1 TO 5
            r = 25 + i * 15
            a = angle * (6 - i) / 3
            mx = 160 + COS(a) * r
            my = 100 + SIN(a) * r
            CIRCLE (mx, my), 3 + i, 9 + i
        NEXT i
        END FRAME
        angle = angle + 0.03
        DELAY 16
    LOOP
END IF

Putting It All Together

Game math isn't scary—it's POWERFUL:

  • Basic arithmetic handles scores, health, damage, money
  • MOD wraps things around and checks for patterns
  • RND adds randomness and unpredictability
  • SQR and distance formulas detect collisions and proximity
  • SIN/COS create rotation, orbits, and directional movement
  • Bouncing is just flipping velocity when you hit walls

And INPUT lets players interact with all of it!


Try It Yourself!

  1. Slot Machine: Use RND to generate three random symbols (1-5), print them, and check if they match
  2. Interest Calculator: INPUT a starting amount and interest rate, loop to show how it grows each year
  3. Homing Missile: Make a bullet that slowly turns toward a moving target using angles
  4. Gravity: Add dy = dy + 0.2 to the bouncing ball to simulate gravity

Math is your superpower. The more you play with these formulas, the more game ideas you'll be able to bring to life!

See you in Chapter 5!


Next Chapter: We'll learn the fundamentals of pixel art—so you can create your own sprites and animations for your games!