Эта страница ещё не переведена на русский язык, поэтому ниже показан английский оригинал.
РуководстваThe platformer2d template
platformer2d is a small side-scrolling platformer: a lantern bearer runs and jumps through the 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, and losing the last one loses the run. It is built on platformer, physics, tilemap, rooms, input, sprite and screen, and its art is drawn by code.
The template includes scenes/level1.scene.yaml as the editor-openable entry scene. The runtime grid in data/level.yaml remains authoritative while collision and gameplay rules migrate to scene components.
go run github.com/nsgo-dev/game/cmd/game@latest new -template platformer2d mygame
cd mygame
go tool nsgo dev
go tool game playtest
nsgo init -template github.com/nsgo-dev/game/template/platformer2d writes the same project, as Templates describes.
What it shows
- A
platformer.Controllerover aphysics.CharacterBodywith variable-height jumps, coyote time, jump buffering, one-way ledges, wall slides and wall jumps, all tuned bydata/player.yaml. - A level drawn as text in
data/level.yaml, built into aphysics.Gridto collide with, atilemap.Tilemapto draw andphysics.Triggersensors for the spikes, sparks, lamp and beacon. - Rooms with their own camera bounds from
data/rooms.yaml: arooms.Directorslides the camera from one room to the next, and the player waits until it lands. - One input map in
game.yamlfor the keyboard and game controllers, bound withProject.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, with a native HUD and an end-of-run panel over the canvas, requesting landscape orientation on phones (
cx.RequestOrientation(core.OrientationLandscape)). - Tests that play the level through to a win with the keyboard, a controller and two-finger touch, run into the spikes until the run is lost, and check the rules, the controller's timing and that every data file is canonical.
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 | 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.
How it is organised
| File | What it holds |
|---|---|
game.yaml |
The project file: 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 by input.TouchControls. |
data/level.yaml |
The level: one character per tile, its tile size and the lives a run starts with. |
data/rooms.yaml |
The rooms in world points and the camera transition into them, in the format of rooms. |
data/player.yaml |
The controller's tuning, in the format of platformer. |
level.go |
The level format: parsing, validation and canonical encoding. |
world.go |
One run of the level: the collision grid, the player, the sensors, the lives, the checkpoint, the goal and the rooms, stepped once per fixed step without knowing about devices or drawing. |
game.go |
The root component, the fixed-step loop, input sampling, the HUD and the end-of-run panel. |
draw.go, art.go |
Drawing a run, and the tileset and player frames drawn by code. |
playtest.yaml, playtest_test.go |
The platformer-runner playtest configuration and TestPlaytest adapter driven by go tool game playtest. |
*_test.go |
The playthroughs, the rules, the controller's timing and the data files. |
The level is plain text, one row per line:
version: 1
name: Lantern Hills
tileSize: 16
lives: 3
tiles: |
#....................#
#.P....^^....o.......#
######################
| Glyph | Tile |
|---|---|
. |
Empty |
# |
Ground, solid from every side |
= |
A ledge you can jump up through |
^ |
Spikes, which cost a life |
o |
A spark to collect |
C |
A checkpoint lamp |
G |
The goal beacon, exactly one |
P |
Where the player starts, exactly one |
name is shown on the panel at the end of a run. Every row has the same width, and every P and C has ground or a ledge under it. Unknown fields, unknown glyphs and a version other than 1 are refused, and the game writes the file back byte for byte, so a review shows only real edits.
Where to change it
The editor opens scenes/level1.scene.yaml as the level1 autoloaded main scene. It places the player start, the lamp and the beacon on the cells data/level.yaml gives them, and TestLevelSceneMatchesTheLevel fails when the two disagree. The tiles, and every rule, still come from data/level.yaml; moving a marker in the scene does not move it in the game yet.
- A new level: edit the tiles in
data/level.yamland keepdata/rooms.yamlcovering every column;parseLevelrefuses a start (P) or lamp (C) with nothing to stand on, andTestDataFilesAreCanonicalandTestRoomsCoverTheLevelreport what is wrong. The playthrough test jumps at spikes, pits and walls ahead of the player, so a route that needs more than that needs a change toobstacleAheadingame_test.go. - How the jump feels:
data/player.yaml. A longercoyote_timeorjump_bufferforgives late and early presses, andwall_jump_speed_y: 0turns wall jumps off. - A new tile kind: a glyph in
level.go, a case innewWorldfor what it does and one innewTilemapfor how it looks. - Enemies and moving platforms: give each a
physics.Body, move it inworld.step, pass moving platforms toController.Step, and add a sensor that setshurt. - Controls: the
inputsection ofgame.yamlfor keyboard and controller, andcontrols.touch.yamlfor the on-screen touch controls. - Art: load an atlas with
sprite.LoadAtlasinnewPlayerAtlasand a tileset image innewTilemap, or load the whole level from a Tiled map withtilemap.LoadTiled. - Sound: a
sound.Mixerconfigured from anaudiosection ingame.yaml. The runtime loadsscenes/level1.scene.yamlalongside the grid and uses itskind: player,kind: checkpoint, andkind: goalmarkers for the corresponding positions. The grid remains the source for collision tiles and collectible placement while the scene gives the editor-visible entry markers a runtime path.