koebiten

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2025 License: MIT Imports: 8 Imported by: 2

README

koebiten

The API is not yet fixed.

Koebiten is a package for making simple games. Koebiten was heavily influenced by a package called Miniten when development began. Since then, additional features have been inspired by Miniten's foundation, Ebitengine.

Demo

You can try the Wasm version of koebiten from your browser.

https://sago35.github.io/koebiten/

games/flappygopher

For now, koebiten only works on zero-kb02 and macropad-rp2040. It needs some improvements to run in a more general environment.

$ tinygo flash --target ./targets/zero-kb02.json --size short ./games/flappygopher
$ tinygo flash --target gopher-badge --size short ./games/flappygopher

games/jumpingopher

$ tinygo flash --target ./targets/zero-kb02.json --size short ./games/jumpingopher
$ tinygo flash --target gopher-badge --size short ./games/jumpingopher

more info : ./games/jumpingopher

Others

The other games are located under ./games.

Refer to the Makefile for instructions on how to build.

hardware

The currently supported hardware is as follows.

Add new hardware

When adding new hardware, please refer to #7. If the hardware you're adding is not a simple, off-the-shelf product like zero-kb02, you'll need a build tag such as go:build zero_kb02. For more details, see #8.

Tags

koebiten_benchmark

If you specify the koebiten_benchmark tag, it will display the time taken to render 32 frames every 32 frames in microseconds (us). If the processing is light enough, the displayed value should be around 1024us. If this value becomes significantly larger, it indicates that the frame rate is unstable.

$ tinygo flash --target ./targets/zero-kb02.json --size short --tags koebiten_benchmark ./games/flappygopher

The Go Gopher

The Go gopher was designed by Renée French.

Documentation

Index

Constants

View Source
const (
	KeyArrowLeft  = KeyLeft
	KeyArrowRight = KeyRight
	KeyArrowUp    = KeyUp
	KeyArrowDown  = KeyDown
)
View Source
const (
	Rotation0 = iota
	Rotation90
	Rotation180
	Rotation270
)

Clockwise rotation of the screen.

View Source
const GeoMDim = 3

GeoMDim is a dimension of a GeoM.

View Source
const (
	KeyMax = 20
)

Variables

View Source
var Termination = errors.New("Regular termination")

Functions

func C565toRGBA

func C565toRGBA(c uint16) color.RGBA

C565toRGBA converts a uint16 color to color.RGBA

func IsKeyJustPressed

func IsKeyJustPressed(key Key) bool

IsKeyJustPressed returns a boolean value indicating whether the given key is pressed just in the current tick.

IsKeyJustPressed must be called in a game's Update, not Draw.

IsKeyJustPressed is concurrent safe.

func IsKeyJustReleased

func IsKeyJustReleased(key Key) bool

IsKeyJustReleased returns a boolean value indicating whether the given key is released just in the current tick.

IsKeyJustReleased must be called in a game's Update, not Draw.

IsKeyJustReleased is concurrent safe.

func IsKeyPressed added in v0.2.0

func IsKeyPressed(key Key) bool

func KeyPressDuration

func KeyPressDuration(key Key) int

KeyPressDuration returns how long the key is pressed in ticks (Update).

KeyPressDuration must be called in a game's Update, not Draw.

KeyPressDuration is concurrent safe.

func RGBATo565

func RGBATo565(c color.RGBA) uint16

RGBATo565 converts a color.RGBA to uint16

Types

type DrawImageOptions added in v0.3.0

type DrawImageOptions struct {
	GeoM GeoM
}

type Game

