Transcript for:
Game Development Course with Lua and Love2D

in this complete game development course you will learn how to build a variety of games using lua and love2d first you will learn the basics of the lua programming language and then you will learn how to use lua to create games steve teaches this course he is an experienced game developer and teacher leave a comment with the type of game you plan on making welcome to your lua crash course so this course will contain everything about lua we'll learn lua and then later on we'll learn a little bit of love 3d and then we'll build a few games with lua but let's not get ahead of ourselves what exactly is lua lua is a high-level procedural programming language designed in 1993 primarily to be embedded into other applications if most of this flew over your head it's perfectly fine most things you'll have to take in here is that lua is a procedural programming language meaning that things like object orientation doesn't really exist in there but you can simulate it these days it is mainly used in games so you'll usually see it being used with love 2d or roblox or it will be used with games built in c or c plus plus as a little bit of extra used on the side now where exactly was lua used well it was used in adobe photoshop lightroom apache the http server the awesome window manager roblox angry birds the sims 2 mafia 2 world of warcraft and fable free as you can see a lot of these are games because lua is very popular to be used with games because of how easy it is to embed lua in your game well what text editor should you use if you want to code along well it can be absolutely anything from a notepad all the way down to intellij idea i will be using vs code during this whole course mainly because that is what i've grown to love and learn but if you have no experience in any of these i would recommend using notepad but if you want something a bit more powerful then definitely go sublime text so just go to the lua website lua.org then here you have a little download option click on that now you want to just ignore all of this because compiling on windows grows who wants to do that and this is a linux only thing if i'm correct so we can actually just go to here if you don't have the time or inclination to compile lugo yourself like any normal windows user get a binary or try the live demo we want to get the binary is just basically the executable now don't be fooled by all of this you don't download it from here you go to this download page right here then make it a massive pain to actually get to what you want it's very interesting then here you have a bunch of options to choose from i would like to go with 5.3.6 because 5.4.2 has a lot of changes and stuff that just sucks so here we have a bunch of things we can choose from this is linux this is windows 32-bit windows 64-bit if you're on windows you probably have 64-bit and take note this will also work on windows 11. just because i'm using windows 10 doesn't mean it won't work on windows 11. and then we have mac which guys seem to not be able to download and then we have more linux or that's a source this is linux now we want the 64-bit zip file so we click that it will start downloading our files you just say save file and i believe it's done we can now open up our file manager and we can go to downloads and you see we have a lua 5.3.6 we can right click and extract file if you don't have this option i recommend you go install winrar or some sort of file extracting software i like using winrar because it just works i'm going to extract to lua 5.3.6 but if you just said extract here it would have been perfectly fine now in here we have a bunch of options there are a bunch of options this is just executables this is the most important file here this allows us to actually run or leave our code which we'll do in a second now what we need to do is we can go back here and we can rename this you can name it to anything you want but i'll just call it lua because it's simple you know that's liver now i'm going to copy this so you can right click and we can say copy and i'm going to go to my local c drive now you can just paste it in here if you want but i do prefer going to program files right there and then just saying paste because it's a program so we want to paste it here there we go now next thing we can do is we can open up the command prompt so you can click here search command prompt and it actually want to go to this page anyways once you search command prompt you should see this now first try type lua don't get anything as perfectly fine try to type blue five free doesn't work okay if you do get this then that means it has not yet been added to path one way to fix it is by rebooting another way to fix this is by going to settings which can i can i search here i can okay go into settings and it's searching here environment variables so here edit the system environment variables and in here you can just say environment variables down here then you don't have to worry about this too much you can go down here to path you can say edit and these are the locations where all the environment variables is as you can see my program files x86 is here but not my normal program files or at least some of these 8x86s are here so i'm going to do this i'm going to say new and i'm going to browse because the thing is we it's a very difficult way to get there so i'm going to just browse to where it is and here program files we saved it in there and there's lua now if we say okay as you can see it added it here for us so it added it to the program files or the environment variables now if we were to say okay i just need to put the folder there because it's going to scan the folder you don't have to worry about the executables you say okay you say okay and you close that then now also if you do want to you could always rename this to lua and just remove the five free i don't know if that will still work but i'm pretty sure it will you then have to exit out of the command prompt and just open it up again okay please open here and to look like a hacker you can say color o2 cool now that we look like a hacker we can clear the terminal or cls and we can say lua five free this will allow you to go lua if you see this you did it correctly also if you didn't download lua 5.3 you will have a different number here depending on the version you downloaded it and that just executes the program now we can say print test to make sure this works if it works then you're good to go next up you probably want to actually try and run a file in lua or a lua file itself i'm going to create a new folder i'm going to call it lua for my lower programs here i'm going to say new text document and make sure you can see this dot txt there we want to change that we're going to say main.lua and remove.txt if it was there and just say yes and you should see this you can then edit it with notepad if they give you the option to do it so let's open it with and let's see here more apps notepad there we go and yeah we didn't need to always open up with that then here we can of course just say print i am cool save this file by clicking file and save or just control this and you can close notepad and now here you can just shift right click and you can say open powershell window here there's multiple ways to do this but this is usually the easiest then you'll see this you can then just say lua fi free and in the file so then right there main.lua we can then say main.lua there you go i am cool if however this did not work then you can just open up your command prompt again bring it over here whoa and now we need to navigate to this folder one way you can do this you can just right click here and copy address now you can just say cd for change directory and right click if you right click then it will paste it here press enter and now you're inside of this folder now you can say lua five free and main.lua there you go i am cool first you open up vs code this is a vs code specific tutorials users vs code if you don't have it installed you do need to go and download it okay then once you have your visual studio code open you want to go to file and an open folder not open file but open folder once you say that you can go to your folder where your lua program is in so that main.lua file or whatever you call that file and here we go i have a main.lua file which just says print i am cool that's all it does it's just this file right here and i can show you that this file exists by going to my document and to that folder i just opened it there we go main.lua cool now that you have this here you can just go to terminal new terminal and here we have a terminal if you have lua already installed you can just say lua five free and then main.lua i am cool as easy as that you have just executed your lua code i'm going to open up my file manager and i'm going to go to documents i have a special folder called trash where i put all of the things that once i format my pc i don't care about it you will probably have a code folder similar to what i have here where i put all of my other code but for me i'm going to put it in trash i'm going to create a new folder and call it lua in here i want to create a new file called main dot lua if you're in windows make sure that this actually ends in main.lua and not main.lua.txt windows will oftentimes append that extension to the file name which you do not want once you have done this you can open up your code editor in this folder since i use vs code i'll quickly open it up here here we go now everything you see here is what will appear here so this main.lua that is here if this right here says main.lua.txt then windows did you win and they try to add a txt extension at the end you can just go here and then rename it and remove that.txt i'm going to close this file manager here because i don't need it open anymore all right now first off i would like to resize everything just so everyone can see great to run my lua code i will use this terminal right here but take note that this terminal is the exact same terminal as this terminal right here the two are exactly the same the only difference is this terminal is inside of vs code while this terminal is not inside of vs code that's the only difference they both run the same thing if you're on windows or mac you don't have to open up a terminal on the side you use a good terminal new terminal and you'll have a terminal to use all right now that we have that out of the way i'm just going to get my setup ready and here we go and this is why i like having this terminal here it just looks so much neater and we have a lot more space to work with next up i'm going to close my file manager because i don't need to see this lua file because i'm already in it so let's start with the basics make sure you have low install by going lua version and actually if you get this then you know lua is installed but if you go lua v then you should also see this if you just type normal lua then you'll be able to do something like print any type of text and you should see this to exit you can press ctrl and d ctrl n z or control and c alright so i have lu installed as you could see there so let's get started first i would like to tell you about comments in lua oftentimes if you're following along you want to leave yourself a little note to explain what is going on and this can be a comment a comment is not read by the computer so when you run this lua code a comment will be skipped so we go dash dash and anything that starts with two dashes is considered a comment so this is a comment this is also a comment you're not limited to the amount of comments you want or have you can just do as many as you want but that is also a single line comment it basically means that once i go to the next line i'm typing regular code you'll notice that here we have green text and here the text is not green that is because this text right here is not a comment so this is a single line comment every time you want to add one you'll have to add the two dashes to say it's the next line you also have a multi-line comment if you plan on having a comment that's a few lines long such as a description of something then you can say dash dash and in two brackets like this this right here is a multi-line comment and then make sure there's no space between your dashes and your brackets here there we go now this can range multiple lines so multi line comment it is amazing because now everything that's between these is part of the comment meaning if you put any code in here it will not execute same here if i put any code in here it will not execute this one is just one line whilst this one can span multiple lines all right next up let me actually show you how to print something to the screen to print something to the screen it's actually simple you just use the print command and i will print something to the screen hello world so print and then in brackets we have quotation marks and then a piece of text we want to print to the screen the quotation marks and the brackets are both important here so do not leave either of those out if you save your file and you run your file with lua main.lua and take note i'm in the same folder as this file i am in this folder if i say terminal new terminal you'll notice it's in document trash lua and that is where i currently am i know there's a lua file in here because if i say dir i can see a main.lua all right if you say lua and in your file name you'll get hello world because that's what we're printing out here you can have as many print statements as you want yet again you're not limited to anything so let's try hello jeff and here we'll now have a hello jeff and here's an example of where you could add comments here we could say print to the screen and here we can say we'll print hello world you'll notice my single line comment doesn't have to be on its own line it can be after a command for example print so here we're saying print and then we're putting a comment after it this comment will not be ready if we were to remove those the comment will be read and we will get an error such as this as you can see here it doesn't know what that is so if we make this then it can continue take note that comments will also stop code from executing so if i were to make this code a comment it will not be read so when we run this it's just going to say hello world and no longer hello jeff because hello jeff is now a comment to split your print up into multiple words or to add multiple things to your print statement we can add a comma i am cool this comma here will add these two together however take note that this comma will add a tab between the two so when we run this we'll get hello world and in a massive tab i am cool if you want to add these two together without getting a tab then you could use two dots and these two dots means concatenate concatenate just means add two pieces of text together if we save this we get one long piece of text if we add a space here then there will be a space between the two pieces of text what exactly is a data type well a data type is very easy to understand for example when i say 5 what is 5 5 is a number or an integer this is a data type five is the data type of number and that is how data types works they're very simple so we have nil nil means nothing empty null undefined so if something is nil it doesn't exist or it's empty we didn't have a number which would be something like 1 2 negative 99 9.87 these are all numbers we didn't have string and string is just a fancy word for text so anything in double quotes or single quotes these are all text we didn't have boolean this is yes or no also known as true or false so true or false if something is true you can think of it as yes if something is false you can think of it as no so when someone says is it true that you were five well that would be yes because it is true that you were five or if when you make a statement and it can be out of true or false i have been to jail that is false i have never been to jail so nope that is not true and then you have tables tables is the equivalence of arrays in lua or lists if you're coming from python tables are out of the scope of this video so we'll be leaving them for a different video on their own okay so those are data types they're the basic ones you'll mostly be working with if you're using lua now let's get an example of a variable a variable is something that can contain something else it's giving a name to something so you can reuse it later for example local name this is how you create a variable by specifying local and in the variable name this can be anything from x to name to example like that one way i like to teach is by using a little bit of math for example we have x now of course if we were to print x here and this will just display what is inside of this variable we'll get nil we can also set x equal to nil and this will be nil you also set x on a different line than when you created it so here's x and we can do that that also works we'll still get an elbow so let's say x is three now if you remember the basics of algebra you can usually say something like x plus eight is equal to eleven now you remember maybe in high school where you had to convert this to figure out what x is but in this scenario we are actually getting x we know what x is we are providing x so we know we'll get 11 here so we run this we get 11. so x here is just a placeholder for this number three here so we do this we'll get the same output but this time we're just storing it in x because now we can reuse it wherever we want so we run it we'll get 11. if we change x here to let's say 8 then x will be changed everywhere to that eight and we'll get to showing you more examples in a second but you do get two types of variables you get the local scope variable which means it is not allowed to be used outside of this file and when we get to it outside functions that is a local variable it's not allowed to be used outside of its scope so in this scenario it's scoped to this file if we create another file we cannot use this variable from that file but then you get your global scope variable which could be something like global variable and this could be 10. this global variable as you can see it does not start with local it is because it is globally allowed to be used so this global variable is allowed to be used outside of this file so if we create another lua file and we try to use this variable which we'll get to in a future tutorial we are able to use this variable however it is definitely good practice to go underscore g dot whenever you create a global variable just this this is just to specify this is a global variable it's not necessary to have this it is completely optional but it is we're kind of recommended to have an underscore g just to give that extra specification we're talking about a global scope variable now a lot of this is flying over your head don't get too concerned we'll be practicing this a bit in future tutorials as well now let's look a little bit more at variables just variables in general we can have a floating point variable and a floating point variable is something like 3.1415 what not we can have an integer type variable both of these are considered numbers and this would be a free we can have strings or single or double quotes and in any piece of text this can include these dashes that are usually used for comments and we can also do boolean so true or false booleans are a little bit out of the scope of this video so we'll definitely get back to them in the future as for strings as i said you can either do single quotes or you can do double quotes but you can also do two brackets and this will allow for multi-line strings take note these strings are taken up very literal so if we were to print f here then it will get a very literal representation of what we had so if you don't want it to have a tab and a new line you might want to just do this and now you'll notice if we were to run this it will look completely different and last thing i want to show you about variables before we move on to an example of them is how you can assign multiple variables at a time for example let's go local one two three all right so we have three variables here separated by commas now we can just go one two three so now one is assigned to this one here two is assigned to this two here and three is assigned to this false here so they're following the same pattern and is how we can assign multiple variables at the same time now let's use an example where we actually use a variable for example let's go here and say local name is equal to steve then print hello my name is and remember to concatenate we can use two dots and then whatever we want to add in this case we can say name we can say name because name is a placeholder for this steve here so saying this is exactly the same as saying this let's go here i have a cool name and in steve and here we have a little bit of an example if we were to run this we'll get hello my name is steve which is here specified as name then here i have a cool name comma steve i have a cool name comma and then the name variable if you wanted to you could do this well this is perfectly fine as well it will give you the same results but think of it this way let's say this was a piece of text that was probably like a hundred thousand lines of code right and now you want to change the name from steve to mike now you have to go mic and you'll have to change it on multiple places so first mic and then you have to go mike and you have to do this a hundred thousand times immediately you can see where the benefit of using a variable would be better because if we use a variable like this then if we change it at one place to mic you can also work then it will change everywhere and take note once you have assigned a variable you can reassign it so if i go here and say name is equal to steve then now name will be equal to steve so it's first mike or muke and then steve so take note you can reassign a variable if you ever wanted to in this tutorial we'll be talking about strings aka text anything in single or double quotes so as we've learned from previous tutorial local x is equal to str this is a string this will be taken as a piece of text and we can use it the same with single quotes str this is also a string this will also be taken up as a string and last but not least these double brackets these are kind of like literal strings whatever you put in here will really be displayed so if i put a tab here this tab will be displayed if i were to print this and run it as you can see it literally takes those tabs and new lines so those are the three ways you'd usually be making a string let's continue to actually using a string so let's say we have a piece of text here hello world this is just a string normal string doesn't do anything to get the length of the string we can put a hashtag here and the hashtag in lua basically means return the length of something so we go here we'll get 11 because there's 11 characters in here we could also put the hashtag here this will also get us the length of the string because remember x here is just a placeholder for this string here so this right here that x is the same as me doing this and of course for concatenation you can just say two dots and then what you want to add there's an exclamation mark and now there will be an exclamation mark let's say you maybe have a number so local num is equal to 20 and you want to convert this number into a string a piece of text because there is a difference between numbers and strings well to do that it's fairly simple let's say local str is equal to to string and this will convert this num to a string now we print num and then also string you won't really see any difference both of them will say 20. but if we print out the type of these and we can do that by saying type and then just like that so now type and then the variable and that will give you the type of this variable if we were to run this we'll get number and then string so you'll see both of them have different types because one is now a string while the other is a number now with strings you have the cool thing of escape characters so here with print we have a bunch of escape characters we can use for example hello backslash n backslash in means a new line so go to the next line world backslash t this means give it a tab the same as saying a comma here and then putting something new there but this is just in one string i am and in backslash v take it back b may not work on windows i have heard some comments say that when they use backslash b they get like a symbol instead of a vertical tab a vertical tab is just when you tab down instead of tab lift and i'll show you that in a second but backslash v gives you a vertical tab almost 19 two backslashes will give you a backslash as a backslash escapes a character so you'll need two backslashes to get one backslash and then here we're gonna say 20 so it's kind of 19 20. and now let's say i wanted to put double quotes in the string there are two ways we can go about this the first way is i can replace these double quotes with single quotes and now i can use double quotes years this works perfectly fine but sometimes that you cannot do that sometimes that will not solve your problem in those cases if you try and do this you'll actually get an error so to keep these back or these double quotes inside of your string we can use a backslash and these will escape the double quotes and the same will be done for single quotes so if you backslash a single quote it will also be escaped and these are just a few of them there are a ton of these escape characters but i'm going to go through every single one of them if we were to print this we get hello new line because backslashing world then a tab not a space this is a space this is a tab let me get a tab because backslash t i am and in a vertical tab this might not work on windows but it just basically taps down instead of left to right almost 19 backslash 20 years and in our quotes the ignored forward slashes are not the same as backslashes so backslashes only two while a forward sash only needs one next up let's say we have a local string and this string just contains hello world with an exclamation mark now if you print it we'll get hello world but there are a few things we can do with this string which is pretty cool first off we can say print and then we can go string and this will use the string functions we have inside of lua there's a lot i'm not going to cover all of them but i will be covering the ones you might like to use the most dot lower and this will convert the string to lower case and just say str here you can do the same and then for upper to upper case and then you also have one for ling and this will get the length of the string it's the same as if you were to say instead of string the ling you're to just go hashtag hashtag you can see as a shortened version of this let's return the length of the string this will convert the string to uppercase and this will convert the string to lowercase let's see what we get so we get our normal hello world then a hello world that's just lowercase a hello world in all caps and in the length of this string so let's start with mathematical operators there's a lot of them for example five plus five we all know what this does this will add two numbers together i'm going to add a few here because we have quite a lot we didn't have minus which will of course subtract and this could go into the negative values you're not limited to just positive numbers we have times which is represented by an asterisk and not an actual x so an asterisk we have divide which is a forward slash not a backward slash but a forward slash and let's say we give 19 divided by five you have to the power of so this would be five to the power of two and then we have modulo and i'll explain this one after i've explained the rest so let's go 17. all right if we run this here we go so we start with 10 because five plus five it's ten five minus fifteen will give you negative ten five times five is twenty five 19 divided by 5 that will give you 3.8 then 5 to the power of 2 will give you 25 and then 17 modulo 5 will give you 2. now all of them are pretty simple to understand we have all learned these in school except for modulo you did learn this in school just not quite the same in a very basic sense modulo is what is left over not literally but it is what is left over for example if i say 17 divided by five well that will be five then 10 then 15 right so we were to say 17 minus 15 will get 2 because 2 is what's left over if you were to divide these two to give an example let's just go here and say 17 divided by five one gives us 3.4 and the other gives us two because 17 divided by 5 will give us how many times 17 can be divided by 5. 17 modulo 5 does the opposite it gives us how much is left over once the 2 has been divided so not this 0.4 here but the actual value because a 5 10 15 and in 20 20 cannot go into 17 so we'll have to stick with 15 because that's the lowest value we can get if we were to do this and of course 15 minus 17 or actually other way around of course 17 minus 15 will give us 2 and this is the remainder that's what's left over so it's almost the opposite of division you don't have to worry too much about it we will be using this a little bit more in the future but you can get more used to it as time goes on one thing i remember that people in my class always have trouble with was the order of execution in math an order of execution can be a difficult topic because you might not remember what order what should be executed in for example five plus two times ten now i'm relatively okay with math so i would immediately notice that this two times ten that should be done first and then after this has been done we'll get this five plus two but not everyone will notice this immediately because that is just normal not everyone is made the same so some may see this and think oh it's five plus two that's seven seven times ten that will give you seventy but in actuality it will give you twenty five because two times ten that's twenty and in twenty plus five is twenty five now if you're one of those that can't always remember in which order this happens or if you just want to change the order in which this happens then i have a solution for you brackets brackets is what we can use to specify when what should be executed so here we could say it's first 5 plus 2 then we times it by 10 because brackets will take priority over everything else and it will be done first now we will get 70 because now it's first doing this then it will add this it's perfectly fine to do it over something that is going to do first i sometimes do this as well to make myself remember this is what will happen first you can see now we'll get 25. and you can of course add more things so now if we go times seven they will first execute this then it will execute this and then it will execute this but if you want to change that you could do this now it will first do this right here then it will do this right here and then it will do this so the order of execution is up to you you can change it depending on what you want and now i can look at a few things inside of the mav library print math dot pi this will print out the value of pi very useful in some cases of 3.14159265 and so on we have math.min which will give you the minimum value of something so let's say 10 5 189 the minimum value here would be 5 that's the lowest value of all of the values passed in if we run that we'll get 5. if we change this to negative 10 for example then now we'll get negative 10 because that's the lowest value you can also do max max will do the opposite and return the largest value so now we'll get 100. then you have a rounding which can be very useful so i have math dot seal and this will round up so 20.9 and we actually give a few here 20.1 so these two both will go up they will round upwards to run this we'll get to 21. they will not round down seal is in a sense the ceiling the ceiling is always above you so if you look up then that's where around it will round towards the ceiling upwards then you have the floor the floor will do the opposite because the floor is below you it's down so it will round down that will give us 20. so this is a way to force rounding to a specific point where it's either down or up and last but not least let's talk about random numbers print math dot random simple as that you'll get a random number run it run it run it that's cool you can do multiple so that's pretty neat but sometimes you want your randomness to be more random to do that you can pass in a seed so math dot math dot random seed and we can pass in os.time and this will ensure that all random values we get here is always random they will never be repeated continuously so now if we run this we'll get more random values you did get a few that repeat here but that's just sometimes if you roll to row random you'll get the same thing and there's only updates once every second you can specify up until where it should go so if we say 10 here then it will random to 10 so 2 10 4 2 7 7 a few sevens there so we'll continuously round the random towards one and ten so between those two you specify another value here for example 50. now it will be between 10 and 50. so will not go underneath 10 and it will not go higher than 50. so your program can make decisions that it is absolutely epic your you can make your program make these decisions by using an if statement like asking a question if something is true then do something else so in this case if true take no that's a boolean then and here we can just print this was true so one of the most basic data types is used of one of the most complex type of statements that is pretty epic we run this this was true now the cool thing here is this code will only run while this here is true if i make this false then this code will no longer be ran so it skips it this makes everything way more powerful for example let's say local x is equal to 2 and if x and then this is a cool symbol is more than and let's say five then print x is more than five run it and oh x is not more than five so this will not run but if we were to say x is 9 because x is more than 5 we'll get something out of it we also have the opposite symbol so less than so we have more than and less than where you can figure out more than we'll try and get the one that's the biggest so if this one is bigger than this one because it will try to eat the largest one so this is a mouth and it's trying to eat the largest one if this is bigger than this then it will be true this is smaller than because now we'll try to eat this one and hope that this one is bigger so this will see if x is less than five run it it skips the code but if we were to make x two again then we'll get x is more that's actually less less than five but you still get the point there's a lot more comparison operators which i'll get a little bit more into but not every one of them so we have the and or not i'll get into a lot of these more than less than more or equal less or equal not equal yeah take note in other languages you'd usually have a not equal like this in lua it's not with an exclamation mark and you have two equal symbols to say equal so these are all you can use in here and they're very powerful to use for example if x is less than five and x is more or equal to two so we're checking if x is more or the number two so two three and up and this is if x is less than five this does not mean including five if x is five this will be false because this only checks if x is less than five if you say less or equal to then if x is 5 this will be true so take note then here we can say x is between 4 and 2 both included so this will come see if x is less or equal to four or less than five this will check if x is more or equal to two or in other words more than one it doesn't matter which one you use and more than more than equal or less than less than equal it more depends on what you prefer i prefer just doing more than or less than but occasionally when i'm a bit more tired i would usually go for more equal and less than or equal and if we run this it works if i make x 5 then it doesn't work but if i say less or equal to 5 which will now include a 5 then it will run it this and here just says both of these should be true so if this is true and this is false it will not execute so if i were to say true and false then it won't execute but if i say true and true it will execute or will do the opposite or we'll see if one of these are true if one of them are true it will run so it will run it won't run or it will run my bad and if both of them are false it won't run there we go and in a not we'll just basically invert something so something that is not false is true so it's kind of like speaking if something is not false it is true if it's not true it is false so not false or false and i will give this will make this one true and here again brackets becomes a important part here for example if we were to do this then it will say if not and then whatever comes from this false or false this will be false so if not false if true then so take the brackets do play a role here if i want to make this one true then it won't work if i were to remove these brackets then it won't work either because true is now false in order to make this true it will work but if i wrap these in brackets it will not work because true and true or true or true will return true and this will result in it true here so if not true brackets can make everything a bit more complex but also make what you can do a lot cooler so let me show you an example of where an if statement could be useful let's say local age is equal to 15. so this person is 15. if age is more or equal to 18. then let's say they want to go into a bar they say print you may enter they may enter the bar after 18 years or older and here if this works it will run they're not 18 or older so if we say 20 then you may enter now what if we want to give them something else for example if they're let's say 17 we want to to tell them to scram they're not welcome here so print you are not welcome be gone but you might already notice what will happen here if i run it we'll get both we could go if and then age is less than 18 and then throw this in there this is perfectly fine so now we have two if statements and it will work if we go here and say 17 then you are not welcome be gone but there's a cleaner way to do this we can go else else is when this here does not execute so when this is false now we can just move this up there and there we go we run it you're not welcome be gone if we change this to 18 then we'll run the top one let's say it's this bar is near a children's tourist attraction so kids that are under the age of 12 might want to go to that attraction so we could go here and say else if h is less than 13 so everyone that is not a teenager yet print the water slides are down the road cool so now we're checking if the user's age that's trying to enter is more equal to 18 they're allowed to enter the bar if their age is under 13 we tell them that the water slides are down the road otherwise they are not welcome and they should be gone because then we're near their teens and they're trying to sneak in to get a quick drink run this you may not enter or you may enter if we change this to 17 then they're not welcome if we change this to 10 or 10 then the water slides are down the road we can of course do more for example local birth day is equal to false now let's say it's their birthday bingo if birthday then print you get a free drink on us so now if it's their birthday they'll get a free drink so you can use an if statement within another if statement if we run this let's just maybe make this 18 we'll get you may enter if you make their birthday true then you get a free drink on us and there's a ton of things you can do more with if statements but most of these you can just get by trial and error and by practicing what we've just learned and last but not least we can also do something like this local name and let's say if local age here is more than 18 so if age is more than 18 and mike or jeff so if the age is more than 18 then their name will be mike otherwise jeff and we can imprint name this is a cool one-liner where everything can be done in one line so now we get mike because their age is more than 18. if we change this to 15 then we'll get jeff so in this one line you can set a variable using an if statement you can of course also set a variable in a normal if statement so true and then name is equal to luke this right here is also valid to print name so this will also work however this is in one line so it might be more comfortable but if you prefer writing it out like this then that's perfectly fine as well however take note if you were to say local name here it will re-create this name variable and it will be scoped between this so it will be scoped here meaning we can't use this name here so if we were to print out name here we'll get jeff if we remove this local they will add trend access this name here and we get look so take note when you declare a variable you're using local so local and this will declare a new variable and it will locally scope it so it's only allowed in its current scope loops is a way to repeat code so it continuously happens so continuously runs until you tell it to stop in lua there are three common loops we can use the most common would probably be the for loop and this is basically for a number until a number run the code so for i and this is just declaring a variable in this scope if for i becomes one so we're starting at one up until 10 so we're going up until the number 10 and then we can just say then or not then but do and here we can say print i this will count from 1 to 10 and print it out so we have 1 2 3 4 5 6 7 8 9 10 from 1 to 10 it will just loop and loop and loop and change this i variable here you can see how many steps it should take for example one is by default so it takes one step one two three up until ten you can say two and now we'll take two steps there we go you can even make it go into the negative value so negative two but this here would cause an infinite loop so we'll have to go 10 to one so it's going to flip the two so it's i is going to start at 10 until it reaches one or less and then it will stop as you can see it goes downwards because we said -2 cool so that's a basic for loop you also have a while loop so while something is true we want to do something else this here is called an infinite loop since true here never becomes false so if i say print mic it will forever print out the word mic until i quit the program with ctrl c so this will infinitely loop until either the program crashes or your pc does you can use break to break out of a loop so loop it only goes once here we could do something like local count is equal to zero and then if the count count is more than ten then we can break and of course here at the top we can say count is equal to itself count plus one cool so now we will break out of the loop without having to make this here false so now we can say that and i will print out mike 10 times cool you could also do this put that there while count and instead of more than 10 we can say listen to it so wall count is less than the number 10 run this loop now if we run it until count is at least 10 it will run this code all right and then last but not least is the repeat loop so one thing i want to show you is let's say count is equal to 10. if we do this the while loop will never run because this here is never true the repeat loops runs at least once it's a while loop but it runs at least once so repeat until and we can just take this right here put it here and then take this and put it here it's the same as a while loop but it will run at least once in other languages this would be called a do while and my bad it goes up until so we should say up until count is more than 10 or in this case let's say 5 just to show you so until this is true so it will repeat this until this is true so unlike with a while loop it will not be while true this will be while false if we run it we'll get mic at least once it will run at least once before it quits user input is relatively easy let's say we go here and say print what is your name and then the user can input something so we can say local a and s for answer but you can call this variable whatever you want and we can say io.read and this will get input from the user and then store it inside of this variable and here we can print ans we maybe go name colon and there we go if we were to run this lua file what is your name i will say mike and there we go name mic another cool thing you might be interested in knowing is how to get input on the same line as your output because sometimes you don't want an output and then an input on the next line it just kind of looks ugly and sometimes is confusing to get input on the same line instead of print you can use i o dot write this is the same as print but it does not have a new line at the end so here we can say enter your name and it will enter and it will ask for the input on the same line as this output now here we can say nick and the name is nick and of course then you can do things such as if answer is equal to nick and they can give special outputs depending on what the user inputs but i think we all already know that so if you're coming from another programming language tables are usually considered lists arrays sits dictionaries objects records cues a bunch of these things can all fit into the idea of a table a table can be all of these if you have no idea what i just said then you don't have to worry about that so let's create our first table i'm going to say local tbl and if at any point say list or a rate just know i'm referring to a table and this table will be used with these right here so these brackets anything that goes in here is part of the table this can contain any types and any amount of items so this and we can put a 2 and a 9.9 and a true all of these they are allowed booleans numbers strings even other tables so we can go here and say okay cool this is all allowed if we were to try and print out table then take note that you might not get what you expect usually when you print out something like this you expect to receive that back but in this case we will receive a memory address but if you want to get the items inside of a table you could use a for loop so for i is oh that's all caps for i is equal to one to hashtag tbl do and hashtag is just a short version of number of items in this table like if you have a string or a piece of text and you do this hashtag with it as you might remember we receive the length of that string the same concept exists for tables and in here we can say table and then pass in i if we do this we'll get each item inside of the table this 2 9.9 true and then another table because this is a table here so we'll have to forfeit that as well if we want to see the items in it now you might be wondering how does this work how can we do that well if we go look here and we say print tbl at index 1 and this is called an index and we run it we'll get this this right here is the first index so if we were to go like this we'll get index 1 which is this index 2 index 3 index 4 index 5 and the more you add of course the more indexes there will be so it starts from 1 and currently it has up until 5. if we go 5 then we'll get a table if we say 5 1 then we'll get okay because here we're saying okay go to this fifth index here and then in this fifth index return the item at index one of it so this would be one and this here would be index 2 of this table we'll cover this a little bit more in a second but this is just as an example if i were to say 4 will get true if i were to give a value that doesn't exist like 9 we'll get nil nil just means empty doesn't exist nothing now usually you'd actually create a table with the same data types it's not too often that you'll see tables using different data types unless you use it in a sense of an object which we'll get to a little bit later but when you just use it as an array so a way to store multiple values inside of one variable then you'll usually just use the same data type so just numbers or just text for example let's say we have local x well let's actually start at a a b c d e and you can imagine this going up until z and we could say we're storing one two three four five now you might already be seeing how this is not really very practical because it's fine to have a bunch of variables lying around that whole storing their own values but all of these values are related in some way but they're stored in their own variables which there's nothing wrong with this method it's just there's a more comfortable way to do this where we could go local alpha nums and as we'll get all the alphabetical numbers which we would have usually done by doing a and then a is equal to one b b is equal to two c and what not but here we can create a table with one two three four five this is the same but instead of having to use a specific variable so print a we've already grouped them together and we can just say print alpha numbers at index one this will give us the same value because a and one is the same thing if you wanted you could also put variables in here you're not limited to just values because variables on the end of the day are just values so this will still work perfectly fine if i say index 2 or b lies then then we get 2. if i were to remove this then this will no longer work so we'll get nil because b has never been defined now let's go here and say we have one three five seven 9 and we can just call this local nums then let's say we want to add another value into this table here let me just also for through each item so one to the amount of nums and here we can print nums at index i if we run this we'll get one three five seven nine now let's say i want to add one thing in here before we run that we can say table dot insert and this will insert an item into an table so first we specify the table in this case nums then you can either specify what you want to put in here or where you want to put it in if you don't specify where it's just going to put it at the end so if i say 19 here and i stop here it will insert 19 at the end so here if we run it we get 19. but if i instead in the middle put two then we'll insert 19 at position 2 here so if i run this we'll get 1 19 free so this here allows you to get a position you want to put it in without it it will just append it to the end of this table you can of course also remove and here you pass in the position not the value but the position so if i say one one will no longer exist if i say four four will no longer exist because four was seven there's also a nice special way you can loop through these tables which is underneath this normal for loop instead of using hashtag nums you can use pairs so you're good pairs and you pass in numbs noms now you can get ka and b so for k and in v in pairs num if we were to print out k we'll get these values so 1 to 5. this would be the index so in this case we could say index and v would be value so index we can actually just do index and in value and it will do work the same so index one has one index two has three index three has five so this is a nice way you can four through a table in case you don't want to for through the numbers maybe you want the index as well as the value well you can get both but then pairs it's split it's up to you which you prefer doing now let's talk about multi-dimensional tables also known as multi-dimensional arrays this is a table inside of another table for example here we can create another table one eight three okay let's create a few more this can have four this can have seven this can have five this can have two and then last we can have six and nine now if we were to print out nums we'll get our table location in the ram if we were to print out number index 1 we'll get the location of the table in that table if we specify another value in here then we'll get the first index of the first item here if we say 2 we'll get 8. if we change this here to 2 then we'll get 2 because this is index 1 of the first table this is index two of the first table and this is index three so this will be the first table now inside of it you have index one of the first table in the first table or the first item in the first table and then index two and index three and then index three two one and then one two three if you were to pass in something like hashtag nums you'll get the amount of tables in this first table so if i were to copy this a few times we'll get five but if you specify an index here for example one you'll get the length of the items here free so this is free this is five we can add more here so now it will have a different length if you want to display all of the values inside of this then you can also use a for loop so here you can go 4 i becomes one to the num or to the amount of tables and nums and then for this time j because we can't really declare i that won't be a good idea for j becomes 1 to the amount of items inside of i because i is a table so we'll get the amount of items in i so this would be i that each item inside of i would be or each value here would be j so this is i this is j so here we could print nums so nums and then at index i and index i will give us the table so we specify the next index which would be j now and in my band this here is a number it's not a table we have to say nums at index i my bad because i is a number just like j is a number it's not the actual value we run this as you can see we get 183 which is the first items and then we get four to six which is the second and then five seven seven five nine which is the last so if you're having a bit of trouble with this just note nums at index and in the first index which we can call x would be the first item or the second out of whatever x is here if you add another one here it will try and access the item inside of this item so x which is this and we'll try to access y inside of it so let's say eight if you have a table inside a table inside a table you could also add your z so if i were to have a table here as well and we can go 2 4 5 and we're gonna have a few more then i could do this where i have three attached to each other to access each item in a table but take note tables can become incredibly complex incredibly fast so i always recommend to when you use a table not add too many items into the table as in don't add three or four tables inside of one table here so this right here is already very complex if i were to go here and i were to add another table here so now i were to add another table of more values then it will be more complex because now it's not no longer x y and z it has started going to complexity where x should now be shifted to something else i'm just going to move this here to the front and we can just put an a here for now so i would say always follow the x y z rule this is the complexity rule where you don't have more than three tables inside of one another if it becomes more than free it becomes very difficult to not only manage but to work with so i'd always say try and stick with free or less tables so here there are three tables if you were to remove this like that then there are three tables so this table here this table here and this table here so unless you have specifically named items like an object which we'll be getting to then never go past the xyz complexity in a table usually just try and keep it as simple as possible then you also have one two three if we're to go like that now let's say we want to convert this into a piece of text well we can go print table dot concat and this will basically join a table of all of its elements depending on the separator you give it for example if we pass in nums to or nums and we pass in a space then it will print it out with just spaces between each item this here is now a string take note this is a piece of text and you can replace this with anything so it can be a semicolon another number so dash dash dash will work and so on and then finally for named items inside of tables and we'll get more deeper into this but for named items you can go local tbl and here we can do something like name is equal to mic and then age is equal to 12. now if we go tbl as index one and we print that then we get nil but if we go tbl and then name we get mike so this is a named table some people also see it as a dictionary or an object and yeah that those are the basics of tables now a function is a snippet of code that you can execute as if it was a variable for example let's go here and say local function say hello and this will just print hello now we can call this by going say hello and then adding two brackets at the end and this will call this function here we will print hello so if we run this we get hello and of course we can put as many things in here as we want as you can see if we were to go print one and then two as well and we were to do this so print it two times by calling hello then we'll get hello one two hello one two so every time you call this function here it will execute the code inside of it and you can call it as many times as you want this is a great way to stop yourself from having to copy paste copy paste copy paste this is a very common practice where programmers will try and reduce the amount of times they have to copy paste a line of code or even a block of code variable variables can save you a line of code a function can save you a block of code now let's say we wanted to make this hello function a bit more complex so instead of just saying hello we want to pass in a name for example hello jack well this here isn't very useful because it will always print out the same name so instead what we can do is we can pass in an argument and we can call it name so this is like a variable that gets passed into this function now instead of saying jack we can say dot dot name dot dot there we go now if we go here and we say look by passing it in here between these brackets and this would be name we'll get hello luke we change this to jack we'll get hello jack sometimes you want to give it a default value so if you don't pass any name you want to name to be a default value well to do that we can say local name is equal to name or jack so what's happening here is we're declaring a new name variable and then we're basically overwriting what this is going to be on the end of the day so we're saying this name is equal to itself or if there's nothing in here if this is a false value such as nil then make it jack so it will default to jack so if we have three here the first has nothing inside of it the second has mike and the third has sally then if we run this we get hello jack hello mike hello sally without that there if we did this we'd get an error because you cannot concatenate a nil value because this name here would be nil if we didn't pass in anything in here let's get a bit more complex and do sum and this will get the sum of two things let's say we get num1 and num2 then here we can just return the value but how does one return a value here because we could print out what it is so print num1 and num or num1 plus num2 and we could call this sum 10 and just to show you could also go local x is equal to 20 you could also pass a variable in here so x that will be 10 plus 20. if we run this we get 30. but what if we want to work with the value that this returns that this would give us so we wanted to store local answer is equal to sum and then print the answer is and then ons what if we wanted to do something like this well we'll get a problem here because we cannot concatenate a nil value this here is nil because sum just prints out this value here to return a value we can use return and this will return this value you could either do it like this or you could go local result is equal to that and then return result now anything here with return will be returned from this function and nothing after this return will be executed which we'll explore in a second if we run this now we get the answer is 30 because this here now returns this result which can be either in its own variable or like that but take note we can go here and say print thunk start and in funk end you'll notice we get some red squiggly lines and the file expected after return that is because after return we are not allowed to put anything there anymore because after return nothing will execute so if we run this it will just press an error because nothing should be after return once you return the function stops executing but this doesn't limit you as much as you think it would if we say let's go here and say local val num1 plus num2 if val is more than 10 then return val if val is less than 100 then return negative vowel otherwise we could just return val times 2. so this is an example where just because we have a return doesn't mean it will stop everything else from executing because this return will only run if the value is more than 10. and this code here will never really work as expected so let's make that equal to 10 there we go so this will only execute if the value is equal to 10. this will only execute if the value is less than 100 but not equal to 10 because this will stop execution first so if we say 1 plus 2 then that will give us negative 3 because it is less than 100 so it will give us this if we say 8 plus 2 it will give us 10 because the value is 10. if we were to say here 101 so 101 plus 2 that will give us 206 because it's the value times two let's first try to create a file that is pretty simple so let me just also open up my file viewer here so we can see if any files appear io dot output and here we can pass in a file we want to create so my file dot txt this will create a file my file.txt if i open the containing folder you'll see there's my file.txt now if i were to delete it from here you'll notice it also deletes it from here this here is the same as this here anyhow so we create this file then that will happen but take note it will empty the file if it already exists so if i go and put some text here let's actually have it here on the side of the screen and i decide i want to execute this as you can see it empties the file so you should take note here it will create the file but if it already exists it will empty the file now this output can be used also with writing to a file so io dot write and here you can put some text you want in the file hello world and we just have to io.close whenever we use any of these because you don't want the file to be open and then later on try to work with it again or if other program tries to work with it but yours already has it open it will cause issues so you always close the file after working with it now if we were to run this we can actually show this here the side again we run it we get hello world if we run it again we'll get the same output because this anyways rewrites this file it will empty it if there's nothing inside of it or if even if there is something inside of it that's the first way you can write to a file now the first way to read from a file we go io dot input this time so instead of output we are using input and then the file so myfile.txt then we can of course io.close and here we can say local file data is equal to io.read and then we can just say asterix all this will read everything inside of this file and you can of course print file data we run it we get hello world we can add some more like x2 x3 i am cool if we save that and we run it here we'll get header world x2 x3 i am cool so you first take the file we want to read and say it's an input file because you want to take input from that file then we read it with the asterix all which means read everything inside of this file and we can print the data that we read and then we can close the file or we could close the file and then print the data whichever you prefer so that's the first way to read and write a file now the second method is what you'll most likely see being used so let's first try right to a file that will be local and we can say what variable we want to save this file in so file io.open the file name so myfile.txt and then what you want to do so here we could specify all of these so read write append read and write write and read i think append and everything else read bite right byte append byte and so on and so forth but we're mainly going to be focusing on these here so first we'll have a write now w just means right so we'll write data to a file so here we can then say if file is not equal to nil so if the file we're trying to open exists then so if the file we're trying to open can be opened then we want to go file right and we can just say hello there and i will write to the file and then of course file close else if we could not open this file we could just print could not open the file now if we were to run this we'll get hello there if we were to delete this file and then run it it will generate the file and enter hello there so this method is usually the preferred method above i o dot output as well as i o dot input because we'll learn how to read right now now there are multiple ways you can read from a file one way you can do it is going file and then read and here we can just specify astrix all and this will read everything so if we go here and say x2 x3 goodbye and we read it of course we should maybe print out what we get so print then we get hello there x2 x3 goodbye you also have asterisks line which will only read one line so we go here hello there if we then do it a few times each time we do this it will read a new line so hello there x2 x3 so if you want to read line by line this is a good way to go about it by using this asterisk line otherwise you can just go astrix all and that will read everything and then last but not least let's take a look on how to append it to a file now when you write to a file you will overwrite everything that's already inside of the file so if i were to go here w and then say right and change this to goodbye then it will overwrite everything and also doesn't help if you print it but it will overwrite everything and add our goodbye but what if we wanted to add something to the end of this file then we could use a which means append so then we can say appended text if you run this we'll get appended text if you wanted to go to the next line you can use a backslash n here which means newline appended text 2. now let's append the text to on the second line so that's how to append now the term module in lua refers to a lua file that returns a single table when it is called so it returns a table or an array or an object as semi to understand it so it's basically any file that's not this main file that returns something on the end of the day and then you also have a package a package is a collection of modules so if i say i'm going to go and install a lua package that means i'm going to install a few modules that will allow me to make my coding in lua easier for example the luna cjson package because that's a package of modules i am going to use in my program to read json all right so let's get an example in here i'm going to create a new lua file call it custom.lua you can call it whatever you want then here i'm going to go and say mod is equal to a table so i'm saying this is a table here we can then go here and say function mod dot sum and here we could say x y and in return x plus y so we're putting the sum as part of this mod here you could also go sum is equal to function and then say x plus y this is just as valid if you just say return because these two mean the same thing if we were to put in x and y here so this will be the same thing and of course we can have our mod here with let's maybe say say hello and in here you can maybe get a name and then we could say print hello name now these are just very basic examples you can put any function you want here i'm just making very basic functions here as an example to show you then we return mod because this mod will now be used outside of this file so here in main.lua we can now use this module local mod is equal to require and then we pass in the name here so custom dot cass tem dot lua or just custom because it will automatically add the dot luo in searching for it this will import this module for us and store all of the things being exported inside of this right here to that variable we can then go print mod dot sum 10 5 or mod dot say hello mic if we run now we get 15 and hello mic all of them are made inside of this module here this is a way to keep one file from going like to 10 000 lines of code because you could split it up so one file could have a thousand another could have two thousand another could have five hundred and they could just make the process of reading this main file much easier since we're importing the stuff we need from outside but as the basics of modules it's just another file that's returning a table and this table contains a bunch of functions or variables that we can use here once we import it with require what exactly is oop oop is object orientated programming it is when you think of your code as objects for example you have a car that's an object a car could have wheels a car could drive a car could do this you have a pit you could have a cat pit you could be able to feed your pit you could be able to give your pet a name because a pet is just an object you could have a cup a cup is just an object that you could fill with water that's the whole idea behind oop everything in your code is an object some people love oop some people hate it i personally love oop when doing game development but prefer using procedural programming basically what we've been doing up until now with normal programs now in lua there are multiple ways to do oop there's not just one not just two there's more i'm going to show you one way to do it but if you don't like this method you could go and search for a different method that you might like more there's one that uses meta tables but i find that method to be very confusing to understand and a bit difficult to use so instead i like to use functions and simulate oop with them i'll show you an example let's create a object local function pit this is your pit now your pet could be a dog or a cat but this is a pet it is what you use when you go charlie come here i have a treat for you you're calling your pet and we will return a table because a table is the best way to get an object if you maybe come from python or from javascript then dictionaries or objects it would be this same concept and here we could do something like name is equal to charlie then here we could go and say local pet or even cat is equal to pet and then we could print cat.name anything inside of this return is public and can be used with this object now if we were to run this we get charlie because this is just a table but we disguise it as a function which generates an object for us this is nice because now i could have two pits one that's a dog and now i could go dog.name both of them will be charlie so this pit is a bit flawed let's allow us to change the name so name we would say name is equal to name or charlie so if they didn't enter a name it will default to charlie here we could say jack so my pet is called jack now my cat is charlie my dog is jack on the end of the day these are just a bunch of tables but we can create or generate tables for us by calling functions so that's pretty neat now take note if something is not in this return it won't be returned for us to use if i go age is equal to 10 i cannot use this age here i could use it here so i could go days alive is equal to h times three six five there we go now here i can't go cat age and then dog days alive only dog will return a value because age is not returned only the things in return is returned also we can just go local here so if i were to run this we'll get nil and 3650 that is because age is not returned so we don't know what it is this can be seen as a private property of your object so something you can use only in your object and it cannot be used outside of it and this here could be seen as a public property that can be used and called outside of this object you then also have two types of functions you have the speak and this function is just a normal function and we can just say print and let's just go meow even if it's a dog let's just say it meows then you also have another function which we could call feed so if you want to feed this pit that can be a function that takes in itself and we'll discuss it in a second and for now right now we don't have to do anything here we can just say print okay so here we have a basic class with two type of functions this first function here can be called with this dot syntax so speak and this is return anything so we don't have to print it but you take note this second function so we could feed here will not work because it takes in self and self is what you use to specify this object this right here this is the self it is this table self is referring to this table it's referring to itself but this won't work so if we run this we'll get meow and eating when we go self dot name and we try and run it now we'll get an error because self is a nil value so once we want to use self here which allows us to reference anything inside of this table we cannot use this dot syntax anymore we have to use colon colon basically passes dog into itself in a sense so this first value here is going to be this here so if we go here and we run this we get eating and then jack so these are the two functions you'll usually get i usually just say make all of them include self even if you're not using self because then all of your functions will require a colon and you'll never have to wonder is this function not working because it needs a dot or a colon it will always need a colon so i always say include self even if you're not using self and itself here can we use to access any value inside of this right here so i can go self speak and now we can speak we run this we get meow eating jack and then meow that is the basics of these types of functions but usually just stick with self and you won't have to memorize what the other does now let's say you want a type of pet that isn't in its own a pet or it is a pit object but it's more specific than that this is called inheritance so let's say i want another type of object so local function dog right dog takes a name but now we go return and we're like okay i want all of these inside of dog as well so we copy we paste we now have all of the same things but now we want to add a few new things we want to add breed which is equal to doberman and we have loyalty because dogs can be very loyal and we'll make that equal to zero so now we have a dog that has all the same items as this pit object here and we can go local doberman is equal to dog jesse now we can say doberman dot breed and now we can get the breed of this doberman and that's while we're adding it doberman dot name if we now run this we'll get jessie which is a doberman cool but now we decide here inside of our pit we want to add another item we want to add status and status will be something like hungary now since we want dog to have everything played as we now have to remember and this could be like a ton of objects that could have the same types of items in pit but now we have to remember hmm we have to copy this and we have to go paste it here as well we have to remember that and a lot of times you'll have five or even more objects that will be inheriting from this object so it will have all the same items so you will have to remember all of the objects that's using this object so you can go and copy paste all of these here this isn't the way to go instead you should use inheritance inheritance is when you inherit all of the different types of properties in one object and you use it in another so here we could say local dog is equal to pet and we pass in this name that gets passed into dog cool now we return dog and then instead of creating this object here we can now just append to this already existing one so dog dot breed and we can make that equal to this right here dog and then dot loyalty like that and then we can remove this part here and now we have successfully inherited all of the properties that's being returned here so we get name we get status we get speak and we get feed we get all of these here and now we add two more properties onto this property we have or onto this table we get from this and return that now we'll have both the name and the breed even though we didn't copy paste any of these here if you run this we'll get the same output and once we change anything here so anything here for example we can add points for some reason and make that equal to 10. i can now in my dog i can access points even though i didn't actually copy paste that data here we can now also go breed and we could rather pass in the breed or doberman so now we could actually go here and say poodle so now we get a poodle instead we run this we get jc poodle we can overwrite functions that already exist here as well for here we say meow we could go dog dot speak and speak is a function that takes itself and then instead of saying meow it says woof so now when we go poodle speak it will say woof this is the power of inheritance so we can have a bunch of objects that's inheriting from this object so now we can maybe go here and we can create a cat which has the same types but this time it maybe doesn't have a breed because they don't know any cat breeds it doesn't have loyalty doesn't have speak and here's a cat and we can go here and say lazy and that would be cat that lazy is equal to true so if the cat is lazy or not now this cat has all of the items inside of pit and one more that's lazy and of course if you wanted to you could make cat inherit everything from dog you just of course have to pass in the breed which i don't know idk and now cat has all of the items inside of dog and the pit because dog has everything inside of pit so cat now has everything inside of pit and dog because cat is inheriting from dog here and this is the basics of oop you might not quite understand everything here yet but once we get to the game development section of this course then you'll quickly start to understand the power of object orientated programming and how everything here connects with one another and hello everyone if you are enjoying this tutorial please leave a like and subscribe for future tutorials and hello everyone welcome back to another tutorial so in this tutorial we'll be looking into love 2d live 2d is basically a framework that you can use to make 2d games in lua now first of all you need to install love2d if you are on windows then you can just go and get the installer if you're on mac you can get the zip and if you're on ubuntu or linux you can just get one of these since i am on arch i can just do this i'm going to go to terminal new terminal and just pac-man dash is love and you just need to consider that and that will woo and that will install love 2d for me so as you can see it's already installed and up to date so i'm not going to install it but if you're on arch that's how you would install it cool then once you have it installed then you can just say love and that should pop up something similar to this you might not have this exam this exactly same thing here it highly depends i have loved version 11.3 if you have a lower or a higher version of love you will see something different i do recommend having the same version of love as i do or at least a higher version because a lot of the features may not exactly work on later or newer versions of love so i'd recommend having 11.3 the same as mine if you ever wonder about anything or whatnot you can just go to the love tv website to love tv.org and you can click on this wiki here this wiki will basically give you all you need to know about love we won't be covering every single thing here but we will be covering quite a lot of this for example let's go to the let's see let's go to sound let's say you want to play some sound which we will cover in later video as you can see you have a bunch of things here you can use to work with sound let's say you want to go to a window then here's a bunch of things you can do with the window so there's a bunch of things you can do here i'm only going to show you the most basic and we will be building two games at the end just so you have a good idea of what we'll be working with okay now i think we're ready let's get started so first we need to just move this to the right because i don't like it if it's at the bottom i do love it if it's at the right side now first let's create a new file i'm going to call this main.lua you need to have at least one main.lua file here in order to be for love to be able to execute otherwise love may not know which file to execute so here it's main.lua and now i'm just going to close that so we have more space so we're still inside that file now take note i have a terminal open but if you're on windows i do believe you can just double click that love that main.lua file and that should run live 2d but i'm not sure if you're on windows i would actually recommend opening up the command line same on mac open up the command line and just saying love dot in that directory because as you can see i'm in this directory right here so if i were to just let's say open up my folder here so funner as you can see there's main.lua i'm in this four right here now first thing we need to do is we need to set up a basic project so to do that we need to go under scrutiny and make sure you do have love to the installed otherwise this might not work dot love this means make this a global variable with the name of love and we're going to require the module called love so basically this is normal lua we're just importing a module called love then next up we need to create three functions these three functions are the basic concepts of running a game so first we have a function called love it needs to have love at the start here love dot load this is very important i'm going to explaining all of this in a second we can just ignore this for now i'm going to copy this and i'm going to paste them two more times and i'm going to close that then this we can make love dot update and update will take in ddt and i'll explain that in a second and here we can say love the draw okay so let's explain what these are now in every game you have three stages you have the load the update and the drawing stage live2d makes this incredibly simple to do the loading the update and drawing in their respective functions one of the reasons i do love love is because of this right here because it's languages like java for example you'll have to actually go through and make these functions yourself and let them work in the background as they should where love2d has already configured a lot of this for you so first loading is to basically load in data when the application starts up so when we boot up the game this will load all of the data we need so we need to put things in here that we want to load then here with update this basically runs every once every 60 frames this should run once every 60 frames so this is 60 frames per second love2d is kind of locked to 60 frames per second it can go lower if the game requires a lot of resources but it's kind of locked to 60 frames per second so once you start this up you'll get 60 frames a second dt is basically delta time it's the time between this frame and the last currently you don't have to worry too much about this it's mostly just math so don't worry too much about dt we'll get deeper into that later but basically this is every frame imagine that it's a frame so you have 60 frames a second this will be every frame it's the time between this frame and the i last to draw oh wait and we're still here and this will just also update everything so let's say we have a player if we want the player to move the player will be moved inside of this update function here so this will move the player then we have draw and this will draw everything to the screen so first we move the player and then we draw the player the player at its new location or we the player gets hit and then we update a heart getting destroyed in the game that's what we draw basically that's how it works we'll get deeper into these things as the tutorials go on but for right now it's not too important but we will we will get to it so let's start off with just creating a basic hello world so we're just going to skip these two for now we don't have to worry about them and here we can say love and you'll notice most of love's important functions start with love got the graphics graphics is what we use to draw something to the screen so we use graphics dot print and this will just basically print to the screen it's very simple it doesn't do too much and we can just print hello world all right now if we were to say love and then dot this means run love in this folder and it will find that main file and will burn it you'll see this but then you'll also see this little pop-up right here just saying hello world there's a little icon there's some text which you can change later and here's like the basic functionality you'll have and yeah that's the basics we have created our first application it it just displays hello world it's a first game now let's do something a bit more intense than just printing something to the screen just so i can show you the difference between load update and draw so here let's create a variable called number and make that equal to zero take note this is a global variable you can either do it like this or you can say underscore g dot number so that this will load in this number when the game starts it needs to be global because we need to access it between the other functions in this update right here we need to update the number so with every frame we want to update this number so we can say number because we're accessing this number right here if you don't know i do recommend you go watch the tutorial i made and you can say number plus one this will take this number and it will just add one to it then here where we do this we can just display number [Applause] so i'll explain what this does more in a second let's quickly run it and see what we get so right here you see a little number constantly increasing that is this number right here so let's just move this to the side first we set the number so when the game loads we set the number to zero so once this boots up this will be zero then every time with every frame this number updates so this number increases with one with every frame and in this drawer right here this will print the number to the screen so as you can see we first set the number and this only runs once this load that only runs once this update however and this draw runs every 60 or every frame so these two runs every frame and this prints it every frame so in this tutorial we'll be covering the live love2d configuration file now this configuration file is basically a file that allows us to configure application more to maybe get certain permissions or to add certain features to the application so this gets started so we just have a very basic application here what goes on in here doesn't matter too much because we wouldn't be using this a lot so first thing we need to do is we need to create this configuration file so you just right click new file or you can open that folder it is in for example you can see here there's my main.lua file it's creating a new file and we can call it conf dot lua like that and now we have a configuration file and inside of this configuration file we can put a function so function and then love.conf and inside of it we can just p put t so t is basically a variable that's going to be passed in for us that will basically contain a bunch of information and stuff we can use and then of course this is just for love and then here we basically say we want to manage the configuration for application so there's a lot of things here for example t dot identity and we can just make that equal to a little for now this will allow us to assign a save file i'm just going to also do this for now so this will allow us to assign a save file we can insert the path to that save directory for example saves or let's say data saves like that and then here we can just create a new folder called data instead of that saves now we have a folder with the name data and inside of that folder named save and there we can see that's the path to it so that's just basically the saves directory not too much there the next app we have and of course you can add multiple things here so i can continue underneath this if i wanted to and next up you have things like the version and here you can see the version of your game this is very useful because sometimes if you update your game especially on android there are two things that can happen one you can install an entirely new application or two that application that already exists will be updated where it is that's what you usually happen to have applications and that's what should be happening so here it can put like a version like 1.0.0 and i didn't do it instead of creation marks like this and then how you can basically determine when you should update what here it's up to you of course you can even add more things here like v1 and lstl or whatnot you want to add to this but usually if it's like this then you can just say for minor updates let's say you fixed a small bug you can increase that by one and let's say you maybe just updated the output so there's just like a so instead of it saying game over it says game lost that will make it two so if every minor change you can do this then with every major or a major change but bigger change so let's say you add a full blown menu system or full-blown settings to your application like it's a new it's a new path to your application like the whole new version almost then you can increase this by let's say one or two and it remember it doesn't have to be small numbers this could be like something like this and it's perfectly fine to make it very large numbers it's it doesn't have to be small values like we put in here for example and then once you have basically a change that makes this the version 2 or the version 3 of your game or like any major version of your game you can increase this so this means it's basically not the first version anymore it's basically like the second version of the game a lot of people also just like to do this just to keep it simple they just use two numbers where if every major update so let's say for example and you implement in whole settings page for your game they will increase this and with every minor update they will increase this so let's say you added like a little title at the bottom in you can just add that or you added like another character that or npc that doesn't do anything add one there like that all right in case you mess around with it too much i just put basic versioning in here let's actually keep this one here just for interesting and remove that one then we also have t.console and this is basically if a console should be attached to that game and there should be a true or false value so false means which is it by default there shouldn't be a console attached to the game this is windows only take note of that if you're on linux or mac this won't really affect anything but if you're on windows you might notice sometimes that there's like a little black box this command line or cmd that pops up when you open up an application that's the console so if this is true this means that the console should be running in the background and it will basically be attached to this window so once you close the window the console will close as well windows only though so take note of that next up let's say you want to save the files but on an external storage like you want to save the files but on and sd card for example then we can say t dot external external storage all lowercase and false or this would be true then this means you want to save the files but you want to do it on the sd card and not on the normal phone storage and i do believe this is android only so take note of that i think this is android only so you should also take in consideration about this we also have t dot gamma correct is equal to true now this will basically in a sense if the system supports it it will enable gamma correct rendering i have no idea what that is but if this isn't supported it will do that it will probably just enable better gamma whatever i think gamma is like lighting or things like that let me google that yes so it is the overall brightness of something so the more bright it is the higher the gamma i guess and yeah it's something like that i don't usually work too much with that i usually just do the basics in the configuration files i would go for camera correct unless someone specifically like gives an issue about the gamma being wrong or something like that then i will enter that but usually i don't care too much about that okay next let's say you're have you played like an online game and it's like a game like pubg on pub g you have the option to click a button and then it will hear your voice speak so you can speak to people on pubg by clicking the microphone button we can enable that as well now on android we need to basically get a prompt that allows us to hear the user's voice because you may have noticed if you want to talk to someone on pubg the first time you click on that button it will pop up a prompt asking you if it can use your microphone that's what we're going to do here so t dot audio dot mic equal true so if it's on android and you want to enable microphone then this will basically give them a little pop-up asking hey can i use the mic cool now let's actually configure something we'll be able to see such as t.window.type and that should be window okay and we just make that equal to whatever title we want for example cool ball game now if we were to love this we'll get this right here which says cool ball game so that's this part right there so that would be the the.title we also have things like the window icon you'll notice i have a little folder here here with an icon inside of it it's called game icon.jpg usually you want to save these as ico because that's a con file but i use the jpg just because so here i can just specify icon slash game icon dot jpg now if i want to run that then you'll notice i have a little icon here now and that's this icon we got right here it's a very dark icon i should have picked something a bit lighter but you can see there's a little icon there so that's to change the icon if you wanted to do that then you can by doing this and you see it's an icon and then game icon take note this path is kind of relative at the moment because you can see here if i were to move my conf file outside of this folder right here so icon was in a different folder it would not find this icon here so keep note that the location of where the conf is and where icon is that's very important but we're not going to go too deep into that that's something completely different you usually have to worry about you can also set the window with for example if we were to do this and you can also set a height it is to go like this and you can say we'll go away with and here we say height and then all we need to do is specify the width and the height for example let's say the width should be 400 and the height should be 200 which is which is a very weird ratio but there you go so the width is now 400 i believe pixels and the height is now 200 i believe pixels you can of course make this as big as you want for example 1200 by 700 which is almost a 16 by nine aspect ratio as you can see that right there now it's a lot bigger so it just depends how you prefer it you'll also notice that if i would just do here again that if i want to resize it i can so i can't resize it so even if i wanted to it just moves it it doesn't resize it and this is by default like this because usually you don't want games to just be resized because that could break the game but if you do want this to be allowed for your users you can say t dot window dot resizable is equal to true now the window can be resized so if i were to bring it over here now to go here then i can resize the window so it by default it is disabled and for good reason as i say you usually do not want to enable this because that could cause the game to break you'll notice in more popular games as well you can't resize the window because it's dangerous to resize the window you can also give it a minimum width and a minimum minimum height which could be better if you have this resizable on for example but i can just copy this and in here instead of just saying we have and height we can just say min and i'm going to copy that width there and just paste it here so now i can sit like the minimum width and height it's allowed to be for example let's say the minimum height it's allowed to be as 500 and the minimum width it's allowed to be is let's say 1 000. now even though it's resizable true it can only be sized to a specific size so here if i were to resize it as you can see after a while it doesn't allow me to resize anymore but i can make it bigger but i can't make it smaller so it has like a minimum size it can be resized two if you want to enable the resizable as i said before rather not you can also remove a window border by saying t dot window dot border list is equal to true this will remove the window border which i don't always recommend but it's a good option if you want to allow your users to have that okay so as you can see it doesn't have anything up here this should be optional if you want to put it in you shouldn't force this onto the user because not all users like this you'll notice with most games you have an option to remove the borders and that's a good there's a good reason why there's an option to remove it and usually by default it's not removed unless it's by default full screen you can also enable vsync so vsync and this you should specify with a number i don't quite know what this number means but you should specify the number if you wanted enabled here it is now vsync enable if you don't know what it is it's i think it's vertical sync and it's to sync sound and and the video but it's it should be optional because yet again not everyone will benefit from b-sync because it's by default it is aligned as it should be but usually you don't worry too much about it okay so that should also be optional for the user to change you shouldn't force it on him again and here's something that could possibly solve my problems display tell it on what display to appear for example if i say display one but actually nope it displays that one if i say display two which is not my main display then ah it does appear here so now what we can do is instead of me having to continuously drag it over to show you i can now go here and i can make full screen i actually didn't know this display was here i just realized that it exists and just as you can see it's full screen and i didn't actually think this through because you know i can't really exit but yeah so that's if you wanted full screen now i'm going to keep that there because that's actually very useful i wish i knew that existed much sooner you can also specify the x and the y coordinate coordinates so we were to copy this and go here then we can like say x and let's make this 200 i don't know if this is not going to go back to this screen because the displays are kind of linked okay so you know it's here so the x is now 200 as you can see right there but to make that list and let's say 100 then it's 100 from the side here so yeah then we also have y and let's make this 100 as well so let's see 100 100. that's very useful if you wanted to be at a specific location but yet again you might just want this if you want to save where the user last had the application when they closed it that could be a good feature but forcing a location yet again not always a good idea because that location may not exist on the user's monitor because let's say the monitor is smaller than yours and you set it to specify beyond 500 500 they might not have a screen which is very rare but they might not have a screen that size so yeah just keep that as note this should usually be an optional thing you add all right so next up is something i do not recommend you play around with it it's good to know it exists but you should not be playing around with this unless you know what you're doing but usually you do not want to play around with it you can disable and enable modules by default all modules are enabled in love2b but you can disable them if you don't want them for example t dot modules dot and let's say timer is equal to false by default this would actually be true because the timer is enabled now this timer right here provides this dt here which is the time between this frame and the last frame so if we remove that module dt so if we make number here dt and then print that value dt would be zero so right there and i've accidentally removed my one thing i liked as you can see there it's zero but if we were to yet again just remove that from there and actually let's bring back our window display i did kind of like that so if we were to remove that disabling module then as you can see dt is a number so just take note you can disable modules which i guess would probably decrease the application size or something i don't know what it will really benefit in doing but you can do it if you want to do it so just take notes of that so usually don't just don't disable anything that's a good rule of thumb if you know you're not using something and you know and you know you're not going to use it then i guess disable it but usually you don't want to disable anything now if you might have fallen behind or you want to maybe remember some of this you can always find it here on the con on the love 2d website so for the most part we're going to use load or update here we're mostly just going to use draw so we can just focus on that let's try and draw a rectangle in lua which is pretty simple so it's just love dot graphics dot rectangle and here we can specify the type it should be so you have different ones like line and fill we'll cover both of these in a second so let's just say fill for now and then we can specify the location and i'm going to explain all of this in a second as well now if we were to run this you'll notice we have a little white square here and yeah so let's go look here what is all of this first we have the type fill so as you can see here this is like an entire box just wide but if we were to make this instead of fill we were to make this line then now we'll have a outline instead of having a fully filled white square then we have our x position which is 50 so this is the x position and then our y position which is 50 and this is like that you can remember which one is x and y by remembering that x is going horizontally and y is going vertically and as you know y if we were to put a y here it has a thing that goes downwards so this would kind of represent a y shape and that allows you to remember that this section here up to down that's why so this point right there would be zero y and zero x here would be 50x but 0y and here would be 50x 50y and here would be 50y 0x we'll explore this a little bit more in a second and then here we have the width and the height so we were to make this 100 then now the width with 100 as you can see here 100. let's change the x so let's make this 10 instead like that now if we were to run you'll notice it's only 10 pixels from the side here as you can see and then here is the y let's increase the y to let's say 500. i don't know if that's actually still on the screen it is there we go so this is 500 pixels so yeah that's the basics of just drawing a rectangle you have your x you have your y you have your width and you have your height so you can of course make the height having bigger so if you wanted to make a little bit larger you could you see there now as i said before if we were to make this zero and then run it again you'll notice that now it's here because the x-axis if we go back here is zero so it's right there it's against the wall because it's zero if we were to make this right here zero and let's say this ten let's just close this then now as you can see it's here at the top because that's the y index of zero as you can see because remember y is going down like that it has like this little y symbol i'll see if i can try and edit that in and of course if we were to make it zero zero then it would be against the wall as you see here zero x zero wise it's against the wall now before we continue to let's say something different let's try and change the color because currently we only have white what if we wanted to make it a different color we can do that by saying love dot graphics dot set color and we can make it let's say one zero zero which would be red and i'll explain why in a second so we will boot this up you'll notice a red square now and not a white square so this right here uses rgb values now rgb if you don't know stands for red green blue and here below we have between zero and one so 0.5 would be fine for example so 0.5 that would be halfway so 0 zero zero so for this was all zero this would be black because that means there's no red there's no green and there's no blue that's it's only black so none of those colors exist whereas if we were to make this one one one then this would mean white because that means all of the red all the green that all the blue is in here now usually you don't really want to figure out colors like this oftentimes you'd want to get your browser and search up color picker and you'll find something like this so let's say you wanted a nice greenish color so let's go here and then we go like this cool and you'll notice here we have rgb we can copy that and now here what we can do is we can paste it here and we can say divided by 255 and if you do this then we'll get this greenish color as you can see right there we divided by 255 because by default usually it's actually 255 so it's a number between 0 and 255 but in lua it's a number between 0 and 1. so if we divide it like this we'll get a value with it so if we go like this then we can just say 75 divided by 255 and that gives us 0.29411 so we could have put we could have done this zero 0.29411 but first of all this is kind of difficult to read because you can't really tell how much that is some people this is pretty simple but to me it looks a lot more than what it really is and that's why i just prefer this so you can choose between using this or this it will give you the same output so if we were to run this we'll still get that same green if we can just get love as you see there we get the same green so yeah but what if we wanted to make this black so zero zero zero you'll notice the background is the same color so if we wanted to make this the same color as the background is now we won't be able to see it so how does one change the background color well it's pretty simple now you can either do it here or you can do it on the on load so you can change it on the unload or you can change it on the draw if you change it here it's going to do that change every time or every frame is going to make that background color the color you set it to every time you call this every time draw is called so that's every frame if you do it on unload it will do it once and that's only when it loads up so for extra performance sake i would recommend doing it here unless it's part of the game to change the background color anyways we can say love dot graphics dot sit background color and here we have the same option so i'm just gonna say 0.5 0.5 and let's maybe say one and now if you were to run this you'll get this purplish color and then with the black box right there so that's how i can change the background color now let's get back to the actual topic of the video let's draw another shape let's try and draw in circle this time so let's go love.graphics.circle as simple as that i'm going to make the circle line and not fill but you can make it feel if that's what you want i'm going to position it at 100 100 and i'm going to make the size 50. now take note this is the radius so it's not a width and a height it's a radius meaning it's going to be 50 pixels to the left and 50 pixels to the right 50 pixels to the top 50 pixels to the bottom this is going to be a pretty big circle as you can see right there we can actually move it out a bit more so let's maybe go 200 or 200 that's an x and let's make it 500 on the y now if we're done this as you can see we get this so it's nice circle and whatnot now what if we wanted to change the color of this circle because we've already set the color have we not well it's pretty simple we just copy that line of code and paste it here again now we can give it a different color so let's say one zero point seven and zero zero zero point one there we go now from this we get this yellowish color that's kind of difficult to see but it is there so if you want to change the color you just basically set it here but take note this is important that these colors stay the same because of every frame it's going to call that again so we do this right now everything is going to be this yellowish color as you can see because i commented out this part so when this code runs again because it's going to set the color and then when it runs again it's going to not set the color again or it is going to set it again but here it doesn't change the color back it just draws and then it sits the color but it's already this color and then it goes here and it does it again and again where with this every time this runs it changes the color back to this this is something that's very important and you'll notice when you build the games that this does come in handy to know how this works i do recommend you play with this because you'll notice if you comment something out then this might not work if you'd maybe move this to a different location it might work so if we put this set color there it won't work as you can see there because the color is set here but then we come here and we change the color then these two execute and then we set the color to this and nothing is really drawn after that if we were to comment this out then now everything would be yellow again even though it's at the end as you can see there very good to note that so let's draw one more shape and then i want to show you how to just make something move so here we can maybe draw an arc i'm going to make it a line once again and let's say we need to exposition y position we can maybe keep this but maybe make this 250 and in here we need an angle one and an angle two so i'm going to make zero and two and you'll see what happened in a second my bad i forgot to add the radius because we also need a radius i'm not going to make it that big let's make it 20. and now if you do this as you can see we have a little thing there actually let's make a bit bigger because it is kind of small so let's go maybe 60 i'm pretty sure that would not 660 should be good enough as you can see here we have like a little cheese shape here now let's see what happens if we change one of the arcs to let's instead of two we make it five what will happen as you can see we get a little pac-man that's pretty cool you can make a pacman game of this because it already draws the arc for you all you need to do is make this change so let's see what if we make this two what will happen now because what does this arc really do as you can see it kind of changes where this and that starts and ends what if we made both of these 5 what will happen then nothing because now both of them start and end at the same place if you were to make this zero and let's make this five again you'll notice this so if we wanted to make a pacman we might want to go one six i'm pretty sure that should be relatively close to man relatively close not quite on it so let me make this zero again and as you can see here we have a little version of pac-man we might want to make the mouth a bit bigger so maybe make that five and make that one as you can see there now we have a pac-man you can play around with this this is mostly just kind of playing around to figure out what you want so you basically give it x to y the radius and remember the radius is basically from middle to the outer ball so it's from here to there that's 60 pixels that part right there and that's there that's also 60 pixels now let's try and move pac-man so notice here we have pac-man let's try and move pac-man here forward because that's what pac-man would usually do it will move forward to eat maybe that little square there so we can even put that in front of pac-man so you can eat it so let's see here let's make this x axis 600 and to 200 i'm pretty sure that should give us what we wanted we go here you'll notice these are square for him to eat if we were to just make it a little bit smaller so maybe instead of six hundred for remains of a hundred i mean we can go to 70 and 70. so let's see that's a nice square for it to eat okay now let's make pac-man move so here i'm going to create a little variable just a table basic lua so we can say pac-man and we can make pac-man equal to nothing you could of course go underscore g dot pac-man that will also make it a global but it's optional both of these are considered global i'm going to do the underscore g pacman i'm going to say pacman dot x is equal to let's say let's give these x and y values here so let's say 200 and then pac-man dot y is equal to 250 okay now we have pac-man's to locations so this is basically the same as saying x and then 200 and y and then 250 and that should be an equal symbol it's basically the same as saying that we just split it up and do multiple lines so it's optional if you want to do that i like splitting it up into multiple lines like this and in here we can just specify the pac-man x and pac-man y so now pack my next and then pac-man y now if we were to run this everything should still look the same as you can see there and let's fill pac-man so so let's fill pac-man as you can see now pac-man is nicely filled now let's move pac-man so here in update we can say pac-man dot x plus equals one let's do that and see what happens oh that might be a little bit too fast so let's maybe and i think this should also be equals pacman dot x plus and then how many this might be a little fast we might need to slow down but let's see now it isn't so as you can see pac-man now moves until it eats this right here cool now let's make the square disappear once pac-man has eaten it so i'm going to go here and make pacman dot eat equal to false cool now i'm going to do is i'm going to go here and say underscore g dot food i'm going to say x i'm going to create an entire table just for food i'm going to make it equal to 600. there we go now here i'm just going to paste food x here and now i can say if pacman dot x is more equal to food x and let's maybe add a little bit just a pacman can move a tiny bit before it actually disappears so maybe maybe 20 pixels so if pac-man x is more equal to this value here then we want to say pacman dot eat is equal to true and then here we can say if not pacman dot eat and still this basic lure if you do not know what i'm talking about here you should go watch my tutorial on it but i'll explain anyways so let's first see what happens before i expect so here we have a little pacman that's now moving trying to eat this little square here once it gets to it it disappears because pac-man just ate the square what did we do in this small game program we made so first we set the color of the background which doesn't necessarily need to be this then we created a pac-man table and pat that pac-man table contain the x of pac-man the why of pac-man and if pac-man has eaten the why in this case isn't that important because we didn't change it the eat is important because if pac-man has eaten meaning if he has eaten that pellet that will basically make us not droid because if this is true then this will not execute because not means if this is not true then execute otherwise if this was false it would be if this is not false as you can see doesn't work that good but you could also have swapped it around and say true and in here you can make false and in here you could just say if pac-man.eat or you could have actually given it to this food variable here instead so instead of doing that we can just say underscore food eaten and we made it made this false and then we could have made food eaten equal to true here and now if we were to now we can just say food eaten this will also work perfectly fine so yeah that's the basics of that whole part there as we run this you'll notice it'll works yeah my bad and just turns out because there's a global variable it doesn't really know that too well we might so it thinks we're reassigning or we're kind of creating any variables i'm just going to put underscore g here so it knows we're talking about this variable right here and this should be not my bad because if it's not eaten if the food is not eaten we want to draw that food so that's what we're doing so if it's not eaten we want to draw it but as soon as it is becoming eaten then we don't want to draw because it has been ate or eaten i didn't know how to do that in the previous 10 or past tense very weird and here we just move pac-man here we check if pac-man has accessed that food palette and in here we just save it should be drawn and in here we draw pac-man so just pac-man pac-man moves and pac-man is about to eat this palette here as you can see but it's a very boring game because we're not doing anything so why don't we move pac-man i'm just going to delete that and here i'm going to go underscore g dot food and i'm going to make that equal to an object and then i'm going to say food dot x and here i'm going to say food dot eaten and now these two are both objects but just to show you that both of these methods work let's actually go x is equal to 600 and eaton is equal to false you can do it like this as well there's nothing wrong with that now here we can say food dot eaten and here we can also say food dot eaten the game should still work perfectly fine at the originals just say and here as well so both of these two places food dot x now the game should still work perfect fine if you run it if pac-man reaches the food palette right here it should pop disappear let's see it does cool now why didn't we try and move pac-man ourselves because currently we're just moving it with every update which is kind of boring so let's try and move it ourselves so first i would like to be able to tell pac-man when to move forward now usually pac-man will move forward on his own but i would like to move pac-man forward only when i tell pac-man to move forward so let's do that so here i'm going to say if well if love dot keyboard dot is down and i'm going to say the letter a then i want to basically move pac-man backwards so pac-man oh no package pac-man dot x is equal to pac-man whoa pac-man dot x minus one and that's your b dot x i'm going to comment this out just so that doesn't or literally actually deleted we don't need it now basically we're saying if this if the a key is pressed because if it is down if it is being held in us down do this that's what we're saying so if the keyboard key is pressed there are better ways to do this than what we are doing but for right now i think that's perfectly fine it's not too complex or anything now pac-man doesn't move but as soon as i press the a key it moves backwards which we didn't really want when we want to move the other way around so let's actually go here and say else if and we can just copy this and let's put a let's put d there and d will move pac-man to the right so we want to just copy this and paste now we can just make that a plus now you'll notice we made pacman here we added that dot wider so we want to move pacman up and down as well so let's actually do that i'm going to copy this i'm just going to paste it two more times and i'm going to make this w i'm going to make this s and now we can say y instead of x like that and this should be minus and let's see what we get now we can move pac-man any way we want because we have control over pac-man that we try and eat this pellet still gets eaten but you'll notice if i press a and w at the same time it doesn't really move up and down at the same time or up and lift at the same time so how do we get around that problem well we just instead of saying else if we just say if and we just add our end here as simple as that so just end and here we can say n so we just add our individual if statements here so instead of saying else if where any one of those can be executed we're saying if meaning all of these can be executed all at once without a problem so now if i run this you'll notice i'll be i'm able to move up and down at the same time as you can see right here and that's something you should take note of because sometimes in your game you want that to be something you can do but sometimes it's not so you should be able to kind of figure out which one you want for your game so you can know if you use an if statement or an if else statement cool now what if you wanted to rotate pac-man because i know the real game nearly rotate that much he either looks up or he looks down but what if we wanted to do that what if we wanted to rotate pac-man slowly if we want to move around because let's say this is the normal pac-man we want to make pac-man that can kind of just do everything well that's not too difficult so you'll notice here we have two angles the x angle and the y angle under the x angle is just angle one and angle two now we need to change this if you want to rotate pac-man because you'll notice if we make this two and then this 6 pacman will know it will now be rotated as you can see right like that so let's try and rotate pac-man so i'm going to go here and say i'm just going to copy this really and now i want to if the user presses down so that's the down arrow on the keyboard if they press down we want to basically take this angle right here and we actually want to modify it a little bit we're going to create maybe this pacman right here and say angle i'm going to just say angle one is equal to one and then angle so two is equal to six and that's what we have here so one and actually it's one and five dollar one and six my bad like that i'm just going to attach it to pac-man here so pac-man will now have two more properties angle one and angle two like that now what we want to do is we want to change the angle one and angle two here so i'm gonna go like that and let's do this and maybe make this angle two now let's just try and see how that works now we can't really see anything happening and actually you know why this didn't work it's because we never assigned angles here my bad i should have done all of this from the start so there we have angle one and now we can make this angle two now if we were to run again as you can see pac-man spins but it's a little fast you know because we can't really control where the spin ends it's too fast for us to actually figure out so what can we do to make this slower well we can actually just say math dot pi times dt and this is where dt comes in real handy because now we can slow it down with a lot as you can see here it's a lot slower now if we want to basically add more here we can just say else i'll see if i mean i'm just going to copy this and i'm going to paste it here i'm going to say up it's going to contain the same code but now instead of being plus it's going to be minus meaning i can now move either up or down as you can see here everything still works currently the angle you keep it on it won't really change which way it moves if you press w s or d but you could change that if you wanted to but it's a little bit more of the scope of this video but yeah that's some basics on actually moving things in your game using your keyboard so getting keyboard input there are multiple ways to get keyboard input and we're going to discover those ways when we create the games but for right now i think this is more than enough to cover what we need so we have a little character here and we want to make him run around just like that this is an image and we basically want to load in an image and make it move so if you want to ask it you can come to www www.gameart2d.com and just this link right here i'll paste it in the description and you can get it right here and yeah let's actually show you what you'll get so if you go here you click on free download then you just wait for it to download boom it's downloaded now you can open it up then if you extract it like i did right here i just extracted it by clicking right and extract to you will probably have the same option on windows if you don't i just recommend you go to windows and or you just download winrar or 7z or whatnot then you'll find a png file and you'll find all of these well the thing is i didn't exactly use these like that what i use is more something like this as you can see it's just one image with a bunch of them in one this is what i like to call a sprite these right here they're to me they're images but they're actually sprites as well now you can of course just go online and find a way to like add these together i believe there is like a sprite option or a sprite website where you can just go and create a sprite from images otherwise you can just all right so let's get started let me just close this browser now i'm going to just delete all of this because we didn't need it and all of this as well and which is very long so we just plop that and then all of this as well cool now first we want to set up some of the core basics we'll need and also actually i noticed i left myself a comment here so if you want to also make the sprite yourself instead which could be good practice if you want to make your own sprites then you can go to this code check dot io image sprites sheet generator and you just browse images and throw them in here that's this is what i used i completely forgot about it anyhow now here we first need to set up some basic information about the character jack because that's what i like to call my character but you can call main character or character or whatever you prefer and we're going to make that equal to and x is equal to zero and y is equal to zero so this will be x and y position of jack and you're going to go underscore g dot to jack to specify jack or you can go here and you can say quick fix and they just disable by diagnostics like that and this is a vs code thing where basically it won't give you an error if you use the lowercase global like this about specifying underscore g okay now you'll notice i have a sprites folder in my directories if i were to open the containing folder there's a sprites folder inside the sprites folder is a spritesheet.png this spritesheet is just what contains the spriteboy over here jack now we need to go inside of sprites and we need to access that so for the character i'm going to also give him another property called sprite to specify this is its sprite it would be love dot graphics dot new image and here we can specify sprites as you can see there so sprites and inside of it we have a spritesheet.png i'm going to rename that and then just copy the text i don't actually remember just copy the text and i'm going to paste the text here this will basically get that entire sprite for me cool now that we have that we can go here to underneath jack and here we need to set the sprite's height and the sprite width so if we were to go back here and we click on this sprite right here i know can we see the properties it doesn't seem like it if i click here nope let's open up my containing folder properties and let's say image as you can see this is the size of this image so 500 5352 by 569 nice now i'm going to copy that and i'm going to go here to my main.lua and just paste it there in a comment because that is the width and the height so i'm going to create two variables to specify the sprite width and height so sprite width and then also sprite height now of course you could have also if you wanted to say sprite and it makes sprites equal to a table so a table within a table then you could have said image is equal to this right here is perfectly fine as well and then you're going to specify the height and the width in here as well you could have done this this is perfectly fine but i don't want to get too complicated with tables because they can be kind of become kind of complicated so for now we can just do that okay cool right now here we just say it's equal to and now we can say 5000 or you just copy this part here paste it here and then just change that into a comma and now this basically says the sprite width is this and the sprite height is this you're going to split them up into two lines but i like to do it in one line just because it's fast so now that you have that you can go to the next line and we can finally start working with the sprite in a sense so we need to specify a quad of the sprite's worth so quad is basically let's say right here this one block here that would be a quad this would be the next quad this would be an x squad now they have to be equal with support otherwise you might get confused and might have a lot of trouble so make sure that they are and what not so here we can just specify the quad stuff so quad width is equal to 669 nice and how do i know this because i actually measured this beforehand if this is 669 per quad i don't know how to measure it versus it did ask me how to do it i can't remember for the life of me and then you have your quad height which will be the same as your sprite height why because as you can see all the quads are the same height and they're the height that the entire sprite is so like that and you could have also put this in one line but i wanted to show you you can do both if you wanted to now what we need to do is we need to basically specify for each quad now you could have just used a bunch of images like they pre like they gave us inside of the folder or if you use what i use you need to do the following you need it for every quad you need to specify to love got graphics dot new quad and here you specify the location you start at this will change the height of it won't change but the width of it will change so we're starting with so this starts at zero zero so meaning it starts right here right there and then we want to specify the quad width row and then the quad height and this will just specify the height and width to get it with for the quad and you need to specify the entire sprite width and sprite height cool now for the next one we need to basically specify the quad width and everything else stays the same and now we need to do it again and again and again and you can already see where this is going you're going to have to do that a ton of times and it's going to be a bunch of copy paste code which we want to try and avoid as much as possible so an alternative way to go about doing this is by creating a quads table like that and then creating a for loop so for i and then we can just remove all of these because i need it is equal to one two and how many was there let's see one two three four five six seven eight there are eight of them so one two eight do the following and we want to just copy this paste it in here and now we can just say quads at index of i is equal to this but now what we can do is we can specify the quad width times i minus one so it starts at zero so by default if it's one minus one this will be zero so then is the quad with time zero which will return zero because anything times zero is zero so just what we had but now it has times so now once this is two so once i turns into two those will become one so there's a quad width and then we have our quad worth times three and whatnot i can actually show you an example of how it would have looked if we didn't do this it would have looked like that so let me actually just do there it would look like this but instead of it looking like this we used a for loop to make it shorter so this quad with part here as you can see it starts with zero then it starts with itself it starts with times two times three times four that's what we do here so quad width times zero one two etc so we just saved ourselves tons of time because now if we just add another quote to this sprite we can just update this as simple as that now we want to draw this sprite to the screen so let's go here to draw and here we can say glove dot graphics dot draw jack because jack is our character right here and we're going to specify the sprite that you draw and you want to specify the quote it should draw so once and let's just specify one for now and then we're going to say jack at index x and also jack at index y to specify the location of the sprite now if we were to run the oh this is in python this is love now in order to run the program we get a massive dude right here but that's a sprite with every sprite this will change because the sprites will provide animation the remakes that sprite 5 will get this where the fit is now moved so yeah now that we know we can do this let's first make jack smaller because jack is very big so let's say love.graphics.scale and we scale it to 0.3 this will just make it back a bit smaller by scaling all of the graphics to 0.3 so now he's about 0.3 is original size of course you could have also just made the sprite smaller by actually when you make the sprite making it smaller but in this case it's not really that necessary we can just say scale it and it will be scaled next up we need to work on the animation and we can create another thing here and yet again you could have put all of this inside of here i just want to show you can make them different but usually you do want to actually keep them in here but that's not too important for now now we can also create an animation option here and this will be its own table it again you could have made this into this table here as well would have worked perfectly fine now i want to specify the direction he's going to move in so if it's left or right so direction is equal to and then here we can specify something like right you also want to specify the if he's standing still or not so idle is equal to true so that means he's standing still he's not doing anything this will allow us to kind of give him a specific position so let's say that's the idle position so if he's standing idle he should look like this if he's not idle he should be moving through the frames then we can create the frame and we can make that one and we can create a max frames and that can be eight so these max frames is basically just the amount of frames we have here this also means now that we have implemented this max frames here we can go and say jack dot animation dot max frames instead meaning if the max frames here ever change we can just not have to worry about it here we can also change the speed so if you wanted to faster or slower and we'll make that let's say 20 for now and then we also have a timer and we can make that 0.1 and this will just determine when the character should change frames so the lower the timer the more the character will change frames all right so now that we have all of this we can start implementing it so first here we want to say if and not jack.animation.idle then basically if he's not idling then we want to say jack that animation the timer is equal to and it is a very long thing to type so i'm just going to copy that and boom and now here we can say plus dt and this will just basically be our timer it will determine when the frame should be changed and here we can see if the timer is more than 0.2 then the timer will be set to 0.1 again and here we can basically specify the character everything so let's first specify just iteration so we can just see the character move so if i'm just going to say frame is more than the max frames then you want to change this frame back to one so just like that and just say frame is equal to one now that we have this in place we can go above this line of code and we can say frame and we just make that equal to itself plus one so this will basically change frame every time the timer hits 0.2 and then once this frame is higher than 1 or higher than 8 which is the max amount here we're going to change it back to 1 and it's going to start over now instead of saying quads here we can just say and frame there we go so now we just specify the frame it on instead of an actual number meaning if we right now we'll we're supposed to be getting a moving body and that's because this is if it's not idle and currently our character is very much idle so let's just for now set the idle to false so you just see a move around as you can see we get a moving body we can't really yet do anything because we haven't given them any way to do anything now that we have that what we can do is we can implement key presses so in the previous video i did show you how to do key process so let's quickly implement that so if it's not idle actually we don't even need to use this if statement we can just say if love dot keyboard [Applause] dot is down and we specify uh let's say b if we specify d the idle should become false so we can just specify this idle and you can make that equal to false because this is being held down meaning it should move and then we can say else well let's go i'll save here and we can copy this paste it here and we can make this equal to a and we'll do the same thing so both of these will do exactly the same thing for right now and in here we can just say true and if none of them are like that we can just say else copy this and make it equal to true again like that so now this we can make a move by specifying or by pressing a and d but you'll notice it doesn't go back to the idle frame that is because we need to set the idle frame so when this doesn't get doesn't get clicked we should set the idle frame so basically here we can say frame is equal to and we can just specify one because if we're not going to press it we can just say one because then by default it will be that one as you can see now goes back to his idle frame pretty simple so yeah so if none of these two buttons are clicked then we're going to set them to idle and set this frame to one we can maybe go here and here we see the directional right so we can get the direction and here we can say control c and just change this to direction and we can make this equal to right and here we can make it equal to lift now i can use this direction here to specify where the character should be moving to so if the whoa direction that is right so if the direction is equal to right then we want to change the characters go to so we can say jack dot x because i want to change the x position of jack is equal to jack dot x plus anyways i just paste that or damn it jack dot animation dot speed so basically we'll move further to one side depending on where don't do that depending on the speed we put here cool now we want to check if it's left as well so here we can see else and we just copy this if statement paste it here but now we can make this if it's lift and we can say minus like that now if we were to run this you'll notice we can move forward and we can move backwards that's pretty neat if you want them to move forward or backwards faster or slower then here we can make that 30 that will make it move forward or backwards faster as you can see here so the more you make this the more you increase this the faster it will move so you can see now it's running i don't have enough frames to cover running that fast interesting also i forgot to mention if you're seeing a very bad frame rate on your end just think that i am filming at 15 frames per second so you might see bad frames compared to what i see anyhow now that we have that we can start making the character switch which way it's looking towards because currently the character just runs forward backwards like it's not normal to run backwards like that so we need to make the character start looking forward so i'm just going to turn down the speed here to something like 20 again now here we can basically just get the direction so i'm going to just copy this if else statement and now we can just draw the character let me just actually do an lc really need the rest now we can just draw the character to the location we want it to so if it's right we can just draw him as normal but if it's lift you have to do a little extra to make it him turn because currently he doesn't turn so we have to flip him because here he's only facing one way so we need to kind of flip him around to make him face the other way so right here after you specify the y position you can specify zero negative one one quad width and zero now you might be wondering what are all of these numbers so this first zero here this specifies the radius or the radians not the radius this will make the character maybe let's say flip himself so instead of looking like that he's facing upwards or facing downwards that's this part right here that's the radiance this part right here is the scale factor to the x-axis and the scale factor to the y-axis so this will make him flip other way around this right here is the offset as well as this this is just to make him not look weird while we're moving around and i'll shoot her in a second so as you can see we can now face two ways so let's start with the first part if we go here and make that 20 for example 20 radians not radius but radians actually yeah as you can see it goes crazy so once we switch him around he's facing a way different way i find it very funny actually but yeah that's if we change that because that will just kind of swap the way he's looking towards so whenever we don't press d it's going to flip him around this is just to keep him looking to his specific location so if i were to remove that negative there is going to look the same way no matter which way we turn actually he's just going to disappear oh wait that actually just moves him to the okay very confusing but yeah that just moves him backwards so i guess my origin offset i got that wrong probably anyways i can make that back to negative one but you'll anyways notice he didn't while i was doing that so as you can see it didn't quite turn either so if you were to make that one it's not going to turn [Applause] you're going to kind of run the same way looking just from a different location as you can see right there and of course you can play around with like making 0.4 seeing how it turns around now could be interesting see here yeah now he just looks weird but basically he's being flipped so he's literally being completely flipped around you can play around with that use x to kind of play around with it a bit more to understand how it works i'm going to leave it right there then here is the same thing but just the other way around so if we were to minus that now we will do the y-axis as well so boom yeah yeah i probably bring him down otherwise we won't be able to see him but yeah that will do it on the y-axis as well and here we just specified a quad width to kind of get the offset and then the height so these two aren't too important we just need to put them there to so it knows where to put it and right now we have created a character they can actually walk around and do as he pleases and hello everyone if you're enjoying this tutorial please leave a like and subscribe for future tutorials you probably want to see what game we're going to build so let me just start up everything here and then boom now here we go so this game is called save the ball it's one of the very first games i made when i try to create a game so we have things like the fps counter which is optional we have buttons and yeah now if we click on play game we basically get this we get a little counter here that counts how many points we have this is based on seconds so every second will give you a counter or a point and then if you have enough points then the next level will start where there appears another ball now these balls will get faster and faster with every level you make for an example in this case the first level is 15 and in the second level i believe is 30 yes and there's another level at 40 i believe and one at 60 and it just goes on but it's very basic game but it teaches you things like collision detection how dt works how to get the fps how to draw shapes all of these things in live 2d and then you also have your game over screen with your score and you can say something like replay if you want and yeah so that's the basics of the game now if you do ever fall behind when trying to play this game you can always just go to well my bad and always just go to github and this link will be in the description if it's not just send a comment my way and i will remember to put it here now in this repository here youtube projects on you can go here to lua and inside of it you'll see a folder called save the ball and in here you'll have multiple parts we are on part one so everything we're going to do is going to be right here so if you fall behind or you want to maybe go check you can come here and part five would be the end result okay cool so let me just move this back to its original screen now here i have an empty folder i'm going to create a new folder and call it save the ball you can add spaces and whatnot i'm going to keep it spaceless and then in here i'm going to create a new document or a new file and call it main dot lua now what do you need to know before we start with this tutorial you need to know love 2d i have a whole course on it with like five parts or something that you can watch if you have not seen those tutorials yet i do recommend you do because they are going to be very useful you don't have to see them we are going to cover a lot of things from the start but i do recommend you do because it will just make things so much easier if you do and you also need love 2d installed of course now i'm going to open up vs code in here but you can open up whatever you want to i just prefer coding in vs code so here's vs code i'm going to close this and here you see there's my main.lua file i created cool now let's start by first importing love so i'm going to say local love is equal to require and love now we can create our free functions we need so function love dot load and this one will be love dot update and this will be love.draw cool so as we know this will load everything we need into the game this will update the game and this will draw everything to the screen now we can save this and we can also open up our terminal which i'm going to move to the right and now here we can say love dot so it does nothing love2d here does it absolutely nothing so what i'm going to do you do not have to do this but i'm going to do this if you can just there we go i'm going to create a new file call it conf lua you do not need to create this configuration file i however am going to create it because i would like my window to pop up on the second screen but you do not have to do this because you might not have a second screen so t dot window dot display and we can make that equal to two this should open it up on this window now we can just run love dot again and there we go cool now we can go back here will you close that con file because i don't need it open anymore and yeah so first i would like to set the title of the window so love dot window dot set title and we can set it to save the ball now you could have also put this inside of that conv.lua file if you saw my tutorial on it you do know how to put it in there although i want to show you the alternative methods of doing these things as well so i'm going to do it on the unload of this love2d game but if you want to you can do it here as well because you can do this windows display thing you can also do it in here you can do the windows resizing in here you can do everything you want in here first thing i want to do is i want us to be able to see the cursor because if you might have noticed or as you may have noticed if i were to just move this back here our cursor is a little white dot that's our mouse i want to code this into the game right now so let's do that let's code it in so first thing we'll need is we'll need to get the player location and that's going to be that little circle so local player is going to be equal to this and you could also put this inside of this load it's optional then we need three things we need the radius going to make that equal to 20 within an x position and we're going to make that equal to 30 and we didn't need a y position and make that equal to 30. you could make this equal to zero as well it doesn't matter i just like doing 30 because why not cool now we need to basically always update that position so player dot x and player dot y is equal to the love dot mouse no mouse dot get position so this right here returns two values it returns an x and a y position value so we're storing the first value we get into this player.x which is this and the second value we get in the player.y which is this and we're doing it on every update because every time the player moves the cursor an update will occur now you may also notice if i move around things might look a little bit laggy but that's just because i record on 15 frames per second because that's just what i'm used to and that's what i edit in so things may look a bit laggy but on your end it should not be looking laggy you should be getting about 30 to 60 frames a second all right now here with the draw we want to basically draw the circle so i'm going to say love dot whoa love.graphics.circle i'm going to go fill method player.x player dot y and also player.radius this will draw a circle where the players x and y meets up and give it the radius that we specified up here now if we were to run this we'll get a massive circle now this is the end game circle because we will have two states that we're going to be using right now and those two states will determine the size of the player so i'm going to create a local game and it's going to be a table with state now we have a menu and we're going to make it equal to true now this will be the states we have so for example we have a menu state if it's a menu state it's going to show the menu if it's the paused state it's going to show a paused menu now here i'm going to make that paused i'm going to make that running and i'm going to make this ended now currently we want all of these to be false and just this one to be true that's for the menu state now down here we want to specify when should it do what size so i'm going to go if game.state and we declared state up here and then in square brackets and in quotation marks i'm going to say if it's the menu state so like that and you'll notice right here so if it's the menu state then we want to basically do this and we just want to divide this by two so if it's the menu state then it should be the normal circle size but just divided by two i'm going to create another if statement but this one will contain a not menu and there's a reason i don't make this an if else statement and you'll see in the future tutorials so here i'm just going to remove that divided by two now if we were to run the game we'll see this a much smaller circle that's because we're in the menu state so the circle is half its original size now if i were to change the menu to false and then the running to true and my bad these two should be running not menu and now that we have that we can actually just move that so divided by two and we can just move it to the bottom one so now the first one is if the game is in its running state meaning if the game is being played give us the full circle otherwise in any other state have the size of that circle now if we run it we get the full circle because it's in a running state any other state we switch to no matter what the state if we make this false and we make the state of paused to true then you will get the smaller version as you can see here so any other state will give us a smaller version but running will give us the big version cool so i'm going to change this back to menu state now we need to hide the cursor because as you can see the cursor is still there we don't really want the player to see the cursor because it breaks the immersion a little bit so to get rid of the cursor what you can do is here in the load we can say love dot mouse dot sit visible and you can make this equal to false this will make the mouse invisible so now if you run it there's no longer a mouse i can still move around but the mouse itself is gone now one last thing i do want to implement is the fps counter because let's say you're getting very bad fps you might have coded something wrong or something like that because the fps should usually stay at around 60 fps may be falling to 30 but usually it should be 60 fps and we need a way to see that so here at the top because we don't want the fps to block anything so it should be at the top so it always if there's something above it the fps doesn't go over it anyways we can say love.graphics dot print print f and here we can just say fps colon and then dot dot is to concatenate the string love the timer dot get fps and should be timer not time and there we go now if we run it we actually get an error and i believe we get this error because we should specify our font so love the graphics the new font and i'm going to specify 16 and let's specify an x and y location as well so let's go 10 10. so now we have a font size of 16 at its x and y position is 10 and 10. now if we run it we still get an error and we just also need to specify where it stretches to so graphics dot with should work perfectly fine and this should be get with [Applause] as simple as that now let's hope for the best there we go we get a little fps as you see it's around 60 fps and that's where it's good okay cool now just because i have a little screen space i'm going to just split this up into multiple lines like that okay so first we display the fps then we set the font size then here is the x position which we can keep here is the y position which i would actually like to go love.graphics but get height -10 and this will put it at the bottom of the screen which is what i want i want the fps to be at the bottom of the screen just because it's less distracting let's make that 30 just to make a bit bigger or a bit higher and then this basically sits how far as you go before wrapping the text now if you were to run the code or fps is here at the bottom and that's the very first part of the tutorial we didn't do a lot we just basically set the game state created the player created the load the update and the draw functions where the load sets the window title it makes the mouse invisible it then we get to update and it updates the player x and y positions then on draw it puts the fps on the screen and if the game stays running it draws a circle with the player x and y position and also the player radius if the game state is not running then it does the same as with running but it divides the player size in two so first of all this right here this doesn't need to be here you can actually remove that it will run perfectly fine because by default love will import it but i'm still going to keep this here just to get all of those errors away and whatnot so yeah that's up to you before we continue right okay so in this tutorial we'll be creating the enemy the enemy that's going to kind of run after us and whatnot so first i would like to give our game another difficulty and difficulty is going to be set to one now the higher the difficulty the more difficult the game should be as simple as that because the speed of the enemies will be kind of dependent on the difficulty as well and then we want to create a table called enemies now this table isn't going to be used too much in this tutorial because we're only going to have one enemy but once we get deeper into it this table will be containing all our enemies so all our images are going to go in here we want to basically create our enemy object now we could do it in here but this file is already going to be very big by the end of this tutorial so we want to create a new file so right here we can right click new file and say enemy dot lua i'm going to give it a capital case e in its name to signify that this is an object it's completely optional it's just how i like to do things if you have seen my tutorial on oop you will probably know there's multiple ways you can do oop in lua and the first way is by using a table the second way is by using a function and a table and there are more ways but i don't like covering the others because they're kind of difficult to understand in my opinion so in this case i'm going to use functions and tables because i found those to be the most easiest to use so first we want to import love so local love is equal to require love cool and we don't need a semicolon we're going to create a function and we're going to call it enemy and this is going to be our object for enemy now at the end of this function we want to say return enemy and this will basically export this enemy when once we import it here so this will be exported this function right here it's just so this massive function won't have to be found or used or what not in here we don't have to see that massive function in here because it will be stored here so let's start with very basics we're going to return and whatever is inside of this right here this table is going to be what's returned to us so first we want to set the level to 1 by default the radius i'm going to make that equal to 20 then the x is going to be equal to and there's going to be enemy position for in case you didn't know i'm going to make it let's say negative 10 and the y is going to be equal to negative 50. so basically this is where the enemy's start position will be we want it to be off the screen and we're going to randomize where they actually come from later but currently we want it off the screen because it should kind of just pop into the screen as the level goes on so more enemies will pop into the screen cool so these are the first few things we'll use next up we need to basically create something to draw the enemy and something to move the enemy let's first try and move the enemy because that's probably going to be the most difficult part here is just moving the enemy and that's going to be a function inside of this table so it's a function inside of the table and it's going to take in three items self it again in my oop tutorial because it's going to take in itself we also want to get the player well player x and the player y you could pass in the entire player in here but i feel like that would just be kind of a waste of memory on the end of the day because you're going to pass an entire object where you can just pass into values so i do recommend passing in these two specifically just x and the y cool now basically we want to make the enemy move to the player that's all we want to do very simple we just want the enemy to go towards the player so it will just be basic if statements so if player at index x or at x position minus self.x and this self is referencing this x right here so where the enemy currently is so if this value is more than zero then self dot x is equal to self dot x plus self dot level so what we are doing here is basically if the player x minus self.x if that is more than zero and we're minusing here because remember x starts basically at this point right here all right so future steve here and you know i realized this wasn't a very great explanation so let me actually explain it better let me just move this to number three so here we have basically our example it's saying if the player at position the x position of the player minus the x position of the enemy so let's quickly go here and just draw it out let's say the enemy is a red square so this is the enemy and but actually let's make it a circle otherwise things might get confusing okay so the enemy is a red circle and the player is a blue circle just draw a nice circle here there we go now here we're saying if the player at position x okay so the x position of the player would be there at the center that would be the x position from here all the way up until there that's the x because remember x goes from side to side that's x while y goes up and down so this would be y so then so just remember that so this is the x right now we go here if the player x and yeah so the player x minus self.x and self.x would be the x of the enemy so this right here this would be the enemy x that's self.x so so this is the enemy south.x right there now if we go here we see if it is more than zero okay so let's put this in a mathematical perspective so let's say this right here is 63 the x position of the player is at position 63 and the x position of the enemy is at position 10. now we are saying player at position x okay so it's 63 whoa just 63 right minus self.x okay so it's minus 10 because remember self.x is the enemy so it's 63 minus 10 right and that will give us 53 so the position between this point and this point is 53 that's basically what's being said here and then we're saying if it is more than zero and we know 53 is more than zero then we're saying self.x so this would be the player x or the enemy x not the player the enemy x is self.x everything self.x is equal to itself okay so self.x which is 10 let's let's make this red because we're talking about enemy so it's 10 because we're saying south of the x okay so it's going to be itself so that is self.x plus its level so plus one so we are saying south to x becomes itself plus one so now self.x will be eleven because it will now be one step closer to the enemy meaning this right here will minus with one because 63 minus 11 and remember we got 11 with that quick equation so that would be 11. so 63 minus 11 is equal to 52. so that will basically close the gap between the enemy and the player this might be a little bit confusing to work with like a lot of the math might be in game development but don't get discouraged because the more you work with it the more you'll understand it so yeah back to the video now we also want to do the other side of x so let's say the enemy is to the right of the player so else if and then player [Music] minus self.x is less than zero so basically if the player x minus south.x because remember x becomes more the more you move right so if the enemy is more right than the player then this will be a negative value this will return something negative so basically let's say this right here that's the enemy and this right here oh yeah no not yellow we can't see that okay and this right there that's the player now let's say on x so x here will be equal to two and x here would be equal to five and remember this is the enemy that's why he's red he is bad he's the enemy bad uh so let's just full screen this so here we're saying if the player minus self.x is less than zero so if the player so two minus self.x and remember this is the enemy so minus five is less than zero then do something now two minus five is negative three so this will return true so if the enemy is to the right of the player it will return true and with the top one if the enemy was to the left of the player it would return true if the enemy you know it would be the same thing just other way around now here you can just say self dot x is equal to self.x minus self.level because we want to decrease the exposition of the enemy if you're having trouble kind of wrapping your head around this don't worry the first time it can be a bit difficult i had the same problem when i first thought of this like oh well my brain can't wrap around this math many worry it gets easier and it's going to be the same thing but for y so if y let me just change all of these to y so if player y minus self.y is more than zero then sound.y becomes the same but plus it's the same thing now it's just why because if i were to open up drawing ooh and basically remember y goes down that's how y goes so this is the player and this is the enemy now let's quickly go check here so if the player y minus the enemy y is less than zero so the enemy y let's say is equal to ten because remember goes down so this would be zero and this would be something like 20 or something and here the y would be equal to let's say five now here we're checking if the player y minus south of y is less than zero so is five minus ten is less than zero and those two minus will be negative five right and that is less than zero then we're saying the wrong window self.y becomes itself minus level so it will decrease so now we're saying okay move this upwards so now the y will become nine because we're moving it upwards and this is going to run with every update so it's going to move the enemy closer to the player with every update so yeah let's continue to the next part then now you want to be able to draw the enemy so that was going to be another function called draw it's going to be function taken self doesn't have to take anything else and here we first want to set the color love dot dot graphics dot set color this is basically just going to set the color of the thing we're going to draw now i'm going to choose a very random number in order to figure this out so i'm just going to say 1 0.5 and 0.7 i just random number that popped up into my head remember you could have also gone like 255 divided by two five five and the same here you could have just gone what's the half of two five five anyway the half of two five five divided by five five so you could have done that as well but i preferred not to in this case and then we're going to draw a circle but you can make the enemy a different shape than a circle i just find it easier to do it as a circle so love dot graphics dot circle and we're going to fill this circle we're going to put our position self.x self dot y and it's make the size of self.radius we're going to draw a circle it's the x index the y index and then we're going to give it this size right there and we set all of that up here and then we can say love dot graphics dot sit colors we're going to reset the color back to white because that's going to be your default background color you don't have to set it here but i prefer setting it here then having to set it here in this draw function over and over and over it also just brings it also just prevents a bunch of bugs that could possibly occur okay so that's the most basic version well later on for in letting jump in from random size but i think for now that should be fine so this is the enemy now here we have our enemy object and now we can insert our very first enemy so here in load we want to insert our first enemy table.insert and it's just a normal insert method and i'm going to insert it into the enemies table right there and we need to import enemy now that i think about it so local nm enemy and you don't have to [Music] give it the same name i just like using this name and we're going to say enemy and it should be capital e that just means basically import enemy from this file right here that's what we're saying right here cool now that we have that we can go here and we can say at index one insert our first enemy and it should not have a capital e because we gave it a lowercase e as a name or we should have there we go lowercase e not capital e okay but you could give it a capital kc name i just preferred going lowercase cool and that will insert our first enemy now here an update before we can actually try and use this enemy we need to basically for i and yeah let's just like that for i equals one and let's also remove all that until the number of enemies and then do so basically for every enemy inside of this enemies table currently we only have one but later we'll have more we want to go enemies or enemy or yeah enemies actually and then add that index so index i colon move take note i'm using a colon here and not a dot and then here we can just pass in the player dot x and the player dot y why am i passing these in because here in move we're taking player x and player y we do not have to pass in self because self gets passed in for us cool so for every enemy do this you can also create an update function instead that will instead of just moving it it will do all of the updating for the enemy that could also be something you can rather implement instead of move but this game is so basic i'm just going to have a move function and not an update cool now we need to draw the enemy let's go here into if the game state is running right then we can go and above the player because we kind of want the player to overlap with the enemy or you could do this below it doesn't really matter that much and actually i'm just going to copy this because it's going to be the same code just a different command so boom and here i'm just going to say draw so this will draw every enemy inside of the table okay so let's try to run this and see what we get love dot let's see and you know what we're not getting anything it's because currently we're in the menu state and everything just gets drawn if the state is not the menu so yeah let's go there and just go false and in here we can just say running is true there we go now it should work as you can see there the enemy comes to get us but currently nothing happens so the enemy just stops once it gets on top of us but it should later on like stop the game if the enemy touches us but you'll also notice the enemy always comes from the same side and that's very boring because we want the enemies to actually come from different sides to make the game more interesting so to do that we can just implement a little bit of math and this map will basically make the enemy come from different sides now here at the top i'm going to declare four variables local well local dice local x and y i take those these have underscores you didn't have to give them names of underscores i just find it much easier to work with them if they have that and also local and an underscore radius cool these two are not going to have any values associated with them at the moment radius will make 20 as a default but you can later on they modify this radius to give you more interesting sizes so let's say you want different size enemies that could be kind of neat now this radius here i'm going to paste it here this x here i'm going to paste it here because it's going to be modified that's why here i'm going to paste it here so these are all going to be modified except for radius so these are all going to be modified and these two will now be set here and we're doing this because we want the self to work when we want these because this is not part of the table it's part of the function but it's not part of the table that's being returned to us but it is part of the function so this one won't be returned to us we won't be able to access these values but we will be able to access any values in here it's something you have to wrap your head around because it can be a little confusing okay and this dice will basically determine from which side will the enemy appear from this side from this side from this side or from this side so i'm going to make this a math.random and we're going to make it 1 and 4. and before we forget in the main.lua file not in the enemy but in the main.lua file we want to just math.random seed and we want to just give it os.time now what we are doing here is we're basically saying every second change the seed we're going to get so whenever the user boots up the game their random numbers will be more random because if we don't add this then the enemies will always start from the same place and we don't want that because currently as you might have seen in my random video the random numbers don't really change they kind of stay the same unless we seed them so at least we see today if you don't understand that yet i do recommend you go watch my tutorial on it i do have an entire crash course on it after all cool now we need to basically give the dice functionality so we can say if dice is equal to 1 then underscore x is equal to math.random so we're going to give it a random position on x-axis so anywhere random here so there we go master random and an underscore radius because we wanted to start at minimum at the radius so the the enemy will always be hidden no matter where it is on the x-axis and then love dot graphics dot get with and this will just further work so it will give us basically a random number between the radius so 20 and the width that's given to the window and then we want to say y well you can actually do it underneath y is equal to minus so negative underscore underscore radius times four so if the dice is one and it should be underscore y so if the dice is one x can be anywhere on top of this right here anywhere but y specifically will be minus the radius so remember it's going to basically be here so y 0 is here so y minus the radius would go up so minus the radius will basically mean hide the entire ball hide the entire enemy and it retimes it by four so it's going to be basically being hidden four times itself this will also give it a little bit of a time you have to wait once the knee level starts before it actually starts to jumping in so it doesn't just go into the screen immediately you'll kind of understand it once we actually use this so now we can just say else and i'm just going to copy this right here all right the little disturbance i can't remember where it was but okay yeah i believe it was just like this and boom okay now else if the dice is equal to two it's going to basically be the same thing i'm just going to copy these else if's because we're going to have kind of a lot of them except for the last one the last one will just be else and this one we can make three and in the else would be if it's not one two or three then just give it this one by default okay so with the second one it's going to be a little bit different it's going to basically be swapped so x will contain this and y will contain this and then here we can instead of say get with we can say get height now why is this like this why did we swap it around here well if the dice is two we're saying okay x should be here but it should be minus its own width so basically if you hide itself times four so it's going to go to the side here four times itself its own width and then with why we're saying okay find any place from this part right here right and just anywhere towards the height you don't necessarily have to specify this radius here you can make the zero because remember it's just going to come from a random side anyways but basically it's going to be able to come in from anywhere on this side right here so if the dice is 2 anywhere here if the dice is 1 anywhere here from the top if the dice is free however then we're going to do everything the same but this one right here this y we're going to say love dot graphics dot get height plus and then we're going to say radius times four whoa radius times four and known minus so why are we saying this here well basically we're saying okay if the dash is free x can be anywhere from this right here and they were saying okay the y should be the height plus itself times four so anywhere from below here so now it's not anywhere from the top but anywhere from the bottom like from here that's what we did here we basically set the y to the bottom of the screen and then here it will be basically the same as with if dice were two so we can do like that but now the difference is and remember to remove this minus symbol right there x here we can just go and well no no no no no we just go and do this and we can say love dot graphics dot get with plus and now what we're saying is okay it can come anywhere from this side right here so now it can come the the ball the enemy can come from anywhere inside of this window here not this one but the game window itself so you can either come from here the top which we did first or it can come from the side which we did second or from the bottom which we did third and or from this side right here from the right side which we did last and now these x and y values as its default values that that ball first gets comes from here and this will be different for every ball that's the nice part because every time we create a ball is going to execute all of this and then it's going to return this to us but this x and y would have changed and if you want you can also change the radius so yeah now if we were to run the game it comes from down the part down here okay let's run it again and see it's happening again oh attempt to perform micro let me go get field a function value so this is enemy 18 let's see here ah there we go that should have those little bracket things there okay let's see and it starts from this side now okay one more time just to make sure we have everything it starts from the top so that's the nice fact every time you start the game it starts from a different angle which is pretty neat and i believe that's all we need to do for the enemy all right so first we want to go to the main.lua file this main one right here and we want to make this true because today we're going to be working on the menu so we want to be able to see the menu by going here to true this shouldn't do much except for resizing your cursor so make sure it does now we're going to create another table here called local buttons and these buttons will basically contain all of the buttons for all of our different states so menu underscore state will then become an empty table cool now that we have that let's go here to the side and just create a new file i'm going to call this button.lua it's going to be a button class and this button class will basically contain all of the things we'll need for buttons so local love which you know is optional but i love doing this require and we can just say love you can create a function called button and yeah simple as that now we want quite a few things we're going to mess around with too many things just yet but i can tell you from the bat our button will probably have text on it and we probably want to set the text when we create the button so something like text and we also need to be able to execute a function so funk because when we click the button it should be able to do something and this func we pass in is what it should be able to do this befit isn't too great i don't love it that much but it does get the job done and that's what we want at this moment we also want to be able to pass an optional parameter into the function so we can include func param and then we want to include the width and then the height of the button okay so next we can just say return and it's going to return a table this table is going to return will contain width and that's going to be either the width passed in or 100 by default and then we want a height which will be the same thing so like that so either the height passed in or 100 then we're going to create a function and this function will be able equal to the function passed in to func passed in or to a function that doesn't do much it's just an anonymous function it's not going to do anything really i'm just going to print this button has no function attached as simple as that if you wanted to you can even make it shorter and make it one line like that it's optional you can make it more than one line i kind of like it as one line because it just does a quick one-liner and then the function parameter will be the function parameter passed in if there's no function parameter passed in it will be nil and no parameter will be passed in and then the text will be equal to text or no text then the buttons x position will be equal to zero the button y position will be equal to zero and then the text on the button should be at position zero and in the text y of the button should be at position zero as well exit this is a lot of things we're just setting by default you don't have to worry too much about it and then of course you want to return the button down here before i forget because i'm going to forget what i did now cool now we are going to do things like making the button clickable and what not but first we want to be able to draw the button that's the most important part right now because we need to kind of get the button just on to see that it works so let's create a draw function for the button which will be a function and this function takes itself and then the buttons x position the buttons y position and in the text x and the text y position now i might be putting it here like the button x y text x and x y might be able to be passed in from here so these four attributes but if you wanted to you could pass them in here the reason i'm doing this is if you ever later want to animate the button so the button moves from one position to another position then you can do it from here you can when you draw it you can just move the button x and y positions as you please instead of having the x and y positions kind of set as soon as you create the button this is personal preference i like doing this you don't have to do this you can put all this you can put it in here there's no problem with that i just want to do it here and this also keeps this from kind of cluttering up and having 50 000 freaking things it needs to take in because now it's being split into two cool now here we want to say self dot button x and of course you could have made this a table as well but we're already in a table so having another table within a table is just uh you know personal preference yet again and that can be equal to button x so this button x right here or self dot button x so this button x here or this button x here one of these two self.button x is referring to this button x don't get too confused here so just as i said you could put the button x and y up here as well and it will be set here as well anyhow and we can just copy that and do the same for y so self dot button y like that then here we can basically set the text position so if text x so the text does exist the x position of the text which is passed in here then we can say self.text at position x is equal to text at position x plus self dot button x else self dot text x is equal to self dot button x now you could have done this yet again in one line where you basically check if this exists and if it does exist you do your things you could have done like this but i find it to be a lot less confusing and just so much easier to also understand if you're reading it if you do it like this where if it's take six do all of this otherwise do this instead of using this or way here which could work but i'd rather not because that's going to be prone to bugs because i'm not too good at reading that and in here we could of course just go text y and in here we can say button at y like that now here we're just saying the text on the button so this is the text that exists on the button if the x exists so if this exposition is being passed in for it then make the original one here make it equal to what is passed in plus the button x we're saying the button x here because the button may be 30 pixels to the right but the text isn't just going to be placed onto the button automatically so we'll need to tell the text to add that x so we're going to have our button x and then we're going to need to add to it that's this here i'll show you how this works in a second once we actually draw this to the screen just so you can kind of get an idea now take that this bottom order i'm going to do here is very important so love the graphics dot set color and you can pass in your own color the color you want for every single button but in this game i'm just going to keep all the buttons the same color 0.6 0.6 0.6 is going to be a lightish color then we want to draw the button so love.graphics.rectangle and if you're still a little bit touchy on this right here don't worry i am going to explain it i just want to kind of give you something you can visualize when i explain it and we can just say fill it because we want to fill it so we don't see the background color of the game and then self.button x position edit x self dot button y self dot with and itself dot height so this is where the button is going to be drawn to and how it's going to be drawn okay so that's for the button we can do that now we need to do the text because remember the text isn't really part of the button it's its own thing so we need to do it here as well so love dot graphics dot dot sit color and we're going to make it equal to black now you can make it a different color but i feel like this color works perfectly with this lightish grayish color then we want to love dot graphics dot print and we want to print self dot text dot text and then self dot text at position x and then self dot text and position y taking it i'm using self everywhere because we want to on the end of the day use this everywhere in our code this will just make things so much easier so whatever you do do not change these values here or any values you sit in here try and keep it as much as possible to these globalist values we have here because believe me it's going to solve a lot of problems later and then we just want to reset this is optional you can do it somewhere else but i'm going to reset the color here to be white so that's going to be my always fallback default color it's going to be pure white okay we have created the button and the button can now be drawn now here we want to basically create the three buttons for our game i'm going to create just one button for now and here we have our menu state button so we want to create it on here so in the load we are going to go here so here with the unload i'm going to create a button or go to our buttons say dot menu state and then i'm going to add a play game button now this play game button is just going to be a button which we need to import let me go here and just import the button and here we can just make that button cool now here we can make that equal to a button which we just imported from this file we have here make sure to export the button by saying return button and in here we can pass in our value so first it wants text so here we can just say play game because that's what it does it tells you to play the game i'm going to pass in nil nil because we do not have a function or a function parameter we want to pass in then i'm going to pass in let's say 50 and 50. we're going to position it nicely later so they're going to be the x and the y position of the button now down here where we draw the game and here it says game.state running we want to add an else if because i'll save the game dot state is not running but instead it is of menu and this should just be in square brackets not circular brackets so else if the game state is in menu state you want to go to buttons dot menu state dot play game and we want to say colon draw to call the draw function and now in here we want to specify a bunch of things so i wonder if yeah there we go so the button x position you can get 10 the button y position 20 and then we can just maybe say 10 and 20 like that just as an example now it's not going to give us anything great and was this really x and y what was this this was oh the width and height so it's going to be 50 with 50 height and then here it's just going to draw it at position 10 and 20 and then it takes is going to be at position 10 and 20 as well but not the same 10 or 20 as this button because we changed that and i'll show you that in a second because i know it's going to be confusing and take note this is still here this if statement because if the game state is not running we just want the circle to be smaller so this the circle should be smaller in this menu state and the circle should be smaller in this running state or it should be larger in this running state anyhow let's go here and here we go so we have a button not really the best it's looking kind of uh kind of small there so let's just make the button a bit bigger so i'm going to go here to the above where it's 50 50. allow me to maybe make it a bit longer so 120 and maybe a bit shorter or bit less tall so 40. ah now that does look much better okay and then here we have a play game and you'll notice it looks kind of weird now if i were to show you something interesting here so what do we have here we have the button x and y position so let's make this text x and y position uh five and five just so i can show you what's happening because this is a kind of part that may have been confusing to a lot of you as you can see here it's five from this left side and five from the top now if we go back to button here and we don't do this we just say okay just make them what is passed in that's all you have to do just make them what we pass in or actually no let's actually uh change this up so go like that and we uncomment these there we go so if we just did this i want to show what would have happened so we just said self.txtx and self.texty is what we pass in then you can't see it because the text is now five pixels from here and five pixels from the top not from the but the button width and height but from the top so that breaks a lot of things so we change the text color then instead so here it's zero zero zero zero we can just make it one one one one then because all these finger pop-ups there we go one then you'll see it's right there now we don't want that right we don't want that that is why we have this right here this self.button x so even if you don't pass in anything it's going to be at the same location our button starts at so for example if i were to comment all of this back in again and i want to not pass in a text x or y which will run this then then it would look like this as you can see it's right there at the button but not at the game so that is why we did that we didn't want this to start here at the game we always wanted to start at the bottom x and y positions cool now let's make this maybe like a 1710 that should look good for right now yeah that looks perfectly fine later on we'll change the font a little bit to be a bit bigger and just match the button better but for now i think that's fine so yeah there's our first button play game let's create two more buttons and i'm going to just go here now this second button is optional because i'm not going to code it it's going to be your homework at some point so settings so it's going to be the settings button and then we want an exit game button and in this button we'll say settings and it's going to have the same width and height and in this button right here it will be exit game or just even exit if you want to and then the same with that and because all of them are the same width and height you could always make this the default width and height like here we redo this you can make it the default here that would actually be a good idea now we can code the exit button right now we can give it its function so love dot event dot quit because this will quit love so once the button is click it will run this code or we haven't really implemented that but that's what it should do take note all these are from the menu state because they will all be displayed once the menu state is true okay now we can go down here again and we can draw the game so let's go like this and let's draw settings and let's draw exit game and let's see how do they look what do we see like we just see x again because that's what's been drawn last so we can just make them all go down a bit we can leave the rest to be exactly the same so we can say 70 and here we can maybe like 120. and that looks fine to me at least so now we have a play game a setting and an exit game they don't do anything if you click on them but now we have that now we want to go back to the button because we need to make some function that will allow us to press the button to click the button so we're just going to check if it's been pressed and we'll do the press doing on the main in the main file so here we can just do something such as check pressed right and in this will be a function and here we can just put a comma cool now this will take in a self a mouse x position so where the mouse currently is the x position the mouse y position so where the mouse is currently at the y position and in the cursor radius so if we ever change the cursor size we don't have to worry about changing it here as well so cursor radius cool now here we can just create a basic if statement so if mouse of the exposure of the mouse plus the cursor radius is more or equal to self. button x so let me actually draw that because i think this will become a little bit more difficult to understand so let me just go like this so we are saying let me just make this a good uh easily seeable color on white so this is our cursor right and this is our button i'm going to say b for button now if i go here you'll see the mouse x position okay so the mouse x position and remember this is our mouse that right there is our mouse but remember our mouse if i can just make this red maybe our mouse is here in the center that is actually where our mouse is that's where the point of our cursor is you might have remembered from the first video where when we drew this you would have noticed the mouse cursor thing it's like that so that's her mouse right there now we go back here there we go we say if the mouse x okay so this is x remember so that's x and this is y so because remember this can make a y ish shape that's why this is y this is x so if the mouse x position uh let's go like that so this is the mouse x right so boom the mouse x position right there let's make that 32 for example right plus the cursor radius so this part right here this is the cursor radius because remember a circle not a good circle there a circle has a radius that right there that's the radius of a circle so take note that term is very important that's the radius it's only half of the circle so this right there so this x plus the radius and let's say this whole circle is 20 right so the radius would then be 10 because of the whole circle from here to here if that whole circle is 20 and the radius is half of the circle then it would be 10 that would be the radius so 32 plus 10 that's 42. so we're saying if 42 is more or equal to the button x position so let's say this button exposure which is obviously further than that circle so that would be this button x position which would be like this this is the button x position i should actually choose a different color at this point let's go maybe something like a bluish color i hope everyone can see that nope it's good like that okay so this is the button x the button x we can say is 62. this would then be false because here we're seeing if it's more or equal to more or equal to very important it's not more or equal to 62. it's 42 it's less than 62. so if we move our cursor so the cursor is let's say like this so you'll notice the end of the cursor is inside of the button this within remember that's the radius right and this is 32 and let's say i know this right here that part right there so from this point right here to this part right here is let's say 45 because remember it's not like a lot there so that's just 45 45 then we can just add the radius to it to get back to the middle that would be 10 so we can make that 55 right now we can add the radius again because we're at center or we could have just added this full 20 that would have worked well but we add the radius again we'll get 65 so remember this right here that's 45 this whole length here that's 45 because this is what number we made up at this point and then we add 20 to get the from this point of the circle to this point or two tens to get two radiuses because two radiuses makes one of the circle because the radius is half of the circle very important to know and then we get 65. 65 is more than 62. thus we are pressing the button we are now pressing the button that is what all of this math does if most of this math went over your head don't worry too much about it you can kind of figure it out on your own as you play with this but basically if a part of the circle no matter how big the apartment if a part of this circle we have goes over the button this right here that's the button then we should say it's clicked so that this is just for the ex right simple as that now we also need to do it the other way around so if we go back here and let's actually can we create a new no i don't know how to create a new one control n doesn't work okay that's fine let's say we have our this is our circle and it's our button so this button now we need to do the same thing but for if the circle does this so we need to do the same thing because we need to take note we should not press this button we should not be able to press it if our circle is here and the button is here or if our button is here and the circle is here we should only press the button when the circle is basically on the button a part of the circle should be on the button but if a part of the circle is not on the button no matter if it's to the left or to the right we should not click that button only if a part of that circle is on the button should we click the button and oh okay it did make more okay good to know good to know so now we need to do the same but the other way around so i'm going to just put that inside of brackets to make it easier to read and i'm going to say and start another pair of brackets and just paste that in here now we want to minus the cursor radius here and we want to say instead of more than or equal to we can say less than or equal to and i recommend if you kind of struggle viewing what we're doing right now if you're kind of struggling seeing what's happening draw it out drawing out math is probably the best thing you can do because that's what we just did and if that helped you to understand at least the first part then that means if you draw it out for yourself then you will be able to understand the second part as well and here we can just add with so plus with self-serve if that is now this self.with remember refers to the width of the button so let me actually draw this again to draw so here we're saying if the mouse x so here's the mouse and let's list this mouse x right right there that's where the mouse x is so let's say x is equal to 32. right if that is a less than or equal to this button x right so this button x right here it starts there that's where the button x starts right there plus the button width so it can even plus the button voice so this is where it's going to be this dot right there that side that corner if it is less than that and we're minusing the cursor radius because we want this point so if this point is less than this side right here then this x this x right there if it's less then we want to do something so if so let's make this blue so if this part right here is past this part so if it's more than this x location because remember x is just a position and this red part right here is below this red part right here then we want to call the button this can be very confusing and i myself got confused here but this math is very important to understand because in all future games this is how you can code a button anyhow so i hope you understood this part if you don't you can still follow along without any problem because believe me i'm also trash when it comes to math cool now we need to do the exact same thing or we want to do it for y so here we can just change mouse x to mouse y right and then we can do the same here button x but button y and instead of with we want to say height we still keep the cursor radius because remember it's just a circle meaning if we go from the center of that circle and we add the circle radius whether it be left right or up or down then we'll get the edge of that circle location on x or y axis depending on which we choose so it should still have the cursor radius so if it's more or equal to the self dot button y and mouse y minus cursor radius is less than or equal to self dot button y plus self dot x or sub height then and take note we're using height with y and width with x because with is left to right height is top to bottom just like how y is top to bottom and x is left to right that's why we use those two respectively okay there we can now we know the button is inside or the cursor is inside of the button we know that for a fact now so we can just start executing stuff so self dot func param so if the function parameter exists if one was passed in right then would you call sao they want to call self.funk and we'll do pass in self.funk parameter like that else we want to do the same thing but this time we just don't want to pass in a parameter so the parameter was not passed then we don't care about it and just like that we have implemented a press operation for our button all right that was quite a long one too we are almost at the end though so don't get discouraged just yet cool now i'm going to create a mouse pressed function so whenever the mouse is pressed now you can do that within draw with an update within load i'm going to create my own entire function for it which is possible with love 2d i didn't do this in the tutorial because i kind of wanted to keep it for this tutorial so if you didn't don't notice this from the tutorials i had don't worry i did skip it for a reason so you can just say function love dot mouse pressed and i find this to be a lot more powerful than the one we had in the tutorial meaning this x y button is touch presses so all of these gets passed in for us we get the x position of our cursor the y position of the cursor where the right button was clicked the left mouse button was clicked or the middle mouse button was clicked i think there's probably support for other mouse buttons like the ones you get on the side of some mouse and stuff like that i just don't know how to work with them and then we have is touch so basically is it a touch screen you know it's a touchscreen mobile support so it is touch and it presses so how many presses i believe so basically if they touched with their finger and here's how many times they touched or how many times they clicked or whatnot we are not going to implement touch screen support in this game because i think that's i don't know i just think that's something you could probably do if you wanted to but it's quite a lot of debugging especially if you didn't have a touchscreen yourself anyhow we're gonna just say if not game.state is running because remember we don't really want the mouse to be able to click anything if the game state is running the mouse buttons shouldn't do anything while the game is running then so if the game is not running then you're allowed to do anything in here because if button is equal to one meaning if we pressed the left mouse button so not the button that brings up this one but the other button the one if i click like this if we click this button then do something so if game.state so if it's the menu state we want to specifically use the menu buttons then go four index and we need a value and we're going to say buttons dot menu state so we're going to loop all of the buttons in the menu state that's why we created it here because we're going to look for all of this buttons inside here you check if they have been pressed that's why it's in the menu state only if it's the menu state this is also to save a little bit of performance we're going to say buttons dot menu state dot for that dot when you say position index so that should be dot so button stop menu state at this index so let's say the play game button for example and then we want to just say check pressed for that button and you want to pass in the x the y and the x and y is basically the cursor x and y that we're passing in and in the player dot radius and a player radius is coming from here there we go player radius which in this case is actually 20. so yeah that's the radius it's half of the whole circle anyhow so you pass that so it's going to check if any of those buttons were pressed so if i were to now run this and let's try x again because that's currently the only button that has anything implemented ah and we're actually getting everybody worked so button.loo at 17 okay button lua at 17 um attempt to perform algorithmic on field height angle value so let's see here height self dot height which is null value oh probably just spout it incorrectly there we go actually i think height might be spouting correctly i don't know i can't grammar for the life of me now let's try it again and it worked our game exited without any problem cool now let's quickly work on the start new game function and then we're done then we can then we can turn it off for today so i like to do that underneath i don't know actually i think i'll do it above load and maybe i'll even put this mouse press right here i'll put it above it as well why not and we can just do that boom okay now here i'm going to create a local function and i'm going to say start new game right and then it doesn't take in anything we can just say start new game then game dot state at menu whoops my man yeah at menu right then becomes false so we're setting the menu state equal to false and currently because we only have the menu and the running state we're not going to worry too much about other states but now we can set this running state equal to true because we're starting a new game so we should start running the game and in here we can also just create our first enemy as well so this part right here we can completely remove it and just move it up so now here we just boom like that now your start new game is pressed we're going to insert a new enemy into our game and we're going to set the menu state to false and then the running state to true okay now let's actually just implement that here so where it says nil we can say start new game without the parentheses take notice with other parentheses because otherwise it's just going to execute before it gets passed in and yeah then one last thing this is going to become more helpful later in the game but we can just say if game that state is running so the game state is running then we want to do this this is so the enemies don't try and keep chasing the player if the player is like in the menu or the game is paused or anything like that because believe me it's going to be very funny but the player isn't going to like that that they can still lose while in the menu or something like that anyhow so we just say they're only allowed to move if the player or if the game stays running cool now let's try and play game as you can see not only does our player grow but the enemies now start following us as well that's pretty neat so we did quite a lot in this tutorial well done if you're still following along so basically we created our first buttons we implemented touch folders buttons we implemented the drawing the buttons and then we implemented the actual functions of the buttons and that's that so let's actually start with the enemy let's actually try and improve the enemy so first off i would like us to actually now receive a level now level is basically going to allow us to say what level every enemy should be because we can have 10 enemies on the in the game like just coming at you but the problem is without the right leveling method those enemies will just be kind of the same thing will it be difficult or anything like that so what we can do is we can go here where it says level and instead of saying one we just make level equal to level and just in case we didn't pass in a level but usually you would be passing in a level but just in case we were not we can just say or let's say one and you could also if you want to make this underscore level instead of level to make it a bit easier to understand although i think this is perfectly fine you can even rename this to be lvl instead all right so that's all we're going to do here for now we will be getting back to this later but right now i think it's perfectly fine you probably want to know how we're going to kind of get the enemies and stuff to come in so i'm going to show you an example of what we will basically be doing so here i have basically what we're going to be doing so when we say play game we will have a little scoreboard here that is going to count and then once this scoreboard gets to a specific amount like 15 it's going to spawn another enemy with a higher level the higher the level the faster the enemy will be a good thing you might want to do is you might want to make every enemy a different color maybe randomly generated or something like that and once the enemy touches you the game ends we'll later on implement an end screen or a game over screen and whatnot but that's the basics of what we're going to implement so we need to implement some sort of point system all right so here instead of game we can actually implement it here so the amount of points we have we can make equal to zero at start you could also put this inside of the player here so then the amount of points the player has but you could also make the amount of points there currently is in the game so whichever floats your boat best and in here we could say levels and this will basically be whenever an enemy spawns you can either have set levels like i am doing now or you can have randomly generated levels or levels at every specific interval something like that but i'm going to just going to have four levels five include the very first level and these levels will basically just be 15 30 60 120 so if you have these points so 15 30 60 and 120 like that so if the player has 15 points then a new enemy will spawn if there are 30 points then a third enemy will spawn because you have the first enemy which is from zero to 14. they have a second enemy from 15 to 29 a third enemy from 30 to 59 and in the fourth enemy from 60 to 119 and then the last enemy at level 120 and above so let's implement the points so here with new game we can just say game dot points is equal to zero so this is basically whenever we start a new game we're going to set the points to zero and now with this enemies here we actually want to do something but different we want to go enemies so we want to create an entirely new enemies table so we're going to clear the previous enemies table and we can just say enemy and then at level one like that that means we can remove that line there because we have our enemy here this will be the very first enemy at level one you can do it like this or you can have it split up into multiple lines so right here where we do the love dot update inside of the game state or inside of the if game state is running this part right here we want to just say game dot points is equal to game dot points plus dt so this will basically increase the points constantly but with dt so 0.016 or 0.032 however so basically about every second you'll get one point now when we go down to draw we can find a way to actually display this to the user so here and if game state is running we can actually put it just above enemies so the enemies can move over it but you can put it below enemies as well we can say love dot graphics dot print if and this will print text to the screen i'm gonna say math.floor game dot points and this will display the game points but to the lowest value so if we have 3.14 it will display three if we have 3.5 it will display three if we have 29.99 it will display 29. so it's basically like math.round but just always downwards and never upwards we can then specify a font because with love graphics print you might notice we need to specify a font so let's just say love dot let's just copy this now we want it to be bigger than something like that fps because it has to attract more attention than the fps so we can make this about 24 instead of 16. in here we can basically put our location so 0 10 and then we can tell it how long to spend so love dot graphics dot get with and then we can also just specify center so all of this does a lot but basically you can think of it as displaying the text at the center of the screen i'll show you how this looks in a second so let's actually try to run this and see if it works so love dot alright we say play game and here it does so it's here at the center of the screen near the top and yeah it just counts about every second i believe this zero here is the x if we were to make this something like 90 and we run it then you'll notice it's a bit more to the right so the 0 is basically the x and this 10 is basically the y cool and then this here just specifically wanted to be basically aligned to the center nice now before we get to the enemy i would like to implement a better way to change the game state because currently the way we change the game state we have to constantly go around and we have to change a bunch of things manually which isn't what we want we want to make it as simple as possible so in the future when we return we never forget about something we might have to change if we change this value here we or if we added this here all right so let's say local and we create a function we can call it change game state i can take in state and here we can say game dot state menu so the game state is menu or we actually just make it state is equal to menu so we basically say make game state menu so true or false if the state passed in is equal to menu so if we pass in menu here it's going to make this value here that and we can do the same for all four of our current states so paused and here we can say running and here we can say and then we say ended so we never have to worry about changing another game state because here it will automatically make things true and false as we want it so for example here where we do this instead of having to sit two three four five states here we just call change game state like this and we want to set it to running like that and that will just make our life so much easier since now whenever we change the game state we don't have to everywhere we want to change it we don't have to constantly basically be doing this because now we have a function let's make sure this works so if i press play game it should start the running state and it does alright so next up we should maybe work on getting if the enemy has touched the player if the enemy has touched the player the game should end because the player has lost because that's the point of the game so what we can do is we can go here to enemy.lua and we can implement a new function check touched now this check touched will contain a function and this function will take itself player x player y so the player x and y position and then also the cursor radius like that and i did explain a lot of this in the previous tutorials i do recommend you go watch that if you have not where i do explain a lot of collision detection math there as well so now we just basically need to detect if the enemy is anywhere near the player this is going to be some some interesting math here so just bear with me for a second math dot square root so is qrt for square root and i'm going to create two brackets here so i don't get confused with mathematical operations and so lua knows what to do first self dot x so basically the enemy current x position minus player underscore x so the player current x position to the power of two and i'll explain this in a second you can say plus and then another pair of brackets self dot y plus i mean minus player underscore y and we just say times or to the power of two less or equal to cursor radius times two so basically this math here will check if the player is inside of the enemy so this is basically like the previous one just kind of in one line to make it easier for us you could split this up into multiple lines again very similar to what we did here inside of the button check pressed you could make it very similar to this not exactly like this because you know one is a square the other is a circle but in hint here both of them are circles but this will basically in the sense of our agreement drawing up here it will do this so this is the enemy this is the player whoa now in this scenario so this is the game scenario it would be false now our math will basically check this this is the enemy this is the player and that will return true so it will just basically check if the enemy or the player is touching each other because if so then they are colliding meaning the player has lost so yeah if you want to know more about this map i do recommend you do go maybe refresh up on some mathematical things you can do with circles because this is one of the things that works with that alright so once you have that you have your collision detection now with your collision detection we can finally do some coding to actually see it do something so let's go here to the running state right here so instead of this for loop we're going to increase a few things here to increase the amount of lines so right here we can say if not enemies at index i and we're getting this right here if not enemies at index i check touched this check touched will just basically execute this math right here to check if the player is inside of the enemy then once you have this you can just say player dot x player dot y player dot radius because this check touched needs three things player x player y and then the cursor radius which are all provided here this will check if it is touched so while it is not touch as you can see there because that has a knot so this will check if it's not true so if this does not call true if it's not being touched then and in here all of our things will happen let's first code the else because this is a quick one it's just change game state to menu later on this will change the game state to end it and what not but we don't really have anything for those states so currently it will just change the state back to menu and see this is why this change state is so useful because now we just call this and it will do all of the other background stuff for us now this enemies don't move here or his enemy move we can move it there so the enemy can move so we can now say for i is equal to one in game dot levels do so we're saying for i becomes one in game with levels you could call this something else even if you wanted to because we have an i here so you might want to call this something like j or k or some other value any game that labels and game the levels is right up here so it's 15 30 60 20. so we're saying for i equals one through that we can go if math.floor it again this is to make sure we round down game dot dot point and this is the points we have scored up until this point if you can remember and we can say if that is equal to game.levels at index i then we want to say table dot insert and enemies one and then enemy and game dot difficulty times i plus one so what are we doing here we're basically saying insert into the enemies table at position one an enemy that has the game the difficulty so the game difficulty is basically being set up here so it's one so in this case it's just going to be one but if you want to increase the gain difficulty then you can times in this case is going to be one plus one which is two but if this was the but if this i increases this would be two plus one which is three and this will basically increase the enemy difficulty or the enemy level aspects go on we did implement the leveling thing with the enemy in the previous vid i think it's actually in the two videos again not the previous video so let's see here at least a search level yeah there we go so self.level in the move so we did implement working with level so we didn't have to mess with that now because we did it before and then this is both to avoid bugs and you kind of tell the player you have done this we give the player one bonus point so game dot points is equal to game dot points plus one so we gave them one bonus point as well as trying to avoid a bug because otherwise this for loop is going to run multiple times because dt is going to take a while before it goes one full point because it's like 0.016 or 0.032 so yeah we have to be careful with that so we give it one extra point so you can also break out of the loop there are also other ways to deal with it but this is the easiest way i find to go around it now once you have that you can just save your game over your code and let's see if it works play game okay here is the first enemy we wait until we get to 15 to see for the second enemy and here we go so now there are two enemies if we are willing to wait to 30 we should see a third enemy that is even faster yeah as you can see there and once one of them touches us we just go back to our main menu and yeah that is it for this tutorial we just implemented basic a basic point system and we implemented the basics of adding more enemies let's start with fixing something up first currently we don't use fonts very nicely because if we want a set font that we might want to use over a large amount of spaces this isn't going to work that well because we have to basically come back here copy and paste or remember this number that is why i think it would be best to create another table and this table we can say it's called local fonts now local fonts will just contain a few fonts for us so medium and this would be of course a medium sized font i'm not going to have small or extra small fonts i'm just going to go medium large and massive because these are the different sizes that i'm going to use nothing is going to be really small and i'm saying medium because the size of medium is going to be 16 and if you're a web developer you know the default font size is basically 16 pixels anyhow so we can say font is equal to and i'm going to copy this font because it's basically just going to be this right here so i'm going to paste that there and then i'm going to say size is equal to 16. we won't you really use this part too much but it's more for future reference because sometimes you want to access the size of the font and not just the font so it would be good to include the size so if you ever want to know like the font size like let's say you're in settings and you want to increase or decrease the font size then the player can maybe choose between large medium and small and you can tell them the font size is 16 and then it's 24 and then 60 or what not cool so that's just for future reference mostly it's not per se you're going to use or not to say it's going you're going to use it now i'm going to have two more fonts here one called large and in one called massive so large will be instead of 16 24 and massive would be instead of 16 it will be 6t like that so now we have a fonts table so first let's go down here to where we draw because this is mostly where we're going to use the fonts now we will we want to set the font to its default whenever we draw this is mostly just because if we do set a font somewhere then that font won't just stay there the whole time and you'll notice when we get to the endgame state that this right here will become quite important so love dot graphics dot set font so we're going to set a default font for every iteration and it's going to be fonts dot medium dot font so we're basically saying add from fonts medium and then for medium we add font and font is just this right here that's basically the medium font in fact we can copy this and we can paste it right here boom now it's fonts medium font so whenever you draw this this will be the medium size this right here this would be the same but instead of medium it would be large so this will be a large font all right and that's currently all the fonts we really have if we run the game we should not be seeing any difference really so as you can see phones still look the same if we say play game that's still the same size and that's still the same size cool now i think it's time we start working on the end screen the game over screen so we'll need to create the buttons for this screen so here with this state right here this buttons we can go here where it says menu state and we can make it ended you can make this game over or whatever you want to call it i'm going to call it ended state so now we can have game over buttons now we can go to love.load right here and we can copy and paste this like that and instead of menu state we can turn that into entered state now the first one is replay so if you want to replay the game so replay game the second one is the menu button if you want to go to the menu so we can say menu and the third is exit game exit game will contain all of the normal values so love it ain't quit and just exit game this will contain menu and here we want to pass in change game state and the state we want to change it to is menu now taking it change game state was declared right here where it just changes the state to whatever is passed in and then of course you want our replay so right here replay for replay game yeah we're gonna make it full replay game and in here we can just say start new game now currently we didn't actually ever see the ended state so we'll need to go here to update and we'll need to change this to ended so from menu to ended this will instead of making it go back to the menu state where we see the menu it will make it go back to the game ended state or the game overstate and we can see the game over screen then we can go down and we can copy this else if c and like that now this else if will be instead of menu it will contain ended cool now i'm going to go love or let's just copy this here love that graphics.setfont because for the ended state we want to set the font to be something a bit bigger so fonts dot large and this will make the fonts a bit bigger this will allow us to basically everywhere within the game while it's in this state just increase everything with size then here we can go back up here and we just copy these three names here and we go back down and we can paste them here and then here we can just make menu ended so let's see what we can see so when we run the game what do we see okay so we see this it doesn't look great we can definitely make it a bit better for example instead of having it here at the side i think it would be better if we had it here in the center and we of course have to resize it a bit just make everything look a bit better so first let's try to center everything because i think that would look best so here everywhere where i say love that gra or where i say 10 we can say love dot graphics dot get with and we can divide it by 2.25 and this is a very specific value here this value was actually calculated while trying to find the perfect way to center our buttons because currently we can set the button x and the button y and this process right here allows us to make the button more centered if you have a different aspect ratio for your game so if i just quickly run this if your game wasn't this aspect ratio which i think is four by three or it's it just actually looks like a square it might be one by one but if it wasn't the same aspect ratio the same size then you will have to change these values here or you might have to it's not maybe you won't have to maybe you're lucky anyhow and in here where we say 20 70 and 120 we can say love dot graph graphics dot get height because this will be the y position we can then just divide that by one point eight but that's only for the first one for the second one we will want to make that 1.53 because remember unlike with x here where basically all of them will just move here oh they will now move top to bottom so if you make this the same they will all be at the same y position and if you're all on the same y position they're going to be stacked on top of each other you can play with this a little bit if you want to to see what the difference would be if you didn't add that and stuff like that and here you can say 1.33 cool then for the last two ones which will be the text positions we can specify 10 10 and then here we can just keep that 17 10 and then here we can change that to 22 10. this is quite a lot of changes so let's first see what we have because currently we did a lot of things so let's see how does our things look all right so that's already relatively close to the center the reason why it's further down to the bottom than what it is from the side is because we want to put our score here in the complete center so this will be below the score that is why it does this and the buttons work so if we go to menu or they don't because we haven't implemented clicking yet so never mind that all right so let's do the points let's quickly draw that before we continue to the button so love dot graphics dot print f and we can say math dot floor game dot points so we're going to put the game points for everyone to see we can say fonts dot massive dot font because we want it to be massive we're going to set this to zero then love dot graphics dot get height and we want to divide that by two because it's going to be kind of halfway and then we also want to minus the font size because remember it's basically going to look like this if i can open up my drawing application it's going to look like this so this is our font right but you have to remember so let's say that is the zero character you have to remember fonts are actually inside of squares just like images so this is actually how the font looks somewhat similar to this so when we do that we're basically saying this now we have to minus the font size specifically because the font still goes down so if we minus the font size it will go from here but up until let's say this point here because that would be the half of this font if we were to circle it like that or something this might be a little bit confusing to understand so let me actually show you so instead of minusing it we'll keep it like this for now and then we can say love.graphics dot to get with to say where it should bend and then we can just specify it should be in the center right once we have that we can do this play game and as you can see the one is right there and if you want this to be a bit more up we'll have to minus the font size so minus fonts dot massive fonts dot massive dot size not the font but the size this time because we want to minus it by 60. now if we were to run this boom r2 is nice above the buttons now what i would like to do is i would like to go up here a bit to where we were here and i like to change all of these 120s to 100 instead and in this 40s to 50s and let me show you why because now look actually a bit bitter so if we could just go like this as you can see it just looks a bit better and i don't think this end game looks that great and i don't really want the buttons to be too long and i want them to all be the same length so instead of exit game they'd rather say quit because quit won't be that long so quit and actually while we're at it let's make this instead of a replay game we make it replay instead and this will make everything nice and short so when we get to the game over screen everything fits into the buttons nicely all right and that is actually just perfect next up we need to make the buttons able to be pressed because currently the buttons have nothing to check if they're being pressed so here where we say if the game status menu we can say else if and then we can do the same thing here where we say menu as the menu we say ended and in here we can just specify this but this time instead of saying menu state we can say ended state and everything else can stay the same you could even maybe create a function that will replace this for you so you don't have to do this every time anyhow if we were to now run the game our buttons will work so if i were to end the game here say replay we can replay the game if i say menu we go back to the menu and then if i were to say quit we exit the game and that is basically all for this tutorial you will notice that the game is not quite finished yet there's quite a lot of things you can still do here and i believe the best way to learn is to do it yourself i am going to bring out another course which will cover some of the topics i'm going to give as homework so if you can't figure it out at all once you watch that tutorial you should be able to get a sense of how to do it so let's first start with what we need first of all you'll notice if archie just moved this to the side a little bit we have a paused state here but we did not implement that paused state implementing a post state is incredibly simple and i will be doing that in the next tutorial as well but currently we don't have that paused state i want you to try and implement that paused state it's very very simple and it's probably easier than you think it is next up is the settings page we have a settings button but currently all it does is say say this button has no function attached this is a bit more complex and there's a lot of ways you can implement the settings button i want you to try and do it first in the next tutorial is going to be your homework again but i'm going to give you a kind of an example a sort of hey you can do this so you can try and do it here and i recommend you do because it's quite a challenge and if you do it right then you have a perfect settings menu otherwise you can wait till the next tutorial where i will give you a little bit of a push towards the right direction i'm not going to implement an entire settings page but i will show you kind of a way to get started with it and next up is make the enemies more different currently the enemies are quite boring they're all circles they're all pink and that's it there's nothing special about any of these enemies in the next course i will show you a basic way of how to make your enemies more different so stay tuned for that if you do want to get the answers to this as well but changing these enemies up to be a bit more different like maybe making them instead of circles make them squares that could be a great idea maybe start a scoreboard as well so let's say you can have a high score and then you can compete to beat your current high score i'm going to show you how to do this in the next course as well but i would like you to try and implement this here as well because it can be quite difficult ooh that's a fast one it can be quite difficult to do so i would recommend you try and do it on your own first and hello everyone if you're enjoying this tutorial please leave a like and subscribe for future tutorials so in this tutorial we will be making a pretty cool game the game's name is asteroids so here's an example it's a little big let me just turn down the sound so everyone can hear me so here we basically have a new game settings and a quit option and we can say new game you can start a game it has faded fading text it has a working high score it has a score and it has lives you can shoot things and you can go to the next level and then there will be more difficult times ahead you can crash and then you'll lose a life you can shoot things now this may look incredibly simple but it is pretty complex with a lot of things working inside of it let me just go out of invincibility mode and there we go then when it's game over you have a fading game over screen and if you have a high near high score it will save that high score and then boom here you go so that is what we will be working on in this tutorial for future reference if you ever do fall behind you can go to get up and the link will also be in the description but you can go to github webnet to youtube project and then in here and go to lua and then the live 2d folder inside of the love2d folder you will have an asteroid game folder and in here you'll have all of the tutorials and you'll have some homework if you want to do it now what do you need in a in order to start with this tutorial because some of you might be completely new first you need to know lua if we go to our crash course and research it on youtube then here at the top is my crash course steve's teacher full lua programming crash course you will need to follow this course if you want to follow along of course not all of the topics are necessary but a lot of these topics are very handy to know about and once you know this it will be very smart of you to know a little bit about love2d so love2d right here and here we go now i recommend you know at least this one this one and this one and then i would also recommend you follow the previous tutorial gamedev tutorial where we created the save the ball game this tutorial covers some really basic items from love2d and if you follow the tutorial then this tutorial that we're going to do now will be a lot easier to follow along first we need to create a folder for our things so i'm going to go to documents going to go to my trash and then i'm going to create a new folder and call it asteroids i don't know if how to spell asteroids but i hope that's correct cool inside of this folder and you don't necessarily have to do in your trash folder i just have a trash folder for tutorials and inside of this folder right here you can create a new file and you can call it main.lua now once you have created a file you can open up vs code in here i wonder can i open up i can't open up from here anyhow i can go here to my terminal but you will probably have an option to go here and just right click and say open in vs code or whatever code editor you use we can then just say code blue code to open up vs code here and you should see something similar to this we can close this folder here because we won't need it now take note you do need love to the installed and lua so if you don't have those to install i do have a tutorial at least on how to install lua then here we have our main.lua if you also use vs code you can get the love to the extension right here and you can also get the lua extension right here and you also debug if you want but this isn't necessary it will probably auto download anyways anyhow now that you have that everything is ready to go let's just set up a basic project i think for today we don't have to go all out so just local love is equal to require love this is an optional step you don't have to do this but it is optional then we can create a function and call it love.load and it's just a basic love2td setup i mean can create two more functions for update i almost said delete here but this should be a draw now since we're starting off very basic we're not going to do a lot we only want to get everything up and running and maybe make sure everything works if i were to run this by saying lab dot and you can also use the vs code inbuilt terminal by going to terminal new terminal and then you will have the same thing as what i have you can just say love dot here as well this is the same thing as what you just saw here i just like this one more because it looks cooler and then you'll see this pretty neat we can then create a new file by clicking on this file icon here and we can call it conf dot lua and here we can just say local love is equal to require love yet again optional you don't have to import love now you can say function love.conf to set up the config file pass in the app we can say app.window.with and you can make it any width you want but i am going to make it equal to 128 128 no 1280 or 1280 and as for the height app.window.height is equal to 720. for some of you this might be too big so just for reference this is a 16 by nine aspect ratio we're working with as long as you work with the same ratio you don't have to change too much for example at 1080p resolution this is 720p but 1080p would be 1920 in width and then 1080 in height as an example this would be bigger of course if you want to go smaller it should probably probably be around 480p but you can google a 16x9 aspect ratio smaller than this and of course we want to set the title app.window.title is equal to asteroids and then lastly we want to say app dot window dot display equal to now take note not all of you will benefit from this display i'm using here i have two monitors the one on my left which you cannot see and then the one on my right which you can see however i don't want the window to spawn on my left monitor which is probably display one i wanted to display on this monitor so let's see does everything work and it does it's big boy and it has the asteroids title and it spawns on this monitor so this one here is optional these are also optional but i recommend using them because why not as for the window width and height i recommend 16x9 smaller size if you want but if you don't want to change the window height and width then you don't have to first thing i want to do is i want to hide my mouse so when i'm here the mouse should be gone and that's pretty simple to do that we can go here and say love dot mouse dot set visible and we can just go false and this will set the mouse visibility to false now for future reference we are going to do that in this tutorial but it will be smart to set our mouse x mouse y equal to zero or whatever you want to here so these will be two global variables containing the mouse x and y positions this is for a future reference so you don't have to put it here now but i do recommend you do because then we don't have to worry about it and then next up here in the update we also want to set the mouse x and y so i will actually just copy this paste it here and then here we can go love dot mouse dot get position and this will return the x and the y position of the mouse both of these are global scope so if you wanted to you can go underscore g dot and that would be allowed but for me here i'm just going to say quick fix and if you're in v is good you can choose one of these and i'll just go here disable diagnostics for lowercase in this file or even in this workspace could work but i'll go file and it will add this where it doesn't show that errors underneath these and then last but not least before we continue i would like to set the show debugging equal to true and this will basically say if you want to see debugging items right now we're not going to use it but we will later if we run this you can see i can no longer see my mouse but if i move outside of the screen you see here there's my mouse but if i go in it's gone that is what we wanted cool now we can create the player to do that we can go here and create a new file called player dot lua i'm using a capital case p just because i believe objects or at least other files should use capital case p unless it's the main files the core file is going to be used by lua i like the conf or the main cool now the player here will be an object if you don't know how to work with objects in lua i do recommend you go watch my objects tutorial i have on this now you can go local love eagles require and then love and if you want to you can copy this and you can put it in here take note that this diagnostics only works if you're in vs code if you're in a different ide slash code editor then this may not work now we can create a function call it player and player will take in whether the player is busy being debugged so debugging so if we're debugging the player then we want to see some extra information and then at the end of the file we can say return player nice now some variables you might want to set at the start will be the ship size these will be constant variables that will never be changed so they are capital case and we won't want to access them outside of this file so we're not putting them inside of the return we can make this equal to 30. you can change this but this will basically determine the ship size because this would be from the left side of the ship to the right side of the ship then we can go local view angle is equal to math dot red 90. to get an example of what this does because it transforms degrees into radians because we want to use radians and not degrees so to understand what we'll get here we can go here and say lua and then say math dot rad and then 90 and that will give us 1.57 radians so yeah we're going to use radians not degrees just keep that as note so this would be 1.57 radians and it's all to do with math stuff so don't worry too much about it then here we can say debugging is equal to debugging or false so if we do not pass in the debugging argument then by default it will be false that's what we are saying here now return is what we want to get back what we want to be able to access within this whole thing here so x will be the player x position which is love dot graphics dot get with divided by two so we want the player to be positioned in the center of the screen once the game starts so that's what we're doing here we can then do the same for y and then while instead of get with would be get height so now the player will be positioned in the center of the screen then the radius of the player and this will kind of be like the hitbox of the player so the circle around the player if that circle is hit what happens anyhow so the radius will be the ship size divided by two and then the angle would be equal to vu angle so the angle by default will be 90 degrees meaning the player will be facing upwards how do i know that because the player will be a triangle if you've seen the demo of the game in the first tutorial i showed you that the player itself will be a triangle and moves around and if the angle of the triangle is 90 in terms of degrees and the radians 1.57 then it will point upwards cool then rotation will be equal to zero so this is if the player has been rotated then thrusting will be equal to false if the player is frosting that means we want to move because he's frosting is blasting out fire and stuff which we won't could in this tutorial but we will code later and then the thrust itself will be equal to the x 0 and then the y zero and i'll explain what all this means later once we get to it and then this speed zero cool now this currently won't do anything so let's draw the player to the screen so underneath this thrust we can just create a function called draw this will be a function that takes itself and then here we go now local opacity is for future reference but basically this will determine how visible the player is to the screen so if it plays on the screen can we see the player or not next up we want to say love dot graphics dot set color and then one one one and this means white and we pass in the opacity which is also one so it means it's completely visible to draw the player we can say love dot graphics dot polygon and in here we can type in a lot of things that will draw the player first of all we want to draw the player as a line not just something that's being filled in so you can say line so it's going to draw lines then we want at least two items to draw that line so let's go self.x plus and this will be a little bit of math so bear with me and we have four divided by three and note i'm using two brackets here this is just for me to know okay i want to do the division first the computer will know let's do division first but i want to know as well let's do division first because when it comes to math operations and when to execute them i am still a little bit junky on that and you can say times self dot radius take note that this is not inside of these brackets instead of this brackets but not inside of these brackets because you want to execute this first so four divided by two and i think that should be three and i'm going to explain this in a second and then times the radius and then once that has completed we want to times that by math dot cos self dot angle now the 4 divided by three is mainly to find the center of the triangle next up here we can say self dot y so we can copy that line say self.y keep everything the same and in here instead of saying because we say sin because we want to go the other way so let's create the player and hope for the best so then here we can say local player capital p is equal to require player and it should also be a capital p because the foul has a capital p then down here in the loading section we can say player lowercase p is equal to capital case p player if you don't understand what i'm doing i recommend you to go watch my course on oop in lua and then we can also pass and show debucking so this will create this little object of player that we can use and get data from now here we want to just see the players so let me just draw player colon draw how can i use this because right here we export the draw function it's inside of return and then it should draw the player for us now if we were to run this do we get no we don't it needs at least three vertices to draw a polygon oof okay so we'll have to do the whole thing unfortunately but okay not a problem so since this is a lot of typing i'm just going to paste this here and then i'll walk you through it so basically here we have what we just coded and in here we say self.x minus the radius right times 2 divided by 3 times math cos south angle so first we're going to times this because pod mass if you want to make sure of that you can do that anyway so we'll first times these and then we'll add this to it and it's going to be the same all the way around for x and y the only things that change is when sin and cos is being used and yet again i didn't quite understand this math it was a 1am thing so just excuse me if i didn't quite know how to explain this but you can play around with it if you want to understand it more i don't really want to understand it more because it breaks my brain not understanding it so scared of understanding it anyways if we try to run it now ah here we go does it quite look like the player let's see what went wrong oh you know what went wrong here we should minus it my bad now if we run this we'll get a triangle cool if you want to actually test it out for yourself and see how it looks what you could do is you could run lua and then open up a drawing application okay now there is our x position under y so x would be side to side as you know and y would be up and down as you also know to remember this you can remember y has like this little finger goes like that and you can add arrows here to specify up and down that's how i remember which one is x and which is why anyhow so there's our center then what we can do is let me just do that so first we get our self.x and let's say x itself is equal to what is it let's make it 20 or 200 because that would be more realistic so 200 plus and since we know this isn't going to execute yet let's first go four divided by three four divided by three that's one point three three three so one point three three four three times and then the radius as we already know is let's see here ship size that's with 15 because ship size divided by two so the radius would be 15 so times 15. so this right here is basically 20. so that's 20. then we times this with math cost and then the angle and this is why we used radians right here and not degrees because if this was 90 degrees then this would be way off so we are using a radiance to give it a much smaller value to work with because 90 would be pretty big compared to one point something anyhow so then we can say dot cos and in here we can specify the angle and the angle as we know is math.rad19 so math.red 90 like that okay so we got our values now we have to times these two together so 19.9999 times six point one two three two three three nine i believe that will give us this right here and then we can add our self.x which would be 200 so this value right here plus 200. now that we have that we can see where the next dot will be drawn on the x-axis so here we have our x and this right here that's 200 to the x so that's 200. now the next one would be 322. so that would be even further beyond so that would be like here or something and not not downwards it just exports a like it would be like here or something there and this is where this one is and you can just imagine that these two or 120 pixels from each other now with the y it will position this one downwards so you can use this to kind of figure out where everything goes if you were to do the math like this this is at least how i can understand math so if i want to spend the time this is how i would understand it so we can draw the player that is good progress we can't do anything yet but we can draw the player nice now we still have a debugging option so let's quickly implement that so right underneath opacity we can say if debugging then and here we can say love dot graphics dot set color and you can make this any color that is not white or black so one zero zero because black is the background and white is the color of the shape so making it a different color will make it easier to see so in this case i'm making it red and then love dot graphics the rectangle and we want to actually see the center of the ship this will just help for debugging for when we try and move the ship and the ship maybe doesn't move but the center moves or something like that and here you can just say fill and then self dot x minus one self dot y minus one two two so this positions it's in the center and i'll show you in a second what this means so if we do this and you see we have a little red dot in the center of our ship the reason i said -1 for the x and the y position was because if i didn't we'll be getting this and as you can see it's a little bit off from the center and we don't want that and the reason for that is just because of how we draw so we add minus one to avoid that and then the two two is just the size so if you were to change this to 4 for example then now we'll get a bigger circle and i will have to change it more to the side because of the size here so this will now have to become -2 minus 2. so the more you increase the size the more you have to move it back because you have to remember if i were to open my drawing up again if we have a square square like that this is the x position of the square right here so right there that's the x position of the square and we are drawing a rectangle inside of the player so we need to minus this to the x and minus to the y to bring this to the center of this square if you understand what that means anyhow so let's just change back to one into two and next i want to draw the collision of the player so when we try and collide with other things we can see where the collision would affect each other so love dot graphics dot circle and here we can say line self dot x self dot y we can say sound that x and self.y because the x and y of a circle is in the center of a circle and not at this side of the circle and itself.radius so the radius of the ship itself if we run this we get a circle around the ship now this circle right here is basically the collision so if anything touches that circle it's touching the player you might think this is pretty basic and it is moving things and it isn't quite accurate but believe me this does decrease the amount of math we have to do and you'll thank me for this later all right now whenever you don't want to see that you can just turn off the debugging by passing in false here cool now let's move the player because currently the player isn't moving at all and we can't move the player even if you wanted to so let's create a function and we can call the function move player or just move both will work perfectly fine we can then say function that takes in self and then local fps so this will get the fps of the game so this isn't the best way to say how fast the player should be moving and we could possibly change this in the future to be a better way but for right now saying the fps will be the easiest way so you can say love dot timer dot get fps and we're gonna scroll it down and then we want to set the friction and the friction will just basically say how fast the player should stop moving so let's go here and say local friction i will play around with this because it's pretty simple is equal to 0.7 now we need to set the rotation speed of the player now i want the player to be able to turn a full 360 degrees so completely around within a second to accomplish this we can say self dot rotation is equal to 360 divided by 180 times math dot pi divided by fps so first yeah there we go so first it will execute this and it will get the value of this so lua 360 divided by 1080 that's two then it will divide these two so math.pi divided by fps so pi and we can assume we'll be getting 60 fps so that's 0.052 so that's 2 times 0.0523 five like that and that should be at times the symbol not an end and they will get this value right here now this value right here is basically how much the player will turn every second cool now let's actually use that because i think it would be easier to understand if we use it i didn't worry too much there wouldn't be that much math in this course it's just the first one has quite a lot of math because of because we're using a triangle and whatnot so you don't have to worry too much about the math we want to basically say when the player should be turning left and right so to do that we can go here and do a quick fix to that and say love dot keyboard dot is down and we want the player to turn left whenever they click on the a button and you don't have to add these extras but i like to add options and you can say or if they press the lift button on their keyboard so that's like the left arrow on your keyboard or if they press the kp six and that's the numpad number c and that shouldn't be six actually before so that's numpad number four so if they're only like the numpad they press four then the player will also go so now they have a lot of options to choose from you don't have to add this mini i just like to and you can say self dot angle is equal to self dot angle plus rotation so every time this runs it will add this to the rotation so it will add 0.1047 to the root to this right here that should be south to the angle so the angle will change every time and as we know the angle is in radians so we need a small value which we got here as you can see here we got 0.147 and we add that to this and you'll see it working in action in a second again copy this and we can then go here and say d if they want to go right and then write for the right arrow key and then kp six if they want to go right and then this one will of course be minus cool let's test this out so if i were to run this and of course we didn't do anything yet because we had to go here into the main file and we have to say player moves right here player move so it should execute this every time this update runs which is like a lot 60 times a second in fact run this you'll notice i can now rotate so that's pretty cool and it takes me a full second to do a full rotation now of course if your fps fluctuates like you have 30 fps then the player might actually rotate faster than a second so that's why they like using the fps but for right now that's the easy way out and we don't really use dt at the moment but we can later nice now let's add a movement so the player can move up and down now we're not going to add the keyboard bindings for this in here we're going to do it out here the reason for that is because we will be using that for more than just the player anyhow so then we can go if self dot thrusting and we set that up here it's just true or false so if the player is moving or if the player is trying to move if they press w the frost will be true then and here we want to start moving the player so self dot thrust dot x and this thrust dot x is set here is equal to itself whoa and then we can go plus self dot thrust dot speed and we get the speed from here which is zero and it shouldn't be zero and i don't think about it it should be five you know you should give it at least some speed so the faster you make this the faster the player will move so the higher this is the faster the player will move and here we can say times math.cos self and this is just to kind of nerf the speed and we can actually remove this in a second and then divided by f ps and then we can copy that again and this time for y and then we still everything the same but here we can go minus and then we do syn and there now currently won't do anything so let's try to implement that so right here between the load and update we can say function love dot key pressed and we can say key and this will allow us to add a key press then we can say if the key is equal to w or and now we can add a bunch of extra options yet again you only need to add one i just like to give choice and here we have three options one that will be the up arrow on the keyboard and then kp8 for eight on the numpad if it's one of those three then player dot frosting equals true so it will make the player frost and if we play it we don't really get anything and the reason for that is because we're not applying this thrust because you we should apply this for us to x position so self dot x is equal to itself plus self dot thrust dot x so you should apply the x amount of thrust to this so let's just copy that and do the same for y now if we run this as you can see he moves but he just kind of moves out of the screen first of all and as you can see also just constantly speeds up that is because frosting is always on now it'll be even worse if we were to remove this right here is that nerfs it a lot so let's go here and now if we were to run this as you can see he moves up super fast and even optionally in the wrong direction but yeah so this just nurse it to be slower and moves to the correct direction all right and you can play around with that next up we need to be able to slow down the player so else then we can go if and you could make this an else if if you wanted to but i like to just go outside in if inside of it self dot self dot thrust dot x if that is not equal to zero so the first x is still running and the same with the y so or because if the first x and y is not zero that means frosting is still continuing and we're going to go here also not zero and that should be y then we should decrease it until it becomes zero so self dot thrust dot x is equal to self dot thrust dot x minus friction times self dot thrust dot x so the higher this friction we sit here as you can see there the faster the player will stop and of course divided by fps and we can do the same for y cool now let's implement that because currently when we press the key we set the frost to true but it doesn't do anything after we set the key we should actually tell it to stop frosting at some point so let's go here so if the key instead of pressed we can go key release and this means once this key is no longer being pressed or being held in we want to turn this to false cool now i can run this let's see and as you can see it moves much slower and he actually stops so if i go like that it will stop at some point now just to give you an example if we were to go here to friction and up that to let's say 1.7 and then go as you can see he stops a lot faster so the player now just almost immediately stops because the friction is so high if you want to do the real space game you can make the friction something like zero now it will always just move forward and this is how asteroid actually is the player never actually stops so this friction just says how fast the player should stop moving 0.7 i found to be a fairly decent value it's not too fast and it's not too slow but any around anywhere around that will work cool the last things we need to do is we want to reset the color so if any color changes happen we want to reset it so love dot graphics dot set color and then one one one one so it's all visible and it's all white and then of course we want to actually just for debugging purposes show the fps so love dot graphics dot print love dot timer get fps and then 10 10 so it will print the fps right here around the top now if we run this you also see your fps comment down below what fps you are getting and yeah that is that we added the player and the player can now move so we have this oh gosh my bad that's a bug we might want to fix like there we go you see the player moves and the player stops at some point as well we should also at some point just implement something here so the player can't go off the screen but yeah and that's that thus we are now going to be working on this right here a little thruster that animates and moves up and down basically showing you that you are basically frosting yourself through space so we'll be implementing that today at least it's not as much math or massive blocks of code as in a previous tutorial so no need to worry first of all we can close this main.lua we'll be focusing mainly on the player cool now i would like to add two more attributes here to frost first i would like to add big flame and the big flame will basically determine when we change our flame size we'll make that false by default and then also flame and then 2.0 and this will just help with the animation of the flame where it goes from big to small and from small to big so that would just help with that so first we need to actually draw the flame so let's create a function for that so right here we can say draw flame thrust now you'll notice in the code i will give you you will see this but i actually want you to instead of going underscores go camel case like this i am not going to do this because i am following a script and i can't really change that script now it's a little late however you can still change it why do we do this because this one here is using camel case this here is using camelcase this here is using camelcase so we want to basically stick with everything using camel case except for of course these constants now i've already scripted it so we have to use this but you can still change this to be camel case i just accidentally messed up but that will be a function and it will take himself the fill type so this will basically say how we're going to fill the thrust on the ship because this will basically automate a lot of the drawing process for us and then as well as the color now we can actually take this and we can copy that and paste it here and of course add a comma here and we'll get to editing this in a second first we want to set the color so love dot graphics dot set color to color that was passed in because we will change we will draw more than just the inside of the flame we'll also draw like the outside of the flame so we'll need to set the different colors and whatnot and in here we specify the fill type which can be either fill or line so when we use this function it will either draw a line or fill it for us now this will change a little bit so here we can say self.x minus solve the radius because this will have to be an upside down triangle solve the radius times and it's basically going to be the same in most regards so 2 divided by 3 a little bit different but still relatively the same just instead of 4 divided by 3 we're using 2 divided by 3 because now the triangle tip will be at the bottom and not the top so we say 2 divided by 3 times and we can take this math.course self.angle and paste that there and we can say plus 0.5 times math dot s-i-n-self.angle now you're basically throwing a thing on its head so don't worry too much and we can copy and paste that so now we have self dot y and instead of minus this will be a plus so plus and we can remove that plus there then it's assaulted radius and this should be times not that times 2 divided by 3 then math and instead of cos this will be s i n for a sin and then this will be cos so we're swapping those two and instead of plus it will be minus then we can copy these two as we can just copy the first and paste it here and this should be downwards then itself.x minus the radius times and this time we want to basically sit the flame of the thrust so right here that will help with the animation this will basically draw the flame a specific size we can remove this whole part for that and in between these we can say self dot thrust dot flame like that and why did i do that let's copy that and then here we can just replace there we go and this will be cos not sen we can then copy that and do the same for y but yet again just invert everything so minus will become plus and then we're good to go after we turn close to sin and then we can copy these two and we can paste them here now they will basically be the same thing but this time we just go from plus to minus and from minus to plus there we go that should theoretically draw or flame thrust but we haven't tested yet and that's quite a lot of math to draw a polygon luckily this should be one of the last polygons we draw not the last but one of the last now we can go to draw now we only want to basically show the thrust when we are moving forward so did we include yes we did include frosting so only when we move forward do we want to show the frosting so to do that we can go if self dot frosting then and then here we can draw everything we need for the flame of course now we need to check is it a big flame or is it a small flame because we can have two different flames a big one or a small one so we can go if not self dot thrust dot big flame so we're checking if it's not the big flame so if it's the small flame then we say self dot thrust dot flame and you can find all of these right there and we can make that equal to itself minus one divided by love dot timer dot get fps to get fps is just to basically give us a bit of animation so in this case it will be negative 1 divided by 60. and this will just allow us to resize it for the animation and remember this draw will basically also run once every frame so you just have to remember that as well that's how we can get this animation rolling and let's leave that for there for now and just say self dot or not not dot that killing draw flame thrust first we want to say fill because we want to fill it and next we can draw the flame color in this scenario you can even make the flame color change depending on its size that's actually a cool feature you can add in the future but for right now i'm just going to give it a static size or a static color so 2 5 5 divided by 2 5 5 then 1 02 divided by 2 5 5 and then 25 divided by 255 and this will give us almost red color now if we save this i wonder if we'll be able to run it attempt to index field thrust a nil value so let's quickly just comment this out before we continue and as you can see we have a flame it's not really animating but we do have a flame and that's what basically what we are drawing and it only appears when we're moving forward so when we're frosting so that right there if frosting cool now let's draw the outer line of the flame before we continue to self draw flame thrust and instead of fill this top we will say line and now you'll notice why i did it like this and not just put it here because we're going to use it two times so why have that code twice when we can have it once and then we can set the color and this time just to give it some variety but usually you should stick with one type so if you're using rgb like that then you should keep it like that but this is just to show you you can have multiple so one zero point one six and zero now if we were to run this you'll notice it has a little bit of an outline not very noticeable but it does give it a nicer feel cool now we can start resizing it and making it feel pretty cool and here is basically where our problems began there should be thrust like that so i kind of messed up the spelling there now if we were to just run this you'll notice the flame just kind of goes weird because this resizes the flame that's actually kind of cool that's like a sword i like that anyhow so this basically resizes it to be come smaller because if we're saying big flame we want it to become the bigger flame now to make that happen we can go here and say if self dot thrust dot flame is less than 1.5 so if the size has gone to smaller than 1.5 then self.thrust dot big flame because we want to make it start giving the big flame why do i constantly did i misspell this somewhere no frost there we go and then big flame equal false so basically once we reach a certain size we want to start growing the flame so we're making big big flame equal to false i mean true not false because we want to start changing the big flame or to the big thing so it can start becoming bigger so now if we start flying you'll notice at some point it stops shrinking because we're not calling that anymore to make it continue shrinking because of this right here because we only run this if this right here is false in this scenario it's true and by default it's false now we need to do the opposite to get this to grow again so we can say else and there shouldn't be no colon this isn't python and now inside of this else we can copy this just paste it there and now instead of going minus 1 to decrease it we should plus 1 to increase it and remember we're dividing it by 60 so it stays small because 1 divided by 60 is pretty small if we go here and 1 divided by 60 there you go so it will basically increase with this amount of course you could spit a specific value in here but this will also make it go shrink depending on the fps not the best way to do this i would recommend instead using dt but we can maybe try that later and in here instead of 1.5 it should be 2.5 and then false so once it's size reaches 2.5 and it should be more not less than then we should turn this to false and it should start doing this one again until the flame is a specific size the small size then we change back to this one and we're doing that toggling with this right here setting to true or false and in here we're checking if it's true or false and executing depending on the size or depending on if it's true or false now if i were to run this you can see it now grows and shrinks now you can make it grow and shrink less it's up to you how much you wanted to grow and shrink to me i think that's a nice amount to ground shrink but you know everyone's eye will be different and feel different about it now one thing i also want to do because that bothers me quite a lot when we move this ship like this and it goes off screen i can't get it back unfortunately it is now gone forever i don't know how to turn that ship around and get it back the right way so what we should do is we should implement something to fix that now to do that let's go all the way down to right underneath this and we can say if self dot x plus self dot radius is less than zero so self.x plus the radius so let's actually open up my drawing app here because this i can explain pretty simple math so here's self.x right here in the center of the circle now the radius would be from this part to this so let's actually draw a bitter circle so here we go and then there now the radius will be from here to here and you might be wondering why i'm drawing a circle the reason for that is as you can see the collision detection it's a circle this also allows us to get this right here now if we say plus it's going to add these and give us this position right here so let me actually mark another red this position right here now when this position goes off screen so if this point right there goes to behind this here so off the screen where we can't see this at all anymore we want to make the ship come out from this side so now it's going to come back here so the ship will start flying here imagine this is the ship because the ship's radius no longer on the x-axis can be seen so we can just as well bring the ship in if you want to make it more complex and you think you have the guts to do that you can make the ship start getting drawn once it has reached halfway or even make the ship gets get drawn immediately once it reaches in here but that's a map i do not want to bring into this video anyhow so let's do that so i can say if already wait where is it f7 so self.x and we now have to change it because it went away here so we now have to make it come back here so that will be equal to love dot graphics dot get with and we say this specifically because remember the window plus its width is this part of the window so it's going to be the end of the window and then we add plus self dot radius this just makes it look more natural once we get here because they will come in more naturally instead of just poop appearing but of course you can make this more complex and make it work cooler so let's see if that works oh gosh that's a weird bug anyhow here we go ready oof gate with cannot call it and that should be a dot my glasses obscure my vision my bad now if we try it let's see and there we go it looked normal the reason it looked normal was because we made it only start appearing there once we've reached with the radius so it will add a radius to it and then it will appear so look more natural but of course if you were to remove this then it wouldn't look as natural it would just kind of plop into view so if we go here pop it looks natural but it still pops into view so it's not like it's animated or anything it's just pop so it's not the same anyhow so there's self.radius now we need to make it do the same for all of the sides so we did this side but we need to do this side as well so we can just say else if and we can specify self dot x minus self dot radius we're minusing this time because here we have our ship and here's the port now we're minusing the x so it's going to go here so this is where we're checking now and now we're going to check once it goes out of the screen here if this part is no longer in the screen that we can see so in the window it's going to start appearing here and the ship will start flying here again so it's the same thing but this time we check for this side instead of this side and we check for this side specifically where it comes in here instead of the other way around with this map i highly recommend playing with it because believe me it can boost your understanding of a lot if you play with it and since we know this gives us the end of the window we can just go if it's more than this and now we can just copy this self.x and actually we don't even need to we can just say self.x is equal to negative self dot radius because here we added the radius but now it should be zero minus the radius so you could say zero minus outer radius but that wouldn't make a lot of sense since by default negative radius would be so if you say zero minus 30 that will give you minus 30. but if you just said -30 it will still give you -30. anyhow so now we should be able to go with both screen so if i go here or both sides there we go there we go this side there we go so that's pretty neat we can do the same for y by just copying and pasting and then transforming all of this to y instead of x we can still add the radius because now the radius will add it downwards and upwards instead of left and right and instead of get with we now have to say get height and this will basically do everything for us now we go here bloop now i can turn it around bloop now we can't go off the screen which is nice because we don't want to go off the screen we want to keep coming back now in this tutorial we will be adding the game state it's nothing you can really see so i can't really show you an example of it in game however this will make our life in the future a lot easier because now we have something to manage the state of the game instead of us having to do it manually so let's open up this main.lua so we have it open and let's actually just create the games state so let's create a folder called states and inside of that a file called game.lua game.lua will contain our game state currently it will not really do anything but it will in the future do a lot so we can say function game and then we can just return game then it's out of here we can just return we have nothing else we need to really do and we can just return a few things first i want to return all of the states we have in the game this means we will have a menu state which will code later a paused state which will code later a running state and an ended state and we'll code in state later as well where we have the running state so we need to define those states so state is equal to and then menu can be false paused can be false running for now can be true so it's going to be the default state and then ended which will then be false so these are our fourth stage you can add more states later on as you please thanks to this snippet of code here it's not too difficult now what might become difficult is switching between states such as saying okay we don't want the menu state anymore we want this state we want to switch to paused or to running state and later on switching that state to the menu state all of these things they can be kind of difficult to implement so we are going to take the easy way out and implement something that does all of that for us so change game state is equal to a function and in the state we want to change to so state we can then go self.state.menu is equal to state and if that is equal to menu so if the text menu is passed in then we're switching to the menu state and we can do this for all of the states we have for example polls running and ended so here one two largest one two three and here one two three there's probably a better way than to implement it with a string but a string for our basic game is perfectly fine as long as you don't misspell something you'll be good to go but you could give it a default state if none of these are true but we're not going to worry about that now that we have this game stake let's add it to the game itself so right here underneath player we can add game is equal to game and of course we need to import game so local game is equal to require and as you may remember it's inside the states folder so states and we're going to go dot game we can go slash game this should import it into this project now we can go down and here with the update we can now implement some of this state checking so if game dot state dot running then only we want to move the player so you can move that in there and we can move this to the top so now we have that so only if it's in the running state can we move the player so if it's in the menu state or if it's in the paused state or the ended state we cannot move the player then the only other time we might want to have a different state for right now can be in the key pressed so we can copy this if statement and then if the game state is running then do this and then if the key is equal to escape then we want to go game and then change game state to paused and this will make it go to the paused game state now we need to be able to change it back so let's go here and say elsif game.state.paused and then we can just copy this so we get the same key input and this can change it back to running so one of the things i left for homework in a previous video was to implement a paused state for the game and i told you it would be really easy to do so and right now we have implemented a paused state for this game so if you were part of the previous tutorial then now you should understand how to do that if you didn't do your homework so yeah this is how you would implement a post state it's incredibly simple now if we were to run this we can go like this and if you say pause by pressing escape then the player doesn't do anything and once we unescape the player will continue and then like this and later we can add like some paused text but for right now it's fine in this tutorial we will be adding text to the game now that we have the state implemented so what we have currently is something like this so where we go like this and we can also pause like that pausing isn't perfect but you get the idea now when we pause i want the user to actually be able to see that they paused because let's say accidentally press the escape button and suddenly their game stops working they will think it's a bug but in fact the game is being paused so we're going to implement that and here's an example let me just bring this over here so let's go like this and then pause as you can see there's no paused text on the screen and now we go like this it's nice cool so that's an example of what we'll be doing now let's get started so we already have a states folder let's create a new folder for components and this folder will be used specifically for components that we might want to use in the game we'll also create another folder and this one will be for objects now the state contains the states is the menu game those types of states the objects will contain objects that we can touch so the player is an object and then the components the components is basically like the text it's not necessarily an object that we can crash into with the player or something like that so yeah now with the player we can move the player to objects there we go move then here we want to say objects player since now the player is in the objects folder we don't actually use the player here so we don't have to worry about it there if we run the game everything should still work fine cool then the components we can create a new file called text dot lua because it's text we include this main.lua and now we can start working on the text so first let's local love is equal to require and just love now you can create a function call it text it will take in some data and then it will return something of course a table and then at the end we can just say return text there we go our basic text object setup now we need to think of properties that text may have so let's actually split it up into a few things here let's go parameters now first i think all text will have some text which would be of like a string or a text type that will be displayed and this text will be the text to be displayed and it's required now you might ask why am i doing this like what does this benefit us with if i were to hover over my text you'll notice now we have parameters text string text to this takes to be displayed required this is called documenting your code so if someone else in the future comes back to our code here they will be able to just hover over a function wherever we use it and see what it needs so that's what i'm doing here if this wasn't here then as you can see we don't get any description so that's what i'm doing here now this is quite a lot of we're going to have to type so since i'm too lazy i'm just going to paste this from the already existing script code there we go we need an x for the x position of the text so from the side of the screen a y for the y position of the text from the top of the screen all of these are as far required we also didn't want from here everything is optional but we also want the font size the default will be p and i'll set up this in a second where we'll actually have some default versions if you know html then this will help you greatly and here are some of the options we can use we didn't have a fade in which will allow us to fade in the text so let's say if it's a game over we want the text to fade in as well as the text to fade out this is just the boolean so true or false if it should fade in and or fade out wrap with this is when the text should be broken so this should be when should the text break the default will be the window width but you can change this and you'll see later where we can change this then there's the align so where will the text be aligned on the screen it's like center left right all of that and in the opacity basically this will allow us to set a default opacity for the text so the text should maybe be a little bit see-through or something along those lines we can now implement that in here so we want the text the x the y the font size the fade in fade out wrap with a line and opacity quite a lot you don't need all of this but i feel like giving as much choice as possible is always a good idea now here above return we can set some of the default values so you can also do this in return if you want to but most of this will be useful outside of it so font size is equal to the font size that has been passed in or it will default to p for a paragraph so paragraph size we can then just create a space and copy five times we didn't want fade in fade out so fade in fade out wrap with a line and opacity so all of these so everyone that's optional then we want to replace all of these with those values and of course these as well and in the fade in would by default be false this will be false this wrap width will be love dot graphics dot get with the line would be defaulted to left so right here on the screen you can make it centers it's in the center or you can make it right so it's on the right and then the opacity here will default to one now we also want to set some constants first we can start with the text fade duration because we won't use the fade just yet but doing all of the setup beforehand will save us a lot of time in the future but you don't have to do it right now but i do recommend you still do it right now if possible local text and this is going to be a constant so it will be all caps fade there for text fade duration we'll make that to 5. now we have the problem here of this right here how will we set that now we can do that by creating something that can hold all of that so local fonts and here we can specify the fonts we want for example h1 will be the size of love dot graphics dot new font and we can make it something like 60 for like 60 pixels we need to copy that two three four five six seven times then here we can just go h1 to h2 three four five six and make this p you can add more you can also use a different structure i just think that to me at least this makes sense since i use html and in html h1 is the biggest text you can have unless you're modified with css but it's the biggest text you can have and then p is like regular small text just regular text p is also about 16 pixels and in h6 is usually smaller than p h5 h4 h3 h2 and an h1 there we go so there's all of our text sizes we won't use all of them but they exist if we need the variety from them you can also give them different names if that's what you're into we also want to set if fade in so if we try to fade in then you want to give the default opacity actually a lower value because if we want to fade in then the default opacity should be pretty low says we wanted to fit in if the default opacity is one it's not going to fade in it's going to pop in so opacity then gets overwritten to 0.1 so no matter what they put in here if we say it's fade in then we're going to force it to fade in by making the opacity 0.1 cool we have now done all of this setup so let's use what we have here first we can go and say text is equal to the text that was passed in x but not that love x is equal to x y is equal to the y and opacity is equal to the opacity now whilst i could put all of this inside of here as well currently i don't think i'm going to use any of this outside of this text so i'm going to keep it here since i'm not using it outside of this text but if i do i can move this for example the opacity that i moved here i can move it to down here because what's in return is things we can use outside of this right here so we can't use this or this outside of this text function however we can use all of this outside of the text function now we can also set our default colors for the text so colors is equal to r which will be one meaning basically full red g will which will be one and then b which will be one so red green blue and since they're all one this also means that by default they are white so the text will be white on the very dark black background now we want to also be able to set these colors at any point and you could move these colors here if you don't think you use them outside of this text function but here we could go set color equal to and we can create a function this function will take itself red scroll down green and blue then here we can say self dot colors dot r is equal to red self dot colors dot green is equal to green self dot colors dot blue is equal to well is equal to blue so now if we use the set color it will set the color to whatever we pass in so we don't have to type this out every time we want to set the color and now for the last function that we'll implement and then we'll wait a while before we try anything else is the raw function because we still need to draw the text to screen that's a function function takes itself and now we basically need to say what text we want i'm going to call this tbl text because the text will later be stored inside of a table and also index and this table is what we're also going to pass in here with the index of what we want to use you'll understand this in a second once we use it we can then go if self.opacity is more than zero they want to do a bunch of things else if the opacity is not more than zero then we want to say table dot remove and we want to remove from tbr text the item at that index that should be t basically this is for our fading stuff so once it fades out we want to remove it from the table because if it's still in the table let's say after two hours of playing the game that means everything that had was text is still in the table two hours later and every table requires ram and the more ram you use the worse the game will perform so if the user plays the game for let's say 10 hours then by hour 10 their game will probably reach like 5 fps or something but yeah and we can just return false because that means there's nothing left to draw otherwise we can return true because there's still stuff left to draw okay now here we can start with the drawing things we can leave the fading stuff for later so let's first go love dot graphics dot set color and here we can say self dot colors dot r as will give us red self dot colors dot green and in self dot colors dot blue and of course self dot opacity and it will set the color of the text we currently want to draw then you want to set the font of the text you want to draw so love dot graphics.set font and then we can pass in fonts and then add the font size so the font size we can find right here which will be something like p or h1 and we pass that in here to get the font size we'll play a little bit with this to kind of get the idea of what it does then we want to say love dot graphics dot print if specifically printf because of how much it allows us to modify how much we can like just do things our way like customization so first we want to specify the text of self.text and then self dot x for the x position self dot y for the y position the wrap with and then the align so as i said you don't have to make a lot of these things things you need to pass in that's why they're optional you could just hard code him in here but i wanted to make it more robust i think that's the word then also after we have done all of this we want to change the font size back to p so we have a standard font size everywhere now that we have this i think we can try it out and then once we have tried it out we can implement the fade in and fade out we won't use it yet but we'll implement it so let's go here instead of game and let's create a draw function inside of the game draw is equal to a function self and also faded i'll explain that in a second so first we can say if faded then text and this text is what we need to import so we can actually just go here to main copy this import go here paste it but now we can say text and text comes from components slash text and we should just say dot dot and then a slash to specify go back one directory so one folder since we're here we want to go back one folder and then jump into the components folder that's what this means those two dots means go back one folder also we can just say local love is equal to require love so yet again it's not required to specify this it's just optional but unlike specifying it anyways and then down here we can say text and we can pass in a bunch of things if we hover over text then unfortunately we don't get any of the documentation we wrote this probably still needs to import all of that but for now it's fine so the text we want to display is going to be paused then we can say zero and that will be for the x and then love dot graphics scroll down a bit dot get height and we can just say times 0.4 and this will kind of position the height in the center of the screen we don't say 0.5 because you should remember that the very top of the of the text is actually where the text gets drawn so if we put it at 0.5 it's actually going to be drawn underneath the halfway mark instead of in the middle of it here we can specify the size for example h1 we can specify false and false for the fading and then love dot graphics dot get with and this is for the word wrapping and then where to align it to and in this scenario center if we run this or if we not run this everything can go and say draw now if we run it we won't get anything because we need to actually now use this draw inside of main so down here if we scroll we have a player.draw but now we can actually use our states to our advantage so we can say if game.state dot running or if the game dot state dot pause so if it's one of these two states we want to draw the player as well as game draw game.state.paused so we are passing in this game.state.paused the reason for it is because game.state.paused is a true or false value so currently if we go here then we are trying to get a true or false value so if it's true then draw this if it's false don't draw this we'll expand on this more later but for now this is fine all right so now we're saying draw and it will basically execute this right here and if the game state is not paused it will not draw anything inside of here because faded will not allow it to it's faded because we're going to change it later but yeah for right now we can just say love dot and see if it works okay now let's go and pause and there we go we get paused isn't that absolutely amazing so that's pretty cool let's make the text a bit smaller let's go instead of h1 we say p if we run that then the text is now super small so we made it able to change the text size by literally just changing one line let's change that back to h1 to make it nice and big now this is more for the future currently it won't benefit as i lost to type this however in the future it will definitely benefit us to time to have this so let's go here underneath opacity and say if fade in so if we should fade in do something else if fade out so if we should fade out then because we don't want them to both happen at the same time so we're using an if else statement so if fade in then we first do fade in and then we can change fade into false and then do fade out because we don't want two different if statements you want one if statement that just has an else if so if we want to fade in it will fade in but it won't try and fade out at the same time then we can go self dot opacity is equal to self dot opacity minus and here you can say one y is a little big though because our opacity is a range of zero to one so by saying -1 will immediately go from seeing text to not seeing text so we can divide that by text fade duration so this 5 right here so depending on how much you make this the text will fade slower and here we can also just divide it by love dot timer dot get fps there we go and this will nicely fade it for us so it looks nice once it fades out we're not going to use it in this tutorial but we will use it in the future then here in the fade in we can check the opacity because if the opacity is more than one that means we have faded in so we now want to fade out if possible so we can check if self dot opacity is less than one so only if it's less than one then increase the opacity else if it's not less than one that means it's more than one then you want to set fade in equal to false now that we have set fade into false here that means fading will be false so this will not be triggered so if fade out is true then we'll fade out the text then here we can say self dot opacity and it will actually just be the same thing so let's just copy that but this time instead of minus it will be plus and now we have implemented fade out and fade in cool now we have added the text and we can use this text in a lot of the parts of the game and hello everyone if you're enjoying this tutorial please leave a like and subscribe for future tutorials so currently if we pause we have a paused text if we unpause this no longer there if we move around we can move around but the game is still pretty boring because currently we can't do anything we didn't have any enemies we just fly around we can fly off screen but we'll just come back so that's no fun so what are we going to do well of course we should add the enemy we should create the enemy and the enemy should of course be the asteroids that's the whole reason the game exists to avoid the asteroids so let me show you an example of what we will be doing today we can move this here there we go we have an asteroid we can't really do anything with the asteroid yet but we have an asteroid and this is the idea we want to be able to create multiple of these guys without any problem you also notice the red around the asteroid just like with the player that is the hit zone so if our players rid around it and that asteroid raid touches each other then boom the game ends and we'll implement that later yet again as the circles are not super accurate but the game itself isn't going to be super complex either if you want to give yourself an extra challenge you can try and make that more accurate than a circle awesome now one thing i want to do before we get into actually coding the asteroid is i want to add a new feature to the game so i'm going to remove this text here go into player and i want the player to now once we draw the player i want it to take in faded like that and once it takes and faded i want us to decrease the opacity of the player so here we can say if faded then we want to set opacity equal to 0.2 you can make it lower or higher depending on what you want now because we have this faded right here we can now go to main and here we can just pass in if the game is paused if the game is paused we want to fade out the player so let's run that and just see that it works so pause i don't see much of it oh wait we don't pause pauses this button as you can see the player fades out so pause the player still there just fade it out we want to implement this for the entire game so where everything fades out once you pause except for the debugging stuff of course cool now that we have that we can also implement it at different places so let's create a new object this object will be called asteroids you could call it asteroid but in the script i made it asteroids accidentally so now we'll have to stick with the scripts but we can say dot lua and now we have an asteroid but you could call it asteroid that would be perfectly fine we can then say let me just close this as well local love is equal to require love i just love saying that i don't know why we're gonna say function and asteroids and we can just return an empty table and then return asteroids we have created our basic asteroid object now first we'd want to take in the asteroid x position the asteroid's y position its size it's level so depending on the level will also depend on some things that asteroid might have and then debugging now here we can say debugging is equal to debugging or false so basically it's going to be either debugging or not debugging next we want to set things such as the asteroids vertices this is basically how many edges the asteroid will have we'll play around with this later so i can actually show you an example so this scenario or in this case we can just say local asteroids and of course you can make it something different if you don't like 10 specifically you can make it 11 15 50 5 whatever you choose i'm going to make it 10 because i actually found that that looks pretty good then we can also set the jaggedness and the jaggedness is basically just saying how round the asteroid should be the more jagged the less round and the more epic it looks but also the less accurate our collision detection will be so make sure it's not too jagged unless you're going to upgrade that collision detection and then we can say asteroid jag for jaggedness i'm not going to spell jack jagged i don't know how to spell that but it's 0.4 but you can make it more or less i found 0.4 looks pretty good then you can go local and then put the asteroid speed speed and that is equal to math dot random why are we saying mafta random because the asteroid speed itself should be random or am i incorrect because if all asteroids are the same speed the game would be less fun but if this asteroid on the left moves five times faster than the asteroid on the right then that would be pretty cool just as an example meaning put in 50 so it gives in a random value till 50 and you can say plus and level times two so depending on the asteroid level will add to the asteroid speed so let's say it's 50 and the level is five so there's 50 plus 10. you could increase this level to something like three or four and that will also make the leveling system much more difficult but i'm going to keep it low for now cool and we'll come back to this here at the top i'm currently just going to go to return for now we're going to specify x is equal to x so the x that's being passed in y is equal to y oh no dot love oh gosh that was supposed to be a comma and then we can say the x velocity this means how fast the asteroid will move on the x-axis so from left to right that will be equal to math.random and now we can say times asteroid speed so that's the one we did right here times velocity now velocity will determine if the asteroid goes forward backwards up or down so an x and x axis it will go either forward or backwards on y axis it will go either up or down so let's quickly create that so let's go here let's say local velocity is equal to negative one by default then we can check if math.random and here we can specify it's less than 0.5 because map to random will give a value between 0 and 1. then and we want to say velocity is equal to 1. so it's negative 1 but if master random is less than 0.5 then we're going to make it one and this will decide where this is going to move this way or this way we don't have to worry too much about this now we're just setting it up now you may have remember before we continue here that in my lua tutorial i did cover math.random and i did cover how it's not always random so to make sure that the player doesn't get the same game every time they boot up the game so the same asteroid is the same way the asteroids move everything we should make it more random before we continue here because i might forget about that meaning go to main and here we can just specify right before the load right before everything math dot random seed as we'll see the randomness and say os.time and this will make everything more random it will seed it to make it less predictable cool now you can go back to asteroids and you can copy this x velocity and you can make a y velocity as well for up and down and it can have exactly the same math so it may not move the same speed up and down as it's moving left and right which is what we want now we need to set the radius of the asteroid because in the end of the day the asteroid is pretty much just going to be a circle so we can say radius is equal to math dot seal for math dot ceiling so always go up and then asteroid size divided by two and this will give the asteroid's radius cool i'm going to leave all of this here for a second and i'll come back to it a bit later let's draw this asteroid because i'm not really sure if we're getting everything right here so let's go function self and also if it should be faded because if we pause the game then we can yet again say local opacity if that is or that is equal to one but if faded then opacity is equal to 0.2 then you want to set the color to not white but kind of like a grayish white because when i think of an asteroid i don't think of it as a pure white blob just flying to me in fact i think of it as a bit dark actually so let's make it like a grayish color in this scenario since the background is pitch black we can't really make it too dark because then the player will see it so you'll notice it's not pure white as you can see here by these numbers you can tweak these numbers to kind of fit your need for what you want the asteroid color to be but yeah that this will determine the asteroid color now next we need to determine the asteroids points now the points basically just say where the asteroid is going to be when we draw the polygon we have a bunch of points so you may have remembered when we draw the player so local points let's go here is equal to and here we can specify a table and then say self dot x plus self dot radius which is right here times self dot off set now offset is just so we can get a little bit of an offset so the game or so the asteroid which is like a circle if you remember with the player it needed a little bit of an offset so the center dot can always be in the middle if you could remember what we did so if we go here and say love dot then you see that little circle that little red dot in the center of our player that had to be on an offset which was i think like -1 and -1 because it wasn't quite in the center of our triangle and in this scenario we'll have the same problem so we're setting it offset now to generate the offset we can go up here and we can say local offset is equal to an empty table then we can say four well not in pairs four i becomes one two and two where to where specifically are we going to set this offset well that depends on the amount of vertices we have so in this scenario we have 10 vertices but we don't want all of the asteroids to have the same amount of jaggedness right we don't want that asteroid to have five points and then the one next to it also have five points we won't want to have two and want to have ten and once we have four we want it there to be variety so let's create a more interesting vertice but you could if you wanted to just put it here put this here like that that's perfectly fine but i wanted to be a bit more random so here we can say local vert for vertices is equal to math.floor because we also want to go down but you could make it go math.cl in fact that might even be better i'm following a script but you could make a method still and that would work perfectly fine math.random because we want to get a random value so just right here so you pass that in there and just say plus one so we don't get zero plus and we paste that android vertices in there again divided by two and this will give us a nice random value which will also determine how less round r asteroid is so how many points it has edges now here instead of saying this specific just always 10 value we could specify vert plus one and now we can insert an offset and this will always generate offset at the correct location there is a table dot insert and then the list and value in this scenario we want to do offset and math.random and i'll explain this in a second times asteroid and let me just scroll down so i can autocomplete that times two plus one minus asteroid jack the thing is i can't quite explain this math here because you can't visualize it that really sucks but once we see this asteroid we'll be able to play around with this i can explain what's happening but take note before we continue that this right here is not the same as saying this they are not the same just take note of that for some of you might want to do that but there we go and we can play around with this later so i can show you an example but for right now let's just leave that like that so we have this vert and offset and i can get the vertices like this so vertvert and then offset is offset there we go so now we have both of these that we can use here so back to points so now we have self.offset which is basically everything we just did and now we need to specify at what index because there will be multiple because if you could see here we are forwarding through this and inserting for every item we're inserting a new offset or a new item for that offset the new location so now here with the points we need to specify and i'm just going to say one so the very first one we have times and we'll later modify this to be a bit more interesting math.cos yet again i can't quite explain this yet because we can't see it so we don't really know what we're drawing here but i'll show you in a second you can solve that angle in there and then with the next one we can just copy it and instead of saying x this will be y keep everything the same and instead of cos we say sin and now we didn't have this angle though you may have noticed that we can quickly generate something that will give us an angle just like with our player how we have an angle we can do the same here we just say angle is equal to math dot red for radians and then math.random and we just say math.pi you don't have to worry too much about the math we're doing in this game div course the more important part is that you can see how everything fits together to create something interesting in so if the math goes over your head don't worry there's a lot of games out there that you can build without all this complex math and in most cases you can do what i do and you can just literally copy math from online because i watched a lot of tutorials and i looked through a lot of documentation and i was like i don't understand any of this i copied paste test and it worked that's what i would recommend you do as well if you're not very good at map if you're good at math and just do this may of course coming out soon at some point so stay tuned if you want to improve your math cool so now we have our points but we still need a lot of the other points as well so this is just the first batch of points because if you remember we need at least two more here in order to complete it into a full triangle so we can create a for loop and then for i becomes one oh that should be i i becomes one to self dot vert minus one so the amount of vertices minus one do and we can just say table dot insert we can specify that we want to insert into points so this one right here and then what we want to insert for now i'm just going to copy this and modify it because it's quite a lot of typing because math so it starts with x plus cell the radius times the offset but instead of saying 1 we say i plus one because here we specified one but now we want to go two and three and four and five if you wanted to you could just specify that i should be two here from the get go but this works perfectly fine as well and then we can specify math.course self.angle but now we need to add a bit of extra mass so we can close this polygon we're drawing because remember a polygon should close if we just drew a triangle with like just three angles they're not going to close itself we need to make sure it closes for example if i were to open up my drawing app here if we just drew this one two three that that's not a triangle is it this is a triangle a triangle closes itself so we need to add some extra math to do this at some point or here or here but not this this is bad because it doesn't close itself it just stays open so we need to do some math to do this like that so now here after the angle we say plus i times math dot pi times 2 divided by self.vert there we go that should be divided awesome we can then insert another one and we can just skip over the math for now because there's a lot of math here which you don't necessarily need to cover at the moment and we can just specify why here everything else will stay the same except instead of cos we'll have sin cool now we can actually go about drawing what we have so we can actually display it on the screen and see what we're working with because currently we're not drawing anything so we're going to say love.graphics.polygon and now we just specify that we want a line oh gosh i want a line and points because we generated the points right here it's inside of a table we did the points and now we're passing in the points so instead of doing what we did with player where we just passed in the points like this as you can see here the math is pretty much similar we did this instead and generated the points for us because asteroids won't all have the same amount of points unlike the player which only has three they might have 10 or 50. we have no idea and we don't want to draw each one of those out by hand because that's going to be a massive pain so we use a for loop to do it instead cool and let's just add some debugging in case you want debugging so if debugging then love dot graphics dot set color one o o so red and you can also specify love dot graphics to draw a circle we can specify we want a line and then we specify self dot x to give it a self x position and a y position and then you just specify the radius and there we go so this would display the hitbox of the asteroid now i still want to implement movement but for right now i think this is fine let's just go to main because i want to see what we draw you can probably hear my voice how excited i'm getting to seeing what we drew let's go down here to draw and now we actually need to generate an actual asteroid however i am wondering if doing it here would be a good idea i say we should rather open up the game states and we should implement a function that could benefit us in the future for example what if we had a function that says start new game and that's a function that takes itself and the player and i'll explain why in a second and this every time starts a new game when we tell it to so let's say the game ends because the player died or when the player says play game then this will trigger and it will restart the game so you could say self and then change the game state to running because we're changing the game state back to a running state since that's what's happening then we are creating a global scope very important to remember this a global scope variable called asteroid so underscore g dot or just plain out here asteroids just lowercase a this is a global scope variable meaning it can be used anywhere inside of this file or actually anywhere inside of this game now because we're going to import this file later we're going to use this and it's going to generate this global scope function or this global scope variable this isn't the best way to do it take note in fact i might even recommend you do asteroids here and say asteroids and in doing this or finding even a better place to do it than here but for now this will work fine we make that an empty table take note it is global scope you will get confused if you do not take that as note then we specify the asteroids current x and y positions so you could do this without needing to put a variable but i'm going to use a variable just to keep things simple so local asteroid x position is equal to math.floor math.random it doesn't need to be random you could specify a specific position for example we could go here and say table dot insert because we want at least one asteroid asteroids and then what index and then what value and in this scenario asteroids object we can go up here and you can copy this and change from text to asteroids change from components to objects and then here instead of saying text we say asteroids and this will import this asteroid object which we are using here and then we specify the x and y so you could give something like 60 100 that's perfectly fine then 100 for the i believe that's the radius if you look here we go up here now that's the asteroid size so depending on the size or depending on what you pass in there the azure will be bigger or smaller the level which will in this scenario just be self.level so it will level up depending on the game level and then true for debugging now you could do this but it's boring because we want asteroids to start up at a different location every time we start up the game so what we could do is we could make it random and that's what i'm doing here and we say love dot graphics dot get with because it's the x position and it can appear anywhere on the x axis same with y but this time instead of saying with we say get height and we pass in the asteroid x we pass in asteroid y we save that and now we can go here back to main then on the game load when we create this game we can say game colon start new game and we can just pass in the player now currently we're just passing in the player this is just for future reference we don't have to worry about it too much now but we are passing in the player because later once we start a new game we want to position the player in the center we want to give the player more hp and so on and so forth so by default that means we'd want a player we could also specify and push in some asteroids in here if you wanted to as well but this is fine for now now be remember asteroids is a global scope variable so don't get confused about what we're doing here because remember it's a global scope very important to take note of so for and we can say underscore because we're not going to use this this is the index we're not going to use it asteroid and asteroids global scope remember that you might get confused so maybe make it somewhere more global or more local instead of global but for right now just remember this is global scope because you will get confused if you don't remember now you can say asteroids or asteroid and it just say draw so called the draw function for every asteroid we have so if you have multiple asteroids then this will call it for multiple asteroids and just pass in game dot state dot pause so if the game is paused so if it should be faded out or not let's run this and hope for the best and ooh asteroid 8 can we perform on local level a null value that means we may not have passed in the level do we actually have a level here yet no we actually do not have a level here yet my bad okay so let's add a level we are currently at level one we can start there now if we run this where's the asteroid i do not see the asteroid let me draw it again i do not see the asteroid oh you know what i think this could be the reasons let's remove that 60 i think i just forgot to remove that let's try and run it now and in here we need to specify the size so i got so confused of all of these things but yeah now i think it should probably yes there we go so now we have an asteroid and let's maybe spawn another that's pretty cool we can of course have multiple asteroids let's just maybe make a four and then we can just copy this for every asteroid x and then copy this for every asteroid bar just so we can have some of an idea of what we see now if we run this we'll have four asteroids that spawn randomly cool and they all look different and that's kind of what i've been doing this whole time with all this math.random this one has one two three i can't actually show my cursor but yeah so you can see they have different amounts of like spikes and the the spikes also look way different compared to each other let's play around with some of the math just so i can show you what i meant if we go here to the asteroids of course if you were to change this to 90 for example then now as you can see they're like super spiky so if you the higher you make that the more spiky they can become that actually looks kind of cool but if you were to make this something like two then yeah you need more than two to actually be able to draw polygons let's make it four i think that should be enough then there you go then they all look kind of plain and in here of the jaggedness you can see how jagged it should be so if you make it a nine then it will be like super pointy and super cool looking but as you can see you have the red circles the hitboxes are not very accurate so unless you're going to do some extra math there to make it accurate i do not recommend making this too high i did pretty f4 but you could put it somewhere else like let's say at five should still be perfectly fine yeah it's still mostly perfectly fine not perfect but yeah and then let's say you were to make it 0.1 then they'll basically all look super round and you make it a 0.0 i wonder what will happen would we crash no but it would look pretty much very round and then the more vertices you add for example let's say 19 then now it'll look like a normal circle because there's just a bunch of little lines being drawn and of course if you were to make this a nine again then now it will look super weird so that's kind of what these were so if you were confused i hope this also cleared it up for you astro speed we're going to work with in a second and here's the offset but you don't have to mess with right now but if you want to you could and you could like remove some things here and see what offset will does we'll do so if we select 10 for example what will happen then as you can see not so great and of course if you wanted to you could say fill instead of line and that will give you like a filled version but then i would recommend choosing a color a bit more gray than the grayish white color we chose because then it just looks weird so i'd recommend something a lot more gray if you want to go something that's not line because it currently it kind of looks a little bit faded compared to uh ours and of course if you pause they'll all fade out a little bit and on pause pause like that cool and that's the drawing section completed some of you might want to take a break right now i'd actually recommend it but if you think you still have the guts to go for it then let's implement the movement so here we have a nothing starting game but in asteroids here we have the draw but the draw is now completed for now so we can now go and say move and move is a function takes in self and dt dt because we actually want to start implementing some good ways to move our objects without using fps because as i said fps is not a great way to specify how fast something should move use dt and remember dt is the time between this frame and the last frame so it's usually very small and it's self dot x is equal to self dot x plus self dot x and we can say bill for velocity and that's right here so that will basically with every move it will move x to where it currently is but plus its velocity and then just say times dt to make a move at a consistent rate so if it's 30 ps or if it's 60 fps it will get from one point to the other point in exactly the same time and you can do the same for y but it's time for y velocity cool so that will move it now we can go to main and we can go up to update where the game state is running and we can actually just copy this paste it here and this time we can call this ast index but you don't have to in fact you could possibly just keep this underscore because we're not necessarily going to use it although we might in the future so i'll keep that there because it didn't make sense to able to access the index from here but as for future reference you could make it underscore for now instead of draw we say move and we pass in dt where do we get dt dt gets passed in from update so update takes in dt thanks to love2d we take this dt and we pass it in here if we run this then now it moves which i think is kind of cool right again that will move a different direction move it again now it's incredibly slow move it again now you'll notice as well oh gosh i closed my terminal but you'll notice as well when i get back to everything that love dot if we run it then it can go off screen just give it a second my player can actually go with it off screen just to show you something you'll notice my player comes back but once the asteroid leaves the screen it never comes back we don't want that so what we can do is we can go to player go to move player copy this go to asteroids and then just paste this here and it will do the same thing as with the player it will make sure the asteroid stays on screen so let's run this and make sure it works and it's going to take a while to get off screen now there we go all right give it a second you can also put some extra math here to make it appear sooner on the other side but i think this is fine and there it goes it now appeared and just like that we made sure that the asteroid never goes off screen forever cool and of course if you want the ashrae to move faster you could upgrade the speed here for example let's just go plus nine for an hour uh let's go plus 10 actually i think that would be pretty good and now we'll move usually much faster but it's still random so let's maybe go here and just make it a default 150. let's see that should make it move pretty fast yeah there you go so you can just upgrade that to make it move faster or slower i maybe say increase this times two to maybe times five or times four times three depending on how difficult you want to gain to be this is something you can tweak to make the game your own to make it something that's just more than what we have and yeah that is that in this tutorial let's add the lasers you know because when you have a spaceship you can go pew pew pew and that should be destroying the asteroids whilst we'll not be implementing the destroying of the asteroids yet we will be implementing the lasers themselves now the lasers are much easier than you might think they are they're basically just dots on the screen that move around so there's not a lot of complicated math in drawing them it's just a little circle or square that moves so to give you an example of what we will be doing let me just go here and start one up all right so here is the example if i press space it shoots an asteroid there is limited amount of asteroids allowed on the screen so if you have too many asteroids on the screen it will remove some of the previous asteroids but yeah currently if you shoot an asteroid it can't because the lasers cannot destroy anything yet and i might have said asteroids earlier i mean there's a limited amount of lasers that can be on the screen not asteroids a limited amount of bullets all right it's that simple let's first create a new object and we can call it laser dot lua because this is an object because it's the laser is a bullet then right here we could just remove that and we could say function laser then we want the x position the y position and then the angle it's shooting at because the player might be at a specific angle and we want to shoot out of that angle so the player is looking upwards we won't shoot upwards of the of that angle anyhow then this will return a table and we can just return laser and there we go now first we want to set the laser speed so sitting this will determine how fast the laser moves so how many pixels per second in our case i think about 500 pixels a second is perfectly fine to some of you that might be a little fast so you might want to turn it down or to some it might be a little bit slow but it's up to you so local laser speed is equal to 500 so in pixels per second we can insert the x equal to the x that was passed in the y equal to the y that was passed in now let's first draw what we have so let's draw that's equal to a function and this function will just taken self and fade it as well so if we pause it also fade out just like everything else and we can say local opacity is equal to one if faded then opacity is equal to 0.2 then love dot graphics dot set color and you can make this red or green or whatever color you think a laser should be but i'm just going to make it white but you can customize the game and make it red green blue purple whatever color fits your eye the best you can make everything colorful and much more playful and we can just also pass in the opacity since we are literally just using a dot we can use a point so just like a point on the screen luckily love2d already has this built in so we can just say love dot graphics dot set point size and this is how big the dot will be i'm just going to say free but we'll play around with this so you can figure out what size is perfect for you then we can say love dot graphics dot points and now we can draw the point on self.x and self dot y and we forgot to require love but yet again it's optional so local love is equal to require love there we go nice we'll add this later but for right now let's just see how everything looks if we put it on the screen alright so let's go to player because here is where we'll be adding lasers we can go to the top and add a new item we can call it lasers and that will just be a table that's empty because we can have multiple lasers so we'll just have a table that can hold all of the lasers for us we can then scroll down and maybe right above the draw we can add a shoot laser function this function will allow us to shoot a laser and that's it this could be inside of lasers or a sort of game or where what not but i feel putting in sort of plays player makes the most sense because if you say player and then colon shoot laser it makes so much sense but if you say just laser shoot laser it's kind of weird so if you say player shoot laser then you know the player is shooting at laser and if you add an enemy like maybe a spaceship and a spaceship can shoot lasers then yet again you can just say spaceship or enemy colon shoot laser you can follow the same structure and it would always make sense cool now for now let's just go shoot laser and that can be a function that takes itself and here we can just go table dot insert self.laser and then create a laser object and we need to import laser so you can say local laser is equal to require and then object laser i don't know this dot syntax works but if it works let's use it but if it doesn't work you can use a slash i'm going to try this dot syntax because my ide constantly auto completes it like that for me so let's see if it works now down here where we do this we can go laser and then it needs the x axis and the x-axis will basically be the same as our player but we'll tweak this a little bit just to get it right let's go self dot x and we'll tweak it so it's fine like this self self.y self.angle cool we'll tweak it more later because this won't necessarily work that great yet but i just want to get it on screen now if we go to main we can go maybe right here to where it says this key release and we can say or actually let's leave that for now let's go here first and we have a few things we might want to do to shoot so there's a few things that the user can press if they want to shoot first i think space is a good item to press then as well as down like the down arrow key and then kp five like that is the five on the numpad i think that's also a good one and in here we could just say player shoot laser and it will shoot the laser now also want the laser to be shot if a mouse is pressed so let's say if the right mouse button is clicked we want the laser to be shot so they want to play with their mouse they can so let's actually do that so we can say function love dot mouse pressed and this takes in quite a few things so first it is x it is y it is the button that has been pressed is touch for example if it's a touch screen and presses which is also for if it's a touch screen i believe this x and y is mainly for the x and y position on screen so let's click here and that would be like 50 50 x y now here you can say if button is equal to one and this is their lift mouse button i think then you can see if game.state.running so only if it's the running state player colon shoot laser and that will shoot laser cool but currently if we're trying to shoot the laser we won't see anything because we are not drawing it so let's get to player and let's put the draw also here so every object can basically contain its own version of the laser so if you have multiple lasers on the screen then every object will handle their own laser so let's go here to draw let's just do it at the very bottom so four and we can just say underscore because we don't need the key laser in pair self dot laser and we can just say laser colon draw and then faded and is faded is basically what's being passed in here just like that let's see if this works okay space and there we go currently they didn't move because we haven't put anything there but now we can create a bunch of cool little lasers and we can not use our mouse button and the reason for that would be let's see ah that should be state like that now we can use our mouse button as well cool so these are our lasers as you can see they're just little dots on the screen there are only little dots because if we go to lasers we're using points if we were to increase this to let's say 30 then it will be massive dots as you can see so it depends on how big you want your lasers to be some people would like them to be really small although then it's kind of difficult to see them oh gosh where's my player nah there we go as you can see it's very difficult to see them so i think free is about a good size but you could go up to something like five or even higher depending on what your preference is 35 is a little big for me personally i'm going to go free because that just looks perfect cool so let's implement the rest of the things into our laser class so next up we want to move the laser so we can go move is equal to a function this function just takes itself now just the lack of the player we need velocities x and y velocities so we can go here and we can say x will for x velocity is equal to the laser speed times math dot cos passing the angle that's the angle we got here so the same angle as the player divided by and then love dot timer dot get fps yet again it would be better to rather put something like dt here just because it would be safer but i don't think it's that necessary for a game this small but if you plan on making this game much bigger then you could definitely implement dt here next we can do the same for the y velocity but this time instead of sin or cause we can go sin like that now we have the x and y velocities we also at some point probably want to remove the laser from the screen and how we determine that is based on whatever you want you can base it on time for example if it's been on the screen for five seconds remove it or something like that i however i'm going to determine that based on the distance it has traveled so let's say it can only go one and a half way around the screen or around the window something like that so we can just say distance for the distance traveled and we can make that equal to zero cool now we can continue to this move function here and we can say self dot x is equal to self dot x plus self dot x velocity we say x velocity because remember that's how fast it moves on the x-axis cool and then we do the cell same for y nice now we want to keep this on screen so we can actually just go here and we can copy this you could possibly make this a function and you just reuse the function constantly because we're copying and pasting this the whole time so it would actually be better to create a function and put this in but that can be your homework if you want to walk the extra mile now for this right here we don't have a radius because remember we're just a dot on the screen we are no longer a circle we're just a dot so here we have the self radius we can just remove that so like that delete now should be self dot x less than zero we set x to the width if it's and there's not the radius of a minus so let's just also remove all of those there we go if it's more in the width then we want to set it to zero and not the radius because we don't have a radius so we're going to set it to zero because remember zero would be this point right here where the window starts same here the y is zero would be this point right here where the window starts on the top next up we want to say how far it has traveled so the distance it has traveled so self.distance is equal to self dot distance plus math dot square root and here first we could say self dot x velocity to the power of two plus self.y velocity to the power of 2 and this will just get us the distance it has traveled from point a to point b there's a part here cool you could use some different math if you want to but this works perfectly fine nice now we have the laser and the laser can move now let's move the laser so i'm going to do that right here inside of this player so right at the bottom and we can just move the laser from here so for index laser in self dot lasers we can then say laser colon move and this will move laser now if you were to run this the laser should move oh gosh it's moving but it's shooting it in the wrong direction okay so we did some wrong math but now the lasers they do shoot so let's just go back to lasers and see what we got wrong oh and right here this should be negative because it's going to move in a slightly different manner now it should go out of yes now it goes out of the top cool now that that is fixed we can have a ton of lasers on the screen i think that's actually pretty cool now never do you really want this many lasers on the screen you want to limit it so we have to basically tell the lasers to destroy themselves once they're completed with all of this once they're done so let's go here to the player and implement that so let's go to the top and we can specify the amount of distance the lasers are allowed to travel so you can say local laser distance can also call this max laser distance if you want and i'll make that equal to 0.6 so they can basically move 0.6 the width of the window before they get destroyed so they can't go the whole width but they can go 0.6 of it you can change this to make it them travel further or less depending on what you prefer we also then specify the max amount of lasers on the screen and that can be 10 you could make it more or less depending on how difficult you want the game to be when you implement maybe levels you could make that let's say on the the most difficult level you can only have like three lasers on the screen at once cool now we need to implement something that will destroy the lasers so i'm going to copy this right here and instead of shoot it will be destroy and this is very basic we can just say table dot remove and we can say from self.lasers and then the index so now here we can also just take in the index so it will remove the laser at that index inside of the table now we remove the laser so down here there we go here we're laser moves we can say if and i'm just going to do this instead of some brackets but you don't have to i just feel more comfortable if i do that love dot distance love that should be laser laser dot distance if that is more than the max laser distance times love dot ooh love dot graphics dot get with and let me just fix this so if the laser has traveled 0.6 of the screen width so that's what we're doing here so 0.6 of this screen width if it's more than that then we want to remove it we can go here and say self dot destroy laser let's pass in self and index the index that we're getting here so this will be the index where this laser can be found which we're using here to destroy it right there now if we run this then as you can see the laser gets destroyed if the laser moves too fast you can make it slower if it's too slow you can make it faster we can only have up until 10 lasers on the screen but we haven't really implemented the whole 10 lasers thing just yet because we can definitely shoot more than 10 if i had to guess i'm going to i'm not going to count that but let's make it free so we can actually see the difference now if we were to run this as you can see there's definitely more than three so what we can do is we're going to shoot laser and we limit it so if the numbers in self dot laser serve the amount of lasers is less or equal to max lasers then only do we want to do this whole insert thing otherwise just ignore it don't allow us to insert another laser so we go here my bad and we run it here we currently only are able to do four because this is less or equal to so you could make it less instead and i will be more accurate as you can see only three at a time now if we were to change this here to let's say 10 then boom bob your uncle we can only shoot 10 lasers at a time we're limited now so you can see there's like a little wait period once i've reached that amount so you can tweak that to make the game more challenging or easier depending on what you prefer cool now one last thing is let's make sure all our buttons can shoot the laser okay closer down works space works the button number five on the numpad works and the mouse works okay so if all the buttons work that means we have done our job correctly we have now implemented shooting lasers in the game in this tutorial we'll be adding collision for those lasers and it will be pretty cool now i have to show you this let's just move this here if we go here and we shoot it splits into two shoots again it splits again and shoot it again now it's gone and this is a very customizable thing that have popped up here so you can make this more customizable and maybe break up into more pieces maybe move faster maybe be bigger or smaller and once you shoot it up nothing happens but we can implement that later isn't that incredibly cool also something you might notice if we were to go here and we were to shoot you'll notice it's a little explosion animation thing where it has a little bit of color once it touches the asteroid nice now i'm going to remove the whole debugging thing not from the game but just from our visual standpoint because currently i don't think we need it but if you do get stuck and some sort of error occurs turn on debugging and see what's happening because especially when we do collision detection which our debugging is exactly doing showing us where the collision happens then that is when it's most important but i'm going to disable it but i'm going to make sure that we can re-enable it easily again later cool so let's create a new file just say new file and this can be globals.lua now these are going to be variables that are going to be global scope like massive global scope everywhere this can be used and that's the point of this file so whatever we put in here we can use anywhere within the game so let's first do asteroid size and let's make that 100. we'll be using this asteroid size in multiple files so it makes sense to put it here because we're not going to attach this specifically to an object this will just kind of be there it will be able to be used whether that file uses the asteroid object or not and you want to have show debugging you can make this all capital case just like with this asteroid size but the difference is we don't want to change this asteroid size in the game it's a constant we don't want to touch this except if we're getting the value but we don't want to set a new value we don't want to change that to 150 while the player is playing the game we could but we don't want to show debugging however we might want the player to allow them to show debugging so like if they go into the settings they can turn debugging on or off if there may be a developer or is interested in that sort of thing and we can make that equal to false by default but you can keep it true if you still want the debugging information now one last thing i would like to do is do our distance calculation math here and this will just calculate the distance between point a and point b we can say function calculate distance x one x two so this will or this can be y one as well we can do x one y one x two x two or y two now this will be the x and y position of the first item then we want the x2 and the y2 so this will be x and y position of the second item we're trying to get the distance from you could put this into like your own little tables instead but i think that's overkill so let's just do it like this we can then return just a simple math function so math dot square root so squirt and then first we can go here and say x 2 minus x 1 and then to a power of 2. you could split this up into multiple lines don't get me wrong you could split this up into multiple lines and why not let's actually show you how it will look let's go here and say local test x and then we can go here and you can move that like that anyway here we can go local this y for the y distance difference and it will be the same thing but this time we will have y instead of x like this so remember it's the second y minus the first y so it's where you are going to minus where you are that's very important to take notes off and then in here we can just say this x minus i mean plus this y and this will give us the distance between x and y so this math right here not between x and y but the distance between those two values x 1 and y 1 and x2 and y2 or yeah all right and that is the basics for this right here so that is how you calculate the distance between x and y of two items and now we can close that because we don't need it anymore and also it seems like this dot notation works since my ide auto completes that i'm just going to leave it like that so you just use this instead of slash from now on but in the game code that i'm going to provide you if you ever download the code it's going to use slash and not dot cool so let's remove the debugging stuff from our objects and stuff so let's go here and to the player and here where we take in debugging we're not taking that in anymore it's now global scope and it's called show debugging instead of debugging so debugging right here so instead of debugging we can say show debugging because it's global scope and you don't have to because lua will automatically know but i would recommend you go require global like that because right there so you want to require that is this isn't required to do this is an optional step but i do recommend if you're going to use global stuff here then you can close player now here in main where we pass in show debugging we can remove that and we can actually remove this debugging as well now let's first go to the laser because that's where a lot of the things are going to happen we're going to add a few new things we're going to make them like states so first will be the exploding state and this can have three values it can have a zero for safe a one for up for exploding so if it's currently exploding and then a two for it done exploding and it also in explode time and this will hold how long it has been exploding so if you wanted to be on the screen for like one second then this will allow you to keep it on the screen for one second cool now let's go to move right here and let's first check if it's exploding so if self dot explode time if that is more than zero then we know we are exploding because otherwise it will always be zero but if it's more than zero that means you're currently busy exploding so self dot exploding is equal to one what i actually recommend you do is you set some sort of state to help you of this for example local exploding enum or something like that and it will be a table that is like not exploding and it will keep zero and then exploding and that will keep one and then i think it was the last one for if it's done exploding like done exploding that's equal to two i'd actually recommend you do this instead just to keep things simple for you and for your future self because if you're going to continue working on this then having something to manage it is the smartest idea you can have however once this course is done i'm done with this project as well so i'm not going to add it to me it's not necessary while we're here let's set the explode duration explode duration and i'll just set this to 0.2 you can change this later depending on what you think is the better time to explode so the more you make it the longer it will be exploding now we need to implement a function that once we want to explode we can so we can go here and say explode is equal to a function and just takes in south and this will just explode in quotation marks our laser now we don't we're not going to mess with the explode time just yet currently we just want to remove everything but for right now we can say if self dot explode time is more than explode duration then end self.explain is equal to two and it will just make it explode and it will kind of allow us later to do some exploding animations next up let's maybe go to asteroids so we can go here we can open up asteroids and we can implement our stuff first let's take out debugging we didn't need that anymore and now we should have something that wants to debug but can't there we go and we can change this to show debugging and we can require globals like that and boom now that that has also been done we can now go to game.lua and here it goes asteroid and it adds this we don't need that extra one anymore because that was just for debugging debugging is now global if you turn debugging off everything will have debugging turned off if you turn it on everything will have it on you don't have to individually go change which one has debugging and not so now let's go to the player.lua and then go to the way button where we are at so right here we are getting the index and laser and now we want to check if the laser is exploding so for right now none of this really matters we can just go if laser dot exploding so if the laser is exploding and we're going to make sure if it's 2 then we can just say self dot destroy laser and this destroy laser if you can remember from the previous video it just destroys the laser very simple this is going to become a bit more complex in a second but i just want to get the idea out next we can go to lasers and when we go to explode we can just make it disappear so we can just actually for right now remove that we don't need that we just hit the exploding tutu now once it explodes it's going to set the exploding two two so we should also go here and say if self not self if laser dot distance so the distance it has traveled if it is more than the max laser distance so actually this right here so let's actually just go here instead and let's just go and add and because it already has the first part so if it has to travel the distance of the max it can and the laser dot exploding is equal to zero so only if it's not currently busy exploding then we can explode the laser so we can just go here and change this to explode and this can now be laser colon explode and it doesn't need anything this destroy laser however needs self and index so we will now actually see if it works we'll move this explode up so if it works it should ex or it should disappear once we run it shoot it's gone okay so it does work we can now just make it work better because as you can see they do disappear all right now that we know it works and it disappears let's upgrade what we have so if this explode we can then say self.explode time is equal to math dot seal to make it go upwards explode duration times and here we can just say love dot graphics dot get fps and it should be timer dot get fps yet again i would recommend we actually use dt but in this scenario it's not too important and we can divide it by 100 and this will basically say how long it has been exploding for then we can go here and say if self dot explode time is more than explode duration we can just put that there and exploding is equal to two then now as we know if the explode time is more than zero so if explode time is more than zero it will sit exploding to one for us and then it will be busy exploding and we can draw things here like it is busy exploding like some nice things now the nice things i do want to draw is maybe like a yellow and an orange explosion animation type of thing and it will just appear once it's busy exploding so let's go here where we draw everything and we can just say if self dot exploding is less than one so if it's not one or two if it's zero then we can just do this what we've been doing this whole time otherwise if it is busy exploding we can go else then we can say love dot graphics dot set color and this will be of the outer explosion so the bigger explosion and we can just say one for complete red then 104 divided by 255 and then no blue and you can also pass in the opacity in case they're pausing and we can set the color for the inner explosion so the smaller explosion inside to give an example of what i'm talking about let me just go here if i shoot as you can see it's paused but as you can see there's like a little orange part and then inside of it there's a yellow part and that's what we're currently doing we first did the orange part we're now going to do the yellow part and let's actually just copy this lava color and yellow would be a bit more greenish two three four and she gives it like a yellowish color we're gonna go here and say love dot graphics and we can draw a circle dot circle and you could of course make this way more complex and cooler but i think this is fine for now fill and then self dot x to give the x position of the laser and then self dot y to get the y position of the laser and then we want to make it a spatial size we first just make it a seven that's about that's not very big but the bigger one and we can just get times one the bigger one however will be times one point five so it will be a teeny weeny top bit bigger so around ten whilst this one here this one will just be seven you can even remove that if you want and just have a seven there but this would be seven and this would be almost ten maybe ten but yeah you could make this more like two and you could put this in a variable to make it a set size and you just add to it but yeah it's up to you you can change this so it's not math it's just a number that you put in there it's up to you now let's go look here if we run this then at 62 instead of our laser we have a problem it's right here and this should not be and this should be times my bad and now if you run it we should not be getting any errors cool they're not going to quite explode as we want just yet because if it doesn't hit anything we don't really want that explosion animation to happen it should just disappear otherwise it would be kind of weird if it just randomly flew and then boom it exploded then that would be bit weird cool let's go to player so first this laser move we don't always want to move laser if it's busy exploding the laser should just stop moving otherwise it's just going to hit an asteroid and keep on moving which is a bit weird but could be cool if that's what you want so we are already doing the whole explode thing here so let's go here to where we say exploding is equal to two and let's add another if statement above it and this one can be if it's one i mean not two but if it's a zero so if it's zero then we just want to move because then it's not currently exploding so you can just go like this but yet again we sit with the problem it can move and it can explode on the same time so you can just go here and say else if so either this one else this one but if it's not zero or two it's not going to do either of these so if it's one it's just going to stand still and it's not going to remove the laser from the screen because here it removes the laser from the screen and here it moves the laser but if it's one it's busy exploding and showing us that orange and yellow so we don't want to then move it or destroy the laser just yet all right now let's go to main and here maybe an update i'm going to hide this we can now go and go for and we don't need the index we just need the laser and we can just say player dot lasers and here we go laser colon or we don't need to expose it first we need to calculate the distance so if and then we can use calculate distance which is coming from that global file so require globals so that's required distance and then go laser dot x laser dot y so this is the first one this is where we are and then we need to go or then we need to add where we're going so the asteroid asteroid dot x and the asteroid dot y and this will calculate the distance between the two if it is less than the asteroid dot radius so if this right here is less than this radius of the asteroid so if it has entered the radius of the asteroid basically then we want to laser curl and explode then we want to explode the laser let's see if that works and that's going off screen and there we go pew and it is although it's not doing much it is there so we give it a shoot shot again as you can see now if it enters or almost enters depending on how the circle is positioned then there we go now if we were to turn on debugging here so if we were to rub globals again and we were to make debugging true we could see where the laser will explode so the red circle is where it will explode so it won't necessarily explode on this part right there it will explode once it hits the red circle cool now once we do this we need to also destroy the asteroid so let's go here to asteroids and we can add another function right underneath move you can also call this explode i'm going to call it destroy because you're destroying the asteroid but it's not necessarily exploding so destroy is equal to a function and this takes itself the asteroid table so this asteroids table will be provided to us it is to basically get the asteroid you want to destroy you could put this somewhere else so you don't have to make it part of the asteroids in fact it would make sense if you didn't make it part of the asteroid but it also makes sense if you do it's up to you if you make a part of the asteroids or not but then we also want the index of the one we want to destroy and then we want to take in the game as well so the game state i'll show you one we'll need all of this in a second first let's just remove it so table dot remove and then from the asteroids table the index so these two are passing in so let's quickly remove game for now when if we have this we can then go here let's let's include game it's not that big of a deal adding it here then here we can say asteroid colon destroy because if it's here then that means the laser has penetrated the asteroids we need to destroy it and we can say asteroids then the ast index that we're getting here so this is the table and this is the index and you can just pass in game as well game which is right here if we save this then the asteroid should also be destroyed once we hit it as you can see it's gone i'll do it again and then my player is gone very annoying bug i go here shoot gone now we want to split the asteroid up into multiple smaller asteroids so let's go here to asteroids and where we say destroy we can then go local min asteroid size and this will be the minimum size an asteroid can get and you say math dot seal and you could put this right here you could put it like up here that will also be perfectly fine i'm just going to do it here because why not and we can say asteroid size to the current asteroid size we have divided by eight you can change this to fit you however you think would be best then if self.radius if it's more then min asteroid size then we want to basically split the asteroid in half so table dot insert asteroids table so into this table right here an asteroid object with the position of self.x so the current asteroid exposition self.y solve the radius and then the game dot level let me go here and say self dot radius if we go up here to draw or not draw but here we're taking an x y the size and then the level and here we could make another one and now we have two new asteroids now if you wanted to you could make this go you can go another step and add another asteroid but only once one of these asteroids has been destroyed so then it splits up into three smaller asteroids so once the second smallest asteroid has been destroyed you split it up into three so the big asteroid that splits into two is your smaller asteroids and then this will also split into two but you could make it split into three if you're not too lazy however i'm going to leave it as is because i'm fine with it as is if we run the game and we shoot there we go now it's split into two there we go now that's also split into two but you could make it split into three if you think that's better and it will be more challenging we go here we shoot now there's two and then you can destroy the wrist and then here we go and the pvp gone and yeah that is the basics of adding collision detection to our game now we're not going to implement quite everything just yet we're going to explain the basics of destroying the player but a lot of the things that we're going to also implement will come later along with different parts of this tutorial so things will maybe be coded out for the future sake as other things will just be lit for later yeah so to give an example of what we'll be doing we go here we fly boom there we go so first let's open up the globals i want to add another item here so destroy asked for asteroid now you could put this inside of the asteroids object and i would i would encourage you to do if you want to however this global object will make it a lot easier to manage this in the future since we will be doing using this destroy asteroid in multiple files cool next up we can open up the player and it will be a lot like our laser course with collision but it won't just be for the player so a lot of the things will stay the same but just a bit modified for the player for example local explode there for exploded duration and that would be free so this is how long in seconds and the scenario three seconds then we can go here and we can say explode time so how long it has been exploding for so we'll make that zero exploding so if it's currently exploding we can make that false and unlike with the lasers we don't need a third item in exploding it can just be either exploding or it's not exploding we do need a third one since in usually what will happen is if it has finished exploding unlike with the laser where it just disappears and you know you can just shoot another laser here if the player has exploded then once the explosion finishes we'll start a new game or not a new game but just we'll put a player back and we'll respawn the player basically and we'll do that in a future tutorial cool now first thing we can do is we can close this we can close this and we can draw the player exploding so right here before the frosting because we don't want to frost or move or things like that if the player is exploding so we can say if not self dot exploding so if it's not currently exploding then we want to do all of this and here we can just say oh we can just end for now it's fine and we can invent this so if the player is not currently busy exploding allow the player to thrust forward and to move around and whatnot however if the player is exploding then do something else so draw something else and this will be very similar to our laser what we have drawn here so this right here so we can actually copy that it will just be modified a bit and we can paste it here so first we want the largest circle to be just pure red just to give it more intensity because the ship itself is much larger and much more complex than a laser so you want the ship's explosion to also look like it means more because it does mean more than the laser the laser explosion disappears faster and it doesn't it only has like two colors and it's very small here we want it to be rather large and to have more than two colors to show hey this is some big thing that exploded here this can stay the same however this time we can use self dot radius instead because we now have a radius to work with instead of just a dot same here we can say self to radius and then just times one or we can remove it and it doesn't have to say times one at all then here we can say one five eight to give it a nice little color here and then if we were to copy this paste it here and in here we can say two three four this will be the smallest one and it will be 0.5 so you can see how they scale in size now that we use times and now you'll understand why we use times to help it scale without us having to do much and this is how it will look once it explodes we'll take a look on how that looks in a second currently let's actually just allow the player to explode so let's go to the move player section i'm just going to hide this draw in the move player section this whole part here we can just say if not self.exploding so if the player is not currently exploding then we can do everything and then paste that in there and you can just invent and there we go so if the player is exploding now you can see if the player is exploding we're going self dot exploding is equal to self.explode explode time could you please autocomplete there we go if that is more than zero so this will return true or false if the explode time is more than zero then the player is exploding cool so this will only execute if it's not exploding however take note that we don't want to actually stop the laser so this whole section here for the lasers we can move that to underneath the end of this if statement so there's this one right here it does not hold the lasers the lasers can still move and stuff if the player explodes the player explodes but lasers are not affected by it we can then save and then finally we can implement an explode function so as a function takes itself and now we can just say self dot explode time is equal to math dot seal and then specify explode duration times love love dot timer dot get fps there we go so this will explode the player for us we can then go here to globals not globals my bad we then go here to the main file and here where we have an update we can go to when it's running so right here when it's running then we have our move player first then we do our asteroid stuff then here in the asteroid stuff we can go if not so the player is not exploding so player dot exploding so if the player is not exploding we can copy this line of code for the laser like that we can paste it here and this will allow us to check if the player has collided however we don't need this for loop so we can just remove that on indent we have a calculate distance but this time instead of the laser it's the player so the player x player y and then player explode and in here we also want to go and then instead of saying asteroid destroy you want to say asteroid or destroy asteroid is equal to true and this is in our globals file destroy asteroid if this is true we want to destroy the asteroid however we don't need to set that here we're only setting it here if the player specifically explodes into an asteroid and you will understand more about why i did this in the future or why we are doing this then here we can say else so if the player is exploding then player dot explode time is equal to player dot explode oh gosh i can type player dot explode time minus one so it will decrease if the player is exploding then after this for loop we can go if so if we want to destroy the asteroid then you want to sit destroy asteroid to false again because we're destroying the asteroid and then we got asteroid colon destroy and we just actually copy this little asteroid destroy thing because it's the same code now one reason why we are doing this right here and not this here as well is because here there are multiple lasers that's going to try and destroy the asteroids so if two lasers collide with this with different asteroids at the same time we're going to have a problem because what is it going to do destroy asteroids is true so what if you understand what i mean here because if two lasers collide against two separate asteroids but a destroy asteroid is true it is going to be incredibly confusing anyhow in there we have our asked index and we have our game and then here we can move the asteroids now let's run the game and see if we explode boom and yep the rest of the asteroids are moving however if we go and we fly against an asteroid we can still see our player we don't want that we don't want to see our player so we should go here to player and you should make sure we got the if else statements correctly so if not exploding then it everything is just held in i mean this is for the moving it's not for the drawing okay let's go to draw now let's see here if not exploding then we can do everything in here however if the ship has exploded then let's see here what's happening all right i see so here we already have a bunch of things but this is only for the thruster we need to include everything as well i kind of missed all of this so let's just go like this and we can just cut and we can go here and we can just say paste so now we'll not only just not draw the thrusters but will not draw anything else while we are exploding so if we go like this and now if we were to try and hit an asteroid we can't see our player anymore and there we go we've implemented collision detection for the player and hello everyone if you're enjoying this tutorial please leave a like and subscribe for future tutorials in this tutorial we'll be adding lives to the player so once the player explodes instead of the game just ending they have a few more lives they can spin before the game is completely over so let's get started with that first i want to give you an example of what we currently have so if we were to go here and say love dot what we currently have there goes our asteroid off screen so it should appear out about there somewhere yeah and boom we explode and we just kind of continue just very boring however when we explode that shouldn't happen or when we hit something that shouldn't happen in fact this should happen so this is an example of what we'll be doing so as you can see at the top our player lives are there when we hit an asteroid the player lives they go red they disappear and we start in the middle of the screen again and we can use up all our player lives until we are no longer there once we are no longer here then game ends the last asteroid does not explode once we hit it that is to prevent a bug but you don't have to worry about that bug because i'm fixing it here for you anyways but let's get started so first we want to now take in the num lives so how many lives the players should have then down here we can just say lives is equal to num lives or by default they can be free so if you don't pass in a number of lives it will be free by default next up let's draw the player lies so maybe right here underneath draw we can say draw lives this is a function that takes in self and faded cool we can copy a lot of things from this player draw so if you do not mind i am going to copy basically everything from here because a lot of this code is copy paste from what we already do in draw because first we set the opacity depending on if it's faded the opacity will change and in here is where it fades a little bit from what we do usually against what we want to do we'll still keep a lot of this for example this right here we want to keep this so let me just delete this part here and this laser part as well we can keep this polygon and this color we can remove the debugging because we will be doing debugging on it and we can also remove all of this and now we have a bunch of the code already given to us because we have already coded a lot of this nice let's just indent this to a there that's much better now first we want to see what color our lives should be because if we go back to this example i have and i can just move it over and as you'll see if we have three lives then it's white the same color as the ship if we hit an asteroid then it becomes yellow and if we hit an asteroid again where we only have one live lift if i don't miss the asteroid which i did again my aim is terrible and now it will be red because hey you should watch out buddy you only have one live left and that's what we want to do here so we want to set the color depending on the amount of lives the player has so first we can say if self.lives is equal to two so they have two lives then we can copy this right here paste it there and we'll modify it in a second then we can say else and this will also contain an if and then else so we're going to go like this and then we can just copy this and paste it here and we can remove this now if they only have two lines left we want to make it one one and in 0.5 and this will make it a kind of a yellowish color if they have one life left then we want to make it red but not completely red just a little red so it will be red and then 0.2 green and 0.2 blue and in here if they have three lives so if they have three or more lives in the scenario because there's a two and a one if you want to add like five lives you can maybe add more colors or you can just make them all white until it hits two and they will change color anyhow if they have one will be red and if they don't have two or one if they are free it will be white and in the draw lives we need basically to get the x and y positions or to give it a default x and y position you can make this like a more globally state one like put it here or even put it here it doesn't really matter too much in this scenario i just want it here at the top of the screen so i'm going to do an easy local x pause y plus is equal to 45 and 30. so x would be 45 and y would be 30. so 45 from the side 30 from the top then we can create the for loop so 4 i and one to self dot live so the amount of lives we have and we want to basically draw free lives so you can just plop that in there because the lives are the same shape as the spaceship and we can go if we are exploding so if self dot exploding then and then if i is equal to self dot lives so the amount of lives you have so we don't want this to happen if we have more less lives on the screen than what we currently have or more you know they want to basically make the heart completely red so love dot graphics dot set color and then one zero zero so basically once we explode then it should change the color of the spaceship to pure red until the explosion is over in which case it will switch to whichever colors are here cool the on here since we can't just draw the spaceship on the same position or at the same position everywhere we need to specify a few things here to make the the lives go in different locations because if we don't then the lives here there wouldn't be three lives like i i wish i could show my cursor here but there will be like three lives there there will only be one and two others on top of it so we need to specify where to position it so instead of self.x here we can go and say i so the current index we are on or the current position times x position and this will give it its wanted x position and then as for the self.y since the y will never change unless you make it go in the x direction instead of or in the y direction instead of an x direction then the y will never change so why pause but if you wanted to you could make the heart go from top to bottom instead of from left to right it depends on what you want personally i think going from left to right is perfectly fine but once you have done that then boom we are basically ready to go now we also need to remember that we don't want these to change angle with the spaceship you could there's nothing wrong with it it's just if you make a change angle with the spaceship then that it could kind of make the player a little bit confused about like while he's playing and suddenly he sees the spaceship hearts moving as well it could be cool but it could be very distracting personally i think it's too distracting and i'm going to remove it but you don't have to if you think it's a cool feature instead we can go here select all of these and remember this view angle we created here at the top that is the default angle or spaceship is always at so we can just specify view angle instead so now it will always face 90 degrees and never change but if you want it to change then you could then again 100 do that i just don't care to make a change cool next before we continue i would like to go to globals and make sure this is here just to make sure that is there so if you forgot about this just put it there nice so next up we can just go to the main file and let's draw our lives so let's go here and we can say player colon draw lives i know if the game is paused so game dot state dot paused because if the game is supposed to want it to be faded and here we go that shouldn't be an end symbol my bad that should be eight times okay now let's try it there we go so now our lives are right up here as you can see so that's nice currently if we hit an asteroid nothing's going to happen we're just going to kind of die and respawn and then hit them again and again again we don't want that to happen anymore because we now have lives we want the player lives to decrease and the player to restart from where he was where she was so let's go to the top and let's see and this is not the right file we go to menu and here it says explode time and then what the explode time does we can go and say if player dot explode time if that is equal to zero then we want to go if player.lives minus one is less than zero meaning if the player doesn't have any lives left then game colon change game state to ended because the player amount of lives minus one is less than zero meaning they have like negative one lives left then end the game and we should make this laser equal to because yeah just because but you could make it less than one if you wanted to because this city player has one life left now they hit an asteroid now one minus or now the player lives i mean changes to zero and zero minus one that is negative one and that's less than zero so in return it should change the game state to end it because the player has zero lives left then we don't want the game to continue and then we can just say return because we don't want this to continue the loop or to continue to continue this loop nice otherwise just always go player is equal to and then a new player and in the player dot lives minus one and there we go so now the player will always have one less of a life once they hit this or once they finish exploding and i should save without formatting now if you run this and let's see what happens when we hit this asteroid we lose the life and we start back at the screen the reason we started back at the center is because we create a new player and when we create a new player the player by default is in the center of the screen because that's what we hard coded here in a sense we basically said hey once we create a new player put them in the center x and y and that's what's happening here we're creating a new player and storing that player in the old player variable so don't get too confused here we're just creating a new player object storing it in here but it kind of has a lot of the old player stuff by that i just mean the lives so yeah now if we hit an asteroid we go we lose a life now we have less lives if it's another asteroid we lose life let's hit another asteroid here and there we go now the game has changed to ended state okay meaning it's basically over now i still want to do a few more things before we continue so here if destroy asked we can now go if and then else now instead of the else we can now put this and in this if statement can check player.lives minus one if that is less or equal to zero then we want to go if player dot explode time is that is equal to zero as well then we want to set this here as well so basically if the player lives is less or equal to zero basically the same check we did here however this check will not execute quite as quickly because this one will execute after this check so this check may be false then this executes where this check could be true but it executed after that so see this executed after just right here so after the check so player lives here could still be two then we go here and then player lives is one because remember code goes from up to down they're doing the same thing here but now it's here at the bottom and since it's going up to down it will trigger this and we check if the explode time is equal to zero then just do everything as normal however if the explode time is not equal to zero then it just don't do anything at all the reason for this is once we basically finish the game yeah once we basically get to the last asteroid and we hit that asteroid for player it can cause a few issues and this will just cause or this will just fix a bug in the future but you don't have to worry too much about this just put that there save our game we run our game and now we fly we hit we respawn we wait for our targets to come back we fly we hit we respawn and now for the last one we fly we hit it doesn't get destroyed immediately and that is exactly what we want because that could cause errors because let's say that's the very last asteroid before we go to the next level and it wants to go to the next level once we hit it but once we hit it we and we have our last life there it's not going to be a good match because let's say we only have one life left and there's only one asteroid left once we hit that asteroid and we lose our one life it wants to go to the next level and this is in the future once we implement levels but if it wants to go to the next level and we just lost our last life that's going to cause a few errors and so we say if we hit against an asteroid with our last life then it's game over then we don't get that point or anything like that in this tutorial we want to add a menu to our game so to give an example of the menu we're going to create we go here we have our little player circle we have a quit we have a settings and we have a new game if we click on quit it will quit the game settings will be your homework so clicking on settings will do will not do anything and then clicking on new game will start a new game then let's go here and then once we reach our last life it will give us a game over screen but we have yet to implement that but once we finish implementing that then after the game over screen it will take us back to the menu screen and that is what we want to do okay but this tutorial is going to be pretty long so we won't be doing the ending screen just yet so first let's go here to the game.lua file and let's go to the top and here where it says state i want to change all of the other states to false and the menu state to true so by default the menu state will be true meaning once we basically start up the game it will give us the menu state not currently but it should give us the menu state cool next up let's create a new component so component new file and then button.lua and this will be the buttons we want on the menu screen unfortunately the menu screen is legitimately just buttons so we have to do this before the menu screen we can just say local love is equal to require love and in local text because we're going to need text to display the buttons is equal to require components there are components dot text or slash text depending on if dot text doesn't work for you you can go slash both of them work for me we can now create a function and call it button it will take in quite a few things so we'll get to that and then return button so let's get everything already ready in here let's just say return and then there now a button will take a lot of things first once we click a button we want to execute a specific function for that button we don't want to implement that function inside of the button itself because if we do that then it could cause a little bit of a problem with us needing to basically for every scenario coded by coder function so it's better to pass in a function and then to execute that function on button click so we can say func for a function we want to take in also the text color of the button the button color itself the width of the button the height of the button the text on the button where the text should be aligned to so the text align the font size and i know this looks like a lot at the moment but believe me we'll go through all of them slowly the button x position the button y position the text on the buttons x position and then the text on the buttons y position cool so now we have the basics of a button here now first let's say local btn text for the button text is equal to an empty table and we'll get back to this in the future then our function is equal to the function passed in or if there's no function passed in we can then say or function and we can just remove this and or we can just not remove it but just bring it to the end of this statement so one liner function print and we can just print this button has no function attached so basically if they save or if they click the button and they didn't add a function to it it will print out to the console hey there's no function attached to this button you can't click it next up i want to position the text of the button now if you remember from the previous tutorial or from the previous game we made where if i were to go here as you see our buttons they have a little space here but if you could remember from the code we wrote for these buttons is that the text in the button is positioned relative to the button itself meaning if we position a button here then and we say the text is 10 pixels to the right then it's going to be inside of the button by default 10 pixels to the right meaning the text isn't going to be 10 pixels from the border of the screen it's going to be 10 pixels from the border of the button if you can't quite remember what we did here i do recommend you maybe go back to the files and just go check on how we created that button and maybe that will bring back some flashbacks but it's not a big piece of code we have to write for that so let's quickly do that so if text wise if the y position was provided then ptn and in text dot y will be equal to text underscore y plus button y so plus the y position of the button because it needs to be the amount we give it plus the button otherwise you'll have to manually calculate if you wanted on the center of the button where the center of the button would be relative to where we already have it so we anyways have to do this later on if you want the ticks to be inside of the button and you can still make the tick outside of the button even with this method here you just use minus or just massive value depending on what you want to do anyhow let's continue because it's a big file then we can hit it again again just go like whoop go like this and then do this but without this now you have a choice here and let me just go back to y here you have a choice you can either do this if else statement or you can go here and say y is equal to and then button y and you can remove this part here this will work just as well so it's up to you what you want to do however i'm going to use this else statement here you're going to copy this and now you can do it for x and in here you can just change this to x and there we go we have just as quickly added our x and y positions for the buttons or for the text on the button now let's go here and we can just say text color is equal to text color or if they didn't specify text color we can just make it white so r is equal to one g is equal to one and b is equal to one like that and we can do the same for button color so button color is equal to button color or and we just copy this one here however the difference will be is that by default the button color should not be white because the text is already white so if they don't enter the text or the button color you want to make sure that the text is still visible since the button would now be black you can swap these two if you want to but yeah and we can go the width of the button is equal to the width we enter or 100 and then the height of the button is equal to the height we enter or 100 the text is equal to text or and you don't have to specify or with any of these i'm just doing it for in case i want to leave something out there in case you might want to leave something out in the future the more customized this is the more things you can later on leave out anyhow and then no text added so new text was added we can just put that there or you could force it it depends on what you want the text x position is equal to the text position passed in and we're not using this text position here quite yet but you will see in a second where we use that or we can make it the button x or if you didn't specify at all we can just make it zero we can do the same for the y text position where all the x's are just y's and you can go here and say the button x position is equal to the button x position or zero and in the button y position is equal to the button y position or zero cool and then finally we need to use a text component that will put the text on the screen for us and we already have a text component that's been imported right here and every button will have its own text component so we can just say text component is equal to text and then we can just pass in all our values so we can say text btn text dot x so here we use the button text but here we specifically use the x and y given to us and you can of course do the same but for y and as you can see also our documentation is here which is very nice and this is some documentation we did a while back so here we go and as you can see we did a bunch of documentation and now the documentation appears as i'm trying to edit this so that's pretty neat so now we know we need the font size and the font size is passed in and then also whether we want it to fade in which is false we don't want it to fade in unless you want to add some animation fade out is also false the width and then the text align like that and then finally for the opacity we can just pass it on we don't want to change the opacity however if you have buttons in your game you want to make this a dynamic value so very value passing as a variable and not just one so it depends on what you want to do if there's buttons in your game and not just any menu so when they pause and you want it to fade out when you pause then making this an actual variable would be better because you can change the variable later let's draw the button because i actually want to get the button on the screen so draw is equal to function self and we just add in our comma then here we just say love dot graphics dot set color so the color we want the button to be and we can go self dot button color and then just specify red and we can do that for all of the colors colors we have so let's do that and there we go now the color will be set to what we want the button color to be then we can draw a rectangle so love dot graphics dot rectangle and you can just say fill this rectangle or don't fill it it depends on what you want but i would like to fill it since it's a button it makes sense to fill it self then the button x self dot button y self dot width and then self dot height and it will draw the button for us next up we need to do the same but instead for the button we need to do it for the text and we also want to reset the color back to white so here we can go here but instead of button color we now go text color and you can just say self dot text component dot draw just colon draw instead and this will draw the text component for us because it already has its own draw function cool now that's the basics for the button we'll be getting back to this a little bit later in the tutorial i just want to get the button on the screen right now next we can go and we can create a new file in the states folder called menu.lua because the new state we have and you can require love so local love is equal to require love local button and it should be with capital b is equal to require components.button and then we can say function menu takes in nothing yet and then we can just return a table and we can return menu and this will return the menu for us now we can just take in the game and we'll get to y and then the player and we'll also get to y next we want to draw the buttons and the buttons are pretty big so local and let's just draw one for now buttons is equal to and then here we say a new button the function we have let me just say nil for now because there's no function then the text color which can be nil and then nil again or the button color the width can be love dot graphics dot get width divided by three and it will give it a nice see rather smallish width and then the height can just be 50. the text can say new game it can be aligned in center i need to make that okay see the text size can be h3 or the font size the button x position can be love dot graphics dot get with divided by three and it'll kind of make it a little bit in the center and i love that graphics that gets height and this is for the y position and this can be times 0.25 and it will kind of make it go a quarter way down okay so these are buttons we have we'll add more a little bit later so here we can just say draw as a function is equal to function taken self for and then underscore button in buttons so for every button we want to draw it we just call button colon draw cool we have just created our menu we'll get back to this a little bit later we can go all the way up to load and now we don't want this start new game anymore because we don't want to start a new game as soon as we start to or as soon as we boot it up we want to go to the menu first so you say menu is equal to menu game player so just passing these two into the menu so the menu has to be below the player and the game now here in the love the mouse pressed we can just add a global global scope variable we can go here else and just say clicked mouse is equal to true so if the mouse has been clicked we'll use this later but we don't have to worry about it now let's just say it exists then you can go down here and where it says if game state is running we can go all the way to the bottom and we can go else if game dot state dot menu so if it's the menu state and you say clicked mouse is equal to false by default and we'll get back to this a little bit later as well and finally here in draw else if game dot state dot menu and you say menu colon draw now when we run the game we should be able to see a button on the screen okay it was the first attempt so oh okay we haven't imported menu yet that makes sense so we can just go here say local local menu is equal to require states dot menu there we go there we go so we have our first button it just looks like text because the button color itself isn't a different color we could if we wanted to change the button color to be something different so here is our menu where we set the button color which would be this one we can go r is equal to g is equal to one and in blue is equal to zero now this should give us a yellow button so there's the button i just made it the same color as the background to kind of made it more mystic so you know so it kind of looks like there is no button but there is actually a button let me just say nil back here again and we have just created our first button now let's draw our cursor or mouse because we go here we can't really see our mouse so you can just go if not and in game dot state dot running and we're not putting this inside of this menu checked state here we're putting it inside of a not game state running the reason for it is because we sometimes you want to see the cursor even if the game state isn't necessary running for example on the game over screen we might want to see the cursor in the settings screen that's maybe another state depending on how you code the game you want to still see the cursor in the paused state you still want to see the cursor so it depends on what you want to do here here we're just saying if it's a running state remove the cursor because we already have the player on the screen we don't want the cursor as an extra annoyance and we can just say love.graphics.circle and we can specify fill mouse x mouse y and a 10. if you want an extra nice kind of challenge you can draw the player cursor the same way you draw an asteroid so then the player cursor is an asteroid that can be pretty cool or you can make the player cursor like the spaceship as well but then you move it with your mouse it depends on what you want personally for me this little circle here that's all i need as you can see here so it's a nice little circle that just serves my cursor now let's finish implementing the button because it's a pretty big thing we gotta implement but we did the biggest part so it's not that big of a deal now so first i would like to add a function to make it easier to change the button color now i want to wait to make the button color not only settable outside of setting the button color on create but to also make it easily setable anywhere in the game so you can say set button color you can do function we take it self read green blue then here we say self dot button color and i'm just going to copy this because this is quite a thing to type out we can go here say that is equal to and we just say red green and blue respectively there we go now it's very easy to set the color we just say set button color and you specify the number we don't necessarily need to specify the rgb anymore next we want to do the same but for the text color so i'm going to copy this paste it and i said i'm saying button color we can say text color red green blue but instead of button color we can say text color easy now bloated raw we can also get or set a few methods here to get the position of the button and the position of the text so get pause you could also make this get button pause it depends on what you want to do but saying get button pause will kind of work with all of these like set button color set text colors things get button position would be smart pass in self and we can just return self dot button x and self dot button y we're just separating by a comma so we're getting two values from this function two of them not just one so take note of that we can then go get text pause and we can actually just copy and paste this to be honest so get text pause and instead of button x and button y we say text x and text y cool we have just implemented a ton of functions that could be useful in the future only two more to go i also want the function that once we click the mouse like once we click it should basically execute the function we have passed in here so down here let me just say click is equal to function takes itself and we just say func so it will just execute the function we have doesn't do anything special that's all it does it just executes that function cool and then the last one is just check hover so we're going to check if we're hovering over the button so the cursor is currently hovering over the button that is equal to a function that takes itself the mouse x position the mouse y position and in the cursor radius then we can just do this i just paste it in but i'll explain it so you first get the mouse x and then plus the cursor radius and i have explained this quite a few times especially in the first tutorial where we did the balls basically if they circle where we have a circle but we want to add the radius to that circle in this scenario since the button x is here we want to add a radius to it both ways that is why the button x here has the plus radius and then the button x here as the minus radius if you haven't seen the previous tutorial where we did build a game where we like play around with balls then i do recommend you go watch that tutorial because it is quite a good intro to this tutorial as well so i don't have to re-explain this right here because that just does everything the same as what we did with our player back then so we just check if the mouse x and y is above a button x or and y if it is we return true so here's the x and then here is the y checking if it is we return true otherwise we just return false cool and that is all there is for the button next is the menu let's finish up here so let's go here and create a bunch of functions we might want to do so local funks is equal to a table first we have our new game so this is when they click the new game button that is equal to a function and this function just goes game colon start new game and we pass in the player and that is why we need the player and the game passed in we also want a quit game function so if they click the quit button which we'll put in a second so quit game is equal to another function and then love dot graphics dot crap exactly event my bad dot quit and this will quit the game for us once we click the button cool now we have our two functions we want to use this button here i'm going to copy and paste it three times first here with this nil i'm going to pass in funcs dot new game take note however i'm not passing in brackets i'm not doing that i'm just passing in the name no brackets take note of that and here at the bottom one you can just say funks funks dot and then quit game yet again no brackets okay then with this button here we can change the text here to settings for the settings button and since all of the buttons will be positioned below each other the x-axis so this git width part right here can stay the same but since they're pushed below each other we can change this right here to 0.4 and that will mean that that will make this button here a tiny bit more below this button right here which is the new game button and in here of course we can say instead of new game we can just say quit and instead of 0.25 we can say 5 5. nice so we have just created our buttons and the functions that go with them now let's see if they get drawn and there we go new game settings quit now we need to do a few checks so once we like hover over a button we wanted to change color to red and if we click a button we want to execute a specific function so let's go down here and first we can say focused that will be in empty string and then we can create a function called run and this function just takes itself and if we have clicked we're gonna get the local position of the mouse so mouse x and mouse y which is equal to love dot mouse dot get position as we return both the x and y position and then we can go for name and button in buttons so the buttons we have here meaning check if button and then check hover which we have just implemented you can pass in the mouse x and then the mouse y if you wanted to you could have just made this mouse x y already implemented inside the chair cover but here i just pass it in because why not and we can pass in 10 and this will be the cursor radius we have i recommend you make this a variable instead that's maybe global or passed in or something like that because if you ever want to change the radius of the cursor then you have to come back here and change it as well so i recommend you make it like a variable and maybe pass it in here or something like that anyhow so if the cursor is hovering above the button we can just say if clicked and we get clicked passed in here then button colon click so we want to execute the button function we have and it's itself.focused and this is basically if the button was not clicked in a sense but it's not inside of an else is equal to name or we make it equal to name and you go button and then set text color to 0.8 0.8 0.2 and it should also be 0.2 as we'll give it a nice reddish color when we hover over it else we want to say button set text color 1-1-1 currently this focused we are not using it we'll use it a little bit later but it is there for once we want to use it cool the menu has now been coded but we are never executing this run yet so we need to go and execute this run now it is pretty simple we can just go here to main.lua here we say mouse clicked is false we can just say menu colon run and then mouse clicked or click mouse there we go and just like that we have implemented the menu that was like the last line of code however we hover over we were not getting anything yet so something might still be wrong however the buttons work so if you go here click quit it works if you go here you say new game it starts a new game so the buttons work however the color is not changing yet and right here where we have this love graphic set color this should actually be self dot text component colon set color which if we were to go to the text component here it has a function called set color where it sits the color for us as well so it has its own color function we're going to take this what we have here and we can paste it in here that is why it didn't go the color we wanted it to go to now we have our button colors as well if we click on settings that will do nothing but just print out some text if we click quit that will quit the game and then finally if we stay new game it will start a new game where we can crash and we can shoot and what not all right that was a pretty big tutorial well done if you sat through the whole thing so in this tutorial i'll be telling you all how to install lua rocks on windows now as far as i know there are three ways you can install lua rocks on windows the first way you can do it is by getting the executable the dot exe file downloading it and then just using that however it doesn't work for me so i'm assuming it also doesn't work for a lot of you another option is is to compile it from a source meaning you have to take the source code for lua rocks and you have to compile it yourself which i don't want to do and i doubt that anyone else would want to do now the third option is to use windows subsystem for linux so this is basically where you have a type of linux on your computer and i'll be showing you how to not uninstall or set up windows subsystem for linux i'll show you how to work with everything you need to do in order to install everything you need to use for lua rocks now to make sure we know how it works i created an example which we can use if our full screen this so here we have a main.lua and this file sits here right here so there's main.lua and there's text json which is these are my documents now here we just read the file as you can see here just in the dashboard we read a file and then we take that file which has json inside of it you don't need to know what json is you can just copy and paste this into your own text.json file anyhow then we decode it into a lua table so we take the json and we convert it into a lua table and then we access the content inside of it and we're doing all of this using the lunar json module which we installed through lua rocks so let me give you a walkthrough on how to set everything up first you need to enable windows subsystem for linux or wsl you can just say turn windows features on or off if you click on that you should be prompted with something such as this just scroll all the way down until you see this windows subsystem for linux if it is not checked then check it so mine is currently checked because it is already turned on so you need to check yours if it's not already turned on then you can say okay and it will install it for you this will take a few minutes so just give it some time after it has installed it you want to reboot your system so it can complete the installation you want to go power and then restart it's going to say it's updating but all it's actually doing is installing windows subsystem for linux now once your system has rebooted you need to install ubuntu luckily you can just go to the microsoft store so microsoft store right here microsoft store and in here you can just search for ubuntu i've already searched for it ah there it's gone two like that and then you can just scroll down until you see ubuntu 18.04 lts you can click on that and here it will say get you just click on get and it will download and install ubuntu for you once this has been completed you can just scroll down here and you'll notice this right here so this is the command you use to start this ubuntu system now i'm going to copy that so ctrl c and then we want to open up either the powershell or command prompt so either command prompt or powershell i'm going to use command prompt so i can look cool open that up now to make sure you look like a real hacker you have to say color o2 there we go now what you can do is you can start up your windows subsystem for linux by right clicking right clicking will paste whatever you have inside of your clipboard in this case i copied this so it pasted this and this will start up ubuntu for me now if it's your first time starting up ubuntu with the windows subsystem for linux then you'll have to enter a username and password this can be anything you want just keep it simple so you never forget it then once you're here it's pretty simple first you want to sudo apt update like that press enter it will ask you for your password you say you put in your password and it will commence to get all of the latest packages it has then you want to say sudo apt update and this will update your linux system you have now i just did an update so i don't have anything to update but it will prompt you yes or no to update all of your packages just say yes once you've said yes it will update your linux system to its latest version now that you have this you want to install lua and lua rocks so you can just say sudo apt and just for in case you don't know you can also say apt cache search and this will search the repositories for what you need in this case if i say lua 5 then here refers that everything i can download now you can choose between lua 5.1 5.2 5.3 and basically 5.0 i want to get lua 5.3 so i'm going to say sudo apt install lua 5.3 and i also want to install lua rocks i can just say lua rocks once you do this it will ask you to install it you just say yes i already have it installed so it's not going to do anything now once you have this installed you can start executing your code so make sure that right here you have this code and this json file make sure you have these because this is the example we're going to use now currently i am not inside of my windows machine i'm inside of linux so to get into my windows machine i can go cd and then a forward slash not a backward slash but a forward slash this will take me to my root directory if i say alice it will show me all of the folders i have inside of this folder you can just go into cdmt for mount and in here you'll see all your drives you have so i have the c drive and the v drive to show you an example of this if i open up my file manager go to this pc then this is my c drive as you can see there's c and here's the d drive now the c drive we can go here and as you see there's the c drive to go into that c drive we can say cd c and then a forward slash now we want to go into users and then here we can just say cd users and take note i'm using a capital case u here because this users also has a capital case u then here is cherise the brain you can go into that that is my current user i am who i am then here we can also go cherise the brain then here to find where my things are yours might be instead of documents inside of desktop instead of downloads instead of music wherever you put your files you have to go mine is inside of onedrive so here i also have to go into onedrive onedrive and you can press tab to let it autocomplete for you and in here you'll notice we have a document folder if i go into documents then you'll see i have a lua folder and this is where my lua stuff are now i can just go here and see cd documents and then lua and press enter now if i say al is i should see a main.lua and a text.json so right here main.lua and text.json now once you are inside of here you'll see these two here to run this lua code you can just say lua 5.3 and then main.lua if i run this i get jack and that means i executed the code inside of this main.lua to see what inside the file i can say cat which means read and in main.lua and you'll notice it's everything i just had inside of vs code luna json requires luna json and in all of this now one thing i did forget to tell you is how to install luna json because i've showed you how to install lua rocks to install luna json you can just say sudo lua rocks install luna json and let me just resize it a bit so everything's on screen oh so here we go sudo lua rocks install luna json and this is how you install any package in lua rocks you just say sudo lua rocks install luna json you press enter and there we go so now it should be installed and now of course if you try and run it it will run your things to uninstall a package from lua rocks you can say sudo lua rocks it should be remove luna json and this will uninstall a package now if i were to try and run this code it will tell me it doesn't know what luna json is because i have uninstalled it and then yet again to install it again i can just say sudo sudo lua rocks install luna json and just like that you can use lua rocks now take note whenever you want to run something you now have to run it inside of your windows subsystem for linux meaning this right here you cannot run it from windows not as far as i know at least because luna json and lua rocks are both installed on linux now and not on windows this is to me is the easiest method around this lua rocks installation issue we're having today we will be implementing the user score system then say in starting a game you'll see we have now two score systems there at the top once we blow up an asteroid with the score increases go here we blow up the asteroid increases and in here we no longer exist so you just boom and let me just boom go now when we go back in i think the score is no longer there we'll implement that later but the idea is once we have the score implemented the score should stay as it is and then we will have a high score and a current score the high score will be implemented in a different video it's not that complex so we'll probably do it near the end cool and just as a comparison this is what we currently have move this up here we say new game as you can see we don't currently have any score to speak of which is unfortunate so that's why we'll be implementing it right now because that's one of the things that really makes a player more happy to see is if they play a game like this and they have a score and they try and beat their score every time they play the game now let's go to our globals.lua now i'm going to assume you have lua rocks installed if you do not have lurox installed i believe you can just google lua rocks and there we go and then you can install lua rocks lua rocks allows you to basically just install extra packages or extra files for you to use in your program so let's say you want to work with mysql which is something you use to work with databases then you can go to lularocks and you can download this mysql package and now you can work with mysql in lua or if you want to make a chat then you can get this lua socket which allows you to basically chat or create a chat app i'm going to assume we have this already installed and if you don't you can just quickly go and install just pause the video go and install it it would be probably as simple as it was to install luba if not easier now if you have it installed make sure that you have the following in your path you need to have lua rocks in your path so this should also be in the installation if it isn't you just have to implement or add it you say dollar then in brackets lua rocks path dash bin and this will allow lua rocks to work and you have to add eval here at the front before this so if we just copy this then you have to go eval and every time you want to use lurox you have to do this what you can do is you can say you can go into your bash rc and this runs every time you start up your terminal and let me just run that again now if we go down here and actually it's at the way bottoms let me just on full screen that so here we go there so this is inside of my bash or c file which runs every time my terminal starts up so every time a terminal starts up we make sure i can use lua rock so i recommend you do the same if you're on linux now once you've done that lua rocks should work now to install what we want to install we need to type lua rocks install and then luna json this package will allow us to work with json because i want us to save our file in json format and you don't need to know json i'll explain it along the way a lot of people would actually prefer if you were to encrypt the save file but i'm not going to go through all that effort it's such a basic game that could be something you do on your own but you might want to encrypt the save file so a player can't spoof how much they really got for example let's say they really only got 5 000 points in the game then you might want to encrypt the files so they can't just go into the json file and change that five thousand to fifty thousand but that's out of the scope for this tutorial so we can just say install and let's see here and i do not have write permissions i'm just going to go sudo and then try it again you might want to use dash locally if you have the same problem i'm just going to go sudo just to keep things simple now once you have this installed you should be good to go now let's go up here and we say local luna json is equal to require luna json and we can use it just like with love because we've installed it so now we have this option here to use now we can create a function and this function will be called read json it will read a json file for us and in here we can just pass in the file name because we might have multiple json files we want to read so this is just for future proofing you don't have to necessarily add a file name here but just in case you add another file that you wanted to read then it easily reads that file for you now i recommend having all our files inside of a specific folder so right here we can create a new folder and we can call it src for source content this can be like your audio and your files and your images and whatnot in this case we can just say data that should be a folder data folder inside of the source folder because this is where all our data files would go preferably encrypt your data files because this is usually things users should not be tweaking and then here we can just say dot json because this will keep the save file or the saved data and then all we have to do is we have to use these curly brackets and then we just have to say hi score like that or if you want you can also use an underscore like that but preferably don't use a space that's not very good so use an underscore or a capsule case but not a space and you can just say zero and this will be the score the user has save that and you don't have to open this file for now for a long time cool now that we have that file we can specify the file we want to read so the file we need to open so local file is equal to io for input output dot open and we pass in the file path so you can say src so that would be this src file or folder then you want to go into the data folder so slash data and then slash because you want to access this save dot json again then just go dot dot file name and in dot dot because you might not want all of your files to be in dot json format you might have multiple formats dot json now this is just how the javascript but because this is saying read json you can just as well not add this here and just let them enter json but because i just want to enter the file name for example save i just say that then it will automatically add this.json for me as you can see here the end result will then look something like this and all i had to do was end and just save here and it will produce this and then of course here we just have to specify we want to open it up in read mode below that we can say local data is then equal to file colon read and we can just say we want to read everything so star and then all this will read everything inside of this file and we can say file code and close because we don't want the file to stay open and in return luna json dot decode and we can just say data now once you've done this everything should be still working fine if you want to test it you can still run your code of love dot and make sure everything starts up which it should cool now next up we can go to this game.lua file because we now want to add the high score to the game because we can now read this json file we have but we want to have it display the score here now this json.d code here before i forget basically just converts json so this right here to a lua object so lua table now down here we can say score by default is zero so by default when you start the game you get zero as the score this is the initial score for the game now here at the top we also want to import save data because now we want to read this file that is json file then we want to pass in the save data into this game here if you want to you could load the high score and all the safe stuff inside of this game here but for me it's better to rather do it outside of the game just to keep things simple so if you might have different functions you want to pass the same data in you don't have to make multiple functions in each one of those you can just pass it in here but we'll get to that a little bit later you can say high score is then equal to save data dot high score and this here is just a table it's just going to contain a high score for a moment if there's nothing there we want to say or zeros it will default to zero next we want to go but down here to where it says draw because you want to update the draw then go up here and say local opacity is equal to one then if faded then here we just want to set the opacity to 0.5 and i can just work off of the opacity so we don't need this if statement here anymore we're going to just unintent this actually this can still be inside of faded why not because this should only be displayed if we're currently faded because only if we're faded we're being paused that's the only time currently the game is ever being faded out so this can still stay in an if statement i don't think about it and because this should be above everything else it should be at the bottom of this drawer if it's at the top everything else is going to be above it so it should be at the bottom we're going to go text to create new text we want to display and you can just say colon draw then this text we want to say score and this will be the default scored player c like when they start up a new game coil in space and then add the self dot score then you can position it negative 20 10 and our living as position i can't actually remember what is everything inside this text here what are we doing okay so first oh yeah so that is positioning for x in the y position cool okay then the font size that will be an h4 we can say false and false and the false and false is just for this fade in and fade out because we didn't want it to fade in or out then you want to wrap with the line and the opacity so the wrap width will be the width of the window so loud.graphics.getwith and you can say right and then faded and opacity or 0.6 this is a little bit weird but basically if it's faded then show the opacity we have otherwise just default to 0.6 and i believe you can actually go here into the lua virtual environment and just test it out so let's say we have x which is equal to true and we have 1 which is equal to let's say 0.5 and then 2 which doesn't exist so you can just say 2 is equal to nil now if x and 2 or 1 we get 0.5 because 2 does not exist but if we were to say 2 is equal to 1 then we get 1. so if x is true which in this case if faded is true and this right here has a value inside of it then display this one here so opacity otherwise if this has no value inside of it and this is false then display this so that's for that now we can copy this and we can do the same for high score so here is a high score and now here instead of dot score we can say dot high underscore score we can then make this zero yet again 10 because it's going to be the same height you can make it h5 because this shouldn't be the main attraction the main attraction should be your score so it's going to be a little bit smaller before we go that right should be a center and we can also change the 0.6 to 0.5 cool and that should draw it so let's just save and then let's run it and see if it works because that should theoretically draw it ah here we go attempt to index local save underscore data nil value you know why it's giving us that it's giving us that because inside of the main.lua if we go up here we are not passing a value in here so we don't have a high score i'm just going to say zero for now and let's run it and see what we get now we still get a value or still problem and my bad this should be a table high score there we go and that should be an equal symbol there we go now it should finally work new game now we have a high score which a little bit faded out because remember the main attraction is actually the player and the asteroids and the score is also a little bit faded but it's bigger because you might want to see your score more than what you see your high score all right now one thing before we continue this right here quite unnecessary because all we need to do is we just need to get the data from globals and we are already acquiring globals so we need to do is let's say it can be maybe like the second thing it does local save underscore data is equal to and we're going to say read json and then save because we want to read this file and you can just take this save data and pass it in here or if you really want to save space you can just do whoops my bad you just do that but i like doing this just to make more readable now let's make sure that works because that should read the file for us and then once it has read the file we should see that value so let's go up here and just run it okay high score is zero let's keep a look out for that if we change the high score to 900 what do we get do we get the same output or did it change because it should be changing new game and there we go now we get 900 so we have successfully created a save file and read from that save file then last but definitely not least we now have to modify the asteroids a little bit because when you blow up an asteroid your score should increase so let's go right here and we can say if self dot radius is more or equal to asteroid size then so the radius is more equal to the basic asteroid size of course that's a little bit big so we want to go divided by two because asteroid sizes from left to right whilst radius is halfway then game dot score is then equal to itself so game dot score plus 20 and game now has a score on it if you go here you can see there's the score plus 20. we can then go else if and we can go self dot radius is less or equal to the min asteroid size then we can do the same thing as this but because this asteroid will theoretically be the smallest asteroid you can get we want to make them get a lot of points for destroying the small asteroids because they can be difficult to shoot and they multiply so they can also be very difficult to dodge at the same time and finally for the medium-sized asteroids we have we can just say else copy this and instead of 100 we can say 50. and then this isn't something that's going to really affect us too much now but we can also go if game dot score is more than game dot high score then we want to say game dot high score is equal to game dot score now this right here is basically whenever the player plays and their score is higher than the high score they have then the high score will also increase along with their normal score can't you tell them hey you are getting a new high score good job and this here will of course just add a score and we're already drawing the score to the screen so we don't have to worry too much about that we're gonna just say love dot to run it move it so everyone can see start new game shoot you'll notice both of these the high score and the score increases because the score is higher than the high score and of course the more we destroy the more we gain now take note of this last part we added here if we make the high score let's say 100 then if we go back here we say start new game and we shoot an asteroid as you can see the high score didn't change but the score did and the high score won't change until our score is higher than the high score then you can see the high score is starting to change cool and that is implementing a basic save or score system for our game so to give you an idea of what we'll be doing here we go we say start new game just wait for that asteroid to come back and then one last time for our last help right here at the top our last life we crash oh gosh missed it completely and then we get a nice fading game over screen and it fades out and after it fades out we get new game so first of all i would like us to go to this menu file right here or not menu i meant to say main and here at the bottom where we say game dot state of menu and game dot state are running and paused what not we want to add one more else if we want to go else if game dot state dot ended so the game state has ended then you just say game couldn't draw because it has to now draw the game in that state but game will already know what state it is cool now currently this wouldn't really do much if we were to run the game we currently have and we were to test it out here we go oh gosh i missed it ah this one has a bad collision then as you can see we just get a dark screen we don't really go anywhere nothing's happening because the game overstate doesn't really have anything to draw yet so let's actually add that we can open up our game state and we can start here at the top underneath high school we can add screen text and a screen text is just a table that will display the text on the screen once we go to game in that state this will remove it and that and whatnot then we need to add a another variable here saying game over showing and this is for the animation so we know when the game over is showing and when it is not showing so you know when to switch from showing the game over state screen to showing the menu state screen so that can be false by default next year in change game state we can go if self dot state dot ended so if the state has ended then we can call the self game over function now this game over function is what we're going to create now basically it's going to do all the game over stuff for us so let's go here and say game over that's a function which takes itself and first we can set up the screen text that it takes that should be on the screen so self dot screen text is equal to a text component which contains the game overtakes which will fade in and out you could also maybe do the same and then also display the high score and the player score also fading in and fading out or what not something along those lines that could also be a very smart idea however we're going to keep it simple and just do the game over screen just game over and then poof done we can say zero love dot graphics dot get height times it by 0.4 to kind of get it in the middle in the center because you remember how fonts are used it's not necessarily starting in the middle of the font as well it starts here at the top so every letter has a top point and it starts there instead of in the middle so you just take note of that it can be size of h1 because it should basically take all of the attention it's the most important and say true true love dot graphics and then get get with and then center now if you're wondering what any of this means you can always just go back to this text file right here and just look at what all of these mean you'll notice or fade in and fade out right here is now set to true nothing else is basically set like this so the first time we'll be really using this fade in and out functionality then after we have done that we can just say self dot game over showing and we can make it equal to true because it's currently showing the game over if it's not showing the game over and the game state is ended then we should know that we want to switch from game state ended to game state menu because it's not showing game over screen and the state has ended so there's no point in showing it anymore now currently this won't do too much then right here instead of the main.lua we can just reduce the amount of lives the player currently has so player and it takes the lives so num lives and you just make that one now if we run it we should be able to see our output that should work we say start new game we uninstall live.exe and boom that is all we get so not really anything just yet so let's go down here because currently i didn't really think about it but this doesn't really draw to the screen so we put the text there but we never actually drew the text so this could do that instead of this draw function and let's go right underneath faded we can then say for index text in pairs self.screen text and we can say if self dot game over showing then and now here we want to draw all of the things while the game over is showing so self dot game over showing is then equal to text colon draw self dot screen text so it's going to draw the text that should be on the screen and then we can just say index if you're wondering what's going on here we can go here to text if we go to draw we take in the table text and then it's current index you'll notice return true or false that's how we know if we're supposed to show it or not if it returns false it's not currently drawing anything to the screen if it returns true it is drawing something to the screen and if you plan on adding more things here you just modified as a teeny bit so it can be able to do this more dynamically because currently it's good for one thing but if you have two or three things it's not going to be that great anyhow then you can say if not self.game over showing then so the game over is not showing then self coded change game state and we're going to change it back to the menu state and then otherwise is the game overstate isn't showing then you can just say else text draw self dot screen text and an index so this will basically start it off it will start to draw so yeah that is about that which we'll need i don't think we'll need any more let's try and run this and see what we get if we just drag this here so everyone can see start new game and end then we start getting a fading out game over screen thanks to everything we have already implemented and why did so much work at the start of the game we didn't have to do a lot now because we've already implemented fading in and fading out and that is for the game over screen and love 2d so in this tutorial we'll be implementing new levels as well as making the player invincible now after this tutorial the game is going to be a little bit broken but we will be fixing that in upcoming tutorials so don't worry too much about that now to give an example of what we'll be working with if i or what we'll have by the end of the day i just move this here now when we start a new game you'll notice the player blinks and if i try and go into this asteroid here nothing happens that is because of our new invincibility we added so when we start a new game or when we were in the player dies then they finally become invincible and they cannot be hurt for another while and this is very useful because let's say you have 20 asteroids on the screen it can be very difficult to avoid them immediately once you respawn so if you're invincible for a few seconds not only does that give you some time to clean up the screen and also make sure that you just don't immediately die once you respawn now to make sure that this works if we crash in here we'll actually explode then now we can try and crash into things again and we won't as for new levels we can just shoot these asteroids and i'll show you all the new levels and one more right here right here there we go so now we have level two and there are now two asteroids on level three there will be three asteroids level four four asteroids and so on but it becomes more challenging because each asteroid explodes into multiple more asteroids so it will become more challenging depending on how everything goes now a bug you might encounter here is let's say you die at level two here so you gave me a second let me there we go if i crash against something it tells me it's game over but once this game over screen leaves and we want to start a new game then you'll notice we're kind of still in the middle of the game but not the same game it's it's kind of weird now this is normal we are going to fix this in fact if you want to you can try and fix this yourself before the next tutorial but it's just normal because we're going to play around with some of the code and it's going to cause this to happen okay so let's try and implement that let's go to our state and open up our game state and let's see here where are we now we are now at draw okay then here in start new game we're going to add a few new things so at the top we want to go if player dot lives so the player lives is less or equal to zero they want to change the game state to ended so this will kind of fix a few bugs but also make sure we don't just start a new game because every time we level up we basically start a new game so if their lives is zero or less that means the game has been or should be ended so let's go here and we say self colon and then change game state to ended else if that's not the case if they still have lives left then their game state should be set to running this means it won't be going to the menu state for example to the paused state it will go right to the running state and now we can remove this one right here because we don't need that anymore then we want to say how many asteroids we want to spawn so we can say local num asteroids and that can be equal to zero and here we have our asteroids and now let's say they do level up we want to display the level on the screen even if it's level one if we run this new game it says level one so even if it's level one we want to display this on the screen we can then say self dot screen text is equal to text and in here we can just pass in all our required parameters so first the level we are in level that should be level with a space and then concatenate that by adding these two dots and saying self dot level so getting the current level say zero love dot graphics dot get height and you just times that by 0.25 so it won't be quite in the center of the screen it will be a little bit above the center of the screen and we can just add a comma there we go and say h1 for the text size true true love.graphics.get with and then finally we want to position it inside into the center of the window okay so currently we also have another problem we have to look out for every time we start a new level there's a small possibility that once we start level the asteroid might appear on the player so we don't want the asteroids to spawn on top of the player we want asteroids to spawn anywhere around the player so to get this we want to create an infinite loop and this infinite loop will basically determine when and where everything should be spawned so let's go create a for loop first and exit 4i becomes 1 and until i is num asteroids so the number of asteroids plus self dot level then we want to go local and in the asteroid exods we have here and we want to say local asy for asteroid y i do believe since we're not really putting anything next to it next to them we could possibly just go like this and it should be fine but just for a convenience sake i'm going to do this and then at the end of this for loop we want to insert into the table just like we usually do however this time we won't need to specify a specific size we can specify the asteroid size because then we have our specific asteroid size we want to spawn we're going to remove this and in between this local and this table insert we want to create an infinite loop that will reposition the x and the y until it is no longer on the player so we can say repeat until and we want to repeat it until the calculate distance from player x and player y and you want to say the asteroid x and asteroid y and if that is more than the asteroid size times two plus the player radius so we're checking if the distance between the player and the asteroid is more than the asteroid size times two plus the player radius so it means if the asteroid can currently damage the player the player was not invincible then you want to regenerate this aesx and asy so let's just put that here and let's just put that in there as well so it will run at least one time and then you will try and figure out if it should run again and will continuously rerun this code until the asteroid is no longer on the player so if the azure is no longer on top of the player and this isn't really required but i do want to just go on top here and say require globals because we are now using a function that's inside of globals but it's not really required okay cool so it's not really a guarantee we're going to see this change because we haven't really seen it in the past so it's not really guaranteed we'll even see it now but if we run our code it should work and nothing should be broken let's go like this say start new game okay and as you can see everything still seems to work but now the difference is the asteroid will always spawn a relatively far distance from the player so that they have a little circle they at least have to themselves okay i also see here we only have one life left but that was for development we can remove that later that's really required now next up we can open up the player so we can add the invincibility to the player so we're going to know how long the player can actually be invincible depending on how many times it has blinked and this will also indicate that the player is currently invincible if i were to just show the example again you'll notice the player blinks and at least to me as a gamer it says hey this player is currently invincible nothing can really touch it and once the blinking stops you're also no longer invincible so here you can see how many times you want it to blink so local local usable links and this will be the amount of blinks you wanted in my case i only wanted to blink ten times but then times two we're adding two to it because of how fast the blinking is compared to how long the player is going to be invincible okay so let's go down a bit then let's go right above the lasers and we can say invincible is equal to true so this is if the player is currently invincible if we cannot touch the player let's make that invincible not even permissible and here we can see invincible scene and then we can make that true as well this is if the player is currently grayed out or not so you'll notice we go from white to grayed out from white to grayed out this will save the players currently grayed out and here we can say time blinked and we can make that equal to the current usable blinks so this is the amount of times the player can blink while immortal cool now let's actually implement the design for all this so let's go to draw flame thrust and let's see here right above everything we can create an if statement and we can call it self dot invincible scene so if we currently are in the invincible scene state then it is a table dot insert into color 0.5 and that's being passed in right here so the color and we're just basically setting it to 0.5 which is half its opacity we're going to enclose this function again and we can go to draw and this is where most of the things are going to happen let's go here into the else or not the else but the if statement here and right underneath debugging we can implement if the player is currently invincible at what should happen so if self dot invincible scene then you want to say love dot graphics dot set color one one one and then faded and opacity or 0.5 so this will basically give us our default faded slash opacity we want or we'll just default to 0.5 cool and in here we can just say else and we can copy this line paste it here and we can just actually we can just move this line up we don't need to copy or paste any lines and then we'll have this instead next up we can close this function and go down to maybe the move player right here we didn't want to take in dt and this is the time between the last frame and this frame for anyone who may have forgotten this will allow us to calculate when the player should blink we're gonna then say if self dot invincible then let me create end here self dot time blinked make that equal to self dot time blinked plus actually minus dt [Music] times two so this will first take the dt times it by two and then take what we have our current time length and subtract it from there we can then go on every uneven blank and make the player fully visible so in every uneven one for 1 3 five the player will be fully visible and not grayed out to do that we can say if math dot seal pass in self dot time blinked and then modulate by two and this is an easy way to see if something is an even or odd value so in this case if something is an even value it will be true here because two modulo two is 0 4 modulo 2 is 0 3 modulo 2 would be one because there's one left you're gonna just say self dot invisible scene make that equal to false otherwise we can say self dot invisible scene and make that to true then we can sit the if the player is invincible and make it false if the time blinked has ended so if self.time linked is less or equal to zero so if the time blinked has ended and there's no longer any reason to blink let me just say self dot invincible and make that false otherwise if the player currently isn't invincible we can just hit self dot time blinked equal to usable blinks so we're basically resetting it and itself dot invincible scene becomes false that's about that for the player and i don't believe it will be working just yet so if we run it the players will most likely not be thinking yes because we even haven't passed him dt yet okay to pass in dt we can go to the main.lua file and right here where it says move player we just pass in dt which is provided to us by love2d and in here where we say if not player.exploding we also want to now go and not player dot invincible because if the player is not invincible we are allowed to calculate the distance and make sure that the player is or is not exploding but if the player is invincible we don't want to do that cool and let's see here right before this elsif we can go if the number of asteroids is equal to zero so if there are no more asteroids left we can increase the game level so again.level is game.label plus one so it's going to increase by one and in game start new game and we can pass in the player and that will basically generate us not only a new player but a new game for the new level great now we save this file and we run it say start new game now the play will blink if we try and hit something we can go straight through and you'll see the level fade in and out now when we destroy this asteroid it should increase the level because there will be no more asteroids left on the screen can go here and just there we go so now it's level two and the asteroids also spawned very far away from us so there's no chance that once we go up a level and azure will spawn directly onto us cool let me just increase my life here so we can make sure if the player does end live we can respawn without a problem let's go here and run one start new game okay we can go through this asteroid we're going to shoot up this asteroid and go to the next level they will let's try and go to level three just to make sure that all labeling systems work thanks to how we built the game implementing levels is fairly simple it's auto generated so come on you can do it come on there we go yes now there's level three if i hit something okay then my hitboxes aren't the best okay now we're invincible again and we can fly right through stuff without getting destroyed until we are no longer invincible where will explode if you try and touch something cool and that's the gist of it now one thing i did want to show you but i kind of forgot in a previous tutorial it's kind of to show you an example of how we created the spawn before we get to the actual video is if we were to go to the i believe it would be anywhere like in the game or something just uh we will just spawn the amount of asteroids yeah so here is the gnome asteroids now when we play the game if i go live dot and we start the game new game then as you can see the asteroid spawns far from us but how do we know in the previous tutorial what we implemented really works that the asteroid will never spawn on top of us well when we go here and we change this to maybe 100 asteroids and we run it again new game as you can see we have like a little radius around us where it wouldn't touch us same way if we were to start a new level it will spawn them all around us instead of add up so if we make this 500 for example and then we run it then as you can see there's a little radius where they won't spawn on top of us also i believe that level 500 is impossible if someone plays this game and they get to level 500 then please show me how you defeat it because that would be very interesting all right so yeah that is just what i wanted to show you just to make sure you know that what we implemented actually works so first off one thing we have trouble with is if we actually start the game right now and we just wait to un immune ourselves oh wait we can like start a new level and whatnot okay now here explode okay that's fine let's continue okay now explode again now we have one life left and now then i can show you what i mean all right so now when i crash into this asteroid which i just missed and it says game over now when we start up a new game you may have remembered this bug from a previous video every new game then we kind of continue flying off where we left off so we just continue if i die here then it's just going to spawn me back at this exact location in the next game therefore we should create a reset game function so let's go into main.lua and right above the love.load and this could also be in globals if you wanted to or you can create a utils.lua file or whatnot but here we can say function reset and this will just reset everything for us and then here we can just say end so first we can read the save data so local save data that is equal to read json and then you just put in or save the json or in this case just save we don't need to specify json underneath that we can then create our player and that will just spawn a new player or create a new player object with free lives we then go game and we can create a new game object save data we just pass that in and then create a new menu object which is equal to menu and you can just pass in the game and the player and then destroy asteroid we can make that equal to false now this destroy asteroid you might have forgotten about it but if we go to our globals.lua and we can actually keep this here then you'll notice here destroy asked just so you know where we are coming from and here we're just making it false again in case you may have forgotten where this is then after that you basically have created a reset function so what we can do now is we can remove this as well as this right here i can you can just say reset because this player game and menu and destroy asteroid are all global because they don't have a local in front of them so they can still be used once we use this reset so now we're going to reset the game once we start it and whenever we die we want to reset the game again to do that we can go down to where update is and scroll all the way down until here where it says game.state.menu then we have our menu run click and then our mouse clicked which is false then here we can check if not reset complete and this is a global variable but actually we want to make it local to this file but global in the file so we can go up here and we can just say local reset complete and we can make that equal to and you might want to make this underscore complete or like this depending on how you name it when i scripted this video it was probably like super late at night and here i have like underscores and here captain camel case you should just stay consistent but i did this late at night so i'm not consistent anymore anyhow this will tell us when we want to reset the game once we're in the menu state if it's true we can just call reset and then reset complete is true then here we can say else if game dot state dot ended so the game state is ended we can say reset complete is equal to false so if we do die then the reset complete will become false then once we go into the menu state it will reset the game for us all right we can now just save and test this out this should make sure that once we restart the game we don't just continue i'm going to just decrease the amount of lives i have because free is kind of unnecessary so let's just make that one for now but you could keep it free i just don't want to wait forever to be able to die all right so let's see what happens i crash the game is over now what should happen is i should spawn in the middle of the screen again so new game middle of the screen level one only one asteroid so we have basically successfully reset the game cool we can now just change the player lives back to free save that next we can go to globals now here we want to write json so we already have read json but now we also want to write the json so i'm going to copy that paste it change it from read to right we also get the file name but then we also need to get the data we want to write now we are going to get a table and we want to convert that table into json so we can store it inside of our save.json because this may look like a table but this is actually json okay so to make sure it works we can also print it out to the screen if you want to but you're anyways going to see how it looks once we put it inside of the save the json so you could print it out to your screen or you could just look how it looks in the save dot json okay so now we can have our local file and we want to open it and it's going to be the same file but this time we want to open it in write mode instead of read mode then we want to go here and say file dot file coden right and the only reason i'm getting these yellow lines is usually because you'd want to check if this file is nil or not first if the file is nil you don't usually want to try it right to it so if you want to be safe you can first check if file is equal to nil then and you can just like return or something anything here would work or you could just do this if the file is not nil so if it's not nil then you want to do this as you can see now the yellow lines are gone because we know file is not nil because we checked for it first so yeah that's just something you want to maybe take note of i'm not going to do that right now but you could there's nothing wrong with it it's actually better practice i'm just not going to do that right now we can then say luna json dot in code so first we used d code d code converts json into a lua table whilst in code converts a lua table into json then here you just pass in data which we get here they want to close the file and then the function is done we don't need to return anything and this will write to our json to make sure it works we can actually just yeah that's already serious we don't have to change that we can now go to game because we want to save our high score so once we boot into the game again we have our high score there so you go to the top and we can create a new function called save game which is equal to a function that takes in self and it just does write json and in the file name which would be save and then the lua table in this case we want to save the high score and just self dot high score and this should save the file for us if we get a new high score now it won't just call itself we need to still call this function so here in game over for right now we can just say self colon save game and it will save the game for us let's try and run this and make sure we actually get that so we can open this save the json so we can see what happens we say new game current high score is zero but if i were to just go up here you'll notice my high score also increases but now we need to make sure it saves so just give me a second and let's blow up now you get a game over our high score is a thousand okay we need to see and make sure that that high score is what we see if i say quit then as you can see it updated our file now the high score is 110. okay now i'm just going to decrease my hp by going here and just saying make it one just gonna constantly have to wait forever in order to finish the game now let's say we only get one score here and as you can see our high score is 110 now and our score is 20. so our high score and our score are two different things at this moment if we crash we get our game over screen and we get our save.json you'll notice our high score didn't change along with it that is because our high score here never changed so as long as this high score doesn't change the high score we save will not change either that is why we could just do go here and we didn't have to do any checks before because we already do checks to make sure that this high score doesn't go over the normal score unless we get more for example if we go here our high score is 100 110 if we try and beat that high score okay so my high score increased as well as my score as you can see since my high score has been surpassed so now both of them will be increasing and once i explode then the high score inside of save json is also updated so you'll notice that the high score only changes once the score is higher than the high score and that is that for today's tutorial this will be your final love 2d tutorial however to all of you who made it all the way here to the very last video well done you have all done such a great job getting here and with one last step you can finally continue on your road to being a game developer now before we continue because i know a lot of you probably missed out a few pieces of code here or might not know what to do after you finish this you could skip this part if you want to because it's not that important but here we can just bring this over so all of the code is on github and this link will be in the description so if you miss anything you can just go here and you'll be able to find it all the code we have up until now now when you go into lua and then into love2d you'll have the asteroids game folder and that's what we are building and we are at number 15. so you can get number 14's code if you have fallen behind or if you just want to get a few things we'll get to in a second once you finish this course and you don't know what game to build next i do have a few things i want you to try and do on your own and also point a few out during the game play whilst i show you what we're implementing what you could also add so here is a lot of things called homework and you don't have to do this but if you do this you will learn how to build and game on your own without my help which is very useful because i would always be there to hold your hand while we code a game together sometimes you'll have to do that on your own and this is where it can start there's a lot of things you can do create a settings page because if we look here we have a settings button but it doesn't do anything so you can implement that settings page that's i want you to do you can add more sound effects which we'll be doing in this video you can add a better design to the menu add mobile support add items to the game so let's say you want to give your player better guns so let's say after every third asteroid to destroy maybe a random gun will spawn that they can if they hit it then instead of exploding they will equip the gun and then they can shoot better lasers than what they currently have which could help them like once they get to like level 500 if you have a very powerful gun it could be an easy game you can add more enemies because currently it's very basic but you could add like in the original game and ufo that will occasionally also jump in and maybe shoot the player or just try and hit him and then you should also try to destroy that ufo you can improve the performance because currently it's not really focused on performance if you have a very powerful pc you won't even notice it but after this tutorial if you have a low end pc you might notice frame drops going until 30 fps if not lower and last but not least already not yet but you can also add a better pull screen and then last but not least you can add multiplayer support this is something that can bring you very far in the future you can go and get yourself a lua module the love2td module or just lua module and you can start trying to code your game that two people on two different computers can try and play the game together it's going to be a very difficult thing to do but if you get this right you can make any game and you can make it multiplayer for more people to play together and the multiplayer game is usually a bit more successful than a single player games because multiplayer games you can play with your friends so here's a bunch of things you can do and we'll go for a few extras in the video as well now in this video we'll be adding background music and sound effects i want you to go here to our github page go to number 15 which is the last one then you'll notice here inside of source we now have a new folder called sounds go into sounds and here you'll also find all the sounds we will be using in the tutorial and you just open all of them and then you'll notice there's a download button and you can just download them all so you click download and did it start to download yes there it goes it downloads and just download all of them until you have all of the sounds there you go once you have all of this let me just move this back to a different screen we can then open up our file manager and here under source we can create a new folder called sounds and we can then go here to downloads where our things should be and we just copy all of the sounds we just got and we can just throw them in here into the sounds folder and there you go now you might have to rename a few of them because the names are now incredibly long and tedious so just go here there we go oh i didn't do anything there we go okay let's just rename all of them all right now latest we have two types of sounds here first we have ogg and mp3 this is just to show you can use multiple sound types but usually you'd find people using ogg and mp3 and a lot of people also use wav or wav now i just chose ogg because i think that's what i got when i downloaded these here you can also play them if you want to i would play them but i don't think you'll actually hear it now that i think about it alright so i quickly fired up a different screen recorder so if the quality might be a little bit low that is just because i'm using obs instead of my simple screen recorder as usual now we can actually hear how this sounds so if we actually go here and just say open containing folder we can hear all of these sounds to the background music [Music] yeah then we have the explosion asteroid so that's when an asteroid explodes then when the player explodes and of course you could change these sounds to be something else i just went online did a quick google search for free music or free sounds and i just downloaded the first one i saw so you could definitely change this if you want that's how laser will sound then an option select like selecting a button then defroster then the thruster but louder yeah i couldn't remember why i had both of these but we'll try and find out right now all right so now we have the sounds next up we can go to components and create a new file called sfx for sound effects and like usual this will go local love is equal to require love and we have our function sfx which will return a table and you can just return sfx all right so first we probably want to select the background music so let's go up here and we say local bgm local bgm is equal to love.audio dot new source and remember if you're wondering about any of this this is on the love to the documentation so just go to love2td.org i think and then you'll find the documentation which explains this as well then we should select the background music now we can say src then sounds and then bgm dot mp3 so src sounds bgm dot mp3 so we just selected it and then we want to set the mode to stream stream is when you have long audio tracks like background music for example that's going to continuously play for a long time so let's say it's 30 seconds or longer you probably want to use a stream cool then you want to go bgm or go to next line bgm colon set volume because the volume will be very loud but you could change this depending on how you like it and i'm going to set mine to 0.1 which would be very soft but the volume will be very loud but if you want it louder you could make it louder by just increasing this number then bgm set looping to true and this will make sure it loops continuously over and over so once the background music ends after it's like 30 seconds or what not of playing it will start playing it again cool you might think this is annoying but a lot of games actually do this where just play the background music over and over and over again and usually people don't find it too annoying unless the background missing itself is an annoying tune cool now we want to link all of our effects so all our sound effects so you can go local effects and this will be a table just to make things easier it doesn't have to be a table but i'm going to make it one i'm going to copy this line of code just remove the local change bgm to ship explosion and make that equal to everything here but we're going to change this to this explosion ogg and we're going to change from stream to static because static is better for these shorter audio tracks like that's five seconds long or 10 seconds long if there's a very short and static would be better we can then just add a comma here and then copy paste it four times there we go then this can be asteroid instead of ship and then of course we just change from explosion player to to asteroid keep it static what not in this we can change to the laser and here i'm spelling it with an s because here i figured out you actually spell laser with an s and not as it so just in case you want to know but if you want to stay consistent because if you linked your laser of a set you could change that to z and then here we say laser here we can call the select and then option select and then thruster which will of course be thruster but loud so if you think the thruster is too loud you could probably go for normal fresher but this is just for making it a little bit louder because i think this normal thruster is probably a little soft cool now first when we play an effect we should have a true or false value to say if an effect has been played so if x for effect and then played we make that equal to false because by default nothing is being played then set fx played and this will basically just set this fx here to true or false you don't have to use a function for it but i'm going to use a function for it because it's just a bit safer we're going to go function self and then has played and then just self dot s or fx played equal to has played now we want to create a function to play the background music when we wanted to so we can just go here and say play bgm for background music that's a function takes itself and we can check if not bgm is playing so if the background music is not being played then end so this right here just checks if the background music is currently being played so if it's going dedede in the background then bg m play so if it's not playing you want to start playing the background music cool now we need two more functions first one is to play the effects so if you want to play a sound effect and then the other one is to stop a sound effect from playing so let's first do stop sound effect because that's the easiest one you can say function takes itself and then the effect then if effects and then we want to pass in the effect so this right here will go to these the effects and see if we pass in like a ship explosion string on our asteroid explosion and then it will try and select that one to see if it currently is playing which if it is playing then you want to stop the effect from playing so we can just copy this and then say stop as simple as that and that will stop the background music from playing next up is to actually play the effect now playing the effect is a bit more complex because we have so many effects we can just say play if x is equal to a function that takes itself the effect we want to play and in the mode we want to play it in and i'll explain this in a second now we will have three different modes we'll have single and the single mode is when we only want this effect and no other effects to play then we have slow mode and this will just play the effect as normal and then we have the else which will just stop this effect from playing if it's already playing and then start playing it again all right so let's implement that so if mode is equal to single and it might sound a little bit confusing right now but you'll understand what i mean in a second then if not self dot fx played then so if the effect is not being played right now then self colon set fx played to true so we want to set it to true and then if not effects effect currently is playing then we want to go effect at effect play and this will play the effect for us so basically this is in the sound effect that's only allowed to be played once that's why it's single next up we want to do the slow mode so else if mode is equal to slow then if not effects effect so the current effect is not being played then effects effect and then play now usually you don't actually have to do it like i'm doing right here it's just i like to have a little bit of variety in my effects they shouldn't all just be generic there should be like different versions of it for example single where it's only allowed to be played once slow where it's allowed to be played but we first check if it's currently being played then we can play it again and then finally last but not least we will stop the effect and start playing it again okay that can be any other command other than slow so else and single of course self stop effects and it will stop the effects like just like here and you can just say effect and then effect at effect and then play it cool and that is the sound effects file it could be much bigger but i like to keep it a little bit simple for right now because you might want to expand on this later and you don't want it to be too complex so let's start by going to the main.lua file and here in reset we can just go sound effect is equal to sound effects to create a new sound effect object and of course we should import it you can either use dot or slash notation as you can see i use both of them here as an example so sfx and we can just go to component dot sfx now when we load the game we want to start playing the background music so we can just go sfx and then play bgm and it will start playing the background music for us cool and let's play our game and see what we get and as you can see we get background music playing for us now all right so that is for the background music next up let's go to the menu here and let's just pass in sound effects for all three of these sfx because we're going to add sound effects to all three of them and we just change the player lives to free player also has another player object being created here so we can just pass in sfx here again there we go and we'll later on change that in the player itself now first let's go to menu which should be here in states and first we can take in the sound effect so sfx and then here where we go to run when we check if the button is being hovered on if it is being hovered then you want to go s f x colon play or play and then fx for the sound effect and you can say we want to play the select and single all right so it should be able to run if it's not going to crash because of those nil values as you can see it kind of plays however we need to also add another line of code right here instead of the else statement so if self got focused so the one is currently focused on is the same as the name then we can go sfx set fx played to false because you'll if you could remember if we go here to sfx and we go to single then here it sets it to true but here we check if it's already false now we are manually setting it to true or false depending on the context so you don't want it to play multiple times over the same like button so we're saying single here and we have to reset it to false here if it has already been played before we are allowed to play it again you could remove this if you want to it's just it does help make everything just work fine now go as you can see you can now go over all of them you can do multiple times cool and as for the menu state next up let's go to the game state and all we need to do here is just where we create this asteroid we just also pass in sfx for sound effect because the asteroids will also have sound effects for once they explode and before we go asteroids we can go player which should be inside of objects and player and first we can take in sound effects and then first here with the shoot laser i believe because that's when we want to play a shoot laser sound effect we can just go sfx play fx and then just the laser and since we're not passing anything in if we go here to sfx that means it will stop the effect if it's currently playing and it starts replaying it let's run that and hope it works as you can see now if i were to spam the button you'll notice it just keeps on playing because the sound effect is so short you don't actually hear the sound effect getting cut in half basically so if you spam it you're going to hear that sound effect being cut in half which it is currently actually doing next up we can go back to player and just go to move player and in here where we say if self.frosting so if we're currently frosting the player forward we want to go here and just say sfx play fx i'm going to say specify the thruster and then slow and slow of course if we go here to slow it will just check if the effect is not currently being played then it will just start playing the effect alright and we can do the same here at the bottom but now we can just say if that we're not currently thrusting ourselves forward sfx stop fx thruster cool let's see if that works oh that's weird let's try it again so at thirty we're trying to index a nil value so right here okay so thruster seems to be a nil value also it seems we haven't taken an sfx here in game so you can just also take that and let's see here uh you know what i spelt incorrectly there we go now it should work okay all right you'll notice every time we now frost it makes a nice sound and it stops once we stop pressing the w key cool [Music] then last but not least we can go to asteroids and go to the top we can take in sound effects then we want to play an effect once we destroy the asteroid so in the destroy function once here when we resize and whatnot we can just pass in the sound effects as well because we're creating new asteroids and then at the end before they remove we can just say sfx play fx and then we can just say asteroid explodes then if we run it and we shoot an asteroid we should hear and there you go so now you hear that every time you shoot an asteroid and just like that we added a sound to our game and believe it or not that actually took me a whole day to implement on my own just because we did it pretty fast doesn't necessarily mean it was very fast anyhow and that is the end of the love 2d crash course thank you all for watching it was a very fun experience doing this with all of you now before we go i might want to give you a little bit extra homework if you're up for the challenge you might want to make this on the background to make it look cool have the asteroids be flying in the background wouldn't that be cool if you're here in the menu state and it does that all right and if you go to new game you might want asteroids to explode when they hit each other wouldn't that be cool as well but then of course the player score doesn't necessarily have to increase but it could be pretty neat if two asteroids were to hit each other and then explode into more asteroids and yeah that's about it you can also try porting the game to android which i have not really tried but it would be a pretty difficult challenge and if you get it right then you can make android games for yourself and for your friends thank you for watching