Added AI-README.md, a guide for AI assistants making audio games
It covers what matters most for an assistant building a game with the library: audio-first rules, setting up a game, a minimal working game, the menu, the keys every game should handle, speech, generated and positional sounds, and generated music. For work on the library itself, it covers the code style and the tests, and says that fingerprints are only remade after a human has listened to the sound. The README links to it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d598818fb1
commit
f1cb50f985
2 files changed
+251
No files matched your search
+247
@@ -0,0 +1,247 @@
|
||||
# libstormgames for AI assistants
|
||||
|
||||
This guide is for AI assistants building audio games with libstormgames, or
|
||||
working on the library itself. README.md is the full reference; this is what
|
||||
matters most, in the order you need it.
|
||||
|
||||
## What this library is for
|
||||
|
||||
libstormgames is for audio games: games played by ear, mostly by blind
|
||||
players using screen readers. Every piece of information a player needs must
|
||||
reach them through speech or sound. A game can have a window, but nothing may
|
||||
depend on seeing it.
|
||||
|
||||
The library handles speech, sound, menus, volume, scores and configuration,
|
||||
and it can generate sound effects and music from code. A game built with it
|
||||
should be able to ship with no sound files at all and still sound complete,
|
||||
so the person you are helping can spend their time on story, mechanics and code.
|
||||
|
||||
## Rules that matter more than anything else
|
||||
|
||||
- **Say it, don't show it.** Announce every state change, score, menu item,
|
||||
error and prompt with `speak`. Never rely on colour, layout or anything on screen.
|
||||
- **Do not use `spd-say` or speech-dispatcher directly.** Use `speak`, which
|
||||
goes through the library's speech provider and its history.
|
||||
- **Keep speech short and put the important word first.** Players hear speech
|
||||
in sequence and often interrupt it: "Goblin, left, close" beats
|
||||
"There is a goblin approaching from your left side".
|
||||
- **Sound carries position.** Use positional playback (`obj_play`) so a
|
||||
player can hear where things are, and keep the sounds of different kinds of
|
||||
things distinct from each other.
|
||||
- **Let players learn the sounds.** Pass `soundDescriptions` to `game_menu` so
|
||||
the Learn Sounds menu says what each sound means.
|
||||
- **Never trap the keyboard.** Escape must always lead out; menus must wrap
|
||||
or stop cleanly at their ends.
|
||||
|
||||
## Setting up a game
|
||||
|
||||
Games usually include the library as a git submodule named `libstormgames`
|
||||
in the game's own folder:
|
||||
|
||||
```bash
|
||||
git submodule add https://git.stormux.org/storm/libstormgames libstormgames
|
||||
```
|
||||
|
||||
Requirements are in `requirements.txt`. Generated sounds and music also need
|
||||
numpy and scipy.
|
||||
|
||||
The layout a game expects:
|
||||
|
||||
```
|
||||
my_game.py
|
||||
libstormgames/ the library
|
||||
sounds/ optional .ogg or .wav files; subfolders are fine
|
||||
files/instructions.txt optional; adds Instructions to the main menu
|
||||
files/credits.txt optional; adds Credits to the main menu
|
||||
```
|
||||
|
||||
## A minimal game
|
||||
|
||||
```python
|
||||
#!/usr/bin/env python3
|
||||
import pygame
|
||||
|
||||
import libstormgames as sg
|
||||
from libstormgames import sfx
|
||||
from libstormgames.sfx import impacts, music, rewards
|
||||
|
||||
|
||||
def play_game(sounds):
|
||||
sg.speak("Collect three stars. Press space to search, escape to stop.")
|
||||
found = 0
|
||||
while found < 3:
|
||||
for event in pygame.event.get():
|
||||
if event.type != pygame.KEYDOWN:
|
||||
continue
|
||||
if event.key == pygame.K_ESCAPE:
|
||||
return
|
||||
if event.key == pygame.K_SPACE:
|
||||
found += 1
|
||||
sounds["star"].play()
|
||||
sg.speak(f"Star {found} of 3")
|
||||
pygame.time.wait(10)
|
||||
sounds["win"].play()
|
||||
sg.speak("You found them all!")
|
||||
|
||||
|
||||
def main():
|
||||
sounds = sg.initialize_gui("Star Search") # sets up pygame, the mixer and speech; loads sounds/
|
||||
sfx.add_sounds(sounds, {"star": rewards.sparkle, "win": rewards.bonus_run, "boom": impacts.explosion})
|
||||
menuMusic = sfx.make_music(music.moonlight_metal()) # takes about a second: do it once, at load
|
||||
while True:
|
||||
sg.play_bgm(menuMusic)
|
||||
choice = sg.game_menu(sounds)
|
||||
if choice == "play":
|
||||
play_game(sounds)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
```
|
||||
|
||||
What `game_menu(sounds, playCallback=None, *customOptions, soundDescriptions=None, optionLabels=None)` does:
|
||||
|
||||
- It speaks the menu, handles arrows, Home, End, Enter and the Alt volume
|
||||
keys, and plays `menu-move` and `menu-select` sounds (generated if the game
|
||||
has no files for them).
|
||||
- It handles Learn Sounds, Instructions, Credits, High Scores and Donate
|
||||
itself, and exits the program for Exit or Escape.
|
||||
- It returns `"play"` or one of your custom options, such as
|
||||
`sg.game_menu(sounds, None, "practice_mode")`. Underscores are spoken as spaces.
|
||||
- It fades the music out when an option is chosen, so start the menu music
|
||||
again each time round, as above.
|
||||
|
||||
## Your own event loop
|
||||
|
||||
Inside the game itself you read pygame events yourself. Handle these keys so
|
||||
players get what they expect from every game built on the library:
|
||||
|
||||
- **Escape:** leave the current game or screen.
|
||||
- **Alt+Page Up / Page Down:** master volume, `sg.adjust_master_volume(0.1)` / `(-0.1)`.
|
||||
- **Alt+Home / End:** music volume, `sg.adjust_bgm_volume(...)`.
|
||||
- **Alt+Insert / Delete:** sound effect volume, `sg.adjust_sfx_volume(...)`.
|
||||
- **F1 / F2 / F3:** previous, current and next spoken message. `game.setup_speech_history_keys()`
|
||||
on a `Game` returns a {key: function} dictionary for these, or call
|
||||
`sg.speak_previous()`, `sg.speak_current()` and `sg.speak_next()`.
|
||||
- **Backspace:** `sg.pause_game()` pauses everything until Backspace is pressed again.
|
||||
|
||||
Do not call `sg.check_for_exit()` from a loop that also reads events: it
|
||||
takes events off the queue and only looks at the first one, so key presses
|
||||
get lost. Check for Escape in your own `pygame.event.get()` loop instead.
|
||||
|
||||
## Speech
|
||||
|
||||
- `sg.speak(text, interrupt=True)`: speak, cutting off what was being said.
|
||||
Pass `interrupt=False` to queue after it, for messages that must not be lost.
|
||||
- `sg.messagebox(text)`: a message the player reads at their own pace; music
|
||||
fades out and pauses while it is open and comes back after.
|
||||
- `sg.display_text(lines)`: a list of lines the player can move through.
|
||||
- `sg.get_input(prompt, text="")`: ask the player to type something.
|
||||
|
||||
## Sounds
|
||||
|
||||
### Prefer generated sounds
|
||||
|
||||
`libstormgames.sfx` makes sounds from code, so the game needs no files. Add
|
||||
them by name to the game's sound dictionary; a file with the same name in
|
||||
`sounds/` always wins, so any generated sound can be replaced later:
|
||||
|
||||
```python
|
||||
sfx.add_sounds(sounds, {
|
||||
"thunder": impacts.thunder, # a function is only called if no file has that name
|
||||
"thunder2": lambda: impacts.thunder(seed=2), # a different seed is a variant of the same sound
|
||||
"beep": lambda: ui.beep(880, 0.15),
|
||||
"jump": movement.jump,
|
||||
})
|
||||
```
|
||||
|
||||
What there is, by what a game needs:
|
||||
|
||||
- **Interface:** `ui.beep`, `ui.fanfare`, `ui.toggle_sweep(rising)`,
|
||||
`ui.denied_click`, `ui.menu_move`, `ui.confirm_chime(step)`.
|
||||
- **Rewards and pickups:** `rewards.sparkle`, `rewards.bell_chime(count)`,
|
||||
`rewards.beacon_blip`, `rewards.power_charge`, `rewards.bonus_run`,
|
||||
`rewards.rising_sweep`, `rewards.item_spill(count)`.
|
||||
- **Hits, explosions, weather:** `impacts.thunder`, `impacts.explosion`,
|
||||
`impacts.heavy_boom`, `impacts.thud`, `impacts.shatter`.
|
||||
- **Objects and materials:** `materials.finger_snap`, `materials.metal_clank`,
|
||||
`materials.rattle`, `materials.hollow_knock(frequency)`, `materials.squelch(...)`,
|
||||
`materials.wet_splat`, `materials.thick_splash`, `materials.wooden_launch`.
|
||||
- **The player moving:** `movement.jump`.
|
||||
- **Creatures:** `creatures.horse_whinny`, `creatures.witch_cackle`.
|
||||
- **Characters talking:** `babble.babble(text)` gives each line its own beeping tune.
|
||||
- **Vehicles:** `vehicles.whoosh(rising)`, `vehicles.skid_spin`,
|
||||
`vehicles.tyre_scrub_turn`, and live voices for tyres, gravel, rumble strips and wind.
|
||||
- **Ambience and alerts:** looping voices in `ambience` and `alerts` (see README.md
|
||||
for how to record a voice into a loop).
|
||||
|
||||
Most generators take `seed`: the default always gives the same sound, another
|
||||
number a variant, `None` a new variant each time. Read the module's docstrings
|
||||
for each generator's arguments before guessing them.
|
||||
|
||||
`add_sounds` and `make_sound` need the mixer, so call them after
|
||||
`initialize_gui` or `sg.Game(...).initialize()`.
|
||||
|
||||
### Sound files and names
|
||||
|
||||
Files in `sounds/` are loaded by `initialize_gui` and named by their path
|
||||
without the extension: `sounds/enemies/goblin.ogg` is `sounds["enemies/goblin"]`.
|
||||
Numbered variants such as `step1.ogg`, `step2.ogg` are picked at random by
|
||||
`sg.play_random(sounds, "step")`.
|
||||
|
||||
### Sounds with a position
|
||||
|
||||
For anything with a place in the world, start a looping positional sound and
|
||||
update it as the player or the thing moves:
|
||||
|
||||
```python
|
||||
channel = sg.obj_play(sounds, "enemies/goblin", playerX, goblinX) # loops by default
|
||||
channel = sg.obj_update(channel, playerX, goblinX) # each frame; returns None once out of range
|
||||
channel = sg.obj_stop(channel) # when the goblin is gone
|
||||
```
|
||||
|
||||
Positions are an x number, or (x, y) with `use_3d=True` for height. Sounds
|
||||
fade out 12 tiles away. Keep the channel each call returns, since it becomes
|
||||
None when the sound stops.
|
||||
|
||||
## Music
|
||||
|
||||
Generated music plays through `play_bgm` just like a music file, so it loops,
|
||||
fades and pauses for dialogue the same way:
|
||||
|
||||
```python
|
||||
battleMusic = sfx.make_music(music.battle_music())
|
||||
sg.play_bgm(battleMusic)
|
||||
```
|
||||
|
||||
The pieces: `battle_music` (heavy metal in D minor in the manner of Bach, 24
|
||||
seconds) and `moonlight_metal` (Beethoven's Moonlight Sonata made heavy, C sharp
|
||||
minor, 27 seconds). Each takes `seed` and `tempo`. Making a piece takes about a
|
||||
second, so make the music a game needs while it loads and keep the bytes.
|
||||
|
||||
## Working on the library itself
|
||||
|
||||
- **Style:** camelCase variables, snake_case functions, PascalCase classes,
|
||||
lines up to 120 characters. Docstrings say what a thing does in plain words.
|
||||
- **Tests:** `python -m unittest discover -s tests` from the library root. Run
|
||||
them after any change.
|
||||
- **Generated sounds are fixed.** Every sound is checked against a stored
|
||||
fingerprint in `tests/data/sfx_fingerprints.npz`, so it cannot change by
|
||||
accident. If a test says a fingerprint changed and you did not mean to
|
||||
change that sound, your change is wrong. Run
|
||||
`python tools/update-sfx-fingerprints.py` only after the person you are
|
||||
working with has listened to the new sound and accepted it, and check that
|
||||
only the sounds you meant to change differ.
|
||||
- **You cannot hear.** When you make or change a sound, render it to a file,
|
||||
analyze it with numpy if that helps, and ask your human partner to listen.
|
||||
Their ears decide, not your measurements.
|
||||
- **Adding a sound effect:** add a generator to the module it fits in `sfx/`,
|
||||
add a case to `tests/sfx_cases.py`, list it in README.md and in this file,
|
||||
and fingerprint it once it has been approved by ear.
|
||||
- **Adding a music piece:** add `sfx/music/pieces/<name>.py` containing a
|
||||
function `<name>(seed=0, tempo=...)` that returns a stereo array at
|
||||
44,100 samples a second; `music.<name>` finds it with no registration. Build
|
||||
it from `sfx/music/instruments.py`, then add its test case and documentation
|
||||
as for a sound effect.
|
||||
- **Contributions:** read the "AI-assisted contributions" section of README.md.
|
||||
A human reviews and tests every change before it is submitted.
|
||||
Reference in new issue
Block a user