type Game interface {
	// Update updates a game by one tick. The given argument represents a screen image.
	//
	// Update updates only the game logic and Draw draws the screen.
	//
	// You can assume that Update is always called TPS-times per second (60 by default), and you can assume
	// that the time delta between two Updates is always 1 / TPS [s] (1/60[s] by default). As Ebitengine already
	// adjusts the number of Update calls, you don't have to measure time deltas in Update by e.g. OS timers.
	//
	// An actual TPS is available by ActualTPS(), and the result might slightly differ from your expected TPS,
	// but still, your game logic should stick to the fixed time delta and should not rely on ActualTPS() value.
	// This API is for just measurement and/or debugging. In the long run, the number of Update calls should be
	// adjusted based on the set TPS on average.
	//
	// An actual time delta between two Updates might be bigger than expected. In this case, your game's
	// Update or Draw takes longer than they should. In this case, there is nothing other than optimizing
	// your game implementation.
	//
	// In the first frame, it is ensured that Update is called at least once before Draw. You can use Update
	// to initialize the game state.
	//
	// After the first frame, Update might not be called or might be called once
	// or more for one frame. The frequency is determined by the current TPS (tick-per-second).
	//
	// If the error returned is nil, game execution proceeds normally.
	// If the error returned is Termination, game execution halts, but does not return an error from RunGame.
	// If the error returned is any other non-nil value, game execution halts and the error is returned from RunGame.
	Update() error

	// Draw draws the game screen by one frame.
	//
	// The give argument represents a screen image. The updated content is adopted as the game screen.
	//
	// The frequency of Draw calls depends on the user's environment, especially the monitors refresh rate.
	// For portability, you should not put your game logic in Draw in general.
	Draw(screen *Image)

	// Layout accepts a native outside size in device-independent pixels and returns the game's logical screen
	// size.
	//
	// On desktops, the outside is a window or a monitor (fullscreen mode). On browsers, the outside is a body
	// element. On mobiles, the outside is the view's size.
	//
	// Even though the outside size and the screen size differ, the rendering scale is automatically adjusted to
	// fit with the outside.
	//
	// Layout is called almost every frame.
	//
	// It is ensured that Layout is invoked before Update is called in the first frame.
	//
	// If Layout returns non-positive numbers, the caller can panic.
	//
	// You can return a fixed screen size if you don't care, or you can also return a calculated screen size
	// adjusted with the given outside size.
	//
	// If the game implements the interface LayoutFer, Layout is never called and LayoutF is called instead.
	Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int)
}

Game defines necessary functions for a game.

type GeoM added in v0.3.0

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

A GeoM represents a matrix to transform geometry when rendering an image.

The initial value is identity.

func (*GeoM) Apply added in v0.3.0

func (g *GeoM) Apply(x, y float32) (float32, float32)

Apply pre-multiplies a vector (x, y, 1) by the matrix. In other words, Apply calculates GeoM * (x, y, 1)^T. The return value is x and y values of the result vector.

func (*GeoM) Concat added in v0.3.0

func (g *GeoM) Concat(other GeoM)

Concat multiplies a geometry matrix with the other geometry matrix. This is same as multiplying the matrix other and the matrix g in this order.

func (*GeoM) Element added in v0.3.0

func (g *GeoM) Element(i, j int) float32

Element returns a value of a matrix at (i, j).

func (*GeoM) Invert added in v0.3.0

func (g *GeoM) Invert()

Invert inverts the matrix. If g is not invertible, Invert panics.

func (*GeoM) IsInvertible added in v0.3.0

func (g *GeoM) IsInvertible() bool

IsInvertible returns a boolean value indicating whether the matrix g is invertible or not.

func (*GeoM) Reset added in v0.3.0

func (g *GeoM) Reset()

Reset resets the GeoM as identity.

func (*GeoM) Rotate added in v0.3.0

func (g *GeoM) Rotate(theta float32)

Rotate rotates the matrix clockwise by theta. The unit is radian.

func (*GeoM) Scale added in v0.3.0

func (g *GeoM) Scale(x, y float32)

Scale scales the matrix by (x, y).

func (*GeoM) SetElement added in v0.3.0

func (g *GeoM) SetElement(i, j int, element float32)

SetElement sets an element at (i, j).

func (*GeoM) Skew added in v0.3.0

func (g *GeoM) Skew(skewX, skewY float32)

Skew skews the matrix by (skewX, skewY). The unit is radian.

func (*GeoM) String added in v0.3.0

func (g *GeoM) String() string

String returns a string representation of GeoM.

func (*GeoM) Translate added in v0.3.0

