programgames/scroller/play.xtl

The side-scroller: run right to the flag. Left and right (or a and d) run, up (or w) jumps; land on a walker to beat it, touch one otherwise and you are caught; mind the pits; pick up the coins; r starts again. The game 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. A game won or lost ends the program (on the page, Play or any key then starts a new one). The rules are Jump.xtl's, built on lib/Ecs.

source · imports j: games/scroller/Jump.xtl

ᵘs̲how : Num a => (Float, Int, Float, a) -> 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 => (Float, Int, Float, a) -> 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 => (Float, Int, Float, a) -> Char

function · line 30

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

ᵘe̲nd ← { s →
  y ← ʲy̲ou s
  1 = ʲs̲tatus s ? @ f̲ormat< "You reached the flag with {f_loor 3 s_elect y} coins in {f_loor 4 s_elect y} seconds!"
  @ f̲ormat< "Caught! {f_loor 3 s_elect y} coins in {f_loor 4 s_elect y} seconds."
}
f̲ormat< expands to
("You reached the flag with " c̲at (f̲ormat (f̲loor 3 s̲elect y)) c̲at " coins in " c̲at (f̲ormat (f̲loor 4 s̲elect y)) c̲at " seconds!")
f̲ormat< expands to
"Caught! " c̲at (f̲ormat (f̲loor 3 s̲elect y)) c̲at " coins in " c̲at (f̲ormat (f̲loor 4 s̲elect y)) c̲at " seconds."
Used in: ᵘs̲how