Need help with understanding the basics of love2d

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
kodachrome91
Prole
Posts: 2
Joined: Sun Feb 04, 2018 7:49 pm

Need help with understanding the basics of love2d

Post by kodachrome91 »

hello!

I am completely new to the forum and video game programing as a whole. A friend of mine suggested that I look into Love2d for my first engine. I've spent hours trying to consume all the information I can on how to get started, but its turning out to be extremely overwhelming. I think my first problem is not having enough understanding on how video game programming works. I have yet to find anything that explains video game development in a simple way that I'm able to grasp. For instance, is there super simplified way that someone could explain what all the moving parts of this whole process are?

Also I'm running Linux Mint. Almost all the tutorials I find are for either Mac or Windows. So thats been a hurdle. If someone would have any good links to reading material or how to videos it would be greatly appreciated.

Another thing that I'm trying to get is why Love2d? What makes someone choose this over the several other engines out there? I'm not knocking love or anything but I'm curious as to what makes some engines more appealing than others. I would assume that the first thing is that its based around making 2d games obviously. I'm sure there are other options out there, but what do you guys hold valuable when choosing an engine.

I'm sorry in advance if I come off like I'm asking everything short of asking somebody to make my game for me. Trying to understand how to ask the right questions has been the biggest struggle on getting information on the internet.

Thanks! Seth
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Need help with understanding the basics of love2d

Post by ivan »

Hello there.
Your questions are too broad to answer meaningfully.
For instance, is there super simplified way that someone could explain what all the moving parts of this whole process are?
Basically, you write your game in Lua then you run it to check for errors.
Most examples/tutorial code should work across all platforms - that's one of the advantages behind Love2D.
If you are completely new to programming, I suggest starting out with non-visual stuff so you can learn the Lua language.
Try to print some stuff to the console and learn how Lua tables work.
Another thing that I'm trying to get is why Love2d?
Very easy to write and test your code, yet powerful enough for making commercial 2D games.
Probably faster and more lightweight than JavaScript.
Cross platform support is a plus too.
I like it because it's based around Lua.

Good luck!
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Need help with understanding the basics of love2d

Post by pgimeno »

Hello, welcome to Löve and welcome to the forums. Your friend's advice is quite sound. Most people who try Löve are attracted by its simplicity. Yes, it may look daunting for someone that has never touched such a thing, but one step at a time, it is simpler than it seems at first.

Why Löve? It focuses on making things simple for the user, without sacrificing too much power, or better put, finding the best balance between power and simplicity. It is well maintained, novice-friendly, and the community is quite helpful.

You don't mention whether you're new to programming at all. If so, well, you have a bigger road in front of you, but if you have some programming background already, it will certainly help.

You need to learn Lua, which is the language that Löve uses. A good resource for learning is "Programming in Lua", whose first edition is available online, here: https://www.lua.org/pil/contents.html - it covers Lua 5.0 but there are not many differences with 5.1, which is the one that Löve uses.

You need to know how Löve is invoked and some of the very basic principles (what files to create, where to put them, how to call them). This will help: http://love2d.org/wiki/Getting_Started . Once you have these basics, and at least an initial grasp of how to do stuff in Lua, you can try to make your first Löve program that does more than printing "Hello, world!".

Löve programs make use of callbacks. Callbacks are Lua functions that you define, and that the engine will call when something happens. For example, Löve will always call a function love.load() before any others, if you have defined it. It will also call two callback functions in turn, love.update() and love.draw(), every time your monitor refreshes the screen (you know that happens typically 60 times a second). Typically, you write your program so that it draws everything your game needs every monitor frame. Löve clears the screen every time right before calling love.draw(), therefore if you fail to draw anything in there, you will get a blank screen, even if you drew something earlier.

How is a game programmed? Well, you have lots to learn ahead. It of course depends on how complex you want your game to be. To get started with the simplest thing I can think of, let me walk you through the process of making a sprite move with the keyboard.

You would first write a love.load() function that loads an image file with the graphic you want to move, into a variable. The position of your sprite can change, so you need variables to track it, and love.load is a good place to initialize them.

Then you would write a love.update() function that reads the keys and changes the coordinates of the sprite according to the pressed key.

Finally, you would write a love.draw() function that draws the sprite to the screen in the given coordinates.

I don't want to overwhelm you with details, so to keep things simple, everything will be done with global variables, and the movement speed will depend on the refresh rate of your monitor. It's a very good practice to keep everything in local variables unless strictly necessary, and it's typically necessary to make use of the delta time for making things move at a constant rate rather than depending on the monitor refresh rate, so you may want to look into these things next.

Note that every function and callback is documented in the wiki, so you are advised to look them up if you have any trouble understanding. For example, the love.draw callback or the love.graphics.newImage system function. You may want to take at least a cursory look at the whole love.graphics page to see what graphics functions are available.

Here's the program. You need an image called 'sprite.png' in the same file as 'main.lua'. This is what to write in main.lua:

Code: Select all

-- The sentences prefixed by two dashes like this one are comments.
function love.load()
  sprite = love.graphics.newImage("sprite.png")
  x = love.graphics.getWidth()/2
  y = love.graphics.getHeight()/2
  speed = 5    -- Note that this is the speed in pixels per frame.
end

function love.update(dt)
  if love.keyboard.isDown("up") then
    -- The up key was pressed, so we move up
    -- (the y coordinate grows downwards so we have to decrease it)
    y = y - speed
  end
  -- Do similarly for the other keys.
  if love.keyboard.isDown("down") then
    y = y + speed
  end
  if love.keyboard.isDown("left") then
    x = x - speed
  end
  if love.keyboard.isDown("right") then
    x = x + speed
  end
end

function love.draw()
  -- Draw the sprite at the required position.
  love.graphics.draw(sprite, x, y)
end
Now, what if you want to draw a background? You first draw the background, then the sprite. What if you want the sprite to be animated? In that case you need several images, and a variable that controls which one to show, so that the program keeps switching between them as adequate. And so on.

Hope this addresses at least a part of your questions.
kodachrome91
Prole
Posts: 2
Joined: Sun Feb 04, 2018 7:49 pm

Re: Need help with understanding the basics of love2d

Post by kodachrome91 »

Hey thanks a million! Sorry for the broad questions, but yea I haven't ever programmed anything before. I don't really have anymore questions now with all the things you talked about. Just have alot of homework to do. I really hope I haven't bit off more than I can chew haha. Finding a starting point is the hardest part I suppose.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Need help with understanding the basics of love2d

Post by ivan »

Like pgimeno mentioned, the documentation is very important:
Love2D: https://love2d.org/wiki/Main_Page
Lua: https://www.lua.org/pil/contents.html
Be prepared to be reading/looking at the "docs" most of the time.
Just note the difference between "pure" Lua and Love2D.
Once you figure out how Lua works, you'll be able to understand other languages too!
Basic "hello world" example using the console:

Code: Select all

-- count from 1 to 10
for i = 1, 10 do
  print(i)
end
-- say hi
print("hello world")
-- add two numbers together
print(1+2)
This is all pure Lua - if your code doesn't use "love." you can run it without Love2D.
For example, you can test your pure Lua code here:
https://www.lua.org/cgi-bin/demo
This is a good place to learn the common error messages that Lua spits back when you make a mistake.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests