10 Print on PICO-8
By Michael Doornbos
- 2 minutes read - 411 wordsIntroduction
Continuing the exploration of 10PRINT, we look at how to implement the 10 Print algorithm on the PICO-8 platform. Or as I like to say, “10PRINT ON ALL THE THINGS”.
I’ve lost track of all the platforms I’ve made it on. I should make a list…
What is PICO-8?
The PICO-8 platform is a fantasy console for making, sharing, and playing tiny games and other computer programs. It is an excellent platform for learning to program and making small games.
The print method
Unfortunately, there is no built-in function in PICO-8 to concatenate or inline-print text dynamically like a terminal would. You must either:
Use print() with precise positioning or Work with screen memory (poke()), or Use sprites to simulate text.
The code
The Pico-8 platform has a built-in function to draw lines. We can use this function to draw the maze.
x=0
y=0
-- how many pixels per 'row' of drawing we want
row_height = 4
function scroll_up(pixels)
-- each row is 64 bytes in memory (128 pixels / 2 pixels per byte)
-- so shifting up by 'pixels' rows means skipping 'pixels * 64' bytes
local row_bytes = 64
local shift = pixels * row_bytes
local size = row_bytes * (128 - pixels)
-- copy screen memory from further down to the top
memcpy(0x6000, 0x6000 + shift, size)
-- clear the new bottom rows that just got "pulled up"
rectfill(0, 128 - pixels, 127, 127, 0)
end
function _init()
cls()
end
function _update()
-- draw a single diagonal each frame
if rnd(1) < 0.5 then
line(x, y, x+row_height, y+row_height, 7)
else
line(x+row_height, y, x, y+row_height, 7)
end
-- move right by row_height pixels each time
x += row_height
-- if we go past the right edge, go to a new "line"
if x >= 128 then
x = 0
y += row_height
-- if we go past the bottom, scroll the screen up
if y >= 128 then
scroll_up(row_height)
-- reset to bottom "row" so we keep drawing there
y = 128 - row_height
end
end
end
function _draw()
-- no cls() here, so we keep what we've drawn
end
It’s slightly longer than 1 line, but it does look cool.
Conclusion
The Pico-8 platform is a fun and easy way to create small games and programs. It is a great way to learn to program and experiment with different ideas.
What other platforms would you like to see the 10 Print algorithm implemented on? Let me know!