No history yet

Introduction to LOVE 2D

What is LÖVE?

LÖVE (often written as Love2D) is a free tool for making 2D games. It's not a full game engine with fancy visual editors like Unity or Godot. Instead, it’s a framework. Think of it like a well-organized toolbox. It gives you all the essential tools for graphics, sound, and physics, but you get to decide how to put them together using the Lua programming language.

Unlike Game Maker and Unity, LÖVE is a framework and not an engine.

This approach gives you a lot of freedom and is a great way to learn the fundamentals of how games work from the ground up. Because it’s so lightweight, you can get a simple game running in just a few minutes.

Getting Started

First, you'll need to install LÖVE. Head over to the official website, love2d.org, and download the version for your operating system. The installation is straightforward.

Once installed, you can run a game project. A LÖVE project is simply a folder that contains a file named main.lua. That's it. You can name the folder anything you like.

myfirstgame/
└── main.lua

To run your game, open your terminal or command prompt, navigate to your project folder, and type love . (the period refers to the current directory). Alternatively, you can drag the project folder directly onto the LÖVE application executable.

The Core Functions

Every LÖVE game is built around three special functions, called callbacks. LÖVE automatically calls these functions at specific times to run your game.

Callback

noun

A function that is passed as an argument to another function and is executed after some event or task is completed. In LÖVE, these are functions you define that the framework calls automatically during the game loop.

The three essential callbacks are:

  1. love.load(): This runs only once, right when your game starts. It’s the perfect place to load assets like images and sounds, and set up your game's initial state.

  2. love.update(dt): This runs over and over again, in every single frame. It’s where all your game's logic goes, like moving characters or checking for collisions. The dt argument stands for "delta time"—it's the time that has passed since the last frame, which helps keep movement smooth and consistent regardless of the computer's speed.

  3. love.draw(): This also runs every frame, immediately after love.update(). Its only job is to draw things on the screen. All your drawing commands go here.

Your First Application

Let's create a classic "Hello, World!" application. Open your main.lua file in a text editor and add the following code.

function love.draw()
  love.graphics.print("Hello, LÖVE!", 400, 300)
end

This code defines the love.draw() callback. Inside it, we use a built-in LÖVE function, love.graphics.print(), to draw text on the screen. The first argument is the text to display, followed by the x and y coordinates where it should appear.

Save the file and run your project by dragging the folder onto the LÖVE application or using the love . command in your terminal. A window should appear with your text printed in the middle.

Now let's modify it to be a bit more dynamic. We'll set up a variable in love.load() and change it in love.update().

local textX
local textY

function love.load()
  textX = 400
  textY = 300
end

function love.update(dt)
  if love.keyboard.isDown('right') then
    textX = textX + 200 * dt
  elseif love.keyboard.isDown('left') then
    textX = textX - 200 * dt
  end
end

function love.draw()
  love.graphics.print("Hello, LÖVE!", textX, textY)
end

In this version, we first declare two variables, textX and textY, to hold the position of our text. In love.load(), we give them initial values. In love.update(), we check if the left or right arrow keys are being pressed using love.keyboard.isDown(). If they are, we change the value of textX. Finally, in love.draw(), we use these variables to position the text.

Run the project again. Now you can move the text left and right with the arrow keys. You've just created your first interactive LÖVE application.

Quiz Questions 1/5

What is the primary role of the love.load() function?

Quiz Questions 2/5

Which of these is a correct way to run a LÖVE project from the command line, assuming you are inside the project folder?

This is the basic foundation of every game you'll make in LÖVE. From here, you can explore drawing shapes, playing sounds, and handling more complex input.