agentleFS
bmcszkbmcszk/agent-skills/skills/ebiten/game-loop/SKILL.md

game-loop

Skill1 starsChanged 2 months agoMITView on GitHub

Ebitengine game loop, ebiten.Game interface, TPS/FPS, window management, RunGameOptions.

The file

We split this skill into the rules it gives an agent

8 rules. About 0 are common, 8 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈8

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Every Ebitengine game implements three methods:

    line 17
  2. Distinctive:

    All game logic goes here: movement, collision, AI, state transitions

    line 30
  3. Distinctive:

    All rendering goes here: draw images, shapes, text

    line 37
  4. Distinctive:

    When you want Update and Draw to be 1:1

    line 124
  5. Distinctive:

    When frame timing matters more than fixed timestep

    line 125
  6. Distinctive:

    Use this skill only when the task clearly matches the scope described above.

    line 236
  7. Distinctive:

    Do not treat the output as a substitute for environment-specific validation, testing, or expert review.

    line 237
  8. Distinctive:

    Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.

    line 238

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Game Loop

The ebiten.Game interface and loop lifecycle.


The Game Interface

Every Ebitengine game implements three methods:

type Game interface {
    Update() error
    Draw(screen *ebiten.Image)
    Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int)
}

Update() error

  • Called once per tick (1/60s by default, configurable via ebiten.SetTPS())
  • All game logic goes here: movement, collision, AI, state transitions
  • Return nil to continue, return error to exit
  • Return ebiten.Termination for clean exit without error message

Draw(screen *ebiten.Image)

  • Called once per frame (tied to display refresh rate)
  • All rendering goes here: draw images, shapes, text
  • Never modify game state in Draw() — it may be called at different rates than Update()
  • The screen parameter is the final render target

Layout(outsideWidth, outsideHeight int) (int, int)

  • Returns the logical screen size in pixels
  • Engine auto-scales logical screen to window/monitor size
  • Return fixed size for pixel-perfect rendering (e.g., return 320, 240)
  • Return outsideWidth, outsideHeight for dynamic/responsive sizing

Starting the Game

func main() {
    ebiten.SetWindowSize(640, 480)
    ebiten.SetWindowTitle("My Game")
    ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
    ebiten.SetFPSMode(ebiten.FPSModeVsyncOn)

    if err := ebiten.RunGame(&Game{}); err != nil {
        log.Fatal(err)
    }
}

RunGameOptions (v2.4+)

opts := &ebiten.RunGameOptions{
    GraphicsLibrary: ebiten.GraphicsLibraryOpenGL,
    InitUnfocused:   false,
    ScreenClearedEveryFrame: true,
    SkipTaskbar:     false,
}
ebiten.RunGameWithOptions(&Game{}, opts)

Window Configuration

FunctionPurpose
SetWindowSize(w, h int)Window size in device-independent pixels
SetWindowTitle(title string)Window title
SetWindowResizingMode(mode)WindowResizingModeDisabled, WindowResizingModeOnlyFullscreenEnabled, WindowResizingModeEnabled
SetWindowDecorated(decorated bool)Show/hide title bar
SetWindowPosition(x, y int)Window position
SetWindowFloating(float bool)Keep window on top
SetWindowMousePassthrough(on bool)Click-through window
SetFullscreen(fullscreen bool)Toggle fullscreen
SetFPSMode(mode)FPSModeVsyncOn (default), FPSModeVsyncOffMaximum, FPSModeVsyncOffMinimum
SetTPS(tps int)Ticks per second (default 60)
SetScreenClearedEveryFrame(cleared bool)Auto-clear screen before Draw
SetScreenFilterEnabled(enabled bool)Enable/disable screen scaling filter
SetVsyncEnabled(enabled bool)VSync control

Read State

FunctionReturns
WindowSize()(w, h int)
IsFullscreen()bool
IsWindowDecorated()bool
WindowPosition()(x, y int)
CurrentFPS()float64
CurrentTPS()float64
ActualFPS()float64
ActualTPS()float64
IsFocused()bool
IsScreenTransparent()bool

TPS Modes

ebiten.SetTPS(60)                        // Fixed 60 ticks/sec
ebiten.SetTPS(30)                        // Fixed 30 ticks/sec
ebiten.SetTPS(ebiten.SyncWithFPS)        // Update called every frame (decoupled)

When to use SyncWithFPS

  • When you want Update and Draw to be 1:1
  • When frame timing matters more than fixed timestep
  • Note: game logic becomes frame-rate dependent (less deterministic)

Game State Pattern

type Game struct {
    state  GameState
    player *Player
    level  *Level
    camera *Camera
}

type GameState int

const (
    StateTitle GameState = iota
    StatePlaying
    StatePaused
    StateGameOver
)

func (g *Game) Update() error {
    switch g.state {
    case StateTitle:
        if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
            g.state = StatePlaying
        }
    case StatePlaying:
        g.player.Update()
        g.level.Update()
        g.camera.Follow(g.player.Position())
    case StatePaused:
        if inpututil.IsKeyJustPressed(ebiten.KeyEscape) {
            g.state = StatePlaying
        }
    case StateGameOver:
        if inpututil.IsKeyJustPressed(ebiten.KeyEnter) {
            g.reset()
            g.state = StateTitle
        }
    }
    return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
    switch g.state {
    case StateTitle:
        // draw title
    case StatePlaying, StatePaused:
        g.camera.Draw(screen, g.level, g.player)
    case StateGameOver:
        // draw game over
    }
}

Scene Management

For larger games, use a scene manager instead of switch/case:

LibraryApproach
stagehandFull scene manager with transitions
bamennSimple scene library
type Scene interface {
    Update() error
    Draw(screen *ebiten.Image)
    Layout(w, h int) (int, int)
}

Clean Exit

func (g *Game) Update() error {
    if ebiten.IsKeyPressed(ebiten.KeyEscape) {
        return ebiten.Termination
    }
    return nil
}

Anti-Patterns

Don'tDo
Modify state in Draw()All state changes in Update() only
Allocate in Update()/Draw() hot pathsPre-allocate in constructor
Ignore Layout() consistencyReturn same logical size unless intentionally dynamic
Use log.Fatal() in UpdateReturn error to let RunGame handle it
Block in Update()Keep Update non-blocking, < 16ms per tick

Remember: Update is logic. Draw is rendering. Never mix them.

When to Use

This skill is applicable when implementing or modifying the Ebitengine game loop, window configuration, or game state management.

Limitations

  • Use this skill only when the task clearly matches the scope described above.
  • Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
  • Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.