Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Behaviors

A behavior is a description of how a sprite moves and looks: “drift right two pixels a tick”, “bob up and down”, “fade in”. You write it once, put it on as many sprites as you like, and I run it for every one of them, every tick, on my own. Your program doesn’t have to be in a loop at all.

SCREEN 320, 240
MEMORYBUFFER "ship", 16, 16
    POLYGON [(0, 16), (8, 0), (16, 16)], 11, 3
END MEMORYBUFFER

BEHAVIOR "drift"
    VELOCITY 1, 0
    OSCILLATE 0, 10 !! speed(2)
END BEHAVIOR

FOR n = 0 ... 4
    ENABLE SPRITE n
    SET SPRITE n !! image("ship") x(0) y(40 + n * 40) behavior("drift")
    SHOW SPRITE n
NEXT
PAUSE

The PAUSE at the end only keeps the game open until you press a key. Everything that moves, moves by itself.

VELOCITY 1, 0 moves one pixel right every tick. OSCILLATE 0, 10 swings up and down ten pixels either way, and !! speed(2) swings it faster.

What a behavior can say

There are lots of words to use inside a BEHAVIOR. A few to start with:

VELOCITY x, yMove this much every tick.
IMPULSE x, yA push, once, that it keeps going at.
ACCELERATION x, ySpeed up this much every tick. ACCELERATION 0, 0.4 is a fall.
BOUNCE x, yBounce off what it runs into, keeping this share of its speed.
ROTATE angle, ticksTurn to an angle, or with !! loop, keep turning.
ALPHA from, to, ticksFade.
SCALE from_x, from_y, to_x, to_y, ticksGrow or shrink.
OSCILLATE x, ySwing back and forth.
ANIMATE image, framesStep through a sprite sheet’s frames.
ORBIT distanceGo round a point (!! center((x, y))) or another sprite.

A sprite can have several behaviors at once, and they add up:

SCREEN 320, 240
MEMORYBUFFER "gem", 24, 24
    POLYGON [(12, 0), (24, 12), (12, 24), (0, 12)], 13, 5
END MEMORYBUFFER

BEHAVIOR "spin"
    ROTATE 360, 90 !! origin((0.5, 0.5)) loop
END BEHAVIOR
BEHAVIOR "pulse"
    ALPHA 0.3, 1, 40 !! loop
END BEHAVIOR

ENABLE SPRITE 0
SET SPRITE 0 !! image("gem") x(148) y(108) scale((3, 3)) behavior("spin")
SET SPRITE 0 !! behavior("pulse")
SHOW SPRITE 0
PAUSE

Behaviors with settings

The same behavior can go different speeds on different sprites. PARAM reads a setting the sprite gives it, with the value to use when it gives none, and the sprite gives it with behavior_params.

SCREEN 320, 240
NEWIMAGE 8, 8, 15, "dot"

BEHAVIOR "fall"
    VELOCITY 0, PARAM("speed", 1)
END BEHAVIOR

FOR n = 0 ... 15
    ENABLE SPRITE n
    SET SPRITE n !! image("dot") x(10 + n * 19) y(0)
        !! behavior("fall") behavior_params({speed: 0.5 + n * 0.25})
    SHOW SPRITE n
NEXT
PAUSE

From 1.x: A parameter was written @speed. Here it’s PARAM("speed", 1), and the second value is what it is when a sprite doesn’t say.

Ticks and frames

I run behaviors on ticks of my own, sixty a second unless you say otherwise with SET BEHAVIOR RATE. The screen’s frames are a separate thing: a slow machine might show fewer frames, but a sprite still moves the same distance every second.

Behaviors are also where sprites find out they’ve hit something, which is the next lesson.