Understanding Love2D
Today we will talk about love… I mean, Love2D - the engine for 2D games. Basically, I really love the game engine because it’s fast, simple and will let you make your first game in just a couple of hours! The process of a game creation looks like just programming on Lua, without any heavy overloaded IDE or special tools. Little by little,we will create a prototype for a 2D RPG with point-and-click controls right now! It will even have an animated character and we will do all of this in less than 100 strings of the code on Lua!
What additionally we will need for the project:
- Graphic map file. As a map we will use just a static image to show it as a background, nothing else. It can be any PNG or JPEG, in the code example I’m using the file “map.png”.
- The character sprite sheet. It’s just an image with frames for animate our character, 4 rows, for each direction of the moving and 3 cols, 3 frames per animation. It should have a transparent background, the frame size in our code example is 32x32 pixels. This is an example of a sprite sheet how we need.
Love2D has VERY good documentation! So please read the very basic manual to skip the part about installation and project running in this article…
So you have these two graphic files? Let’s begin to code. First things first, let’s make variables and constants what we will use in the code.
We just initialized our engine which will process the graphics, made arrays for store dynamic parameters (such as character coordinates) and made some constants for store configuration of our game. Next we will make a function to create animation quads from a spritesheet.
Next we will load our graphics, such as map and the player sprites. As well we will place our character to the middle of the screen at the beginning.
Next it will not be so hard, we just need to make some logic to make the character move. It will be a bit of mathematics here, but first you can just copy this to your file, then you will understand it.
Ok, it will move. Now we need a function that will get the coordinates of the point where we have clicked and where the character has to go.
We are almost done! Now just drawing everything to show it.
As I said at the beginning, you will find all the information about how to run the game depends on your operating system. So please check it if you still don’t. When you launch the game project, you will see a character walking to the clicked point with correct animation. For make additional functions in your new game, again, read the documentation.
If you are interesting in this game engine, I highly recommend you to watch this 11-hours long course at the YouTube from freeCodeCamp. These guys have tons of high quality courses about programming at different languages and related things.