Games in Love2D
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 Do This!
First of all, I'd like to explain three very important functions that Love uses. These are
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.
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
32. Lastly, we give our player its
y in the middle of the screen by using
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() has a parameter called
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
speed to either
y depending on the key down. We find the key down using
love.keyboard being another one of those "modules". We multiply the speed by
dt to ensure that the amount of speed added/subtracted from
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
1. By using this function, you can input a value from
255, and it will return a value from
1. We do this by normalising it. To normalise this value, we divide it by
255 to get the number between
1. This will make it much easier to choose the colour we are looking for in
Again, we make
love.draw() like the others:
love.draw(), we use
love.graphics.setBackgroundColor() and the
rgb function we created to set our background colour. We then use
rgb, once again, to set the colour of our rectangle. The rectangle is created using
love.graphics.rectangle() which takes in the parameters
height. We set the
fill (you can also use
line for just the outline), then
height all come from the
You should now be able to move your player around the screen using
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"
Oh this one is awesome leon! I think many people would enjoy a tutorial about what the repl.it console is able todo!