Learn to Code via Tutorials on Repl.it!

← Back to all posts
Games in Love2D

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:

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:

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

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 love.graphics.getWidth() and love.graphics.getHeight(). Love is split into a few different "modules", if you want to call them that. love.graphics is one of these "modules" that we will be using in this tutorial.

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

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:

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:

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:

In love.draw(), we use love.graphics.setBackgroundColor() and the rgb function we created to set our background colour. We then use love.graphics.setColor() and rgb, once again, to set the colour of our rectangle. The rectangle is created using love.graphics.rectangle() 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!


Hi I'm watching a course where we are developing Pong game, and displaying of graphics is very slow...from some reasons love.graphics.rectangle('fill', 5, 20, 5, 20) is not displaying at all, while love.graphics.rectangle('fill', width / 2, height / 2, 5, 5) is displayed in the center of the screen...can someone help with this, please? Thanx

Solved, everything is on the screen, but it couldn't be visible because screen is cut for about 80-90 pixels from left and right.


I copied the code exactly, but the keyboard part wasn't working. I have had similar problems with Pygame. It just says "could not open device"


@AidanCashman do I need to install anything




https://repl.it/@jpmartel98/VastNavajowhiteServer i learn alot about lua and programming love2d just nead to lay back and think about it


This doesn't work :/


some of the feature a broken...... keyboardisdown dont seems to work...


@jpmartel98 sorry for the long wait.

did you use keyboardisdown or keyboard.isDown (capitalisation is importatant).


@LeonDoesCode got it working, i had to put in a update function! (am not 100% sure)


@LeonDoesCode https://repl.it/@jpmartel98/eee cluckky i know but it work lol


@jpmartel98 cool! You may want to fix the arrow keys being the wrong direction, but it's cool to see that it's coming along!


@LeonDoesCode hey just a follow up,here what am working on been a while https://repl.it/@jpmartel98/love


might add sonds to it there so much to do hahaah


Oh this one is awesome leon! I think many people would enjoy a tutorial about what the repl.it console is able todo!


@enigma_dev With the amount of languages available, anyone can create something awesome, even if they don't have the best knowledge.


Yes. Definetly. That is a reason why it is so innovative. @LeonDoesCode


@enigma_dev That's true!