### Install and List Demo Cartridges Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Commands to install, navigate, and list the built-in example cartridges. ```text INSTALL_DEMOS CD DEMOS LS ``` -------------------------------- ### Controller Setup Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Information on how PICO-8 handles controller input and custom mappings. ```APIDOC ## Controller Setup ### Description PICO-8 uses the SDL2 controller configuration scheme. It detects common controllers on startup and looks for custom mappings in `sdl_controllers.txt`. ### Details - **Custom Mappings**: Create custom mappings in `sdl_controllers.txt` (one mapping per line). - **Mapping Tools**: Use SDL2's `controllermap` program or http://www.generalarcade.com/gamepadtool/ to generate mapping strings. - **Controller ID**: Find your controller's ID by searching for "joysticks" or "Mapping" in `log.txt` after running PICO-8. This ID may vary by OS. - **Keyboard Mapping**: Use `KEYCONFIG` to set up keyboard keys to trigger joystick button presses. ### Resources - SDL2 Controller Mapping: https://www.lexaloffle.com/bbs/?tid=32130 ``` -------------------------------- ### Raw Memory Write Example Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Shows how to write raw bytes to memory addresses using the '@' command. This example writes 4 bytes to video memory. ```lua ?"\@70000004xxxxhello" ``` -------------------------------- ### Save Cartridge Example Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Example of saving a cartridge named 'FOO'. The output confirms the file is saved as 'FOO.P8'. ```pico8 > SAVE FOO SAVED FOO.P8 ``` -------------------------------- ### Manage Custom Menu Items Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Examples of adding, updating, and handling callbacks for pause menu items. ```Lua MENUITEM(1, "RESTART PUZZLE", FUNCTION() RESET_PUZZLE() SFX(10) END ) ``` ```Lua MENUITEM(1, "FOO", FUNCTION(B) IF (B&1 > 0) THEN PRINTH("LEFT WAS PRESSED") END END ) ``` ```Lua MENUITEM(2 | 0x300, "RESET PROGRESS", FUNCTION() DSET(0,0) END ) ``` ```Lua MENUITEM(3, "SCREENSHAKE: OFF", FUNCTION() SCREENSHAKE = NOT SCREENSHAKE MENUITEM(NIL, "SCREENSHAKE: "..(SCREENSHAKE AND "ON" OR "OFF")) RETURN TRUE -- DON'T CLOSE END ) ``` -------------------------------- ### Install BBS Cartridges Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Command to install a collection of community-made cartridges. ```text INSTALL_GAMES ``` -------------------------------- ### Draw Gradient to Screen Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Example demonstrating PSET to draw a gradient across the screen based on pixel coordinates. ```lua FOR Y=0,127 DO FOR X=0,127 DO PSET(X, Y, X*Y/8) END END ``` -------------------------------- ### Export Cartridges via Command Line Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Example of using the PICO-8 executable to convert cartridge formats from a shell. ```text pico8 foo.p8 -export foo.p8.png ``` -------------------------------- ### Coroutine Example Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Demonstrates the creation and execution of a coroutine using COCREATE and CORESUME. The coroutine can be suspended using YIELD() and resumed later. ```lua FUNCTION HEY() PRINT("DOING SOMETHING") YIELD() PRINT("DOING THE NEXT THING") YIELD() PRINT("FINISHED") END C = COCREATE(HEY) FOR I=1,3 DO CORESUME(C) END ``` -------------------------------- ### Random Pixel Color Manipulation Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Example using PGET and PSET to randomly sample pixel colors and apply them to other locations. ```lua WHILE (TRUE) DO X, Y = RND(128), RND(128) DX, DY = RND(4)-2, RND(4)-2 PSET(X, Y, PGET(DX+X, DY+Y)) END ``` -------------------------------- ### Send Data via SERIAL Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Example of sending a byte bit-by-bit to an APA102 LED string using the SERIAL function. ```lua VAL = 42 -- VALUE TO SEND DAT = 16 CLK = 15 -- DATA AND CLOCK PINS DEPEND ON DEVICE POKE(0X4300,0) -- DATA TO SEND (SINGLE BYTES: 0 OR 0XFF) POKE(0X4301,0XFF) FOR B=0,7 DO -- SEND THE BIT (HIGH FIRST) SERIAL(DAT, BAND(VAL, SHL(1,7-B))>0 AND 0X4301 OR 0X4300, 1) -- CYCLE THE CLOCK SERIAL(CLK, 0X4301) SERIAL(0XFF, 5) -- DELAY 5 SERIAL(CLK, 0X4300) SERIAL(0XFF, 5) -- DELAY 5 END ``` -------------------------------- ### PICO-8 Lua Single-Line Comment Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Use two dashes '--' to start a single-line comment in PICO-8 Lua. ```lua -- USE TWO DASHES LIKE THIS TO WRITE A COMMENT ``` -------------------------------- ### Set multiple color transparencies using a bitfield Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Sets transparency for multiple colors using a bitfield. This example makes colors 0 and 1 transparent. ```lua PALT(0B1100000000000000) ``` -------------------------------- ### Play Music with MUSIC Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Starts music playback from a specified pattern. Can stop music or fade it in over time. Channel masks can reserve specific channels for music. ```lua MUSIC(0, 1000) ``` ```lua MUSIC(0, NIL, 7) ``` -------------------------------- ### Set Fill Pattern with Second Color Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Specify a second color for the fill pattern by using the high bits of a color parameter. This example uses brown and pink. ```pico8 FILLP(0b0011010101101000) ``` ```pico8 CIRCFILL(64,64,20, 0x4E) ``` -------------------------------- ### Set Fill Pattern via Color Parameter Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html The fill pattern can be set by modifying bits within a color parameter, such as for COLOR() or the last parameter of LINE(), RECT(), etc. This example uses POKE to enable fill patterns in high bits. ```pico8 POKE(0x5F34, 0x3) ``` ```pico8 CIRCFILL(64,64,20, 0x114E.ABCD) ``` -------------------------------- ### Save Cartridge with Label and Preview Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html To save a .p8.png cart with a label image, run the program and press CTRL-7. The first two lines starting with '--' are used for the label. ```pico8 -- OCEAN DIVER LEGENDS -- BY LOOPY ``` -------------------------------- ### Remap entire screen colors with a table Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Remaps the entire screen's colors using a table. This example shades the screen gray, including already drawn elements. ```lua PAL({1,1,5,5,5,6,7,13,6,7,7,6,13,6,7,1}, 1) ``` -------------------------------- ### Get Sprite Flags as Bitfield Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Retrieves all flags for sprite 2 as a single bitfield value. The example shows the result after setting bits 0, 1, 3, and 4. ```lua PRINT(FGET(2)) ``` -------------------------------- ### PICO-8 FOR Loop with Range Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Iterate a specific number of times. The loop includes both the start and end values in its range. For example, FOR X=1,5 DO prints 1, 2, 3, 4, 5. ```pico8 FOR X=1,5 DO PRINT(X) END ``` -------------------------------- ### Make Directory Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Use the MKDIR command followed by a directory name to create a new folder. ```pico8 MKDIR BLAH ``` -------------------------------- ### Custom Main Loop with FLIP Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Demonstrates a manual frame update loop using FLIP to push the back buffer to the screen. ```lua ::_:: CLS() FOR I=1,100 DO A=I/50 - T() X=64+COS(A)*I Y=64+SIN(A)*I CIRCFILL(X,Y,1,8+(I/4)%8) END FLIP()GOTO _ ``` -------------------------------- ### List Directory Contents Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Use the LS command to list the files and directories in the current directory. ```pico8 LS ``` -------------------------------- ### Map Functions Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Functions for getting and setting map tile values, and drawing the map. ```APIDOC ## MGET(X, Y) ### Description Gets the map value at the specified coordinates (X, Y). ### Method N/A (Function Call) ### Endpoint N/A ### Parameters #### Query Parameters - **X** (number) - The X-coordinate on the map. - **Y** (number) - The Y-coordinate on the map. ### Request Example ```lua -- Get the map value at (10, 5) local tile_id = MGET(10, 5) ``` ### Response #### Success Response (200) - **value** (number) - The map value (tile index) at the specified coordinates. Returns 0 if out of bounds by default. #### Response Example ``` 15 ``` ## MSET(X, Y, VAL) ### Description Sets the map value at the specified coordinates (X, Y) to VAL. ### Method N/A (Function Call) ### Endpoint N/A ### Parameters #### Query Parameters - **X** (number) - The X-coordinate on the map. - **Y** (number) - The Y-coordinate on the map. - **VAL** (number) - The value to set at the map coordinates. ### Request Example ```lua -- Set the map value at (10, 5) to 15 MSET(10, 5, 15) ``` ### Response #### Success Response (200) N/A (This function modifies map data, it does not return a value). ## MAP([TILE_X, TILE_Y], [SX, SY], [TILE_W, TILE_H], [LAYERS]) ### Description Draws a section of the map to the screen. It can draw a specified rectangular area of tiles starting from a given map position to a target screen position. ### Method N/A (Function Call) ### Endpoint N/A ### Parameters #### Query Parameters - **TILE_X** (number) - Optional - The starting X-coordinate in the map tiles. - **TILE_Y** (number) - Optional - The starting Y-coordinate in the map tiles. - **SX** (number) - Optional - The screen X-coordinate to draw at. - **SY** (number) - Optional - The screen Y-coordinate to draw at. - **TILE_W** (number) - Optional - The width of the map section to draw in tiles. - **TILE_H** (number) - Optional - The height of the map section to draw in tiles. - **LAYERS** (number) - Optional - A bitfield specifying which map layers (sprite flags) to draw. ### Request Example ```lua -- Draw a 4x2 section of the map starting from map coordinates (0,0) -- to screen coordinates (20,20) MAP(0, 0, 20, 20, 4, 2) -- Draw the entire map with default settings MAP() ``` ### Response #### Success Response (200) N/A (This function draws to the screen, it does not return a value). ### Notes - If TILE_W and TILE_H are not provided, the entire map is drawn. - Sprite 0 is considered empty and is not drawn by default. This can be changed with POKE(0x5F36, 0x8). - Out-of-bounds map access for MGET can be customized using POKE(0x5f36, 0x10) and POKE(0x5f5a, NEWVAL). ``` -------------------------------- ### Get Persistent Data with DGET Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Retrieves a number stored at a specific index (0-63) after calling CARTDATA(). ```pico8 DGET(INDEX) ``` -------------------------------- ### Export PICO-8 Game to HTML Folder Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Exports the game to a folder, using 'index.html' as the main file. Use '-F' to specify folder output. ```bash EXPORT -F MYGAME.HTML ``` -------------------------------- ### Get Text Width Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Calculates the width of a string by printing it off-screen and capturing the returned right-most X position. ```lua W = PRINT("HOGE", 0, -20) ``` -------------------------------- ### Create Interactive Program Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html A basic program structure using _UPDATE and _DRAW callbacks to move a circle with cursor keys. ```lua X = 64 Y = 64 FUNCTION _UPDATE() IF (BTN(0)) THEN X=X-1 END IF (BTN(1)) THEN X=X+1 END IF (BTN(2)) THEN Y=Y-1 END IF (BTN(3)) THEN Y=Y+1 END END FUNCTION _DRAW() CLS(5) CIRCFILL(X,Y,7,14) END ``` -------------------------------- ### PICO-8 Program Structure: _INIT, _UPDATE, _DRAW Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Define special functions _INIT, _UPDATE, and _DRAW for program initialization, game logic updates, and rendering, respectively. These are called automatically by PICO-8 during execution. ```pico8 FUNCTION _INIT() -- ALWAYS START ON WHITE COL = 7 END FUNCTION _UPDATE() -- PRESS X FOR A RANDOM COLOUR IF (BTNP(5)) COL = 8 + RND(8) END FUNCTION _DRAW() CLS(1) CIRCFILL(64,64,32,COL) END ``` -------------------------------- ### PICO-8 Table Initialization and Assignment Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Create and populate tables, which act as key-value stores. Tables can hold mixed data types and be used as arrays with integer keys. ```pico8 A={} A[1] = "BLAH" A[2] = 42 A["FOO"] = {1,2,3} ``` -------------------------------- ### Export PICO-8 Game to Binary Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Generates stand-alone executable binaries for Windows, Linux, Mac, and Raspberry Pi. Use '.BIN' to specify the output file name. ```bash EXPORT MYGAME.BIN ``` -------------------------------- ### Get Value Type with TYPE Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Returns the data type of a value as a string. Useful for checking if a variable is a number or string. ```pico8 PRINT(TYPE(3)) PRINT(TYPE("3")) ``` -------------------------------- ### Running EXPORT from Host OS Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Executes the PICO-8 exporter in headless mode from the host operating system. Use '-export' followed by parameters as a single lowercase string. ```bash pico8 mygame.p8 -export "-i 32 -s 2 -c 12 mygame.bin dat0.p8 dat1.p8" ``` -------------------------------- ### Get Pixel Color Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Retrieves the color index of the pixel at the specified coordinates (X, Y). Returns 0 if coordinates are out of bounds. ```lua PGET(X, Y) ``` -------------------------------- ### Change Directory Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Use the CD command to navigate directories. 'CD BLAH' changes to a subdirectory, 'CD ..' moves up one level, and 'CD /' returns to the root. ```pico8 CD BLAH ``` ```pico8 CD .. ``` ```pico8 CD / ``` -------------------------------- ### Export PICO-8 Game with Extra Files Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Includes an extra file in the output folder or archive. Use '-E' followed by the file name. ```bash EXPORT -E README.TXT MYGAME.BIN ``` -------------------------------- ### Set Single Sprite Flag Bit Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Example of setting a single flag bit (bit 4) for sprite 2 to TRUE. ```lua FSET(2, 4, TRUE) ``` -------------------------------- ### PICO-8 0-Based Array Initialization Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Create arrays with 0-based indexing by explicitly defining the 0th key. Subsequent elements are added sequentially. ```pico8 A = {[0]=10,11,12,13,14} ``` -------------------------------- ### Get Button State with BTN Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Retrieves the state of a specific button for a player. Can return a bitfield of all button states if no parameters are supplied. ```lua BTN(0) ``` ```lua BTN(0, 1) ``` -------------------------------- ### Open User Data Folder Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Opens the PICO-8 user data folder, where GIFs, screenshots, and audio are typically saved. ```lua EXTCMD("FOLDER") ``` -------------------------------- ### Execute Console Commands Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Basic commands for drawing shapes and printing text directly in the PICO-8 console. ```lua PRINT("HELLO WORLD") RECTFILL(80,80,120,100,12) CIRCFILL(70,90,20,14) FOR I=1,4 DO PRINT(I) END ``` -------------------------------- ### Set Multiple Sprite Flag Bits Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Example of setting multiple flag bits (0, 1, and 3) for sprite 2 using a bitwise OR operation. ```lua FSET(2, 1 | 2 | 8) ``` -------------------------------- ### Get Map Value with MGET Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Retrieves the value from the map at the specified X, Y coordinates. Returns 0 if out of bounds, unless a custom return value is set. ```lua POKE(0x5f36, 0x10) ``` ```lua POKE(0x5f5a, NEWVAL) ``` -------------------------------- ### Accessing Bundled Cartridges at Runtime Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Demonstrates how to load data or run other cartridges bundled during export. Use RELOAD() for specific data ranges or LOAD() to run another cart. ```lua RELOAD(0,0,0X2000, "DAT1.P8") -- LOAD SPRITESHEET FROM DAT1.P8 LOAD("GAME2.P8") -- LOAD AND RUN ANOTHER CART ``` -------------------------------- ### Control Audio Playback Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Use the \A command to trigger SFX playback or define note sequences directly within a print statement. ```PICO-8 ? "\A" -- SINGLE BEEP ?"\A12" -- PLAY EXISTING DATA AT SFX 12 ``` ```PICO-8 PRINT "\ACE-G" -- MINOR TRIAD ``` ```PICO-8 PRINT "\AC..E-..G" -- STACCATO MINOR TRIAD ``` ```PICO-8 PRINT "\AS4X5C1EGC2EGC3EGC4" ``` -------------------------------- ### Configure Input Keys Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Opens the built-in utility to remap controller inputs. ```text KEYCONFIG ``` -------------------------------- ### Remap display colors Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Remaps the entire display palette for screen-wide effects. This example changes all gray (color 6) pixels to pink (color 14) after they have been drawn. ```lua PAL(6,14,1) ``` -------------------------------- ### Export PICO-8 Game to HTML Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Generates a stand-alone HTML player for your PICO-8 game. Use '.HTML' to specify the output file name. ```bash EXPORT MYGAME.HTML ``` -------------------------------- ### Set Sprite Flag Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Sets or gets the value of a specific flag (F) for a given sprite (N). Flags are bitmasks from 0 to 7. VAL can be TRUE or FALSE. ```lua FSET(N, [F], VAL) ``` -------------------------------- ### Get Sprite Sheet Pixel Color Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Retrieves the color index of a pixel within the sprite sheet at the specified coordinates (X, Y). Returns 0 if coordinates are out of bounds. ```lua SGET(X, Y) ``` -------------------------------- ### External Commands (EXTCMD) Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Commands for interacting with the PICO-8 environment, such as recording GIFs, managing files, and opening folders. ```APIDOC ## EXTCMD ### Description Used to execute external commands within the PICO-8 environment. ### Methods - `EXTCMD("REC")` or `EXTCMD("VIDEO")`: Starts recording a GIF to the desktop. - `EXTCMD("VIDEO", scale)`: Records a GIF with a specified scale factor. - `EXTCMD("VIDEO", scale, output_type)`: Records a GIF with specified scale and output type (0 for default, 1 for user data folder). - `EXTCMD("FOLDER")`: Opens the user data folder. - `EXTCMD("REC_FRAMES")`: Records a GIF frame by frame, ensuring one frame per `FLIP()` call. - `EXTCMD("SET_FILENAME", "name")`: Sets a custom filename prefix for recorded GIFs and screenshots. ### Parameters - `command` (string): The name of the external command to execute. - `scale` (number, optional): The scaling factor for GIF recording. - `output_type` (number, optional): Specifies the output location for recorded GIFs. - `filename` (string, optional): The custom filename prefix. ### Examples ```apidoc -- Record a GIF with default settings EXTCMD("REC") -- Record a GIF scaled by 4 EXTCMD("VIDEO", 4) -- Record a GIF and save to user data folder EXTCMD("VIDEO", 0, 1) -- Open the user data folder EXTCMD("FOLDER") -- Record frame by frame EXTCMD("REC_FRAMES") -- Set custom filename EXTCMD("SET_FILENAME", "my_capture") ``` ``` -------------------------------- ### Enable Devkit Input Mode Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Enables mouse and keyboard input by poking flags into memory address 0x5F2D. ```lua POKE(0x5F2D, flags) -- where flags are: 0x1 Enable 0x2 Mouse buttons trigger btn(4)..btn(6) 0x4 Pointer lock (use stat 38..39 to read movements) ``` -------------------------------- ### PICO-8 Table Length Operator and ADD function Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Get the number of elements in a 1-based integer-indexed table using the '#' operator. The ADD function appends an element to such tables. ```pico8 PRINT(#A) ADD(A, 15) PRINT(#A) ``` -------------------------------- ### PICO-8 Table String Key Access with Dot Notation Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Access table elements with string keys using dot notation for cleaner syntax. PLAYER.X is equivalent to PLAYER["X"]. ```pico8 PLAYER = {} PLAYER.X = 2 PLAYER.Y = 3 ``` -------------------------------- ### Set color transparency Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Sets the transparency for a specific color index. This example makes red pixels (color 8) transparent for subsequent sprite and tline drawing calls. ```lua PALT(8, TRUE) ``` -------------------------------- ### PICO-8 Basic Comparison Conditionals Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Demonstrates various comparison operators for conditional checks. These include equality (==), inequality (~= or !=), less than or equal (<=), and greater than (>). ```pico8 IF (4 == 4) THEN PRINT("EQUAL") END ``` ```pico8 IF (4 ~= 3) THEN PRINT("NOT EQUAL") END ``` ```pico8 IF (4 <= 4) THEN PRINT("LESS THAN OR EQUAL") END ``` ```pico8 IF (4 > 3) THEN PRINT("MORE THAN") END ``` -------------------------------- ### Draw a series of lines forming a circle Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Draws a series of lines to approximate a circle. This example demonstrates drawing lines from a calculated point on a circle's circumference to the center. ```lua CLS() LINE() FOR I=0,6 DO LINE(64+COS(I/6)*20, 64+SIN(I/6)*20, 8+I) END ``` -------------------------------- ### Print String Using Shortcut Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Uses the '?' shortcut to print a string without brackets. This is equivalent to PRINT(STR). ```lua ?"HI" ``` -------------------------------- ### Screenshots and GIFs Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html How to capture screenshots and record GIFs within a running PICO-8 cartridge. ```APIDOC ## Screenshots and GIFs ### Description Capture screenshots and record video/GIFs directly from a running PICO-8 cartridge. ### In-Cartridge Controls - **CTRL-6**: Save a screenshot to the desktop. - **CTRL-7**: Capture cartridge label image. - **CTRL-8**: Start recording a video (resets video starting point). - **CTRL-9**: Save GIF video to desktop (8 seconds by default). ### Configuration - **Maximum GIF Length**: Use `CONFIG GIF_LEN ` (maximum: 120). - **GIF Reset Mode**: Use `CONFIG GIF_RESET_MODE 1` to reset recording every time for a non-overlapping sequence. ### Notes - PICO-8 uses 33.3fps for GIF recording as it's the closest match to 30fps. - If saving to the desktop fails, configure an alternative desktop path in `config.txt`. ``` -------------------------------- ### Assign colors using a table Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Assigns specific colors using a table for remapping. This example remaps colors 12 and 14 to red (color 9) and color 8 respectively. ```lua PAL({[12]=9, [14]=8}) ``` -------------------------------- ### PICO-8 60fps Mode: _UPDATE60() Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Use _UPDATE60() instead of _UPDATE() to enable 60fps mode. This provides more processing time per frame but halves the available CPU budget. PICO-8 may still drop to 30fps if the host machine cannot sustain 60fps. ```pico8 _UPDATE60() ``` -------------------------------- ### Record GIF with Custom Scaling Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Saves a GIF to the desktop with specified scaling. Use CONFIG GIF_SCALE to change the default scaling. ```lua EXTCMD("VIDEO", 4) ``` -------------------------------- ### Remap sprite colors Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Remaps subsequent sprite drawing calls to use different colors. This example changes orange (color 9) to red (color 8) for sprite rendering. ```lua PAL(9,8) ``` -------------------------------- ### Perform Bitwise Operations Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Demonstrates the use of bitwise functions and operators in PICO-8. ```Lua PRINT(BAND(X,Y)) -- RESULT:0B0010 (2 IN DECIMAL) ``` ```Lua PRINT(67 & 63) -- result:3 equivalent to BAND(67,63) ``` -------------------------------- ### P8SCII Control Codes for Text Formatting Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Illustrates using P8SCII control codes within a PRINT statement to set background and foreground colors. Control codes are represented by escape sequences starting with '\'. ```lua PRINT("\#C\F5 BLUE ") ``` -------------------------------- ### Include Lua Code with #INCLUDE Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Use #INCLUDE to inject plaintext Lua code, a single tab from another cartridge, or all tabs from another cartridge into the current program at boot time. Filenames are relative to the current cartridge. ```lua #INCLUDE SOMECODE.LUA ``` ```lua #INCLUDE ONETAB.P8:1 ``` ```lua #INCLUDE ALLTABS.P8 ``` -------------------------------- ### EXTCMD(CMD_STR, [P1, P2]) Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Executes special system commands like screenshots, video recording, or window management. ```APIDOC ## EXTCMD(CMD_STR, [P1, P2]) ### Description Special system command for controlling the host environment. ### Parameters - **CMD_STR** (string) - Required - The command string (e.g., "pause", "reset", "screen", "video"). - **P1** (number) - Optional - Command-specific parameter. - **P2** (number) - Optional - Command-specific parameter. ``` -------------------------------- ### Export PICO-8 Game with Custom Icon Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Exports a binary with a custom icon from the sprite sheet. Use '-I' for icon index, '-S' for size (NxN), and '-C' for transparent color. ```bash EXPORT -I 32 -S 2 -C 12 MYGAME.BIN ``` -------------------------------- ### Memory Layout and Access Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Details the different types of memory in PICO-8 and how to access and manipulate them using functions like PEEK, POKE, MEMCPY, and MEMSET. ```APIDOC ## PICO-8 Memory ### Description PICO-8 has 3 types of memory: Base RAM (64k), Cart ROM (32k), and Lua RAM (2MB). - **Base RAM**: Accessible with PEEK(), POKE(), MEMCPY(), MEMSET(). - **Cart ROM**: Same layout as base ram until 0x4300. - **Lua RAM**: Stores the compiled program and variables. **Technical Note**: Data modified in the editor is in cart rom, but API functions operate on base ram. PICO-8 automatically copies cart rom to base ram when a cartridge is loaded, run, or when exiting editor modes. ### Base RAM Memory Layout - **0x0 - 0x0FFF**: GFX - **0x1000 - 0x1FFF**: GFX2/MAP2 (SHARED) - **0x2000 - 0x2FFF**: MAP - **0x3000 - 0x30FF**: GFX FLAGS - **0x3100 - 0x31FF**: SONG - **0x3200 - 0x32FF**: SFX - **0x4300 - 0x55FF**: USER DATA - **0x5600 - 0x56FF**: CUSTOM FONT - **0x5E00 - 0x5EFF**: PERSISTENT CART DATA (256 BYTES) - **0x5F00 - 0x5F3F**: DRAW STATE - **0x5F40 - 0x5F7F**: HARDWARE STATE - **0x5F80 - 0x5FFF**: GPIO PINS (128 BYTES) - **0x6000 - 0x7FFF**: SCREEN (8K) - **0x8000 - 0xFFFF**: USER DATA User data can be used for any purpose. Persistent cart data is stored if CARTDATA() has been called. Color format (gfx/screen) is 2 pixels per byte. Map format is one byte per tile. ### Remapping Graphics and Map Data Addresses for GFX, MAP, and SCREEN can be reassigned: - **0x5F54**: GFX mapping (0x00: default, 0x60: use screen memory). - **0x5F55**: SCREEN mapping (0x60: default, 0x00: use spritesheet). - **0x5F56**: MAP mapping (0x20: default, 0x10-0x2f, or 0x80+). - **0x5F57**: MAP SIZE (width). 0 means 256. Defaults to 128. Addresses are in 256-byte increments. Map data can only be in regions 0x1000-0x2FFF, 0x8000-0xFFFF. GFX and SCREEN can also be mapped to upper memory locations (0x80, 0xA0, 0xC0, 0xE0). ### PEEK(ADDR, [N]) #### Description Read a byte from a base ram address. If N is specified, PEEK() returns that number of results (max: 8192). #### Parameters - **ADDR** (number) - The base ram address to read from. - **N** (number, optional) - The number of results to return. #### Request Example ```lua -- Read the first byte from address 0x6000 local byte_data = PEEK(0x6000) -- Read the first 2 bytes from address 0x6000 local byte1, byte2 = PEEK(0x6000, 2) ``` #### Response Returns the byte(s) read from the specified address. ``` -------------------------------- ### Set Default Print Attributes Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Configure global print settings by writing to memory addresses 0x5f58 through 0x5f5b. ```PICO-8 // poke(0x5f58, 0x1 | 0x2 | 0x4 | 0x8 | 0x20 | 0x40) -- pinball everywhere ``` -------------------------------- ### POKE(0x5F2D, flags) Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Enables devkit input mode for mouse and keyboard support. ```APIDOC ## POKE(0x5F2D, flags) ### Description Enables devkit input mode. Flags: 0x1 (Enable), 0x2 (Mouse buttons trigger btn(4)..btn(6)), 0x4 (Pointer lock). ``` -------------------------------- ### Run or Stop Cartridge with RUN and STOP Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html RUN restarts the program from the beginning, optionally accepting a parameter string. STOP halts the cart and can display a message. RESUME continues execution. ```lua RUN([PARAM_STR]) ``` ```lua RUN() ``` ```lua STOP([MESSAGE]) ``` ```lua RESUME ``` ```lua . ``` -------------------------------- ### Blink LEDs using POKE and GPIO Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html A program that blinks LEDs attached to GPIO pins by POKEing values to the GPIO memory range. Values are digital (0 or 255). ```pico8 T = 0 FUNCTION _DRAW() CLS(5) FOR I=0,7 DO VAL = 0 IF (T % 2 < 1) VAL = 255 POKE(0X5F80 + I, VAL) CIRCFILL(20+I*12,64,4,VAL/11) END T += 0.1 END ``` -------------------------------- ### Load Cartridge Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Use the LOAD command to load a PICO-8 cartridge from the current directory. The .P8 extension is optional. ```pico8 LOAD BLAH ``` -------------------------------- ### Floor and Ceiling Functions Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Rounding functions for floating point numbers. ```lua ?FLR ( 4.1) --> 4 ?FLR (-2.3) --> -3 ``` ```lua ?CEIL( 4.1) --> 5 ?CEIL(-2.3) --> -2 ``` -------------------------------- ### STAT(x) Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Retrieves the state of mouse and keyboard inputs. ```APIDOC ## STAT(x) ### Description Returns the state of various input devices. ### Parameters - **x** (number) - Required - The stat ID (e.g., 30 for keypress, 32 for Mouse X, 34 for Mouse buttons). ``` -------------------------------- ### STAT(X) Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Retrieves various system status values. ```APIDOC ## STAT(X) ### Description Get system status based on the provided index X. ### Parameters - **X** (number) - Required - The status index (e.g., 0 for memory, 1 for CPU, 4 for clipboard, 80-85 for UTC time). ``` -------------------------------- ### PICO-8 Commandline Parameters Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html These commandline parameters override settings found in config.txt. ```APIDOC ## PICO-8 Commandline Parameters ### Description These commandline parameters override settings found in config.txt. ### Method Commandline ### Endpoint pico8 [switches] [filename.p8] ### Parameters #### Commandline Switches - **-width n** (integer) - Set the window width. - **-height n** (integer) - Set the window height. - **-windowed n** (0 or 1) - Set windowed mode off (0) or on (1). - **-volume n** (0-256) - Set audio volume. - **-joystick n** (0-7) - Joystick controls start at player n. - **-pixel_perfect n** (0 or 1) - 1 for unfiltered screen stretching at integer scales (on by default). - **-preblit_scale n** (integer) - Scale the display by n before blitting to screen (useful with -pixel_perfect 0). - **-draw_rect x,y,w,h** (integers) - Absolute window coordinates and size to draw PICO-8's screen. - **-display n** (integer) - Set the preferred display index when multiple displays are present. - **-run filename** (string) - Load and run a cartridge. - **-x filename** (string) - Execute a PICO-8 cart headless and then quit (experimental!). - **-export param_str** (string) - Run EXPORT command in headless mode and exit. - **-p param_str** (string) - Pass a parameter string to the specified cartridge. - **-splore** - Boot in splore mode. - **-home path** (string) - Set the path to store config.txt and other user data files. - **-root_path path** (string) - Set the path to store cartridge files. - **-desktop path** (string) - Set a location for screenshots and gifs to be saved. - **-screenshot_scale n** (integer) - Scale of screenshots. Default: 3 (368x368 pixels). - **-gif_scale n** (integer) - Scale of gif captures. Default: 2 (256x256 pixels). - **-gif_len n** (1-120) - Set the maximum gif length in seconds. - **-gui_theme n** (integer) - Use 1 for a higher contrast editor colour scheme. - **-timeout n** (seconds) - How many seconds to wait before downloads timeout. Default: 30. - **-software_blit n** (0 or 1) - Use software blitting mode off (0) or on (1). - **-foreground_sleep_ms n** (milliseconds) - How many milliseconds to sleep between frames. - **-background_sleep_ms n** (milliseconds) - How many milliseconds to sleep between frames when running in background. - **-accept_future n** (0 or 1) - 1 to allow loading cartridges made with future versions of PICO-8. - **-global_api n** (0 or 1) - 1 to leave api functions in global scope (useful for debugging). ``` -------------------------------- ### Export PICO-8 Game with Custom HTML Template Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html Exports the game using a custom HTML template. Use '-P' followed by the template name (e.g., ONE_BUTTON). The template file is located in {application data}/pico-8/plates/. ```bash EXPORT MYGAME.HTML -P ONE_BUTTON ``` -------------------------------- ### List Files with LS Source: https://www.lexaloffle.com/dl/docs/pico-8_manual.html LS lists .p8 and .p8.png files in a specified directory, relative to the current one. Directories are indicated by a trailing slash. When called from a running cartridge, LS returns a table; from BBS, it returns nil. ```lua LS([DIRECTORY]) ``` ```lua LS("..") ```