Terminal games
For a portable terminal game, declare StructOp/core and StructOp/game in intent.st. The same frame loop, drawing and input abilities use ncurses on C and bracket-lib on Rust. See C and Rust targets for the provider declarations.
On this page
A game loop#
ability "game" "Flappy" {
title = "Flappy Dragon"
key = ability.value.key
frame_ms = ability.value.frame_ms
on_tick = [ability.clear.Sky, ability.call.Play]
}
ability "clear" "Sky" {
background = Color.navy
}
ability "text" "Help" {
x = 0
y = 0
message = "Press SPACE to flap."
}
ability "draw" "Dragon" {
x = 0
y = data.value.player.current.y
glyph = "@"
color = Color.yellow
}| Ability | Does | bracket-lib |
|---|---|---|
game | opens the screen (width × height, default 80 × 50), runs on_tick every frame at fps (30) | main_loop, GameState::tick |
clear | empties the grid, optionally to a background colour | cls, cls_bg |
draw | one glyph at x, y with color and background | set |
text | a message at x, centred without x, right-aligned with right | print, print_centered, print_color_right |
bar | value out of max as a bar width long | bar_horizontal |
quit | ends the loop after this frame | ctx.quitting = true |
key and frame_ms name value abilities the game writes each frame. Keys arrive in a value of type Key (Key.left, Key.space, Key.p, Key.key1, Key.none when nothing was pressed) and are matched with switch. Colours are the Color enum.
Playing it#
Run structop apply. A Rust game opens a bracket-lib window; a C game draws with ncurses in a terminal at least as large as the game, reads the keyboard without waiting, and restores the terminal when it quits.
Testing it: headless mode#
When standard input or output is not a terminal, the same program runs headless: nothing is opened, each line of standard input is one frame's key by name, the run ends with the input, and the last frame is printed as text, so a test can compare that screen.
p
space
spacePress SPA|E to flap.
Score: 0 |
|
@
|Random numbers repeat when STRUCTOP_SEED is set (set it to 1 in tests), and each headless frame lasts exactly 1000 / fps milliseconds, so physics and monster moves are the same on every machine.