Löve Tutorial: Hello World!

Chapter #0: Why Love2D?

Love2D is since lately my framework of preference to code with, it’s fast, lightweight, and works very well. I could recommend any starting game developer to start with this framework, it’s very easy to get, cross platform, and not to mention the community is great! You can take a look at the forums, the wiki is very well written and is basically everything you need once you get the hang of it. There are many tutorials on the website, and if all else fails, there is a lövely group of people on their IRC channel (#Love on the oftc server) which will be able to answer any question.

Chapter #1: Installing the Love player.

If you have installed the love player already, you can skip this part, and go ahead and read about setting up an IDE. If you have not, lets go to the frontpage of Love2D and download the latest player meant for your OS, for now, Love2D player is available for Linux, OS X and Windows. If you run Linux, the best thing to do is to set up the PPA, so that you won’t have to worry about updates. For Windows, you can easily download the installer from the frontpage of Love2D (this tutorial is written while the latest version was 0.8.0, so to be safe, just download that one). Installing would go like you are used to with any application.

If you are still on the website, download the 0.8.0 demo as well, at the bottom of the download section. This will enable us to see if everything went accordingly. Inside the demo archive (unzip it), you will see a bunch of .love files, you can double click on these, and the love player will start playing the demo. If you see a piggy instead, something went wrong. Since the piggy is default screen, which means there is no game loaded.

Chapter #2: Choosing an environment to code in.

Since the love player has a debugger built in, you might want to choose for a lightweight IDE without all the extra features you aren’t going to need anyway. When I was still a Windows user I found that Notepad++ did the trick for me, since the most important part in coding for me was syntax highlighting. There are many alternatives to this. I use Geany, it’s comparable to Notepad++ in many ways, perhaps a trimmed down version. Geany works very well on Linux. Another alternative that is both cross platform and lightweight is Sublimetext. Why I prefer these environments is because there is nothing to setting it up, just install, and run. It’s as easy as that!

Chapter #3: Hello World!

Go ahead and make a folder for you game, call it for instance “Tutorial 1″, but it could be called anything, really. There are many ways to do this, but I will just cover the way that I find the quickest. The “Tutorial 1″ folder will be the folder where all the magic happens. Create an empty file in the folder called “main.lua”. This file is what the love player will search for, and it will run the code in that file. Everytime you start a Love2D project, you will find yourself writing these 6 lines first, so go ahead, open main.lua in your coding environment of your choice, and set the syntax highlighting (if the software hasn’t already) to Lua. Now, write these lines:

function love.load()
 
end
 
function love.update(dt)
 
end
 
function love.draw()
 
end

Now, these are the three most important functions there are for making a game in Love2D. The load() function, will just run once, it’s to load assets in to memory, so you can access them while your application is running. Update() will have your game mechanics. It will for instance let a sprite move by updating it’s position, or calculate damage after the player collides with an enemy. What dt is for, we will discuss later.

The draw function is probably the most important aspect of a game, the graphics! It’s the loop that draws things on the screen. For this we need love.graphics, lets go ahead and add the next line inside that function

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

There you go, you have made a Hello World application!

Complete source:

function love.load()
 
end
 
function love.update(dt)
 
end
 
function love.draw()
  love.graphics.print("Hello World!",400,300)
end

Sidenote: Although you don’t need the update and load function in this example, I think it’s convenient to have covered the purpose of them, since in the next tutorial we will be needing both of them.

Chapter #4: Running your code.

There are multiple ways of running your code, in Unix based systems you can type “./love” and it will run that folder in Love. The easiest way on Windows is to archive the contents of the folder to a .zip file (so that when you open the .zip file, main.lua is visible), and rename the extension from “.zip” to “.love”. You can run this love application now by double clicking on the .love file. Another approach is to have a shortcut handy to the love player, and just drag and drop the folder with main.lua in it on the shortcut.

Chapter #5: Profit!

No, not really, there is a long way to go before you are ready to publish your games for commercial purposes, but these are the very basics!

Cheers,
~VividReality.

Tags: