Documentation
¶
Overview ¶
Package pi provides API to develop retro games.
Please note that the entire pi package is not concurrency-safe. This means that it is unsafe to run functions and access package variables from go-routines started by your code.
Index ¶
- Constants
- Variables
- func Atan2(dx, dy float64) float64
- func Btn(button Button) bool
- func BtnBits() int
- func BtnPlayer(button Button, player int) bool
- func Btnp(button Button) bool
- func BtnpBits() int
- func BtnpPlayer(button Button, player int) bool
- func Camera(x, y int) (prevX, prevY int)
- func CameraReset() (prevX, prevY int)
- func Circ(centerX, centerY, radius int, color byte)
- func CircFill(centerX, centerY, radius int, color byte)
- func Clip(x, y, w, h int) (prevX, prevY, prevW, prevH int)
- func ClipReset() (prevX, prevY, prevW, prevH int)
- func Cls()
- func ClsCol(col byte)
- func Cos(angle float64) float64
- func Line(x0, y0, x1, y1 int, color byte)
- func Load(resources fs.ReadFileFS)
- func MouseBtn(b MouseButton) bool
- func MouseBtnp(b MouseButton) bool
- func MousePos() (x, y int)
- func Pal(color byte, replacementColor byte)
- func PalDisplay(color byte, replacementColor byte)
- func PalReset()
- func Palt(color byte, transparent bool)
- func PaltReset()
- func Pget(x, y int) byte
- func Print(text string, x, y int, color byte) (rightMostX int)
- func Pset(x, y int, color byte)
- func Rect(x0, y0, x1, y1 int, color byte)
- func RectFill(x0, y0, x1, y1 int, color byte)
- func Reset()
- func SetCustomFontHeight(height int)
- func SetCustomFontSpecialWidth(w int)
- func SetCustomFontWidth(w int)
- func SetScreenSize(w, h int)
- func Sget(x, y int) byte
- func Sin(angle float64) float64
- func Spr(n, x, y int)
- func SprSize(n, x, y int, w, h float64)
- func SprSizeFlip(n, x, y int, w, h float64, flipX, flipY bool)
- func Sset(x, y int, color byte)
- func Stop()
- func Time() float64
- func UseEmptySpriteSheet(w, h int)
- type Button
- type Controller
- type Font
- type MouseButton
- type Position
- type Region
- type Screen
- type SpriteSheet
Constants ¶
const ( Left = 0 Right = 1 Up = 2 Down = 3 O = 4 // O is a first fire button X = 5 // X is a second fire button )
Keyboard mappings:
player 0: [DPAD] - cursors [O] - Z C N [X] - X V M player 1: [DPAD] - SFED [O] - LSHIFT [X] - TAB W Q A
First connected gamepad controller is player 0, second player 1 and so on. On XBox controller O is A and Y, X is B and X.
const ( MouseLeft = 0 MouseMiddle = 1 MouseRight = 2 )
const (
SpriteWidth, SpriteHeight = 8, 8
)
Variables ¶
var ( // Update is a user provided function executed each frame (30 times per second). // // The purpose of this function is to handle user input, perform calculations, update // game state etc. Typically, this function does not draw on screen. Update func() // Draw is a user provided function executed at most each frame (up to 30 times per second). // π may skip calling this function if previous frame took too long. // // The purpose of this function is to draw on screen. Draw func() // Palette has all colors available in the game. Up to 256. // Palette is taken from loaded sprite sheet (which must be // a PNG file with indexed color mode). If sprite-sheet.png was not // found, then default 16 color palette is used. // // Can be freely read and updated. Changes will be visible immediately. Palette = defaultPalette )
User parameters.
var ( // DrawPalette contains mapping of colors used to replace color with // another one for all subsequent drawings. // // The index of array is original color, the value is color replacement. DrawPalette [256]byte // DisplayPalette contains mapping of colors used to replace color with // another one for the entire screen, at the end of a frame // // The index of array is original color, the value is color replacement. DisplayPalette [256]byte // ColorTransparency contains information whether given color is transparent. // // The index of array is a color number. ColorTransparency = defaultTransparency // TimeSeconds is the number of seconds since game was started TimeSeconds float64 GameLoopStopped bool )
var Controllers [8]Controller // 0th element is for Player 0, 1st for Player 1 etc.
Functions ¶
func Atan2 ¶
Atan2 converts DX, DY into an angle from 0..1
Similar to Cos and Sin, angle is taken to run anticlockwise in screenspace. For example:
atan(0,-1) // returns 0.25
func Btn ¶ added in v0.6.0
Btn returns true if a controller button is being pressed at this moment by player 0.
func BtnBits ¶ added in v0.6.0
func BtnBits() int
BtnBits returns the state of all controller buttons for players 0 and 1 as bitset.
The first byte contains the button states for player 0 (bits 0 through 5, bits 6 and 7 are unused). The second byte contains the button states for player 1 (bits 8 through 13).
Bit 0 is Left, 1 is Right, bit 5 is the X button.
A bit of 1 means the button is pressed.
func BtnPlayer ¶ added in v0.6.0
BtnPlayer returns true if a controller button is being pressed at this moment by specific player. The player can be 0..7.
func Btnp ¶ added in v0.6.0
Btnp returns true when the controller button has just been pressed. It also returns true after the next 15 frames, and then every 4 frames. This simulates keyboard-like repeating.
func BtnpBits ¶ added in v0.6.0
func BtnpBits() int
BtnpBits returns the state of all controller buttons for players 0 and 1 as bitset.
The first byte contains the button states for player 0 (bits 0 through 5, bits 6 and 7 are unused). The second byte contains the button states for player 1 (bits 8 through 13).
Bit 0 is Left, 1 is Right, bit 5 is the X button.
A bit of 1 means the button has just been pressed.
func BtnpPlayer ¶ added in v0.6.0
BtnpPlayer returns true when the controller button has just been pressed. It also returns true after the next 15 frames, and then every 4 frames. This simulates keyboard-like repeating. The player can be 0..7.
func CameraReset ¶
func CameraReset() (prevX, prevY int)
CameraReset resets the camera offset to origin (0,0).
func Circ ¶ added in v0.10.0
Circ draws a circle
Circ takes into account camera position, clipping region and draw palette.
func CircFill ¶ added in v0.10.0
CircFill draws a filled circle
CircFill takes into account camera position, clipping region and draw palette.
func Clip ¶
Clip sets the clipping region in the form of rectangle. All screen drawing operations will not affect any pixels outside the region.
Clip returns previous clipping region.
func ClipReset ¶
func ClipReset() (prevX, prevY, prevW, prevH int)
ClipReset resets the clipping region, which means that entire screen will be clipped.
func Cls ¶
func Cls()
Cls cleans the entire screen with color 0. It does not take into account any draw state parameters such as clipping region or camera.
func ClsCol ¶
func ClsCol(col byte)
ClsCol cleans the entire screen with specified color. It does not take into account any draw state parameters such as clipping region or camera.
func Cos ¶
Cos returns the cosine of angle which is in the range of 0.0-1.0 measured clockwise.
If you want to use conventional radian-based function use math.Cos.
func Line ¶ added in v0.9.0
Line draws a line between points x0,y0 and x1,y1 (inclusive).
Line takes into account camera position, clipping region and draw palette.
func Load ¶ added in v0.19.0
func Load(resources fs.ReadFileFS)
Load loads files like sprite-sheet.png, custom-font.png
func MouseBtn ¶ added in v0.11.0
func MouseBtn(b MouseButton) bool
MouseBtn returns true if the mouse button is being pressed at this moment.
func MouseBtnp ¶ added in v0.11.0
func MouseBtnp(b MouseButton) bool
MouseBtnp returns true when the mouse button has just been pressed. It also returns true after the next 15 frames, and then every 4 frames. This simulates keyboard-like repeating.
func MousePos ¶ added in v0.11.0
func MousePos() (x, y int)
MousePos returns the position of mouse in screen coordinates.
func Pal ¶ added in v0.5.0
Pal replaces color with another one for all subsequent drawings (it is changing the so-called draw palette).
Affected functions are Pset, Spr, SprSize, SprSizeFlip, Rect and RectFill.
func PalDisplay ¶ added in v0.5.0
PalDisplay replaces color with another one for the whole screen at the end of a frame (it is changing the so-called display palette).
func PalReset ¶ added in v0.5.0
func PalReset()
PalReset resets all swapped colors for all palettes.
func Palt ¶ added in v0.4.0
Palt sets color transparency. If true then the color will not be drawn for next drawing operations.
Color transparency is used by Spr, SprSize and SprSizeFlip.
func PaltReset ¶ added in v0.4.0
func PaltReset()
PaltReset sets all transparent colors to false and makes color 0 transparent.
func Print ¶ added in v0.7.1
Print prints text on the screen using system font. It takes into consideration clipping region and camera position.
Only unicode characters with code < 256 are supported. Unsupported chars are printed as question mark. The entire table of available chars can be found here: https://github.com/elgopher/pi/blob/master/internal/system-font.png
Print returns the right-most x position that occurred while printing.
func Rect ¶ added in v0.8.0
Rect draws a rectangle between points x0,y0 and x1,y1 (inclusive).
Rect takes into account camera position, clipping region and draw palette.
func RectFill ¶ added in v0.8.0
RectFill draws a filled rectangle between points x0,y0 and x1,y1 (inclusive).
RectFill takes into account camera position, clipping region and draw palette.
func SetCustomFontHeight ¶ added in v0.19.0
func SetCustomFontHeight(height int)
func SetCustomFontSpecialWidth ¶ added in v0.19.0
func SetCustomFontSpecialWidth(w int)
func SetCustomFontWidth ¶ added in v0.19.0
func SetCustomFontWidth(w int)
func SetScreenSize ¶ added in v0.19.0
func SetScreenSize(w, h int)
func Sin ¶
Sin returns the sine of angle which is in the range of 0.0-1.0 measured clockwise.
Sin returns an inverted result to suit screen space (where Y means "DOWN", as opposed to mathematical diagrams where Y typically means "UP"):
sin(0.25) // returns -1
If you want to use conventional radian-based function without the y inversion, use math.Sin.
func Spr ¶
func Spr(n, x, y int)
Spr draws a sprite with specified number on the screen. Sprites are counted from left to right, top to bottom. Sprite 0 is on top-left corner, sprite 1 is to the right and so on.
func SprSize ¶
SprSize draws a range of sprites on the screen.
n is a sprite number in the top-left corner.
Non-integer w or h may be used to draw partial sprites.
func SprSizeFlip ¶
SprSizeFlip draws a range of sprites on the screen.
If flipX is true then sprite is flipped horizontally. If flipY is true then sprite is flipped vertically.
func Stop ¶ added in v0.2.0
func Stop()
Stop will stop the game loop after Update or Draw is finished. If you are using devtools, the game will be paused. Otherwise, the game will be closed.
func Time ¶
func Time() float64
Time returns the amount of time since game was run, as a (fractional) number of seconds.
Calling Time() multiple times in the same frame will always return the same result.
func UseEmptySpriteSheet ¶ added in v0.19.0
func UseEmptySpriteSheet(w, h int)
UseEmptySpriteSheet initializes empty sprite-sheet with given size. Could be used when you don't have sprite-sheet.png in resources.
Types ¶
type Button ¶ added in v0.6.0
type Button int
Button is a virtual button on any game controller. The game controller can be a gamepad or a keyboard.
Button is used by Btn, Btnp, BtnPlayer, BtnpPlayer, BtnBits and BtnpBits.
type Controller ¶ added in v0.19.0
type Controller struct { // BtnDuration is how many frames button was pressed: // Index of array is equal to controller button constant. BtnDuration [6]uint }
type Font ¶ added in v0.7.1
type Font struct { // Data contains all 256 characters sorted by their ascii-like number. // Each character is 8 subsequent bytes, starting from the top. // Left-most pixel in a line is bit 0. Right-most pixel in a line is bit 7. // // The size of slice is always 8 * 256 = 2048. // // Can be freely read and updated. Changes will be visible immediately. Data []byte // Width in pixels for all characters below 128. For Width > 8 only 8 pixels are drawn. Width int // SpecialWidth is a with of all special characters (code>=128) // For SpecialWidth > 8 only 8 pixels are drawn. SpecialWidth int // Height of line Height int }
Font contains all information about loaded font and provides method to Print the text.
func CustomFont ¶ added in v0.15.0
func CustomFont() Font
func SystemFont ¶ added in v0.19.0
func SystemFont() Font
func (Font) Print ¶ added in v0.15.0
Print prints text on the screen at given coordinates. It takes into account clipping region and camera position.
Only unicode characters with code < 256 are supported. Unsupported chars are printed as question mark. The entire table of available chars can be found here: https://github.com/elgopher/pi/blob/master/internal/system-font.png
Print returns the right-most x position that occurred while printing.
type MouseButton ¶ added in v0.11.0
type MouseButton int
type Screen ¶ added in v0.19.0
type Screen struct {
// Width and height in pixels
W, H int
// Pix contains pixel colors for the screen visible by the player.
// Each pixel is one byte. It is initialized during pi.Boot.
//
// Pix on the screen are organized from left to right,
// top to bottom. Slice element number 0 has pixel located
// in the top-left corner. Slice element number 1 has pixel color
// on the right and so on.
//
// Can be freely read and updated. Useful when you want to use your own
// functions for pixel manipulation.
Pix []byte
Clip Region
Camera Position
// contains filtered or unexported fields
}
type SpriteSheet ¶ added in v0.19.0
type SpriteSheet struct {
// Width and height in pixels
W, H int
// Pix contains pixel colors for the entire sprite sheet.
// Each pixel is one byte. It is initialized during pi.Boot.
//
// Pixels in the sprite-sheet are organized from left to right,
// top to bottom. Slice element number 0 has pixel located
// in the top-left corner. Slice element number 1 has a pixel color
// on the right and so on.
//
// Can be freely read and updated.
// Useful when you want to use your own functions for pixel manipulation.
Pix []byte
// contains filtered or unexported fields
}
func SprSheet ¶ added in v0.19.0
func SprSheet() SpriteSheet
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package ebitengine uses Ebitengine technology to run the game.
|
Package ebitengine uses Ebitengine technology to run the game. |
examples
|
|
controller
Example showing how to test pressed buttons of game controllers.
|
Example showing how to test pressed buttons of game controllers. |
hello
Example animating HELLO WORLD text on screen.
|
Example animating HELLO WORLD text on screen. |
keyboard
Example showing how to use virtual keyboard.
|
Example showing how to use virtual keyboard. |
memory
Example showing how to directly modify screen memory.
|
Example showing how to directly modify screen memory. |
pal
Example showing practical use of palette swapping.
|
Example showing practical use of palette swapping. |
print
Example showing how to print text to screen.
|
Example showing how to print text to screen. |
resolution
Example showing how to change screen resolution and run π functions before game loop.
|
Example showing how to change screen resolution and run π functions before game loop. |
shapes
Example showing how to draw shapes and use a mouse.
|
Example showing how to draw shapes and use a mouse. |
state
Example showing how to save and load the state.
|
Example showing how to save and load the state. |
trigonometry
Example plotting sin and cos on screen
|
Example plotting sin and cos on screen |
Package image provides API for decoding images.
|
Package image provides API for decoding images. |
internal
|
|
Package key provides functions for handling virtual keyboard input.
|
Package key provides functions for handling virtual keyboard input. |
Package snap provides functions for taking screenshots.
|
Package snap provides functions for taking screenshots. |
Package state provides functions for managing persistent game data.
|
Package state provides functions for managing persistent game data. |