LÖVE2D and Lua 101

Tracy Chen
3 min readJul 9, 2022

--

Everything you need to get started developing your first 2D game

LÖVE (Love2D) is a framework that you can use to make cross-platform (Windows, Mac, iOS, Android and Linux) 2D games. It is open-source and uses Lua and C++ as its scripting language.

Installation

Head over to LÖVE and choose your download based on your operating system.

Lua

Lua is one of the programming languages utilized by LÖVE. Lua is an object-oriented programming language (OOP) that utilizes Tables are the main data structure. You can think of Lua Tables as similar to a JavaScript object or a Python dictionary.

Classes are not inherent to Lua. Luckily there is a library we can use to facilitate this development.

Add the below class.lua file to your application.

class.lua

--[[Copyright (c) 2010-2013 Matthias RichterPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.Except as contained in this notice, the name(s) of the above copyright holders shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.]]--local function include_helper(to, from, seen)if from == nil thenreturn toelseif type(from) ~= 'table' thenreturn fromelseif seen[from] thenreturn seen[from]endseen[from] = tofor k,v in pairs(from) dok = include_helper({}, k, seen) -- keys might also be tablesif to[k] == nil thento[k] = include_helper({}, v, seen)endendreturn toend-- deeply copies `other' into `class'. keys in `other' that are already-- defined in `class' are omittedlocal function include(class, other)return include_helper(class, other, {})end-- returns a deep copy of `other'local function clone(other)return setmetatable(include({}, other), getmetatable(other))endlocal function new(class)-- mixinsclass = class or {}  -- class can be nillocal inc = class.__includes or {}if getmetatable(inc) then inc = {inc} endfor _, other in ipairs(inc) doif type(other) == "string" thenother = _G[other]endinclude(class, other)end-- class implementationclass.__index = classclass.init    = class.init    or class[1] or function() endclass.include = class.include or includeclass.clone   = class.clone   or clone-- constructor callreturn setmetatable(class, {__call = function(c, ...)local o = setmetatable({}, c)o:init(...)return oend})end-- interface for cross class-system compatibility (see https://github.com/bartbes/Class-Commons).if class_commons ~= false and not common thencommon = {}function common.class(name, prototype, parent)return new{__includes = {prototype, parent}}endfunction common.instance(class, ...)return class(...)endend-- the modulereturn setmetatable({new = new, include = include, clone = clone},{__call = function(_,...) return new(...) end})

In your main.lua file, import the class.lua file by using the keyword “require”. We can use this method to import in any other classes in our application.

-- ADD in your main.lua class to import a classClass = require 'class'

To create a new class, set class name = Class{}.

-- ADD in your NameOfClass.lua to create a new classNameOfClass = Class{}

LÖVE main functions

Love calls the below functions in sequential order. Love.load() runs once at the start of the application, then it will repeatedly loop through the love.update() and love.draw() functions.

-- Function that runs once at the start of an application function love.load()-- Callback function used to update the state of the game every frame function love.update(dt)-- Callback function that draws and renders your graphicsfunction love.draw()

Additional LÖVE Tutorial

This is a great site that I found for learning Love2D.

--

--

Tracy Chen
Tracy Chen

Written by Tracy Chen

A product person who still dabbles in code.

No responses yet