sourcegames/asteroids/play.xtl
1⍝!/usr/bin/env xetal
2⍝│ Asteroids: break every rock, three waves of them. Left and right (or
3⍝│ a and d) turn the ship, up (or w) thrusts, the space bar (or f) fires;
4⍝│ a big rock breaks into two medium ones, a medium into two small; a
5⍝│ rock that hits the ship costs a life; r starts again. The game moves
6⍝│ with the clock: on the page it is in a dialog, and the clock's ticks
7⍝│ (twenty a second) and your keys come to the program as events; at the
8⍝│ command line, lines such as "tick 0.05", "key LEFT" and "key space"
9⍝│ on standard input. When every wave is cleared or the lives are gone
10⍝│ the program ends with the score (on the page, Play or any key then
11⍝│ starts a new game). The rules are Rocks.xtl's, built on lib/Ecs.
12"ro:" u̲se< "Rocks"
13
14⍝│ Show the game, then wait for the next event.
15ᵘs̲how ← { s →
16 _ ← ⎕S̲HOW ʳᵒd̲raw s
17 0 < ʳᵒs̲tatus s ? ᵘe̲nd s
18 ᵘl̲oop s
19}
20⍝│ The next event: a tick moves the game on and shows it; a key acts.
21ᵘl̲oop ← { s →
22 e ← ⎕E̲VENT @
23 "end" m̲atch ⎕E̲KIND e ? ᵘq̲uit s
24 "tick" m̲atch ⎕E̲KIND e ? ᵘs̲how s ʳᵒt̲ick f̲irst ⎕E̲AT e
25 n̲ot "key" m̲atch ⎕E̲KIND e ? ᵘl̲oop s
26 name ← f̲ormat ⎕E̲KEY e
27 "r" m̲atch name ? ᵘs̲how ʳᵒn̲ew 0
28 ᵘl̲oop s ʳᵒk̲ey name
29}
30⍝│ The end of the events: the score so far.
31ᵘq̲uit ← { s →
32 _ ← 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}."
33 "THE END"
34}
35⍝│ How the game ended (the program ends with it).
36ᵘe̲nd ← { s →
37 sc ← f̲irst ʳᵒs̲core s
38 1 = ʳᵒs̲tatus s ? @ f̲ormat< "Every wave cleared! You scored {sc}."
39 @ f̲ormat< "Game over. You scored {sc}."
40}
41ᵘs̲how ʳᵒn̲ew 0
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 ".")