Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Lua 2d Game Code-along (or trying to teach/learn lua)  (Read 7890 times)

Warmist

  • Bay Watcher
  • Master of unfinished jobs
    • View Profile
Lua 2d Game Code-along (or trying to teach/learn lua)
« on: December 28, 2011, 02:28:17 pm »

So this is it. After gathering all my (virtual) social skills i bring you this: an opportunity to create a (yet another) game. But this time in lovely language called lua. Chapter 0 will be about lua itself and engine that we're going to use, other will be purely practical coding of a simple (yet somehow new i hope) game.

Specifics: I will be using Lua (see homepage or more specifically documentation about language itself) edited with notepad++ (see here or use any text editor other) and for the engine I will use lovely engine called LÍVE ( here)

All suggestions are welcome. Later in development I will probably ask for some assets (like sprites and sfx) if nobody will provide i'll just use what is freely available.

Chapters:

Download:
nothing yet.
Code repo:
Will be here
« Last Edit: January 08, 2012, 03:23:55 pm by Warmist »
Logged
My stuff (i'm working/thinking about):
Mini mod collection
pretty lights
Multiplayer

Warmist

  • Bay Watcher
  • Master of unfinished jobs
    • View Profile
Re: Lua 2d Game Code-along (or trying to teach/learn lua)
« Reply #1 on: December 28, 2011, 03:21:23 pm »

Chapter 0
If you know lua you can skip this (although if you know lua there is not much to learn here :) )

In lua there are  basic types of objects: strings (like "hello world" or 'hello worldz'), numbers (3 or 5,2), booleans or bools for short ( can only have true or false value), functions and tables. There are some more advance things but i hope that they will be easy to understand when we come to them.
Last two elements need some explaining. Functions are the most basic thing in almost all programming languages that do stuff. They can have arguments and results. It's like a normal (math) function e.g. sin(x)=y. Here x is argument y is result. In lua (like other programing languages) things need to be declared before used. In math that means that first you need to say f(x)=x*x only then you can use it to prove something or do other operations with f. So in lua:
declarations looks like this:
Code: [Select]
function dostuff(x)

end
using it sowhere else:
Code: [Select]
y=dostuff(4) -- do stuff with 4 and assign the value thats returned to y.
You will notice that there is '--' here. It means that lua should ignore everything after this. It's called a comment. For another example lets do something more usefull:
Code: [Select]
function square(x)
  return x*x -- multiply x by itself (thus squaring) and return it to be used later
end
functions can return one (or more separated with ',') value or not return any value at all. In that case there comes a special value called 'nil'. Setting something to nil means that that item does not exist or is not set.
Code: [Select]
favourite_number=5 --  :) five my favourite number
favourite_number=nil -- on the other hand i don't need a favourite number.

Other of the more advance things (and basic building blocks) is a table. Table is basically what think it is. Every thing in one side is attached to other thing. E.g.:
Code: [Select]
box={} -- this is to create an empty table called 'box'
box['thingy']='candy' -- set item in the box called 'thingy' to string 'candy'
y=box['thingy'] -- set y to what the 'thingy' is in the box
y=box.thingy -- same thing as a line before, this is more comfortable but limited in some ways
Also you can create manipulate tables in other ways:
Code: [Select]
box={} -- lets recreate the box (forgetting the old one)
table.insert(box,"candy") -- it adds "candy" to the box table like this- first it finds last numbererd item in the box and appends candy after it.
y=box[1] -- gets "candy"
here table.insert is one of many helpful functions supplied by lua to make our life easier. Also it can be replaced (only in this example) by this:
Code: [Select]
box[1]='candy'


I hope this explains some of the most basic stuff. And if you don't understand something please ask :)
« Last Edit: January 02, 2012, 11:33:10 am by Warmist »
Logged
My stuff (i'm working/thinking about):
Mini mod collection
pretty lights
Multiplayer

Warmist

  • Bay Watcher
  • Master of unfinished jobs
    • View Profile
Re: Lua 2d Game Code-along (or trying to teach/learn lua)
« Reply #2 on: January 02, 2012, 11:32:48 am »

Chapter 1
First for anything practical we will need to install LÍVE. This is very very easy: from here download latest version for your OS. Then install it.

Now LÍVE games can be run two ways: either from folder, or that folder is zipped and renamed (might need to turn on "show known file extensions" on windows) to have extension ".zip".

Let's get started coding. Create a new file called "main.lua" (again you might need to turn on "show known file extensions" on windows to rename main.txt to main.lua, or just save empty text file as "main.lua") in a dedicated directory (e.g. mine is called "IAII" - infinite adventures in infinity) and write this in that file:
Code: [Select]
function love.draw()
   love.graphics.print("Hello World", 10, 10)
end
Save it and zip it, then rename it to have the ending ".love". If everything is done correctly you should see "Hello world" in a black screen. To run games quicker (without reziping and renaming files) you can make a ".bat" file:
Code: [Select]
"C:\Program Files (x86)\LOVE\love.exe" %CD%
Just replace "C:\Program Files (x86)\LOVE\love.exe" with the path to where you installed LOVE.

Here I'll explain what does the code do in more detail:
Spoiler (click to show/hide)
Logged
My stuff (i'm working/thinking about):
Mini mod collection
pretty lights
Multiplayer

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Lua 2d Game Code-along (or trying to teach/learn lua)
« Reply #3 on: January 03, 2012, 08:13:13 pm »

Following because "Learning Lua" is on my ToDo list for 2012, as well.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

darius

  • Bay Watcher
  • ^^
    • View Profile
Re: Lua 2d Game Code-along (or trying to teach/learn lua)
« Reply #4 on: January 08, 2012, 03:17:50 pm »

Ignore this.
Logged