GitHubCommencer

Cette page n’est pas encore traduite en français ; la version anglaise est affichée ci-dessous.

Guides

Templates

A new project starts from a template: a small, complete game that builds, runs and passes its tests before you change a line. game new writes one into a new directory, and NSGo's nsgo init -template writes the same files from the template's Go module.

The templates

Template What it is nsgo init -template module
starter A tap-to-score 2D game: an animated sprite on a fixed-step loop, a native HUD and headless tests, the smallest project to grow from. The default. github.com/nsgo-dev/game/template
action3d A third-person 3D action adventure: an orbit camera, run, jump and attack animations blended by a state machine, melee enemies, and a key that opens the gate to the goal (guide). github.com/nsgo-dev/game/template/action3d
cards2d Suit Ladders, a single-player card game: cards dragged between a draw pile, a hand, a discard pile and four ladders, rules in code, the deck and the table in YAML, numbered deals and saves. github.com/nsgo-dev/game/template/cards2d
fighting2d A one-on-one 2D fighting game: two original fighters with special moves from motion inputs, combos, guards high and low, rounds and a timer, against a CPU opponent or a second player, played with the keyboard, game controllers or touch. The fighting game template describes it. github.com/nsgo-dev/game/template/fighting2d
fps3d A first-person shooter arena in 3D: hitscan and projectile weapons, enemies that patrol, hunt and shoot with ai and physics3d, health and ammo pickups and three waves to clear, played with the keyboard and mouse, a game controller or touch. The fps3d template describes it. github.com/nsgo-dev/game/template/fps3d
idle An idle game: generators, upgrades, large numbers, offline progress on return and prestige, with a win and a loss; The idle template describes it. github.com/nsgo-dev/game/template/idle
platformer2d A side-scrolling platformer (guide): a controller with coyote time and jump buffering, three rooms with camera bounds, spikes, pits, sparks to collect, a checkpoint and a goal, played with the keyboard, a controller or touch. github.com/nsgo-dev/game/template/platformer2d
puzzle2d Spark Slide, a 2D grid puzzle: slide a spark across the board to light every lamp, with levels in YAML, undo and redo, a move limit, stars per level and a level select that saves progress; plays with the keyboard, a game controller, touch and the mouse. github.com/nsgo-dev/game/template/puzzle2d
racing3d A 3D racer: two laps of a closed circuit against racing-line rivals, with raycast cars, checkpoints, a countdown, a best-lap ghost and a results screen, played with keyboard, gamepad or touch. Racing 3D template github.com/nsgo-dev/game/template/racing3d
rhythm Pulse Lanes, a four-lane rhythm game: charts in YAML on a beat map, a music clock that keeps the notes in time with the music, Perfect, Great, Good and Miss judgements, combo, score, rank and an energy bar to clear or fail by, audio and video latency calibration kept per device, and best clears saved; plays with the keyboard, a game controller, touch and the mouse. github.com/nsgo-dev/game/template/rhythm
roguelike2d A turn-based roguelike (guide): five floors generated from the run's seed as BSP rooms and caves, bump-to-attack combat, weighted loot with pity, stats with equipment and timed tonics, a quaff and a cleave on turn cooldowns, permadeath and a run summary, played with the keyboard, a controller or touch. github.com/nsgo-dev/game/template/roguelike2d
rpg2d A top-down RPG: a town and a meadow to walk, people with branching dialogue, an inventory with equipment, one quest, turn-based battles and three save slots. Guide. github.com/nsgo-dev/game/template/rpg2d
rts2d A 2D real-time strategy skirmish: gather crystals, build barracks, train three unit types and destroy a computer opponent's headquarters, with fog of war, box selection, control groups and keyboard, gamepad and touch controls (guide). github.com/nsgo-dev/game/template/rts2d
shmup2d A vertical scrolling shooter: enemy waves and a stage in YAML, bullet patterns, power-ups, bombs, a boss with three phases, lives and a score, played with the keyboard, the mouse, a controller or touch. github.com/nsgo-dev/game/template/shmup2d
towerdefense2d A 2D tower defense game: towers with upgrades beside a road enemies follow, waves defined in YAML, money and lives, played with keyboard, gamepad or touch; Tower defense template describes it. github.com/nsgo-dev/game/template/towerdefense2d
visualnovel A short branching visual novel: two characters, drawn backgrounds, choices that change the ending, a backlog, auto and skip modes, save slots and two languages. github.com/nsgo-dev/game/template/visualnovel

nsgo game new -list prints the same list from the command itself, the default first.

puzzle2d

A spark slides until a wall stops it and lights every lamp it passes; lighting all of a level's lamps within its moves wins the level, and a slide into a void or running out of moves loses it. The template shows how a puzzle game grows: the rules in board.go, where each move is an undo.History move that undoes and redoes exactly; the levels in levels.yaml, a versioned YAML file with unknown fields refused and a canonical form, which levels.go reads and writes; the input map in game.yaml, bound into input.Actions with swipes from input.GestureRecognizer; moves animated with tween on a fixed-step game.Loop; and the stars of each level kept with save. To change the game, add or edit levels in levels.yaml, whose tests check that every level can be won in its par; rebind the controls in game.yaml; add a tile in board.go and draw it in draw.go. The template's README lists the controls, the level format and every file.

