Pico-8

 Pico-8

An overview of using the Pico-8 fantasy console to develop games using the Lua programming language.  We look at the steps and code for building a number guessing game and a simple space shooter.
Video presentation - https://youtu.be/glNCIkmy93Q

2b81e34e6f1c272752161742f44790ed?s=128

Levi D. Smith

July 14, 2019
Tweet

Transcript

  1. Pico-8 Knoxville Game Design July 2019 Levi D. Smith

  2. Overview • ”Fantasy Console” • Game saves to image file

    “carts” • https://www.lexaloffle.com/pico-8.php • Carts can be loaded to website • No lowercase characters, shift for special characters • Playable online or offline • $14.99 - Windows, Mac, Linux – Humble Store • LUA code • https://www.lua.org/manual/5.3/
  3. Pros and Cons • Pros • Easy to get started

    • Built in sprite / level / sound effect / music editors • Web builds by default • Gamepad works by default • Fast to load/play • Small size (few kilobytes), easy to share • Access to Pico-8 repository built-in (splore) • If you’ve uploaded your game to the website, you can just tell someone the same of your game and they can load it • Lots of examples. All uploaded carts are “open source” • Cons • Limited resolution (128x128) / colors (16) • Not many helper functions • (write your own collision method) • Can’t (easily) load resources from external files (png, wav, etc) • Not for beginners • No console support • Limited lines of code (8192 “tokens” / 65535 characters) • No concept of rooms/scenes
  4. Pico-8 Hello World • Esc • Enter code • Esc

    • > run Esc to stop a running program Esc switches between editor mode and console mode
  5. Data types • Number / String / Boolean • foo

    = 42 • bar = “hello” • baz = true • Concatenate with .. • Add and assign +=, but no increment ++ • Comparison >,<,== • Tables (array / hash) • mytable = {} mytable.name = “hello” • anothertable = { name=“hello” } • Sequences • obj = { foo = 42, bar = “hello” } • mysequence = {} • mysequence.add(obj) • print mysequence[1].bar --index starts at 1 • #mysequence -- length
  6. Control structures • if (boolean_expression) then code elseif (boolean_expression) then

    code else code end • for i=1, 10 do code end • while (boolean) do code end • Logic comparison – or, and, not (not pipe or ampersand) • foreach(myarray, do_something) -- the object gets passed as the parameter to the function
  7. Number Guessing Game

  8. Saving / Loading games • save <filename>.p8 • load <filename>.p8

    • save <filename>.p8.png (capture cart image with F7) • export <filename>.html (web build) • folder – shows saved game folder • Change default folder • C:\Users\<username>\AppData\Roaming\pico-8\config.txt • root_path E:/<new path> • Other settings • Set window size - window_size 1024 768
  9. More Console Commands • reboot (close project) • shutdown (close

    pico-8) • help • ls (list directory files) • cd (change directory) • keyconfig (configure buttons)
  10. Examples • api • Hello • jelpi

  11. Special functions • _update() • Game logic • _draw() •

    Drawing code • _init() • Called at the start of program execution
  12. Drawing • Primitives • rect(0, 0, 50, 50, 11) –

    green rectangle outline size 50x50 • rectfill(100, 200, 110, 205, 0) – black filled rectangle at 100, 200 with width 10 and height 5 • circ – circle • circfill(x, y, r, color) – filled circle • line – draw line • Sprites • spr(sprite, x, y) • spr(sprite, x, y, w, h, flip_x, flip_y)
  13. Inputs • Gamepad (four direction d-pad and two buttons) •

    btn(0) --left • btn(1) --right • btn(2) --up • btn(3) --down • btn(4) --circle (keyboard default z,c,n) • btn(5) --X (keyboard default x,v,m) • Use btnp(n) to check if a button was pressed that frame (you don’t want the action to constantly happen) • btnp will still be invoked while key is down (like word processor key held down) • Direct keyboard and mouse input is experimental
  14. Helpful Built-In Functions • rnd(100) – returns a random real

    number between 0 and 100 • flr(3.14) – rounds number down to nearest integer • cls() – clears screen • camera(18, 85) – moves camera right 18 pixels and down 85 pixels • print(“hello”, 8, 16, 9) – prints “hello” at 8,16 in orange
  15. Sprite Editor • Use pencil to place pixels • Select

    color from palette • Select sprite index at bottom • Can make editor area larger and smaller (8x8, 16x16, etc)
  16. Audio • Play sound effect • sfx(0) • Play music

    • music(1)
  17. Functions • function foo(param1, param2) code return value end •

    Return value is optional
  18. Simple Space Shooter - Ship • Define ship variables •

    position (x,y), width, height, velocity (x,y) • Draw ship to the screen • Handle input • When arrow button is down add to velocity for each axis • Set maximum and minimum limits for velocity (clamp with mid) • When no arrow button down on axis, subtract from velocity • Update • Add velocity values to position
  19. Simple Space Shooter - Shooting • Create list of bullets

    • x, y, lifetime • Create a bullet drawing function • Create bullet update function • move bullet, add lifetime, delete if lifetime reaches maximum value • Spawn bullet on button down • Add ship shoot delay variable
  20. Simple Space Shooter - Enemies • Spawn enemies at the

    start of the game or throughout the game • x, y, w, h, lifetime • lifetime counter used for movements • Draw enemy at it’s x, y location • Add simple AI (move left and right) • Change direction every second (30 frames)
  21. Simple Space Shooter – simple collision • Make distance function

    • Pythagorean Theorem • distance = sqrt(a2 + b2) • For each bullet, determine if it has collided with an enemy • Will collide if the enemy center is four units away from the center of the bullet • Alternatively, use bounding boxes • Delete enemy and bullet on collision
  22. Simple Shooter - Improvements • Collision with player, game over

    state • Sound effects • Music • Sprites • Score • Powerups • Faster shooting • Spawn enemies at regular rates • Enemies shoot back • Increased difficulty as game progresses • Background image
  23. Docs • Official Docs • https://www.lexaloffle.com/pico-8.php?page=resources • Wiki • https://pico-8.fandom.com/wiki/Pico-8_Wikia

    • https://pico-8.fandom.com/wiki/Lua - Lua • Cheat Sheet • https://www.lexaloffle.com/bbs/files/16585/PICO- 8_CheatSheet_0111Gm_4k.png
  24. Tic-80, Tiny Computer • Web based, https://tic.computer/ • Free (pro

    $5)