Skip to content

Grid based Camera Movement

BearThorne edited this page Oct 21, 2017 · 10 revisions

I know there's already another tutorial on a scrolling camera...

the above tutorial is really good, and even shows you how to make a "passive" camera!
but I prefer Grid-based Movement for my games. and it didn't work well for me to use a smooth moving camera when my character simply jumps from one tile to the next. So Here's a quick tutorial on camera movement for a Grid-based game.

You can play the Complete Demo here:

First, Here's the full code:

-- title:  Grid-Based Camera
-- author: Bear Thorne
-- desc:   Scrolling Camera demo
-- script: lua


c={x=-7,y=2}  --cam
p={x=8,y=10}  --player
e={x=20,y=10} --npc
function TIC()

 if btnp(0,6,6) and mget(p.x,p.y-1)<1 then 
 if btnp(1,6,6) and mget(p.x,p.y+1)<1 then
 if btnp(2,6,6) and mget(p.x-1,p.y)<1 then
 if btnp(3,6,6) and mget(p.x+1,p.y)<1 then

 --here we draw the screen graphics

Here's how it works...notice our player object:


This keeps track of our player...sure that's normal. but look at the spr() function in charge of drawing the player to the screen:


It doesn't change to show the players current position! it just draws the player floating in the center of the screen! (30x17 visible map area...half is about 15x8...multiply by 8 for cell size to get the pixel coordinates)

now look at the map function:


this is where the magic happens. The map() function is told to draw the map starting at 15 cells to the left of the player, and 8 cells above the player. since that agrees with our player sprite's position...the "static" sprite always floats over our hero,

but what about items, and NPCs and bad guys?! How do we draw them so they move with the map?
unfortunately, the drawing functions in TIC-80 draw things in screen coordinates, not map coordinates, so if you draw them on a portion of the map that's off-screen, they'll never be seen because they're actually drawn off the screen!

That's why we have a camera object.


now before you ask why it's (-7,2), I don't really know. Just by trial and error I found that if the camera offset is (-7,2) then the objects will be drawn at the correct place.

I get it depends on where you place the player to start out. Camera minus Player has to equal the offset we used on the map. in this case:

p= {  x= 8,  y=10}
c= {  x=-7,  y=2 }
map(p.x-15,p.y-8, MAXX,MAXY)

--the map offset is 15x8

p.x- c.x  =map x offset
 8 - (-7) = 15

p.y-c.y=map x offset
10 - 2 = 8

So if you put the player at (0,0) then start the camera at (15,8)
That puts the camera at the relative zero for the screen.

The way this works is that everytime we move our character, we log the change with our camera. Then we just subtract our camera coordinates from the objects coordinates:


so if we move up once, it moves the object down once to keep it correctly placed on the map.
This should also work for things that walk enemies and NPCs

feel free to use any of the above code in your own projects, and if you're feeling generous, leave a credit for me at the top of your file!

if you have questions, or requests for future tutorials, you can email me at

You can’t perform that action at this time.