rhythm

Notes fall down four lanes to the beat of a song, and the player hits each lane as its note reaches the line; hits build a combo and fill the energy bar, misses drain it, and the chart is cleared by reaching the song's end with energy left and failed by running out. The template shows how a rhythm game grows: the song list, the judgement windows and the energy rules in songs.yaml, a versioned YAML file with unknown fields refused and a canonical form, which songs.go reads and writes, with each song's tempo in a rhythm beat map beside it; the rules in run.go, a Run that judges hits by track position and knows no clock, so its tests drive it exactly; the music played through an audio.Player and followed by a rhythm.Clock, with the template unwrapping the position a looping track shorter than the song reports (unwrapLoop) before passing it to rhythm.Clock.Sync; hits judged by when the key or the finger went down rather than by the game step that reads them; latency calibration on rhythm.CalibrationScreen, through the same input as the charts, kept per device by rhythm.OffsetStore; the input map in game.yaml, bound into input.Actions with each lane's column bound to touches and the mouse; and the best clear of each chart kept with save. To change the game, chart a new song in songs.yaml with its beat map and music under songs/, whose tests check the file is canonical and every chart fair; tune the windows and the energy there too; rebind the controls in game.yaml; and change how the playfield looks in draw.go. The template's README lists the controls, the songs format and every file.

shmup2d

shmup2d is one stage of a vertical shooter on a 270×480 portrait playfield. Enemy waves fly in on dive, weave, cross and hover paths and fire bullets patterns; destroying a whole wave drops a power, bomb or life item; a boss with three phases ends the stage. Defeating the boss is the win, losing the last life the loss. It plays with the keys or WASD, a controller's stick, D-pad and buttons, and by dragging with a finger or the mouse, with a bomb button on touch screens.

  • data/stage.yaml is the stage: the ship's speeds, lives, bombs and weapons, the enemy kinds with their paths and patterns, the waves with their times, places and drops, and the boss with its phases. stage.go defines the format, with a version, unknown fields refused and a canonical encoding, which a test and a fuzz test hold to.
  • data/patterns.yaml holds every bullet pattern in the bullets format and data/explosion.yaml the explosion in the particles format.
  • world.go is a run of the stage: the ship, enemies, waves, items and boss, and the systems that step them, which take one step's controls and report the events the game plays sounds for. controls.go turns the input map and the drag into those controls, game.go and hud.go hold the loop, the title, play and end screens and the native HUD.
  • The sprites are painted in code and the sounds come from the placeholder pack; CREDITS.md lists them.
  • The tests play the stage through nsgotest: a pilot that dodges and shoots clears it with a controller, and a ship left idle is shot down through the keyboard. Other tests cover touch, the mouse, pausing, every system of the world and the data files.

Its README.md lists the controls and where to change the stage, the patterns, the paths, the items, the art and the sound.

visualnovel

Lantern Crossing: a cartographer has to get her map across a foggy river before dawn, and the ferryman decides how far to trust her. Two choices earn or lose his trust and the third, at a fork in the river, reaches the harbor or the fog, so the story has a good ending and a lost one.

It is built on vn, which presents the script: portraits with expressions, a text box with name plates and a typewriter, choices, the backlog, and auto and skip modes; flow, whose dialogue runner plays the Yarn Spinner script and keeps its variables; save, for three save slots and the player's settings; and l10n, for English and Simplified Chinese string tables. The backgrounds and portraits are drawn in code, so it needs no image files. The keyboard, a controller and touch each play all of it, from the input map in game.yaml.

File What it holds
story/story.yarn The story. Speakers are cast ids, every line and option has a #line: id, <<bg name>> changes the background, <<ending name>> names the ending, and each node's chapter: header names what a save slot shows.
story/cast.yaml The characters: name plate colors, stage slots and expressions, in vn's cast format.
strings/en.yaml, strings/zh-Hans.yaml The interface's strings and the names, chapters and endings in each language. English lines are the script's own text; another language translates each line as line.<id>.
story.go Loads the story files and shows lines and name plates in the player's language.
novel.go The game: the title screen, the story and its menus, input, save slots and settings.
ui.go, stage.go The buttons and menus over the stage, and the drawn backgrounds and portraits.
story_test.go, game_test.go Checks that the story files agree (every line translated, every background and ending known, every path ending in an ending), and plays both endings with the keyboard, a controller and touch, saving and loading on the way.

A save slot keeps the options chosen since the start, the id of the line shown and how many times that line had shown since the last choice, and loading replays that path, because the dialogue runner's saved state holds its variables and visited nodes but not where in a node it stopped. Saving over a used slot asks first. The lines read and the endings found are player settings, kept apart from the slots and saved again when the app leaves the front or quits, so skip mode knows the lines read in every playthrough. The text box keeps clear of a phone's notch and home indicator, and the backlog's Older and Newer buttons scroll it for touch and the mouse.

