Game Maker Studio 2: Tower Defense Game 2/3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys real pets GML here back with another game maker studio 2 tutorial and today is part 2 of our tower defense game that we're creating and today we're gonna do the exciting part and we're gonna create the towers in our game so let's hop right into it first thing I'm gonna do is I'm gonna add a new group at my sprites and I'm gonna name it towers and let's go ahead and just create the sprites for our towers so I'm gonna name this s tower 1 I'm going to resize it to 32 by 32 edit the image and then let's just create like a little border yeah there we go and that's our tower pretty simple what we're gonna do is also middle Center the origin because this is where the bolts are gonna be flying out of so we don't want the bolts to be flying out of the top-left corner we wanted to be flying out of the middle origin so when you set an origin it's pretty much saying this is the X&Y position the true X&Y position of where we're gonna draw it so if we drew this at 0 0 you would actually only see that portion there because it's actually placing it at 0 0 on this origin all right so I'm gonna duplicate this and let's create duplicate it again let's create two more towers so tower 2 will edit it and I'll make it like a purple color and then Tower 3 we'll make it Orange perfect all right so now that we've got that what we're gonna do is set up a system to where in the top the bottom right corner you can click on each Tower that you want to create you can drag it along your map and then place it so this is a beginner tutorial so I'm gonna make it we're a beginner can actually make sense of what we're doing so technically what I could do is make some advanced system where we only need one object to do all of this but to make it simple I'm gonna make three different objects okay and this is going to be our tower clicked our tower dragged and our actual tower itself and these are all going to have different properties for what they do so the tower clicked is basically going to just be the tower that just sits there and then you can click on it to buy it and then our tower dragged is the tower you can actually drag around and then our tower just our base tower is going to be have all of the logic for killing the enemies all right so I'm going to create three different groups for that so towers create another group tower clickable z' and will create one more tower drag all right so let's let's create our tower clickable first so I'm going to create an object I'm gonna call it o tower one C and that just stands for a clickable alright and in here we're gonna set up a couple base values now let's go ahead and make it the sprite so we'll do Tower one sprite in the create event we can drag over there there we go Mouse was being weird what we can do is set up a base cost for this tower so we're gonna create a variable called cost and it's gonna equal let's say 50 coins now we haven't set up our coins yet but you can we're gonna do that in the next episode but essentially just the coins will be every time you kill an enemy you get a base amount of coins and then you can use those coins to buy towers pretty simple right all right so there's the create event that's all we're gonna do is set up our cost for our Tower alright now in the draw event we're gonna set it up so that we can essentially like show a cost for it so it has the cost but we want to let the actual player know what the cost is so first thing I'm gonna do is use an inbuilt functioning game maker called draw self which is just gonna redraw our sprite anytime you have a draw event you're gonna want to have to redraw that sprite all right I'm gonna set draw a set color and this is gonna be C black and then I'm gonna draw a text at X - 15 Y minus 35 and then the string we're going to say string cost all right so it's just gonna show the cost we're always gonna want to draw a set color back to see white because this is what game maker uses as a default especially in 3d space this isn't 3d but anytime you change a color you change a font or you change an alpha or something like that you want to run the code so it's basically sandwiched in between so if we draw C black at the end of it we want to draw white if we set the alpha 0.5 at the end of it we want to draw it back to 1 so it doesn't translate over to different objects and you get this really weird thing going on all right so now the next thing we want to do is say our mouse left released and here what we're going to do is set up so that you can buy this tower so what we're gonna do is actually let's go into our spawn gonna set up another variable called global coins and we'll set it to equal 100 to begin with all right so in here and our Left released what we can say is if global dot coins is greater than or equal to our cost then we can start developing we can then trigger our drag to tower to actually place it around so I could say instance create depth at our mouse X our mouse Y will set the depth to negative 9 and we're gonna create our Oh Tower 1 D which we haven't created yet here we're gonna say global that coins minus equals our cost and that's it alright so here we've just developed the simple object that we can then click and drag so if we go into our room here let's take this tower and let's put our instances layer above our actual tiles and I'm just gonna put 3 down here just because we haven't developed the other towers yet just so you can see what it looks like all right and this is basically what we're gonna click alright so now let's create another object and this is gonna be our drag Tower Oh Tower 1d alright I'm gonna go ahead and give it a sprite and again this is once we click that initial object this is going to be the object that we can drag around and place our actual tower in right so in the step event what we want to do is constantly set our X&Y positioning to our mouse X and our mouse Y so x equals Mouse underscore X y equals Mouse underscore y pretty simple and then let's go ahead and go to our mouse left pressed so this is going to be when you find the location for your tower and you're gonna want to place it down so here we're gonna say instance destroy we want to destroy this object and then we're going to create the actual Tower so instance create depth and this is going to be mouse underscore X Mouse underscore Y our depth will be let's say negative one and it's gonna create our Oh Tower one so if we go to our towers group and we create that Oh Tower one just like that now we can create the tower so let's run the game so here we have the cost we could click it we can now drag it around and then when we want to place it we can so let's place one more and now we can't click anymore because we're out of coins all right so now how do these towers actually do something in the game so that's what we're gonna do next so in our Tower one object what we're gonna do is in the create event we're gonna set up a couple base values for our Tower okay now this is the fun part where you can tinker around and really make the tower anything you want so here I'm gonna set up a range for our tower so I'm gonna say 100 pixels that's gonna be the total range of how far I can shoot all right let's set up a fire rate so fire rate equals and I want it to shoot 3 bullets as a second right so I'm gonna say room speed divided by 3 okay and that's just gonna shoot 3 bullets a second we're gonna set up a variable called shooting and it's gonna equal false and I'll tell you more about that when we get into the actual logic of the shooting and then we want to set up object to shoot equals no 1 and this object to shoot variable is basically gonna take on the value of any enemy within our distance that we want to shoot at okay so let's go into the draw vent and here we can start drawing all these things out so first thing I want to do is say draw self the next thing I want to do is draw a circle around what our range is right so I'm gonna say draw a circle it's going to be XY the radius is going to be our range and outline is going to be true we want to outline the circle all right so now what we can do is set up the logic for how the towers gonna shoot so here what I'm gonna say so I'm gonna create a temporary variable called en stands for enemy and it's gonna equal instance nearest at our XY and our Oh enemy okay so basically what's happening here is this value is taking on the nearest enemy our object can find within a radius okay so I'm gonna say if en does not equal no one so in other words this en now has a value we've found somebody within a radius then what we can say is if point distance X y en dot X en dot Y is less than or equal to our range then we can start to shoot so here's what we're gonna use our variable shooting so if we're not shooting I'll explain this all in more detail as once the code is actually finished we're gonna set our alarm zero to equal one and we're gonna set shooting t equal true okay so this is gonna start up the process of our firing rate we're gonna set object to shoot to equal en and we're gonna draw a line now you don't need to do the circle or the line you can come up with better methods but what I want to do is draw a line to prove that we're locking on to each enemy so you could take this out in the final game it really doesn't matter right so we're gonna draw a line from X Y to our en dot X and our en dot y alright so now what happens if en does equal no one well then we're gonna say else we want to set our shooting back to false we want to set object to shoot to equal no one so the reason we're doing this is because let's say for example en does equal an enemy right and so we're going through it we're setting up the alarm we're constantly firing but let's say it goes out of range now en equals no one so we you want to set our shooting t equal false in our object to shoot to equal false so let's go into alarm zero and in here we'll write the code to shoot bullets at it right so before we do that let's go ahead and just create a new sprite I'm gonna call it s bullet I'm gonna change it to let's say four by four and if we edit the image just make it a yellow sprite hole middle center of the origin and we're good to go so I'll just create that an object Oh bullet and there it is okay so in our alarm zero of tower one what we want to say first is hey if instance exists object to shoot then we want to carry on so let's say for example you know there is an enemy in range but before we can even start the shooting process it goes out of range so now we we've switched the object to shoot or object to shoot main equal nobody and we're left with an error because it doesn't know what object the shoot is object issued is nobody and we're trying to get variables from it like the X&Y positioning alright so here what we're gonna do is if instance exists our object is shoot we're gonna create a new variable Bowl it and it's gonna equal instance create depth this is gonna be X Y negative nine and our old bullet and because we created a variable bullet this is a temporary variable bullet it now this variable Bowl equals the ID of the object we just created so now we can use a dot parameter to get into the actual code of our bullet and change its speed and route towards that enemy so I can say bullet that speed equals ten or whatever value you'd like and bullet that direction equals point Direction X Y object to shoot dot X an object to shoot that Y right and then after that we're gonna set alarm zero back to our fire rate now if our object to shoot equals nobody we're gonna want to set shooting to equal false okay very very basic so let's hop into the game and see how it works so here we have our enemies I'm gonna click this pop it down so now we have a radius and now as you can see it's constantly shooting at the nearest enemy you can find with those bullets and you can see it's locked on by that line there once it goes out of range it can no longer shoot all right now this is also going with the center origin so as you can see when they went over here it didn't shoot even though it was in range that's because the middle origin of our sprite wasn't in range of it so if you'd like to if you'd like to fix that what we can do is in our draw event we could say range plus I don't know 10 okay maybe 15 and so now it's going to grasp just the very basic you know let's see let's try and put it in the same position that might still be a little bit out of play let's find out all right so what's going through try something like that yep and it just kind of clipped it so now it's just completely it's a little bit in range so if you like that play style you can definitely do that so let's go over this code again what exactly is happening so our variable en is initially created for the just like in our alarm zero this bullet here is taking the ID of the bolt we just created so we're creating variable en to take on the ID of the nearest enemy so this is a built-in functioning GameMaker you can look it up in the documentation and it'll basically tell you the same thing that it's going to return an instance of the idea of which object we choose so if en does not equal nobody in other words we found an ID for the variable we just created we're just gonna check okay is our enemy within range if it is and we're not shooting yet let's start shooting we're gonna set object to shoot two equally n and we're going to draw that line else if en does equal nobody then we just set the shooting equal false and the object to shoot equal no one all right now when our alarm 0 if instance exists object to shoot we create the bullet we set the path and speed for it we set our alarm zero back to our fire rate and then shooting equals false if it doesn't exist alright so that'll get rid of a couple of errors for you so now what I'm gonna do is pretty much duplicate each object so our Oh Tower I'm gonna say Tower 2d we're gonna change this to that and we're going to change this to tower to that it creates duplicate are clickable Oh tower to see change the sprite and the creative n I'm gonna make the cost one hundred and it's gonna create ro tower 2d then we can duplicate our Tower one change it to Tower two and let's make the range fifty and let's make it shoot six bolts a second alright all this can say the same I'm going to do the same thing for our third tower 3d left price that's going to create our Tower three duplicate are clickable however our three see the cost is going to be 200 and we're going to create Tower 3d and we're going to duplicate the actual tower itself change that name to tower 3 the range will say is 200 but the fire rate we'll say is only two bullets a second and the rest can stay the same so in our room 0 we can take these out now and let's pop in our actual clickable z' for each tower and in our o spawn I'm just going to set our coins to equal thousand so we can play around with it so let's see how it works so here's our basic tower here's our low range tower and here's our high range tower so as you can see it's got a higher click rate or a fire rate this has got just a standard fire rate and then here as you can see we have a longer range so now we can start developing something so we come into an interesting problem though first off you can build towers on the pathway which is that right and you can build towers over other towers which we don't want either so in the next episode we're gonna go ahead and fix that we're gonna add in a GUI for our coins in our level we're gonna make it so that we can create a spawning system so that after you kill all the enemies more enemies will come down and we'll use our enemy progression that we originally created so we'll leave like go and subscribe thanks for watching visit Cody made simple calm to learn more about programming and I'll see you guys next up so peace [Music]
Info
Channel: RealTutsGML
Views: 19,858
Rating: undefined out of 5
Keywords: Game, Maker, tutorial, beginner, gaming, studio, java, programming, lets, build, game, tutorials, game maker tower defense, game maker studio 2 tower defense, game maker studio 2 game, game maker tower defense game tutorial, game maker studio 2, game maker studio 2 dev, game maker development, game maker studio 2 tower defense tutorial, game maker tutorial, game maker studio tutorial, game maker studio 2 tutorial
Id: 9poN9EOHRmk
Channel Id: undefined
Length: 21min 25sec (1285 seconds)
Published: Sun Sep 17 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.