Sparky

Sparky API documentation

Index

General information

  1. I tried to follow the order of Pico-8 documentation, so that it's easier to compare the two and transition to Pico-8 if you want something more advanced.
  2. In all drawing operations where the color is optional, if you leave it out the operation will use the draw color set by the color function (the default is 0, or the brightest yellowish white).
  3. The line and rectangle operations follow Pico-8's style of taking two sets of coordinates instead of one set plus width and height in order to make code more interchangeable between the two.
  4. The api is very small and mainly concerns graphics, sound and input. If you need math functions, array operations etc. you have the entire considerable power of Javascript to do your bidding.
  5. I've tried to make the lib code as simple and succinct as possible, so there are not too many safeguards. Sometimes you might get unexpected results when leaving out non-optional parameters or messing with types.

Basics

init(config)

Creates a sparky game.

Graphics

pget(x, y) -> number
pset(x, y, [c])

Returns or sets the color c of the pixel at coordinates x,y. If you don't specify color, the current draw color is used.

sget(x, y) -> number
sset(x, y, [c])

Returns or sets the color c of the pixel at coordinates x,y of the spritesheet. If you don't specify color, the current draw color is used.

fget(sprite, flagNumber) -> boolean
fset(sprite, flagNumber, [c])

Returns or sets the color c of the pixel at coordinates x,y of the spritesheet. If you don't specify color, the current draw color is used.

print(str, x, y, [c])

Prints the string str to coordinates x,y in color c (or current draw color if that is not specified). sparky has no concept of "cursor", therefore you must always provide coordinates to draw text. All characters in sparky's font are three pixels wide and five pixels tall.

color(c = 0)

Sets current draw color to c.

cls(c = 5)

Fills the entire screen buffer with color c.

camera(x = 0, y = 0)

Offsets all subsequent drawing operations by -x, -y.

circ(x, y, r, [c])
circfill(x, y, r, [c])

Draws a circle (or filled circle) with its center at coordinates x,y and radius r, in color c (or current draw color).

line(x0, y0, x1, y1, [c])

Draws a line from x0,y0 to x1,y1 in color c (or current draw color).

rect(x0, y0, x1, y1, [c])
rectfill(x0, y0, x1, y1, [c])

Draws a rectangle (or filled rectangle) with its top left corner at x0,y0 and bottom right corner at x1,y1, in color c (or current draw color).

pal(c0, c1)

Swaps color c0 in the current palette with c1 from original palette. If called with no arguments, resets the palette to the initial state, including transparency data.

palt(col, val)

Sets the transparency flag of color col in the current palette to val (a boolean value). If called with no arguments, resets the transparency data to initial state (i.e. color number 5 is transparent).

spr(index, x, y, w = 1, h = 1, flipX = false, flipY = false)

Draws sprite at index to coordinates x, y. If w and/or h are specified, it will draw a region from the spritesheet which is w sprites wide and h sprites high. The flipX, flipY flags will flip the drawn image horizontally or vertically (respectively) when set to true.

Input

btn(key) -> boolean

Returns the boolean state of button no. key. Instead of using numbers, you can pass one of predefined constants provided by sparky: BTN_LEFT = 0 BTN_UP = 1 BTN_RIGHT = 2 BTN_DOWN = 3 BTN_A = 4 BTN_B = 5 BTN_START = 6 BTN_SELECT = 7 Directional buttons are mapped to arrow keys, a is mapped to either z or c, b is mapped to x, start is escape and select is tab. If you connect a gamepad, it should also be detected and the button layout should be reasonable.

btnp(key) -> boolean

Returns the state of button no. key, if it has just been pressed. If the button is held down, it will only return true once, and then return false on subsequent frames.

Audio

sfx(index)

Plays the sound no. index.

Map

mget(x, y) -> number
mset(x, y, v)

Returns or sets the sprite value v at map coordinates x,y.

map(celX = 0, celY = 0, x = 0, y = 0, w = 127, h = 127)

Draws the map starting from cell coordinates celX and celY, at screen coordinates x, y, with the width and height in cells equal to w and h. Calling it without any parameters will draw the entire map starting from the top left corner of the screen.

FAQ