2D Game Programming With Love2D – RPG – Learning Lua – Part 1

In this tutorial we will focus on the very basics of Lua and LOVE2D. You should have your programming environment up and ready to go at this point. Create a blank file, and while saving it be sure to name it main.lua , which will be the file that controls the bulk of your code.
I will keep most technical definitions and lingo out of these tutorials in order to maintain the simplistic nature of these tutorials.
You can always view the Lua and LOVE2D documentation to get a better understanding of what we learn.
Also, there are much more efficient ways to code a lot of things that we will make, but we will only use what we have learned in the tutorials to make
our game. Some aspects may get recoded as we build on what we learn.

Rather than programming in a console window we are going to start our coding in LOVE2D so we can get familiar with both lua and LOVE at the same time. The first thing we are going to do is simply print a message to the screen. Exciting!
Let’s type in the following code in your editor and save the file.

Print To The Screen

function love.load()
  -- Future Code Here
end

function love.draw()
  love.graphics.print("INVENTORY",350,50)
end

Run the code and you will get the word INVENTORY on a black screen. You did it!

First run of our game.

We have two functions here, love.load() and love.draw()
These are required in order to make LOVE2D be called into your lua code. Don’t be too concerned with not understanding what a function is, as I don’t want to overcomplicate the beginning stages of your coding experience.

The love.load() function begins on line ones and ends with the word “end” on line 3.
Everything between this code will run will LOVE2D executes the main.lua file. This code will run before the love.draw() function.
This is a great place to place some variables (our next topic) or other code that can be used throughout the file, such as a variable to hold how much Health we have, or how many Rose Petals we have. The love.load() function will only run through once at the moment the game loads.

The love.draw() function is where you place any code that actually executes drawing, or printing, to the screen.
In this case, we simply printed “INVENTORY” to the screen using love.graphics.print(text, x. y) where text was a string on characters (INVENTORY) surrounded by quotes, x is how many pixels to the right of the screen we want to start the text, and y is how many pixels down from the top of the screen.
Just remember, when referring to “x” think of left and right, or back and forth. For “y” think of up and down.

love.graphics Module

Modules are basically chunks of code that contain many functions that are pre-made for you. LOVE2D comes with several modules that do many things. Without these modules you would have to code everything from scratch, and trust me you don’t want to reinvent the wheel when it comes to coding.

love.graphics is a module responsible for the drawing of lines, shapes, text, Images and other drawable objects onto the screen. It can also load external files (including Images and Fonts) into memory, create particle systems, canvases, and manage screen geometry. All of which we will cover more later.

love.graphics contains many many functions we can use in our games that make game programming so much easier for you. In this first tutorial, we are going to take a look at a few of these functions for our little INVENTORY program.
For a full list of functions available in love.graphics click the following link: LOVE2D love.graphics module

Let’s introduce love.graphics.getWidth() and love.graphics.getHeight()

These two functions, a part of love.graphics, will get the width and height of the window of our program, or the resolution in other words.
How could this be useful in our INVENTORY game? What if we wanted to center the text (or an image) on to the screen? Let’s check it out.

First we will use love.graphics.getHeight()
Please be aware that Lua is case sensitive, so using getheight() is not the same as getHeight() and will cause errors in your code.
getHeight() gives us the number of pixels in the height of the screen.
So, if we want to center our text to the screen our awesome math skills tell us that getting the height of the screen and dividing it by two will give us halfway, or the middle. Let’s try it in code by replacing 50 with the getHeight() function and dividing it by two.

function love.load()
  -- Future Code Here
end

function love.draw()
  love.graphics.print("INVENTORY",350,love.graphics.getHeight()/2)
end

THIS SECTION STILL NOT COMPLETE..CHECK BACK SOON…

We are going to end the first tutorial at this point.
At the end of each section I will post a few exercises for you to try on your own.
I highly encourage you to do each of these to give yourself a chance to think through your code.
Copying and pasting code will not sink in the same way coming up with your own would.
I also highly recommend saving a backup copy of your code file if you’re heavily modifying it.
This way if you totally break your code, and you WILL break it a lot, you have a backup copy to restore
it back to a running state.
Exercises:

  • Make the screen display GAME OVER instead of INVENTORY
  • Place GAME OVER at “y” location 200 instead of 50 and run the program
  • Place GAME OVER at “x” location 0 instead of 350 and run the program
  • BONUS: Get GAME OVER closer to the exact middle of the screen “x” position