To make the template your own, write a story in story/story.yarn and run its tests, which name every line without a translation and every background, ending or chapter the game does not know; add characters to story/cast.yaml with a look in stage.go, backgrounds to backgrounds in stage.go, endings to endings in story.go, and a language as a string table listed in story.go. The template's README.md lists the controls.

Create a project from a template

nsgo game new -list
nsgo game new -template starter mygame

Outside a project, install nsgo-game once (go install github.com/nsgo-dev/game/cmd/nsgo-game@latest) for nsgo game (Command Line), or run go run github.com/nsgo-dev/game/cmd/game@latest new directly with nothing to install.

Without -template, game new uses starter. A name that is not in the list is refused with the names that are. Shell completion for game (Command Line) completes -template with the same names. Command Line describes the other flags.

NSGo's own command writes a template from its module without the game tool, with an NSGo command that supports Go module templates, such as the installed nsgo of v0.3.4 or later (Command Line):

nsgo init -template github.com/nsgo-dev/game/template -module example.com/mygame mygame

Use the module the table names for each template, and add @<version> to pin one. The project's game.yaml and nsgo.yaml keep the template's own game name and identifier until you edit them, where game new sets them from the directory or -name.

How the templates are laid out

Each template is a Go module of its own in this repository, because nsgo init -template writes the template directory at the root of the module it is given. starter is the template/ module itself; every other template is the module github.com/nsgo-dev/game/template/<name> in template/<name>/:

template/
├─ go.mod                     module github.com/nsgo-dev/game/template
├─ template/                  starter
└─ <name>/
   ├─ go.mod                  module github.com/nsgo-dev/game/template/<name>
   └─ template/               the <name> template
      ├─ go.mod.tmpl
      ├─ go.sum
      ├─ game.yaml
      ├─ nsgo.yaml
      ├─ main.go
      ├─ …_test.go
      ├─ README.md
      └─ .gitignore
internal/gamecli/
├─ templates.go               the registry: one entry per template
└─ _templates/projects/
   ├─ starter/                a copy of template/template
   └─ <name>/                 a copy of template/<name>/template

game new embeds the copies under _templates/projects, so it works from the module cache without this repository. The repository's go.work uses every template module, so from a checkout go tool nsgo dev ./template/<name>/template runs a template and go tool nsgo test ./template/<name>/template tests it against the local game layer.

Add a template

A template is added in one pull request of its own:

  1. Create the module: template/<name>/go.mod declaring module github.com/nsgo-dev/game/template/<name> with the go line of template/go.mod, and the project in template/<name>/template/. Its name is lower-case letters, digits and -, starting with a letter, and not template.
  2. Add ./template/<name> to the use list of go.work.
  3. Copy the project into the game CLI package, from internal/gamecli: cp -R ../../template/<name>/template _templates/projects/<name>.
  4. Add one entry to projectTemplates in internal/gamecli/templates.go: newProjectTemplate("<name>", templateModuleRoot+"/<name>", "<one line saying what the game is>").
  5. Add a row for the template to the table above, in docs/en/templates.md and docs/zh/templates.md, and a fragment under changelog.d/.

The project in template/<name>/template/ follows these rules, which the tests check where they can:

  • go.mod.tmpl starts with module {{.Module}}, requires a published version of github.com/nsgo-dev/game that has every package the template uses, and has tool github.com/nsgo-dev/nsgo/cmd/nsgo. It is the only file with a template action: game new and nsgo init render nsgo.yaml, main.go and any other .tmpl file too, but CI renders only go.mod.tmpl and the template must also build as it is in the workspace, so each of those must render to itself, and main.go cannot hold {{, as a composite literal such as []T{{...}} does; put such code in another Go file. Other files are copied as they are and may hold any bytes. A file named go.mod would make the directory a module of its own, which neither go:embed nor the workspace can include.
  • go.sum holds the sums of the pinned modules. Write it by turning go.mod.tmpl into a go.mod in a copy of the template outside the repository and running GOWORK=off go mod tidy there.
  • game.yaml is canonical, as go run github.com/nsgo-dev/game/cmd/gameproject check reports, and nsgo.yaml agrees with it; game new replaces the name and id of both.
  • main.go, README.md, .gitignore and tests are there, and go run ./cmd/game index template from the repository root has nothing left to do.
  • Every file the template's author writes is text, as for any project in this repository.

The tests of internal/gamecli fail when any of these steps is missing: when the copy under _templates/projects differs from the module's template directory by a byte, when a template/*/template directory has no registry entry, when a registry entry has no copy, module or go.work entry, and when this page or its Chinese counterpart has no row naming a template and its module. They also create a project from every template, build it against the local game layer and run its tests.

CI's templates job needs no edit for a new template: it lists template/template and every template/*/template directory and tests each template the way a developer's project gets it: it copies the template out of the workspace, renders go.mod for example.com/<name>game, and runs go tool nsgo test ./... with GOWORK=off, so the project builds against the game layer version go.mod.tmpl pins rather than this checkout. A template that needs a package not yet published bumps that pin once the package is on main.

Modifier cette page