Instantly share code, notes, and snippets.

What would you like to do?
How to use Löve2d's SpriteBatch
function love.load()
-- Load our image we want to draw many times
image ="dirt.png")
-- The number of tiles we want to draw is pretty much the number
-- that will fit on the screen
maxX = math.ceil( / image:getWidth()) + 2
maxY = math.ceil( / image:getHeight()) + 2
local size = maxX * maxY
-- Set up a sprite batch with our single image and the max number of times we
-- want to be able to draw it. Later we will call spriteBatch:add() to tell
-- Love where we want to draw our image
spriteBatch =, size)
function love.update(dt)
-- If we were using a moving background, we would want to only call
-- setupSpriteBatch when its range or position changed
function setupSpriteBatch()
-- Set up (but don't draw) our images in a grid
for y = 0, maxY do
for x = 0, maxX do
-- Convert our x/y grid references to x/y pixel coordinates
local xPos = x * image:getWidth()
local yPos = y * image:getHeight()
-- Add the image we previously set to this point
spriteBatch:add(xPos, yPos)
function love.draw()
-- Draw the spriteBatch with only one call!,255,255)
-- Draw FPS in the bottom right corner,0,0),,

This comment has been minimized.

evanbrierton commented Jan 22, 2018

Tried this out and it works perfectly but I'm left wondering what is incrementing x and y within your setupSpriteBatch() function?


This comment has been minimized.

zorggn commented Feb 25, 2018

@evanbrierton for loops auto-increment their iteration variables, that's why they exist. (lines 29, 30)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment