platformer2d

command
v0.4.0-rc.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 30 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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.Controller over a physics.CharacterBody: run, variable-height jumps, coyote time, jump buffering, one-way ledges, wall slides and wall jumps, tuned by data/player.yaml.
  • A level in the scene file scenes/level1.scene.yaml, which the editor opens: a tile layer painted with the YAML tileset tilesets/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 a physics.Grid to collide with, a tilemap.Tilemap to draw and physics.Trigger sensors for spikes, sparks, the lamp and the beacon.
  • Rooms with their own camera bounds from data/rooms.yaml: a rooms.Director slides a tilemap.Camera between them and the player waits while it does.
  • One input map in game.yaml for the keyboard and game controllers, bound into an input.Actions with Project.BindInput, paired with input.TouchControls from controls.touch.yaml so 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.png and 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 by input.TouchControls.
  • scenes/level1.scene.yaml: the level, the main scene game.yaml autoloads: its tiles, the player (with the lives a run starts with in its lives property), the sparks, the lamp and the beacon.
  • tilesets/terrain.tileset.yaml and tilesets/terrain.png: the tiles and what each does: grass and dirt are solid, the ledge is oneWay, and the spikes have hurt: 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.go and art.go: drawing the run, and the player frames drawn by code.
  • main.go and project.go: the entry point and the embedded game.yaml.
  • playtest.yaml and playtest_test.go: the platformer-runner playtest configuration and TestPlaytest adapter driven by go 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.yaml in the editor (go tool game editor), paint tiles on the ground tile layer and move or add objects on the actors layer. Each object has a kind property, player, checkpoint, spark or goal, 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. Keep data/rooms.yaml covering every column. TestLevelSceneIsCanonical, TestProjectPassesAssetCheck and TestRoomsCoverTheLevel say what is wrong. If you change the route, change the playthrough's obstacleAhead in game_test.go too, or give it a list of jumps.
  • Change how the player feels: data/player.yaml. Raise coyote_time and jump_buffer for a more forgiving jump, set wall_jump_speed_y: 0 to turn wall jumps off.
  • Add a tile: draw it into tilesets/terrain.png, raise columns and tileCount in the tileset and mark it solid, oneWay or hurt there. A new kind of rule takes a property in the tileset, a glyph and a case in levelFromScene and one in newWorld for what it does.
  • Add enemies or moving platforms: give each a physics.Body, move it in world.step, pass moving platforms to Controller.Step and add a sensor that sets w.hurt.
  • Rebind a control: the input section of game.yaml for keyboard and controller, and controls.touch.yaml for the on-screen touch controls.
  • Replace the art: redraw tilesets/terrain.png, and load an atlas with sprite.LoadAtlas in newPlayerAtlas; then run go tool game index and update CREDITS.md.
  • Add sound with sound.Mixer, configured from an audio section in game.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

Documentation

Overview

Command mygame is a 2D side-scrolling platformer built with NSGo and game.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL