Game Development in Love2D [Part 2] [Beginner Series!]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to another space camera tutorial today I'm gonna be teaching you a little bit more about how to develop games in Lua with love 2d we're gonna actually add some gameplay this time and add some enemies that our player can shoot so in the last tutorial I showed you how to run what love on your project from the command line however that's a little bit confusing and may cause some difficulties for certain users especially Windows users so I'm going to show you quickly how to set up your project in zero bring studio which if you haven't already installed you should check out the video I'll have a link in the description how to set it up so it's very easy to get set up in zero brain all you have to do is go to project project directory choose and then select the folder or directory that you have your project stored in if you created in the last tutorial if you didn't you can just start from a new project so if you try to build it immediately you may have some trouble because it's using the default goo interpreter which doesn't support love so what we have to do is just change the interpreter from Lua to the love 2d interpreter which has to point at the application in your system and once it's working you can just hit the little green button to run and build the project and it will work when you run it from scratch if you're loved TV isn't in a standard location you can add it to your you can configure 0 brain you know where it is by going to your preferences so go to edit preferences settings user and just set path dot love to de equals and then you put the path in here so now we've got our configuration of the way let's actually write some code so let's start by creating animated table this is because we need to have some kind of storage for our enemy and we're gonna have multiple enemies so it's gonna be slightly different than the way we've written the player so like the player will have X location and Y location a bullets table a cool-down and a speed so let's copy all those and instead of player we'll just set it to enemy enemy please copy and paste enemy okay so now that we've got those out of the way we'll set the Y location to zero because we don't want it to spawn in the same location as the player and then the bit where it changes is actually the code to make it and I'm gonna be fired so instead of putting it in a function like we have here inside a via variable an assignment so player dot fire we're actually in create a function outside so in this case can be function enemy : fire alright so this little colon here is actually a simpler way to write enemy dot fire itself and what self is is it's a reference to the function that we call it on so it's a reference to the table that we called on so if we call enemy dot fire later on I mean enemy : fire later on it'll pass us self self the self variable inside of here will be the enemy that called fire so instead of every enemy firing every time we call the fire function we can individually allow each one to fire so we don't I don't think we need to pass any thing to it has access to all its internal things for yourself so self dot cooldown oh yeah so it's actually just this bit right here copy it and paste it and everywhere we say player instead of placing enemy which is a reference to only one variable we can say self this will make it general-purpose and allow us to use it anywhere this may sound a little bit confusing right now but we'll see why in a minute bullet equals x self dot y and self dot bullets is going to have bullet appended to it also we're gonna create a table called enemies because we want to have multiple enemies so we'll probably have to create an enemy spawn enemy function actually we might as well do that right now so function enemies spawn enemy and so this will create an enemy then we can just copy put our code in here enemies controller let's name it and enemies controller enemies is gonna be our table that has all the enemies in it we use controller use tap complete because it makes our lives a little bit easier and then self dot enemies equals actually we're gonna have to use table dot append table dot insert sorry insert and we're gonna insert into our self dot enemies table enemy so once again self-taught enemies is just enemies closer dot enemies because self is just a name for enemies controller as long as we're inside this enemies controller : spawn enemies function and that coin was important you can't use a dot there otherwise it doesn't implicitly pass self otherwise you have to say self here you have to actually pass it okay so right now it's not going to do anything new because we haven't had it added the logic to create an enemy so let's actually create one in our initialization function so enemies controller spawn enemy like so now we have an enemy in position zero zero now we may want to change this in a minute to allow us to create an enemy anywhere in space because otherwise it will create them all in the one corner but at the moment this is fine because we're just gonna hope that one enemy shows up so if we build it you get an error 22 enemy's controller is no value ah yes because this has to be created outside of the function because it's love dot initialize isn't called before this function is attempted to be created so if it tries to get created before the table exists we're going it's gonna think it's nil and then it's going to blow up so if we just move this outside it's fine because it'll create it at the right time so let's gold it again and enemy is a nil value as well so let's oh na is a nil value so we just have to create an enemy table up above even though because this is just kind of our class it's like a placeholder for now it's not actually ever gonna be created as as an entity in the game so there we go it worked didn't blow up but we don't have any enemy appearing on screen and the reason for that is we haven't added it to our draw function so let's do that so we're just gonna do a little for loop for in let's say in pairs which table is it it is our enemies controller dot enemies table do love graphics dot rectangle and rinse or fill and it's gonna be the X location Y location and our width and height let's just set it to the same all right actually that's the same with there's a bullet let's set it to our player because we already used some other things that were the size of the player all right so now if we run it there we go we've got an enemy showing up in the top corner so it doesn't do anything at the moment and he's the same color as the player so the first one week the first problem we can easily fix let's just make it a different color make them red so it's obvious that he's not the good guy so red is 255 0 0 0 because this is a red than green and blue so if we set red to 255 and the others to 0 green and blue to 0 it's of course can be red so if we build this now we get our red enemy and let's make our enemy move a little bit in space invaders they move in kind of a zigzag pattern downwards so you could simulate that with a sine wave but that's like the simplest way to do it but for now let's just have them move straight down because it doesn't will blood that bit later so I love that update function we're going to say or the I pairs we can do pairs actually doesn't really matter we don't care about the index of each thing right now so for that in pairs of enemy controller dot enemies will just say actually let's use instead because II means enemy and e dot y equals e dot y -1 so now they should just move down or up in this case that's wrong we have to add one because Y positive is actually downwards so there we go our enemy is moving downwards so let's add multiple guys on screen we'll say let's just let's just add two for now maybe we'll randomly spawn them later but for now we'll just add two so will spawn one in me so I'll spawn two now and if we build this immediately like I said before there and have and they're both in the exact same X&Y location so we'll still only see one rectangle so let's when we spawn enemies let's give let's add the ability to set their X from Y location so back to our enemies controller spawn enemy we're just gonna say X comma Y you know arguments to pass next to my location so then x and y here which allows which means it'll just grab the X location pop it in and me X Y location pop it in there and me got why push it to the enemies table and then when we draw it it'll be in the new X in my location so we'll have our first guy spawn at 0 0 our second guy spawn at 40 0 and when we build we see two rectangles that are extremely close to each other so it looks like one so let's make it a fiber distance there we go so there we've got a little bit of a gap now so we can see the difference between the two all right so I got requests in the comments to teach how to load graphics so I guess we should add that before adding collision detection because collision detection is a little bit more intensive and confusing so we'll probably get to that in the next tutorial but for now I'll just add some lots of graphics to make it look nice so first of all we need to have some kind of pixel editor or graphics editor of any type if you've got Photoshop or you can use those as well I have pics in which is a little editor for mac it's a pixel editor for mac and we'll just create something reminiscent of the original space invaders you so even though it doesn't show up in our project outline thing it is actually here oh there goes just takes a little while to refresh so it shows up there eventually and we have our P and G in here now so what we can do is load it in at the beginning because we don't want to load the image every single frame otherwise we're gonna use up all our system resources because every time we load the image and it sticks it in memory and if we load it in a bunch of times very quickly as it would if we loaded the image every frame it would cause our computer to slow down and possibly even crash our game so we definitely don't want that whenever you load images always be sure to load them at the beginning whenever you load any kind of resource actually you want to load it at the very beginning before the game starts or during the initialization or before the level starts you never want to do it more than once so we could do this in our left out load function and what we're gonna do from now is just load it in our enemies so let's say the other issue is every time we spawn an enemy we could load the image or we could load it and then have any every enemy points to that image so we're gonna do that we're gonna say enemies controller dot image so this will be our global image that all our enemies use equals glove got graphics load left uh graphic stock image sorry image and we'll say enemy dot PNG because sending the exact same path so if we build it now just to make sure we didn't do anything stupid it shouldn't crash sorry got new image and that love the graphics a new image we haven't set it to anything yet though so our so we have to draw it still so in our draw function instead of drawing a rectangle we're gonna say la for our enemies inside this loop here you're gonna say love graphics dot draw and in here we fill in instead of this string fill we're gonna put a the image that we want to draw so cell up enemies control our image is the image we want to draw for every enemy we don't need to set the color anymore so we just set it to 255 so it doesn't mess with our image so if you said to pure white whatever the image draws will get drawn on the screen instead of being tinted by whatever previous color it is and instead of setting the width and height here we'll come back to that in a minute but for now we'll just leave it like this and there we go we see these little terribly drawn green squids falling down from the screen so now what we can actually add these widths so they're a little bigger because they're kind of tiny at the moment but when we do that they're rotated and funky so the reason for that is this first value is actually the rotation and not the width so it's zero the love diagraph except draw function is the image in the XY location the rotation the width the height the skew and the skew x and y which you never going to use so I don't ever even go out that far but these final values are optional so that's why we can leave them out if you only set the width it'll set it it'll set the height to that as well so we're just going to do that for now that looks terrible and is way too big so we'll set it to 10 and then these will be a hundred pixels long because it takes the initial width and multiplies it times this with that we add so that's still a little bit big I think we'll say four times the size five times the size and that's looking closer so this this looks like crap right now because these guys are being there they're way too small but they're trying to be left is trying to interpolate and make them look better by when you scale them up and it doesn't look good with linear interpolation and pixel art pixel art is using constant interpolation which will look very pixel II which is what we want but if you try to scale other images with it sometimes it looks bad so the blurring is just the default behavior and we can change that so we can change that if we go to the love download function or really anywhere you can just say love dot graphics that's set in Terp no set filter set default filter it's changed to between versions and we're going to set it to nearest the min and nearest for the mats so when it makes them smaller they'll use nearest interpolation this one could be actually set to linear and we wouldn't notice but this one the max definitely has to set be set to two nearest because when you scale up you don't want the linear interpolations trying to smooth things out so now when we build it maybe we have to speak so you have to set it so you can't put it anywhere you have to put it at the very beginning before you load any emmys resources but once you do that it won't look like crap when it loads and they'll actually be sharp as they should look so we could do the same thing for the player so for our player we'll just draw a little spaceship of sorts we can do the same thing for the player see player dot image equals love graphics and once again sleep layered up kg and I'm gonna set the scale rather than for each image individually because otherwise we'll have to set the scale for each one will actually set the we'll say set width set scale of graphics dot scale and we'll scale it to five so that way it'll scale everything five times as large as the images whoops nice flare yep I miss my wife so now it'll scale everything up five times the amount actually scales up the entire game it scales the axes as well so 20 is now 120 pixels apart is now 100 pixels apart so you just have to change that but if you set it early on it's not quite as much of an issue so now it's loved our graphics dot image for our player to have a rectangle no sorry left i graphic stuff draw amber to say player cut image we don't have to set width height will move our set color up so we don't have to call it twice which makes things a little bit faster even though it's not that noticeable it's just one less call and if we draw it immediately as I said before our transform operations are off so we have to reset our players location it's be five times smaller than what it originally was so if we set to 110 now we get it at the bottom of the screen which moves really fast because it has to be the speed has to be much slower now so we'll set it to two and we'll set our enemy's speed to two so there we go our bullets are screwed up as well so let's fix that so bullets will be spawned you just take everything and divide it by five really that's all you have to do if we scale up by five and divide everything else by five it'll just result in being the same relative location as it was previously take 35 divided by 5 which is 7 and the bullet scale is going to be smaller now - it's gonna be 2 pixels okay and their X&Y location that are a little bit off because it's not exactly using the players x and y's locations now so Claire that X part of why this is just gonna be a little bit of trial and error here there's a lot of that game development actually so we'll say five that's more lined up now actually maybe four even if that looks better bullet speed is gonna be slower now which is when we update our bullets so be that y equals minus B the line ten instead that's gonna be divided by five which is 2 and then our bullets are rolling slower and then lastly we can just get it to line up with the barrel of our players gun a little bit more Oh coz there's a bad cuz we're should I make its lower there I think we forget one more pixel down maybe even another alright there we go looks pretty cool now so in our next tutorial we'll actually add some collision detection which is a heck of a lot of fun so prepare yourself to be confused because the logic for it is a little confusing I'll have some diagrams to help visualize it a little bit which should help out a little bit but that's gonna be our next big thing to tackle so thank you very much for watching I hope you enjoyed make sure the like favorite subscribe let me know in the comments if you want to see anything specific like this week it was the graphics but next week it could be something else some of the more complex things I probably will pass over in this series since this is supposed to be an introduction but if you have a specific question about this game engine or some feature you want to add to the game make sure to let me know and I'll be happy to at least address it in the comments or in the next video so thank you very much for watching we will see you all later
Info
Channel: Spunky Kangaroo
Views: 21,393
Rating: undefined out of 5
Keywords: tutorial, games, love, love2d, game development, graphics, enemies, space invaders
Id: FeLljv5clnw
Channel Id: undefined
Length: 27min 6sec (1626 seconds)
Published: Fri May 22 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.