So I admit it, I always wanted to create a game, but I have been too easily scared by the amount of work that seemed to be required to delve into something like Unity. I did some easy attempts at HTML canvas based Javascript games, but nothing impressive enough. Although if you are so inclined, do check the great PhysicsJS library and you might end up creating something cool.
Then my quest had a clear goal: Be able to make a cool game with the most minimal amount of effort possible. That's where Lua and Löve come in.
Lua is a very lightweight language which is used often to be embedded and extend other programs, but it's also absolutely standalone in its own right. In the scenario that we are seeing today, it will be complemented by the Löve 2D engine for a perfect combination of easy to learn and great results.
Note: This post is intended as a super lightweight introduction to create games this way, so you won't end up with a complete example, but you will learn about some basic techniques that can get you started.
Requirements:
- Lua installed in your system. I am using macOS, so I can skip this step because it already comes installed by default. If you are using Windows, you probably want this
- Löve installed in your system. Download it from their site picking the package that fits your operative system.
Get something up and running
Create files:
There is not a very strict requirement on what your folder structure should look like. Let's create a folder called love-tutorial
and inside, a file called main.lua
If you open your terminal in that folder, in mac you can do:
$ open -n -a love .
Which means "run love
with all contents of the current folder. Since you need to give Löve a folder rather than a single file.
The code
Roughly speaking, Löve is based on three big callback functions. There are many more, but the following three are crucial:
- LOAD
This function is called at the start and it's supposed to load all your data and variables. For instance, you could initialize a table (similar to what would be a Python dict or a JS object) with the data about a character:
function love.load()
hero = {}
hero.name = "Ruben"
hero.health = 100
hero.alive = true
score = 0
end
- UPDATE
This function is running repeatedly once and again during the duration of your session. dt
is the time passed since the last run. You can use this to update your data.
function love.update(dt)
if hero.health < 1 then
hero.alive = false
end
if enemy.dead then
score = score + 1
end
end
- DRAW
Like update
, this function is also called continuously, but rather than calculate the values, its mission is to draw on screen all the game elements. For instance:
function love.draw()
love.graphics.print(score, 400, 300)
end
So you can see how the value being printed score
would constantly be redrawn, but since we are also updating the value of it in the update
function, on every redraw we would get the most updated value.
Beyond the super basics
Ok, you have seen the principle of how a Love game works, but this is not even the beginning of the beginning. There is a lot of ground to cover. For instance, movements:
if love.keyboard.isDown("right") then
hero.x = hero.x + hero.speed * dt
end
Or actually drawing elements:
love.graphics.rectangle("line", 400, 300, 100, 100)
Which could become an image with a small change:
love.graphics.draw("path to img", 400, 300, 100, 100)
Conclusion
Rather than a game making tutorial, this article was geared towards encouraging you to explore Lua and Love, since the learning curve is relatively small (compared to other alternatives) and can get you enjoying your own games in very little time.
Top comments (2)
Also, check out PICO-8 and TIC-80. The latter is a free/open-source spin on PICO-8.
Both are "fantasy consoles" (virtual machines) that come with a code editor, sprite editor, map editor, and SFX mixer built in, all with retro fonts and graphics :)
Unity's great, too.
I'm really liking Lua as a language, though! Aside from the fact that everything's a global unless you force it to be a local variable.
Nice! Thanks for the tip about PICO-8 and TIC-80, I didn't know them, and they look super interesting!