paulgorman.org/technical

Lua and the LOVE Game Framework

(January 2017)

LOVE is a multi-platform game framework. Write games in Lua (Lua 5.1).

Debian has a love package, or download from https://love2d.org/#download.

Put games files in a directory. The root of the directory should contain the main.lua file. Games can be packaged into mygame.love files or run from the directory. Run a game like:

$ love /home/me/gamedir/
$ love /home/me/gamepackage.love

main.lua for helloworld/:

function love.draw()
	love.graphics.print("Hello World", 400, 300)
end

Callback Functions

A callback function works backwards. With a non-callback function, we call Lua or LOVE code and expect it to do something. With a callback function, we write code that LOVE calls. This acts as a structural tool.

love.load() LOVE always calls our love.load function when first starting the game, so love.load is a reliable place to put our initialization stuff.

love.update() LOVE continuously calls love.update.

function love.update(dt)
   if love.keyboard.isDown("up") then
	  num = num + 100 * dt -- this would increment num by 100 per second
   end
end

love.draw() What LOVE continuously calls update graphics.

function love.draw()
   love.graphics.draw(image, imgx, imgy)
   love.graphics.print("Click and drag the cake around or use the arrow keys", 10, 10)
end

love.mousepressed() LOVE calls this whenever the user presses a mouse button. (Available since LÖVE 0.10.0)

function love.mousepressed(x, y, button, istouch)
   if button == 1 then
	  imgx = x -- move image to where mouse clicked
	  imgy = y
   end
end

love.mousereleased() LOVE calls this whenever the user releases a mouse button. (Available since LÖVE 0.10.0)

function love.mousereleased(x, y, button, istouch)
   if button == 1 then
	  fireSlingshot(x,y) -- this totally awesome custom function is defined elsewhere
   end
end

love.keypressed() LOVE calls this everytime a KeyConstant is pressed. See https://love2d.org/wiki/KeyConstant

function love.keypressed(key)
   if key == 'b' then
	  text = "The B key was pressed."
   elseif key == 'a' then
	  a_down = true
   end
end

love.keyreleased() LOVE calls this when the user releases a keyboard key.

function love.keyreleased(key)
   if key == 'b' then
	  text = "The B key was released."
   elseif key == 'a' then
	  a_down = false
   end
end

love.focus() LOVE callse love.focus when the user focuses or de-focuses the game window.

function love.focus(f)
  if not f then
	print("LOST FOCUS")
  else
	print("GAINED FOCUS")
  end
end

love.quit() LOVE calls this when the user clicks the window’s CLOSE button.

function love.quit()
  print("Thanks for playing! Come back soon!")
end

Packaging

Package games.

https://love2d.org/wiki/Main_Page https://love2d.org/wiki/Tutorial:Callback_Functions