Games in Love2D
LeonDoesCode (382)

More Games!

That's right! We are going to be looking at making even more games! I'd say that I prefer Love2D over any Python Game Framework any day. While I do love Python, I would prefer to use a language like Lua to create a game.

So what is Love2D anyway?


I'm glad you asked! Love2D is a Game Framework written for Lua. Since Lua is a very "easy going" language, it can be very fun to create games with. That's why I thought that it would be the best place to move onto from Python. If you want to know about Lua, click here, or for Love2D, click here.

Let's begin!

Let's Do This!

First of all, I'd like to explain three very important functions that Love uses. These are love.load(), love.update() and love.draw(). The names are pretty self explanitory, but just in case:

  • love.load() is called when the game starts.
  • love.update() is called every game update.
  • and love.draw() is called every game draw.

That should clear things up if you couldn't guess what each one does.

We will be creating a similar example to the one found in my Games in Python tutorial. Now, on to the code!

Firstly, we are going to create a player table, like so:

-- Create our player
player = {}

This is going to be where all our player variables are stored later on, when we define them in love.load(). Speaking of the devil:

-- When the game starts
function love.load()

First of all, we create our love.load() function, and end it with an end statement. Let's put some code in it:

  -- Give the player a speed
  player.speed = 32

  -- Give our player a width and height
  player.width = 32
  player.height = 32

  -- Make our player's x and y the top left
  player.x = ( / 2) - (player.width / 2)
  player.y = ( / 2) - (player.height / 2)

We give our player a speed of 32, which will be used later to help with movement. Next we give it a width and height of 32. Lastly, we give our player its x and y in the middle of the screen by using and Love is split into a few different "modules", if you want to call them that. is one of these "modules" that we will be using in this tutorial.

Now, let's move onto love.update():

-- When the game updates
function love.update(dt)

love.update() has a parameter called dt. dt stands for delta time, which we will be using to make sure that the player moves at the same speed at any frame rate. This is useful when creating a game, as you don't want someone who is running at 60 fps to move faster than someone at 30 fps. If you think about this in terms of speed-running, you can see how this begins to affect the game play.

Talking about using dt, we will be using it in out movement system, which you can see here:

  -- Setup player movement
  if love.keyboard.isDown("w") then
    -- Change players cooridnates using speed and dt (deltatime)
    player.y = player.y - player.speed * dt
  -- Setup up the rest
  if love.keyboard.isDown("s") then
    player.y = player.y + player.speed * dt
  if love.keyboard.isDown("a") then
    player.x = player.x - player.speed * dt
  if love.keyboard.isDown("d") then
    player.x = player.x + player.speed * dt

As you can see, we add the player's speed to either x or y depending on the key down. We find the key down using love.keyboard.isDown(), love.keyboard being another one of those "modules". We multiply the speed by dt to ensure that the amount of speed added/subtracted from x or y is the same at any frame rate.

Before we move on, I want to mention a function that I create whenever using Love. This is the rgb function, which look like this:

-- Makes RGB colors easier to write
function rgb(val)
  -- Get our rbg value (0-255) and divide it by 255 to normalize the value
  return val / 255

The reason I do this, is because, as of recent, Love takes in RGB values as a number between 0 and 1. By using this function, you can input a value from 0 to 255, and it will return a value from 0 to 1. We do this by normalising it. To normalise this value, we divide it by 255 to get the number between 0 and 1. This will make it much easier to choose the colour we are looking for in love.draw().

Again, we make love.draw() like the others:

-- When the game renders
function love.draw()
  -- Set background color, rgb(50), rgb(50))
  -- Draw our player, rgb(210), rgb(210))"fill", player.x, player.y, player.width, player.height)

In love.draw(), we use and the rgb function we created to set our background colour. We then use and rgb, once again, to set the colour of our rectangle. The rectangle is created using which takes in the parameters mode, x, y, width and height. We set the mode to fill (you can also use line for just the outline), then x, y, width and height all come from the player variables.

You should now be able to move your player around the screen using w, a, s, and d.


As you can see, it is super simple to create a game using Love2D, even simpler than using Pygame in Python! The awesome thing is that you can even release the game for Windows, Linux and Mac using this guide, which is also much more easy than in Python. I hope you get the chance to make some awesome games with Love2D. Have a great day!

If you have any suggestions for tutorials, leave them in the comments and I'll be sure to have a look. If you like one in the comments, then give it an up vote to show that you want to see it. It makes my life so much more easier. Thanks in advance!

You are viewing a single comment. View All