programgames/racer/play.xtl

The racer: three laps against three rivals. Up (or w) speeds you up, down (or s) slows you, left and right (or a and d) steer; keep off the grass, which slows you; r starts again. The race moves with the clock: on the page it is in a dialog, and the clock's ticks (twenty a second) and your keys come to the program as events; at the command line, lines such as "tick 0.05" and "key UP" on standard input. When you finish the program ends with your place (on the page, Play or any key then starts a new race). The rules are Race.xtl's, built on lib/Ecs.

source · imports rc: games/racer/Race.xtl

ᵘs̲how : (Num a, Num b, Truthy b) => (Float, Float, a, b) -> Char

function · line 14

Show the game, then wait for the next event.

ᵘs̲how ← { s →
  _ ← ⎕S̲HOW ʳᶜd̲raw s
  0 < ʳᶜs̲tatus s ? ᵘe̲nd s
  ᵘl̲oop s
}

ᵘl̲oop : (Num a, Num b, Truthy b) => (Float, Float, a, b) -> Char

function · line 20

The next event: a tick moves the game on and shows it; a key acts.

ᵘl̲oop ← { s →
  e ← ⎕E̲VENT @
  "end" m̲atch ⎕E̲KIND e ? "THE END"
  "tick" m̲atch ⎕E̲KIND e ? ᵘs̲how s ʳᶜt̲ick f̲irst ⎕E̲AT e
  n̲ot "key" m̲atch ⎕E̲KIND e ? ᵘl̲oop s
  name ← f̲ormat ⎕E̲KEY e
  "r" m̲atch name ? ᵘs̲how ʳᶜn̲ew 0
  ᵘl̲oop s ʳᶜk̲ey name
}

ᵘe̲nd : (Num a, Num b, Truthy b) => (Float, Float, a, b) -> Char

function · line 30

How the race ended (the program ends with it).

ᵘe̲nd ← { s →
  p ← ʳᶜp̲lace s
  1 = p ? "You won the race!"
  @ f̲ormat< "You finished in place {p}."
}
f̲ormat< expands to
"You finished in place " c̲at (f̲ormat (p)) c̲at "."
Used in: ᵘs̲how