Ebitengine 2D game development guide (ebitengineer) #
Genre-agnostic engineering guidelines for building modular, high-performance, cross-platform 2D games (top-down, tactics, shmup, puzzle, isometric, and platformer) in Go using Ebitengine v2 (github.com/hajimehoshi/ebiten/v2).
Trigger conditions #
Activate this skill whenever:
- Designing or implementing 2D game architecture, scene flow, camera systems, or rendering pipelines in Go.
- Working with Ebitengine interfaces (
ebiten.Game), state machines, input management, sprite animation controllers, audio streams, Kage shaders, orebiten/v2/text/v2typography. - Compiling games to native desktop and WebAssembly (
GOOS=js GOARCH=wasm), optimizing cycle timing, synchronizing WASM audio, persisting saves, hosting on Cloud Run, or writing unit tests for Go game components.
Companion skills #
ebitengineer works alongside these focused skills when building a complete 2D game:
vibe-game-developerandgame-design:vibe-game-developerorchestrates the end-to-end build pipeline whilegame-designdefines the Game Design Document (GDD), core loop, virtual resolution (320x180,640x360,1280x720, or1920x1080), state progression, and feel budgets.ebitengineerturns those specifications into decoupled Go packages underinternal/.sprite-design: Supplies declarative.spritefiles, binary.asepritev1.3 containers, or normalized.png+.jsonsprite sheets at any resolution (retro pixel art like32x32and64x64, or native 2D HD like256x256and512x512). Pre-slice every grid cell once at boot (NewGridSpriteSheet) and drive playback with a zero-allocationAnimationController(Update(dt)inUpdate()andDraw(screen, x, y, flipX)inDraw()).procedural-sfx,procedural-music, andlyria: Shares a single44100Hz Ebitengineaudio.Contextacross procedural.sfxcues,.musicBGM scores, andlyriaAI audio streams. Decode or synthesize all audio into 4-byte aligned 16-bit little-endian stereo PCM byte slices (len(pcm) &^ 3) during boot, enforce per-cue SFX cooldowns inUpdate(), and seek BGM streams to 4-byte aligned offsets (offset &^ 3) on first user gesture in WebAssembly.godoctor,swarm-coding, andparallel: Usegodoctorfor Go static analysis, AST inspection, and build verification across desktop and WASM targets. When building multiple decoupled subsystems (internal/physics,internal/camera,internal/ui,internal/tilemap,internal/ai,internal/save), define package interfaces first and implement and unit-test each package independently.
Core architecture and fail-fast engineering rules #
1. Modular architecture (internal/) #
Organize the codebase into modular subsystems under internal/. Never build monolithic single-file games.
- For the complete directory tree and package responsibilities, see
references/project_structure.md.
2. Fail-fast asset embedding and validation (no silent fallbacks) #
- Zero-dependency embedding: Embed all game assets (
.sprite,.aseprite,.pngsprite sheets,.sfxand.musicscores, TTF/OTF fonts, Kage shaders) directly into the compiled Go binary usingembed.FS(internal/assets). - No silent fallbacks: Never ignore errors (
_ = err), never substitute dummy1x1fallback textures when an asset fails to decode, and never silently clamp out-of-bounds tile or frame IDs. Always fail fast and return richfmt.Errorfdiagnostics duringBootinitialization. - Mandatory binary format validation: Always validate asset files using file identification tools (
file,mimetype, orhttp.DetectContentTypein Go) during asset ingestion. Misnamed or corrupt asset files cause decoding failures or black screens on WebAssembly.
3. Premultiplied alpha invariant (color.RGBA vs. color.NRGBA) #
- In Go’s
image/colorand Ebitengine,color.RGBAis alpha-premultiplied: every channel must satisfy $R \le A$, $G \le A$, $B \le A$. - Never construct non-premultiplied literals like
color.RGBA{255, 0, 0, 100}(where $R > A$). Use valid premultipliedcolor.RGBA{100, 0, 0, 100}or straight-alphacolor.NRGBA{255, 0, 0, 100}.
4. Aspect ratio and virtual pixel canvas #
- 16:9 widescreen priority: Unless specified otherwise, target a 16:9 virtual resolution (
320x180,640x360,1280x720,1920x1080). - Virtual pixel canvas: All entity physics, collision math, camera coordinates, Kage shader pixel units, and UI layouts operate strictly in virtual canvas coordinates.
- Automatic multi-resolution scaling:
Layout(outsideWidth, outsideHeight int)returns constant virtual dimensions (virtualWidth, virtualHeight).
5. Cycle timing and frame synchronization #
- Target frame rate: Standard target is 60 TPS (
ebiten.SetTPS(60)). - Delta-time integration: Integrate movement, camera exponential decay, and timers using delta time ($dt = 1.0 / 60.0$ or measured frame delta) for consistent behavior across refresh rates.
- Windowing and fullscreen toggle: Support fullscreen toggling via
F11orAlt+Enter:
if inpututil.IsKeyJustPressed(ebiten.KeyF11) || (ebiten.IsKeyPressed(ebiten.KeyAlt) && inpututil.IsKeyJustPressed(ebiten.KeyEnter)) {
ebiten.SetFullscreen(!ebiten.IsFullscreen())
}Sprite, audio, and WebAssembly runtime patterns #
1. Zero-allocation sprite and animation playback #
Whether rendering low-res pixel art (32x32, 64x64) or high-res 2D HD sprites (256x256, 512x512) from sprite-design, pre-slice all frames from the atlas once during Boot initialization and advance a zero-allocation AnimationController in Update():
// Pre-slice grid cells once at load time (never call SubImage inside Draw)
sheet, err := NewGridSpriteSheet(atlasImage, frameWidth, frameHeight)
if err != nil {
return fmt.Errorf("boot: slice sprite sheet: %w", err)
}
ctrl, err := NewAnimationController(sheet)
if err != nil {
return fmt.Errorf("boot: init animation controller: %w", err)
}
if err := ctrl.AddTag("idle", 0, 3, 150*time.Millisecond, LoopForward); err != nil {
return err
}
if err := ctrl.Play("idle"); err != nil {
return err
}- Pixel art vs. 2D HD filtering: Use
ebiten.FilterNearestwhen drawing pixel-art sprites or1:1native 2D HD frames so pixels stay crisp. Useebiten.FilterLinearwhen scaling down high-resolution256x256or512x5122D HD sprites onto a smaller gameplay canvas.
2. Unified 44.1 kHz stereo PCM audio #
Create a single audio.NewContext(44100) for the entire game process. Ebitengine requires all raw PCM streams to use 16-bit signed little-endian stereo PCM (4 bytes per sample frame: 2 bytes Left + 2 bytes Right):
- Always mask buffer lengths and seek offsets with
&^ 3(aligned := length &^ 3) before passing them toaudio.NewInfiniteLoop,audio.NewInfiniteLoopWithIntro,ctx.NewPlayerFromBytes, orSeek(). - Pre-synthesize
.sfxand.musicscores to[]bytebuffers duringBootinitialization (never insideUpdate()orDraw()), and enforce a per-cue cooldown (such as40msto80ms) on rapid SFX triggers to prevent voice stacking clipping.
3. Dual-target CLI build verification #
Always verify both the test suite and the WebAssembly compilation target before shipping:
go test ./...
GOOS=js GOARCH=wasm go build -o /dev/null ./cmd/gameEngine reference modules #
Consult these reference guides for complete, zero-allocation, fail-fast Go implementations:
| Module | Reference File | Key Topics Covered |
|---|---|---|
| Project structure | references/project_structure.md | Package tree and internal/ subsystem responsibilities. |
| Camera and game feel (“Juice”) | references/camera_and_juice.md | 2D ebiten.GeoM camera, exponential lerp, trauma screen shake, hitstop freeze frames, squash-and-stretch springs, Transform2D pivot matrix order, easing curves, and zero-alloc ParticleSystem pools. |
Kage shaders and text/v2 | references/shaders_and_text_v2.md | Kage //kage:unit pixels shaders (2D lighting, CRT, hit-flash) and ebiten/v2/text/v2 font loading and aligned layout. |
| WASM audio and save storage | references/wasm_audio_and_persistence.md | 4-byte aligned (&^ 3) 44.1 kHz stereo PCM playback, SFXBank cooldown throttling, WASM autoplay unlock Seek(), and build-tagged desktop vs. WASM save storage. |
| UI and HUD system | references/ui_and_hud.md | Premultiplied alpha invariant (R <= A), pre-sliced 9-slice panels, anchoring math, zero-alloc progress bars, and text/v2 buttons. |
| Physics and collision | references/physics_and_collision.md | Genre-agnostic Circle and AABB overlap, axis-separated sweep resolution, spatial hash grids, and slope math. |
| Tilemaps and levels | references/tilemaps_and_levels.md | Pre-sliced tileset validation, camera frustum culling, isometric projection, and 16-pipe bitmask autotiling. |
| Input action mapping | references/input_action_mapping.md | Rebindable action maps, radial analog deadzones, and zero-alloc gamepad polling. |
| Entity management | references/entity_management.md | Pre-allocated slice pools, deferred deletion buffers, and Structure-of-Arrays (SoA) Light ECS. |
| Pathfinding and AI | references/pathfinding_and_ai.md | Fail-fast A* grid pathfinding, steering behaviors (seek and arrive), and enemy decision FSM. |
| Server and Cloud Run | references/server_architecture.md | WebAssembly build, multi-stage Dockerfile, hardened HTTP server, and Cloud Run leaderboard REST API. |
Game state machine and scene flow #
Finite state machine (FSM) lifecycle #
Model all scenes using a State interface with strict lifecycle hooks (Enter() error, Update(dt float64) error, Draw(screen *ebiten.Image), Exit() error).
Standard scene progression #
Boot (Logo & Asset Preload) -> Intro -> Title Screen -> Game Play -> Game Win / Game Over -> Title ScreenTransition cleanliness (no leaks) #
Upon Exit(), every state must:
- Stop or fade out BGM/SFX channels belonging to that scene.
- Flush active particle emitters, animations, hitstop timers, and camera trauma.
- Reset transient input buffers so button presses do not bleed into the next state.
Attract and demo mode (arcade style) #
- Idle timeout: On
Title Screen, if no input arrives after a timeout (such as10s), transition toDemo Mode(autonomous AI/CPU plays the game). - Instant interrupt: Any user input in
Demo Modeinterrupts execution immediately back toTitle Screen. - Alternating flow: Consecutive
Title Screenidle timeouts alternate between launchingDemo Modeand re-playingIntro.
WebAssembly (WASM), shaders, and typography rules #
- Audio late-sync (4-byte PCM alignment): Browser WebAudio stays locked until the first user gesture. Track elapsed time during silent load and
Seek()to a 4-byte aligned stereo PCM byte offset (((samples * 4) % loopLengthBytes) &^ 3) on first interaction. Seereferences/wasm_audio_and_persistence.md. - Custom typography (
ebiten/v2/text/v2only): Never useebitenutil.DebugPrintfor game UI and never use deprecatedebiten/v2/textv1. Always load TTF/OTF fonts viatext.NewGoTextFaceSource,text.GoTextFace, andtext.Drawfromgithub.com/hajimehoshi/ebiten/v2/text/v2. Seereferences/shaders_and_text_v2.md. - Kage shaders (
//kage:unit pixels): Always include//kage:unit pixelsin Kage shaders and pre-allocateebiten.DrawRectShaderOptionsand uniform maps at load time. Seereferences/shaders_and_text_v2.md. - Build-tagged save persistence: Implement separate
//go:build !js(atomic file write inos.UserConfigDir()) and//go:build js && wasm(window.localStorage) storage backends behind a common fail-fast interface. Seereferences/wasm_audio_and_persistence.md.
Unit testing and performance rules #
- Zero allocations in
Draw(): Never callebiten.NewImage(),SubImage(),ebiten.NewShader(), or allocate slices or maps insideDraw(). Pre-allocate allebiten.DrawImageOptions,text.DrawOptions,Uniforms, andSubImageframe slices during initialization. - Decouple logic and render: Keep all state mutation strictly inside
Update().Draw()must be a read-only projection of current game state. - Unit test coverage: Write deterministic unit tests for state transitions, collision and spatial hash math, camera lerp and trauma decay, 4-byte PCM alignment, A* pathfinding, and save serialization.