func (g *GeoM) Translate(tx, ty float32)

Translate translates the matrix by (tx, ty).

type Hardware

type Hardware interface {
	Init() error
	GetDisplay() Displayer
	KeyUpdate() error
}

type Image added in v0.3.0

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

Image is a Wrapper for the pixel.Image type.

Image implements the Displayer interface.

func NewImage added in v0.3.0

func NewImage(width, height int16) *Image

NewImage creates a new Image with the given width and height.

It returns a pointer to the new Image.

func NewImageFromFS added in v0.3.0

func NewImageFromFS(fsys fs.FS, path string) *Image

NewImageFromFS creates a new Image from the filesystem.

func (*Image) ClearBuffer added in v0.3.0

func (i *Image) ClearBuffer()

ClearBuffer clears the buffer.

It implements the Displayer interface.

func (*Image) ClearDisplay added in v0.3.0

func (i *Image) ClearDisplay()

ClearDisplay clears the display.

It implements the Displayer interface.

func (*Image) Display added in v0.3.0

func (i *Image) Display() error

Display does nothing.

It implements the Displayer interface.

func (*Image) DrawImage added in v0.3.0

func (i *Image) DrawImage(dst Displayer, options DrawImageOptions)

DrawImage draws an image onto the display.

func (*Image) Fill added in v0.3.0

func (i *Image) Fill(clr color.Color)

Fill fills the image with the given color.

func (*Image) SetPixel added in v0.3.0

func (i *Image) SetPixel(x, y int16, c color.RGBA)

SetPixel sets the pixel at the given x and y coordinates to the given color. The color is converted to a pixel.Monochrome color. If the x and y coordinates are outside the image, the function does nothing.

It implements the Displayer interface.

func (*Image) Size added in v0.3.0

func (i *Image) Size() (int16, int16)

Size returns the width and height of the image.

It implements the Displayer interface.

type Key

type Key int
const (
	Key0 Key = iota
	Key1
	Key2
	Key3
	Key4
	Key5
	Key6
	Key7
	Key8
	Key9
	Key10
	Key11
	KeyRotaryButton
	KeyJoystick
	KeyRotaryLeft
	KeyRotaryRight
	KeyLeft
	KeyRight
	KeyUp
	KeyDown
)

func AppendJustPressedKeys

func AppendJustPressedKeys(keys []Key) []Key

AppendJustPressedKeys append just pressed keyboard keys to keys and returns the extended buffer. Giving a slice that already has enough capacity works efficiently.

AppendJustPressedKeys must be called in a game's Update, not Draw.

AppendJustPressedKeys is concurrent safe.

func AppendJustReleasedKeys

func AppendJustReleasedKeys(keys []Key) []Key

AppendJustReleasedKeys append just released keyboard keys to keys and returns the extended buffer. Giving a slice that already has enough capacity works efficiently.

AppendJustReleasedKeys must be called in a game's Update, not Draw.

AppendJustReleasedKeys is concurrent safe.

func AppendPressedKeys

func AppendPressedKeys(keys []Key) []Key

AppendPressedKeys append currently pressed keyboard keys to keys and returns the extended buffer. Giving a slice that already has enough capacity works efficiently.

AppendPressedKeys must be called in a game's Update, not Draw.

AppendPressedKeys is concurrent safe.

func PressedKeys deprecated

func PressedKeys() []Key

PressedKeys returns a set of currently pressed keyboard keys.

PressedKeys must be called in a game's Update, not Draw.

Deprecated: as of v2.2. Use AppendPressedKeys instead.

type RotatedDisplay

type RotatedDisplay struct {
	Displayer
	// contains filtered or unexported fields
}

func (*RotatedDisplay) SetPixel

func (d *RotatedDisplay) SetPixel(x, y int16, c color.RGBA)

func (*RotatedDisplay) Size

func (d *RotatedDisplay) Size() (x, y int16)

Directories

Path Synopsis
examples
GeoM command
drawing command
games
all command
blocks command
flappygopher command
goradius command
jumpingopher command
snakegame command

Jump to

Keyboard shortcuts

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