game

package
v0.5.5 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 3 Imported by: 0

README

game

What games need beyond what apps do, for MyGo, opt-in: MyGo is an app framework.

  • game.View runs a game's loop in a native UI element, in fixed steps with the player's input, game.Camera looks at its world, and game.IntegerFit shows a canvas of pixel art pixel-perfect at any size.
  • gamepad reads game controllers in one standard layout, on macOS (GameController.framework), Windows (XInput), Linux (evdev) and in browsers (the Gamepad API).
import "github.com/egoist/mygo/plugins/game/gamepad"

for _, pad := range gamepad.Pads() {
	s := pad.State()
	x, y := s.LeftStick()
	move(x, y)
}

See the documentation.

Documentation

Overview

Package game has what games need beyond what apps do, opt-in, out of MyGo's core ui: a game loop that updates a world in fixed steps with the player's input and draws it every frame (View), and a camera that looks at a 2D world (Camera), shown pixel-perfect at any size through a ui.Canvas (IntegerFit). Its package gamepad reads controllers.

type farm struct{ x, y float32 }

func (f *farm) Update(in *game.Input) {
	if in.Down(ui.KeyD) {
		f.x += 2
	}
}

func (f *farm) Draw(p *ui.Painter, r ui.Rect, alpha float64) {
	game.Camera{X: f.x, Y: f.y, Zoom: 3}.Draw(p, r, func() { drawWorld(p) })
}

ui.View(func(c *ui.Context) { game.View(c, f).Fill() })

Index

Constants

View Source
const Rate = 60

Rate is how many steps a second a View runs a game, unless the game has a method Rate() int giving its own.

Variables

This section is empty.

Functions

func IntegerFit

func IntegerFit(p *ui.Painter, w, h int, r ui.Rect) (dst ui.Rect, k int)

IntegerFit returns where a canvas of w×h pixels shows in r as large as it can with a whole number of the screen's pixels for each of its own, at least one, centered (letterboxed): pixel-perfect scaling, for p's screen, as Godot's viewport stretch and Stardew Valley draw pixel art. k is that number. A game draws its world into a ui.Canvas of a logical size, as 480×270, and shows it there:

dst, _ := game.IntegerFit(p, 480, 270, r)
p.Image(world.Bitmap(), dst, ui.FillBox)

The pointer at x, y of the view is at ((x+r.X-dst.X)*480/dst.W, (y+r.Y-dst.Y)*270/dst.H) of the canvas.

func View

func View(c *ui.Context, g Game) ui.Element

View creates an element that runs g: Update in fixed steps of the game's rate, with the keys, pointer and wheel the element takes as Input, and Draw every frame, as long as it shows. It takes the keyboard focus as it appears and when clicked, and lets go of what was held when the window loses the keyboard. Size it as any element, as with Fill:

game.View(c, g).Fill()

Types

type Camera

type Camera struct {
	X, Y     float32
	Zoom     float32
	Rotation float32
}

A Camera looks at a 2D world: the world's point X, Y shows at the middle of the view, Zoom times as large (0 is 1), turned Rotation degrees, as raylib's Camera2D. World units are DIPs at a zoom of 1.

func (Camera) Draw

func (cam Camera) Draw(p *ui.Painter, r ui.Rect, fn func())

Draw draws what fn draws in world coordinates through the camera into r, clipped to it. Unturned, the world moves by whole pixels of the screen, so that the edges of tiles drawn at whole world units stay on them, while parts of bitmaps, which ui.Painter.ImageRegion places where they are under a transform, glide.

func (Camera) ToWorld

func (cam Camera) ToWorld(r ui.Rect, x, y float32) (wx, wy float32)

ToWorld returns the world's point at x, y of the view r, from its top-left corner, as Input.Pointer gives it.

func (Camera) Transform

func (cam Camera) Transform(r ui.Rect) ui.Transform

Transform returns the transform from the world to the window, for the view r, with the origin (0, 0): Painter.Transform(cam.Transform(r), 0, 0, draw). Draw is that, snapped and clipped.

func (Camera) Visible

func (cam Camera) Visible(r ui.Rect) ui.Rect

Visible returns the part of the world the camera shows in the view r: the bounds of it when turned. A game draws only the tiles in it.

type Game

type Game interface {
	// Update advances the game one step, of 1/Rate seconds unless
	// the game has a Rate method, with the input since the step before.
	Update(in *Input)
	// Draw draws the game into r. alpha, from 0 to 1, is how far the
	// frame's time is into the next step: a game may draw what moves
	// that far between its last two places, for motion smoother than its
	// steps.
	Draw(p *ui.Painter, r ui.Rect, alpha float64)
}

A Game is a game View runs: it updates the game's world in fixed steps, whatever the display's rate, and draws it every frame, as Ebitengine's Update and Draw.

type Input

type Input struct {
	// contains filtered or unexported fields
}

Input is what a game's player did, as a step sees it: the keys and pointer buttons held, and those pressed and released since the step before, even if pressed and released between two steps. Keys are by position, as PhysicalKey: KeyW is the key in W's place whatever the layout, as games want for WASD.

func (*Input) Down

func (in *Input) Down(k ui.Key) bool

Down reports whether k is held.

func (*Input) Focused

func (in *Input) Focused() bool

Focused reports whether the view has the keyboard, which it loses with its window or to another element: a game pauses without it, as the player is elsewhere.

func (*Input) Pointer

func (in *Input) Pointer() (x, y float32, over bool)

Pointer returns where the pointer is in the view, from its top-left corner, and whether it is over the view.

func (*Input) PointerDown

func (in *Input) PointerDown(b int) bool

PointerDown reports whether pointer button b (0 the primary, 1 the secondary, 2 the middle) is held.

func (*Input) PointerPressed

func (in *Input) PointerPressed(b int) bool

PointerPressed reports whether button b was pressed since the step before.

func (*Input) PointerReleased

func (in *Input) PointerReleased(b int) bool

PointerReleased reports whether button b was released since the step before.

func (*Input) Pressed

func (in *Input) Pressed(k ui.Key) bool

Pressed reports whether k was pressed since the step before.

func (*Input) Released

func (in *Input) Released(k ui.Key) bool

Released reports whether k was released since the step before.

func (*Input) Size

func (in *Input) Size() (w, h float32)

Size returns the view's size.

func (*Input) Wheel

func (in *Input) Wheel() (dx, dy float32)

Wheel returns how far the wheel or touchpad scrolled since the step before, in DIPs, down and right positive.

Directories

Path Synopsis
Package gamepad reads game controllers: their buttons, triggers and sticks in the standard layout of an Xbox controller, which the W3C's Gamepad API and SDL's game controller database name, whatever pad it is:
Package gamepad reads game controllers: their buttons, triggers and sticks in the standard layout of an Xbox controller, which the W3C's Gamepad API and SDL's game controller database name, whatever pad it is:

Jump to

Keyboard shortcuts

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