StructOp Handbook
Contents Examples Commands
Handbook / Game development

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#

main.st
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
}
AbilityDoesbracket-lib
gameopens the screen (width × height, default 80 × 50), runs on_tick every frame at fps (30)main_loop, GameState::tick
clearempties the grid, optionally to a background colourcls, cls_bg
drawone glyph at x, y with color and backgroundset
texta message at x, centred without x, right-aligned with rightprint, print_centered, print_color_right
barvalue out of max as a bar width longbar_horizontal
quitends the loop after this framectx.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.

input.txt (one line per frame)

p
space

space
last frame
Press 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.