Skip to content

gamedev-skills/awesome-gamedev-agent-skills / skills/ebitengine/ebitengine-game

ebitengine-game

Verified

Creates, debugs and packages 2D games in Go with Ebitengine v2: a play-and-retry loop, input, pixel-art scaling, text, audio and desktop, WASM or mobile builds. Use when go.mod requires ebiten/v2 or the user wants a game in Go, or on blurry pixel art, missed key presses or stutter. Not for engine patches; pair with `game-feel`, `game-ui-ux` or a genre skill for design.

assets/starter/game.go

View on GitHub
package main

import (
	"fmt"
	"image"
	"image/png"
	"os"

	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/inpututil"
)

// Scene is one screen of the game. Update changes state; Draw only reads it.
type Scene interface {
	Update(g *Game) error
	Draw(screen *ebiten.Image)
}

type Options struct {
	ShotPath string
	ShotTick int
	Demo     bool
}

// Game implements ebiten.Game and owns what every scene shares.
type Game struct {
	opts   Options
	assets *Assets
	sound  *Sound
	input  *Input

	scene, next Scene
	tick        int // number of Update calls so far: the game's clock
	shotDone    bool
	shotErr     error
}

func NewGame(opts Options) (*Game, error) {
	a, err := LoadAssets()
	if err != nil {
		return nil, err
	}
	g := &Game{opts: opts, assets: a, sound: NewSound(), input: NewInput(opts.Demo)}
	g.scene = newTitleScene(g)
	return g, nil
}

// SetScene switches scenes at the start of the next tick, never mid-frame.
func (g *Game) SetScene(s Scene) { g.next = s }

func (g *Game) Update() error {
	if g.shotDone {
		if g.shotErr != nil {
			return g.shotErr
		}
		return ebiten.Termination // ends RunGame without an error
	}
	if g.next != nil {
		g.scene, g.next = g.next, nil
	}
	g.tick++
	g.input.Update(g.tick)

	alt := ebiten.IsKeyPressed(ebiten.KeyAltLeft) || ebiten.IsKeyPressed(ebiten.KeyAltRight)
	if inpututil.IsKeyJustPressed(ebiten.KeyF11) || (alt && inpututil.IsKeyJustPressed(ebiten.KeyEnter)) {
		ebiten.SetFullscreen(!ebiten.IsFullscreen())
	}
	return g.scene.Update(g)
}

func (g *Game) Draw(screen *ebiten.Image) {
	g.scene.Draw(screen)
	if g.opts.ShotPath != "" && !g.shotDone && g.tick >= g.opts.ShotTick {
		if err := savePNG(screen, g.opts.ShotPath); err != nil {
			g.shotErr = fmt.Errorf("save screenshot: %w", err)
		}
		g.shotDone = true // Update returns Termination on the next tick
	}
}

// Layout returns a fixed logical size, so game code never deals with window
// size; Ebitengine letterboxes and scales the screen.
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
	return screenW, screenH
}

// savePNG reads the screen back from the GPU. It is slow; use it for checks,
// not every frame.
func savePNG(screen *ebiten.Image, path string) error {
	b := screen.Bounds()
	img := image.NewRGBA(b)
	screen.ReadPixels(img.Pix) // premultiplied RGBA; the screen is opaque here
	f, err := os.Create(path)
	if err != nil {
		return err
	}
	if err := png.Encode(f, img); err != nil {
		f.Close()
		return err
	}
	return f.Close()
}
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 323kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 198kUpdated Claude CodeCodexVerifiedInstall