Has anyone posted a good tutorial for tilemaps?

Hi there, I got a bit confused with the examples above and still couldn't managed to draw tiles on the screen. So I kept at it and made it work in my own way. The part that my smoothbrain forgot is that duh, once you have a tilemap you need to put some actual data in it.
Here's the simplest, most basic way to put some tiles on screen that I found, I hope it helps someone!

-- Core libraries
import "CoreLibs/graphics"
import "CoreLibs/math"
-- Constants
local pd <const> = playdate
local gfx <const>  = playdate.graphics

-- Setup
local tileset = gfx.imagetable.new("images/tilemap_packed-table-32-32.png")
local tilemap = gfx.tilemap.new()
tilemap:setImageTable(tileset)

--[[ 
Our tilemap has been created but it's still EMPTY! We need to feed it some actual level date with tilemap:setTiles().
Now this is the part that should be done with a level editor such as LTdk, but here I'm just gonna fill it with random tile indexes.
]]
local myLevel = {}
for i = 1, 100 do
	table.insert(myLevel, math.random(20))
end

-- tilemap:setTiles(data, width) : where "data" is a 1-D array and "width" is its width on screen IN TILES.
-- For example for a level that just fits the screen (400 px wide), if your tiles are 16 px wide, you wanna set the width to 400/16 = 25 

tilemap:setTiles(myLevel, 13) -- My tiles are 32 pixels, so it takes 13 of them to cover the screen's width.
tilemap:draw(0,0) -- x, y in PIXELS where you wanna start drawing the level

function playdate.update()
end

This successfully displays a "level" made of random tile indexes. But I do have some questions: Oddly, it seems that using tilemap:setSize() isn't even necessary. Is it because tilemap:setTiles() overrides it?

Also, it seems that if I remove the import lines at the beginning, math and graphics function still work. Why is that? Are they implicitly imported at compile time if I call them somewhere, rendering importing them useless?

1 Like