My First Game - DnD - Setup & Movement - Space Rocks (Part 2)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to the second tutorial on making space rocks in game maker if you're here that means you've opted to use drag and drop to continue with the series so before we get started let's just have a think about all the elements we're going to need to set up our project so in the game we have a ship that is controlled by the player we can tilt the ship left and right and move by pressing forwards we also have some asteroids that the player can interact with so these are the elements that we're going to be setting up today and I just want to give you a heads up that there will be pop-ups throughout the series that will expand on what I'm describing so I encourage you to pause and have a read of those when they come up now if you haven't already create a new project in drag and drop and now before we do anything else I'm going to come over to the main game options and change my frames per second so remember we can do that by pressing the COG here or clicking over here in the options main and we'll change this to 60 because that is the industry standard alright so we'll hit apply okay all right the next thing I want to do is make our surprise so remember sprites are how we represent images in game maker so let's come over here right click and create a sprite I'm going to call this SPR which stands for sprite underscore ship and hit enter so the essence I'm going to be making here are going to be very basic so you should be able to follow along even if you're not much of an artist I'm certainly not but if you want you can also just download the assets that I've created so the first thing we have to do is decide how big we want the sprite to be by default game maker will sign you off at 32 by 32 but we can change this by clicking here resize sprite so if you want more space in the canvas then feel free to resize this but I'm gonna stick with 32 by 32 so to edit the sprite now we can double click here or just hit edit image and just to help me draw it I'm going to turn on this grid over here and I can change the size of the grid with these alright so that is my ship so let's come back out of that and the next thing we have to do is set this thing here so this is called the origin of the sprite so the origin is where the sprite is going to be anchored and basically where the object will be anchored in the game so where it's X Y position is going to be so if we leave it at the top left then that means when we turn the ship it's going to start turning from the top left so it's going to turn in a very strange way but we want it to turn just right at the middle so I'm going to change the origin to be at the middle center alright so next up I'm going to make three different sized asteroids so first I'll make a small one which is just going to be 16 by 16 and we'll call that SPR I started small and I'm just going to make sure that the origin is at the middle center and then I'll just duplicate this change this to medium and I'll make this one 32 by 32 and we'll do that one more time to make a huge one and I'll make this 64 by 64 so they're all half of each other so again I'm going to draw them quite simplistically doesn't have to be anything fancy and I'll speed this up and come back to you once I've finished all right so these are my asteroids so just make sure that all their origins are set to the middle Center and that is it for our spice so now let's make the objects so let's come to objects right-click make a new objects we'll call this one obj ship and you can see over here currently no sprite is set to this object so it's going to be an invisible object at the moment but we can click here and set what's called its sprite index to a certain sprite and then when we put this into the room it will draw that sprite all right and let's make one for obj asteroid and I'm actually only going to have one asteroid object and we can set its sprite to any of these three for now and then what I'm going to do is when this asteroid gets created I'm going to have it randomly selects from those three sprites that we created and reset its sprite index and that way we're going to get different sized asteroids in the room even though we'll just be using the one object all right so now let's come into rooms so double click on this one here and for the time being we'll just have one room then we'll make a few in later tutorials and this will just be our room for testing so let's rename this one hit f2 and call it RM game and now we can just drag our objects into the room and make sure that you're dragging them into the instances layer so just like this and now if we run the game so remember just hit this play button here or hit f5 then we should see all of our objects coming up and obviously we can't do anything yet because we haven't added any logic or input to our objects but we'll do that in a second so also notice just the size of this screen so it's set to this particular size because over here the room width is a thousand and 24 by 768 so let's change that so I'm gonna set this to 500 by 500 which is kind of small but it's a pretty retro looking game so it should fit with the vibe and now my objects are a little bit off so I'm just going to highlight all of these and drag them over alright and now if we run the game we should see that the game window is a lot smaller and it matches the size that we've said so you can actually make game windows that are different to the size of the room so for example you could have a massive room but only be showing the player a small bit of it and that has to do with setting up the viewports down here we're not going to cover that in this tutorial because we only need the one window for our whole game but if you're interested then you can explore that in the documentation alright let's come back to the workspace over here and let's set up the object for our ship I'm just quit out of the room so let's make the ship move now remember how I said in the previous tutorial that the fundamental logic of programming is basically if this then that or in game maker it would be if a certain event or trigger happens then do a certain action so to make this ship move according to our input an event we could do is to use a keyboard press so let's go at event key down and then left and now we can add something for it to do into this panel here so remember we want it to rotate so right here is set instance rotation so let's drag that into here so now one important thing to note here is what direction is in game maker serve right is actually zero degrees so that starts right here just like it does in the unit circle then the top is 90 degrees left is 180 and down is 270 so it is actually important how you've drawn the sprite so note how I've drawn the pointing to the right so that means is currently facing zero degrees and that's what we want so now if we change the angle it's going to match where the sprite is facing so make sure you have your sprite pointing to the right so now because we're going left that means we want to add to our current rotation and for example we could add 5 to it but this would actually set the direction to 5 that's not what I want to do I want it to continuously add to it so we have to take this relative here all right and next we'll just do the same thing with the right so key down right drag over the same action and we'll set this to -5 and also relative all right so let's test that let's hit f5 and now let's go left and right all right great so that's working all right now let's do the movement now generally the way that we make things move in game maker is to change the XY position of the object in the room so for example if I just double click on this you can see it's current X&Y location and as I move this around the XY is going to change so over here for the X is 0 and as I move it across this increases and the same actually goes for y so the origin for Y is actually at the top which might be a little bit counterintuitive to you if you're used to a kind of graph setup but for GameMaker Y starts at the top and it increases as we go down so that's just something to keep in mind but to actually get the ship moving like it's in outer space then we want it to be using momentum and that actually involves calculating a bunch of vectors if we were going to do this from scratch and so that we don't have to do a 30 minute math interlude let's just use a built in function so let's make our new key press and we'll use up so that is the way that we're going to move so there isn't a block for this but in the game of documentation there is actually a function that adds momentum to an object so to call a function which is actually part of the game make a language so we can still use whatever function we want even if we're in a drag-and-drop project so right here under common we can drag this in and call a function with any number of arguments so the function we want is called motion add and this one as you can see it adds to the direction and movement speed of an instance and it takes in two arguments so it takes a direction and a speed so basically it adds movement to an object in a certain direction so we need to here in the first argument set a direction and that is just going to be whatever direction the sprite is facing so that's going to be what we were changing here and this is actually referred to as image angle and this is a built-in variable that all instances have just like instances have an X and Y variable they have one called image angle and then for our other arguments remember there was those two arguments the direction and then the speed so generally when I'm trying out new functions I might just put any number so for example just one or something but if we put one remember if we're holding down this key for let's say one second then that means because there's 60 frames or steps per second this function is actually going to be called 60 times so it's going to accelerate from 0 up to 60 in just one second so it's gonna go very very fast so let's keep this number pretty small let's go 0.05 and now this last one is actually if a function has a return value so all functions are going to do something they're going to perform some kind of action but sometimes a function once it's performed its action will also return some kind of value for example there are functions for collision checks and if I check for a collision at a certain place that function could return true or false true there was a collisional false there wasn't a collision but some functions like this one right here don't return anything they just perform the action so this one just moves the object that means we can just leave this target blank so feel free to play with any of these values that we've set there is an infinite number of ways to code something so don't think I have magic numbers or that I'm all-knowing or that using these functions is the only way to do something it's definitely isn't the case this is just one way to do it but all that said let's give that a test let's hit f5 and press forwards and we're moving you'll notice as we go outside the room we disappear and what we're going to do is have the object wrap around the room when it goes outside of it and so we could set up that logic ourselves so we could check for example if the exposition of the player is outside the room so if X is less than zero or larger than 500 but again our life can be made easier if we use a built in function and there is one cold move wrap and for this I don't want to put it in any of these events because I want it to apply all the time so we're gonna set up a new event called the step event and the step event is a really important event it's actually akin to a frame of the game so remember how we set our game to 60 FPS at the start in game maker frames are basically cold steps so there are going to be 60 steps per second you know okay so that means that whatever logic that we set up here is going to be run 60 times per second so the step event is a kind of general purpose event where you can put a lot of checks and actions in your objects that needs to be updated very quickly all right so let's come down to movement and see if anything can help us with that so if you hover over these you can see this one is called wraparound room so that sounds good let's drag that in and you can see we have a margin and then we can set it to wrap horizontally and vertically so we'll leave those ticked because we do want it to wrap both horizontally and vertically and for margin we'll leave that at zero for now and we'll just run the game because I want to show you what it means it's a bit hard to describe so if we go outside the room you can see it kind of pops over to the other side and it looks a little bit weird and that's because our room wrapping is kind of warping the object as soon as its origin which is in the center it's the edge right because there's zero margin so as soon as it hits that edge but what we can do is make sure that the ship is entirely over the edge before it wraps so we'll change its margin to be half the size of the ship so again there is a built-in variable called the sprite width / - so I would encourage you to read the documentation page on the built-in variables for instances because you will probably want to be changing these in your projects and it's good to be familiar with them you'll find that you'll need to refer to these basically all the time all right so let's run that again and let's move to the site the room and you can see that's a bit smoother now ok so now let's move on to the asteroids so they weren't actually doing anything in the game and we just had that one size of asteroid so let's come over to it and have it randomly select its sprite index and let's add an event and we're going to be using the create event this time the trigger for this is kind of unique because it's only going to happen one time when the object is created and then never again so this makes it a great event to do any kind of setup because you can perform months of actions in here and now I want it to randomly select a sprite so I'm not actually going to be using this one right here because this would only allow us to set the sprite to one thing but I want to randomly choose from three different sprites so let's come down to the random section here so there's a couple different ones here but I'm going to use this one this choose one because it will randomly choose one of the supplied arguments so that's exactly what we want so here there's a target which is the variable that we want to end up changing that is the sprite index of the object so let's just put that in here and now we need to set options so we can just hit Plus here to increase the number of options and then let's type in all the names of our sprites so SP our asteroid small SP our asteroid meed and SP our asteroid huge ok and now another thing I wanted to do is choose a random direction and then head off in that direction so we're going to use a few more of these random functions so this time I want it to get a random number so we just drag that into here and now the target is going to be the direction of the object so that's type direction and this is different to the image angle because the image angle is just how the sprite is drawing but the direction has to do with the objects movement so I'm going to have it choose a random number so not a decimal but an integer so that's just including whole numbers and we're going to go between 0 and 359 right we don't have to put 360 because that would be the same as 0 all right so that will set direction to any number between 0 and 359 and I'll also do the same for image angle just so that the ester is start looking a bit differently so what I can actually do is copy this and then paste it here and then we just have to change the target to the image angle so we've set the sprite the direction and the image angle but the asteroid actually won't be moving yet because we haven't set its speed so we need to do that let's go back up to movement and set the speed so let's drag that one in as well and I'll set that's one and as you can see here it's going to be moving in whatever its direction is set to and we don't need that to be relative that would be for if we wanted to increase or decrease its speed but we just want it to be set to the number one and now because it's moving around the room just like the player object is it could be moving outside the room so I'm going to also get it to wrap so let's add the step event and we could come up to the player and again just copy this and paste it here and the final thing I'm gonna do is just get it to also while the game is running slowly rotate the object so we're gonna get it to increase its image angle so let's go to set instance rotation and let's say it's angled to be increasing by one relative to whatever it currently is so that will be adding to what it currently is set to alright and that's it let's give that a run great so we've got all of our asteroids moving wrapping around the room you can see that there's a few different sizes for the asteroids next time we're going to be working on some shooting and collisions so I hope you enjoyed this tutorial and I will see you in the next one
Info
Channel: GameMaker
Views: 98,455
Rating: undefined out of 5
Keywords: YoYo, Games, YoYo Games, GameMaker, Game Maker Studio, GameMaker Studio, game, maker
Id: swKAjNjXXcM
Channel Id: undefined
Length: 17min 15sec (1035 seconds)
Published: Tue Oct 23 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.