programgames/asteroids/play.xtl

Asteroids: break every rock, three waves of them. Left and right (or a and d) turn the ship, up (or w) thrusts, the space bar (or f) fires; a big rock breaks into two medium ones, a medium into two small; a rock that hits the ship costs a life; 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", "key LEFT" and "key space" on standard input. When every wave is cleared or the lives are gone the program ends with the score (on the page, Play or any key then starts a new game). The rules are Rocks.xtl's, built on lib/Ecs.

source · imports ro: games/asteroids/Rocks.xtl

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

function · line 15

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, Int, Int, Float, a, Float) -> Char

function · line 21

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 ? ᵘq̲uit s
  "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
}

ᵘq̲uit : Num a => (Float, Int, Int, Int, Float, a, Float) -> Char

function · line 31

The end of the events: the score so far.

ᵘq̲uit ← { s →
  _ ← p̲rint! @ f̲ormat< "Score {f_irst ro:s_core s}, wave {3 s_elect ro:s_core s}, lives {2 s_elect ro:s_core s}."
  "THE END"
}
f̲ormat< expands to
("Score " c̲at (f̲ormat (f̲irst ʳᵒs̲core s)) c̲at ", wave " c̲at (f̲ormat (3 s̲elect ʳᵒs̲core s)) c̲at ", lives " c̲at (f̲ormat (2 s̲elect ʳᵒs̲core s)) c̲at ".")
Used in: ᵘl̲oop

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

function · line 36

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

ᵘe̲nd ← { s →
  sc ← f̲irst ʳᵒs̲core s
  1 = ʳᵒs̲tatus s ? @ f̲ormat< "Every wave cleared! You scored {sc}."
  @ f̲ormat< "Game over. You scored {sc}."
}
f̲ormat< expands to
("Every wave cleared! You scored " c̲at (f̲ormat (sc)) c̲at ".")
f̲ormat< expands to
"Game over. You scored " c̲at (f̲ormat (sc)) c̲at "."
Used in: ᵘs̲how