README
¶
NSGo Game Platformer Template
A small 2D side-scrolling platformer built with NSGo and github.com/nsgo-dev/game. A lantern bearer runs and jumps through three rooms of Lantern Hills, collects sparks, lights a lamp that becomes the respawn point and reaches the beacon to win. Spikes and pits cost a life; losing the last one loses the run.
It shows:
- A
platformer.Controllerover aphysics.CharacterBody: run, variable-height jumps, coyote time, jump buffering, one-way ledges, wall slides and wall jumps, tuned bydata/player.yaml. - A level in the scene file
scenes/level1.scene.yaml, which the editor opens: a tile layer painted with the YAML tilesettilesets/terrain.tileset.yaml, whose tiles say which are solid, one-way and hurting, and the player, sparks, lamp and beacon placed as objects. The game builds it into aphysics.Gridto collide with, atilemap.Tilemapto draw andphysics.Triggersensors for spikes, sparks, the lamp and the beacon. - Rooms with their own camera bounds from
data/rooms.yaml: arooms.Directorslides atilemap.Camerabetween them and the player waits while it does. - One input map in
game.yamlfor the keyboard and game controllers, bound into aninput.ActionswithProject.BindInput, paired withinput.TouchControlsfromcontrols.touch.yamlso on-screen touch controls sit inside the safe area in view coordinates. - A 320 by 180 virtual screen scaled by whole multiples, a native HUD and end-of-run panel over the canvas, the tiles in
tilesets/terrain.pngand the player drawn by code. - Tests that play the level through with the keyboard, a controller and touch to a win, run into the spikes to a loss, and check the data files, the rules and the controller's timing.
Controls
| Keyboard | Controller | Touch | |
|---|---|---|---|
| Run | Left and right arrows, A and D | Left stick, d-pad | The d-pad at the bottom left (slide between left and right) |
| Jump (hold to jump higher) | Space, up arrow, W | South button (A on Xbox, Cross on PlayStation) | The JUMP button at the bottom right |
| Pause and resume | Escape or P | Start | The Pause button in the HUD |
| Play again after the run ends | R, Enter | Start | The Play again button |
The on-screen controls appear from the start on phones and tablets, stay hidden on desktop until the screen is touched, and hide again when a key or controller is used.
When the active controller disconnects and no other controller is connected, the run pauses. Reconnect the original controller, press a button on another controller, or press a key to clear the alert. A manual pause stays active. Edit the alert's strings in strings/.
The run also pauses when the window loses focus or the app goes to the background, and the pause menu stays until Resume is pressed.
Running and testing
go tool nsgo dev # the desktop, with hot reload
go tool nsgo dev -target web # the browser
go tool nsgo dev -target ios_simulator
go tool nsgo dev -target android
go tool nsgo test ./... # headless tests
go tool game playtest # automated bot playtest and report
go tool nsgo check -target macos,windows,linux,android,web
GOFLAGS=-tags=release go tool nsgo build -target macos_arm64,windows_x64,linux_x64,web
Debug the game's Go code with Delve (dlv on PATH):
go tool nsgo dev -target native -debug
Attach from VS Code with the Attach to game configuration in .vscode/launch.json, replacing 40000 with the port in Delve's API server listening at line, or from GoLand with a Go Remote configuration at the same host and port.
The game's settings live in game.yaml, its project file. Edit the name and identifier there before shipping it, then write them into nsgo.yaml and check the project:
go run github.com/nsgo-dev/game/cmd/game project sync
go run github.com/nsgo-dev/game/cmd/game project check
Layout
game.yaml: the project file, with the window, the virtual screen, the input map and the export presets.controls.touch.yaml: the on-screen touch layout (a bottom-left d-pad and a bottom-right jump button) read byinput.TouchControls.scenes/level1.scene.yaml: the level, the main scenegame.yamlautoloads: its tiles, the player (with the lives a run starts with in itslivesproperty), the sparks, the lamp and the beacon.tilesets/terrain.tileset.yamlandtilesets/terrain.png: the tiles and what each does: grass and dirt aresolid, the ledge isoneWay, and the spikes havehurt: true.CREDITS.md: the author and license of each image; list your own there when you add them.data/rooms.yaml: the rooms, in world points, and the camera transition into them.data/player.yaml: the controller's tuning.data.go: embeds and parses the scenes, tilesets and data files.level.go: reads the level from its scene into the grid a run plays, and checks it.world.go: a run of the level: collision grid, player, sensors, lives, checkpoint, goal and rooms, stepped once per fixed step and independent of devices and drawing.game.go: the root component, the fixed-step loop, input sampling, the HUD and the end-of-run panel.draw.goandart.go: drawing the run, and the player frames drawn by code.main.goandproject.go: the entry point and the embeddedgame.yaml.playtest.yamlandplaytest_test.go: theplatformer-runnerplaytest configuration andTestPlaytestadapter driven bygo tool game playtest.*_test.go: playthroughs, rules, controller timing, the level scene and the data files.
Where to change it
- Make a level: open
scenes/level1.scene.yamlin the editor (go tool game editor), paint tiles on thegroundtile layer and move or add objects on theactorslayer. Each object has akindproperty,player,checkpoint,sparkorgoal, and stands in the empty tile its position falls in; the level has one player and one goal, and a solid or one-way tile under the player and every checkpoint. Keepdata/rooms.yamlcovering every column.TestLevelSceneIsCanonical,TestProjectPassesAssetCheckandTestRoomsCoverTheLevelsay what is wrong. If you change the route, change the playthrough'sobstacleAheadingame_test.gotoo, or give it a list of jumps. - Change how the player feels:
data/player.yaml. Raisecoyote_timeandjump_bufferfor a more forgiving jump, setwall_jump_speed_y: 0to turn wall jumps off. - Add a tile: draw it into
tilesets/terrain.png, raisecolumnsandtileCountin the tileset and mark itsolid,oneWayorhurtthere. A new kind of rule takes a property in the tileset, a glyph and a case inlevelFromSceneand one innewWorldfor what it does. - Add enemies or moving platforms: give each a
physics.Body, move it inworld.step, pass moving platforms toController.Stepand add a sensor that setsw.hurt. - Rebind a control: the
inputsection ofgame.yamlfor keyboard and controller, andcontrols.touch.yamlfor the on-screen touch controls. - Replace the art: redraw
tilesets/terrain.png, and load an atlas withsprite.LoadAtlasinnewPlayerAtlas; then rungo tool game indexand updateCREDITS.md. - Add sound with
sound.Mixer, configured from anaudiosection ingame.yaml.
Git setup
Each clone enables the object-by-object scene merge driver once (.gitattributes names it, while Git keeps driver config local to .git/config):
go tool game merge -setup