My First Game - GML - 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 echo if you're here then that means you've opted to use GML to continue with the series sir before we get started let's just have a think about all of the elements that we're gonna have to create for 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 then we can also move by pressing forwards we also have some asteroids that the player can interact with and I just want to give you a heads up that there will be pop ups throughout this video that will expand on what I'm describing so I encourage you to pause and have a read of those when they come up so if you haven't already create a new project in GML and now before we do anything else I'm just gonna come over to the main game options and change my frames per second so we can do that here with the little cog or we can also come in two options in the main and we'll just change that to 60fps because that is the industry standard and we'll hit OK alright the next thing I wanted to is make our sprites the Remer sprites are how we represent images in game maker so let's make a new sprite and I'm going to make this SPR which stands for sprite ship alright so the assets I'm gonna be making today are gonna 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 have created so the first thing that we kind of have to decide is how big the sprites are going to be so if you want more drawing space then feel free to make this larger I'm actually going to keep this one at 32 by 32 so we double click here to start editing the image and I'm just gonna draw a very basic ship so I'm gonna turn on this grid just to help me with drawing it and you can change the size of the grid like this alright so that is my ship the next thing that we have to have a think about is this thing so if you just click on this picture we are setting what's called the origin of the sprite and this is kind of where the sprite is going to be anchored to and also where the object will be acting to so basically where the X Y position of the object will be in the room so if we leave it up the top left and that means for example when we're turning the ship its gonna start turning from the top left so it's gonna turn very strangely but we want it to turn from the middle so I'm going to change this to be the middle center all right next up I'm going to make three different asteroids so I'm going to make a small one which is going to be 16 by 16 and I'll also just put it in the middle Center and then I'm going to make a medium one which will be 32 by 32 and a huge one which will be at 64 by 64 though they're all kind of half of each other so that when we're breaking them apart they can kind of have into two more asteroids so again I'm going to draw them quite simplistically it doesn't have to be anything fancy and I'll speed this spit up and come back to you when I'm finished all right so these are my asteroids so just make sure that all of them are centered at the middle Center and that is all for the sprites now let's make the objects the objects right-click and make a new object we'll call our first one obj ship and you'll see here that we can actually assign a picture to this object so that when it's in the room it won't just be an invisible object this will mean that it will draw this sprite so we have now set its fright index to SPR ship let's also create obj asteroid and I'm actually only gonna have one asteroid object and we can just set any of these for now but what I'm actually going to have it do is randomly selects from three of these sprites when it's created so that way we're gonna get some different kind of objects appearing in the game so if we come down to our rooms but for the time being we're just gonna have one room just a test our game and I'll call this our M game and we can just drag our objects in here and if we'd run the game now we should see those coming up all right so they're there so obviously we can't actually move yet because we haven't added any input to our objects and you'll also notice just the size of the screen at the moment the size of this game window is the same size as the room so it's 124 pixels by 768 so maybe let's just change that I'm gonna make this 500 by 500 which is kind of small but it's a pretty retro looking game so I think it's gonna fit with it so if we run the game now you can see that the game window it's a lot smaller and it matches the size of the room you can actually make game windows that are different to the size of the room which has to do with the viewports but we're not going to cover that in this tutorial all right so let's come back to the workspace and let's do the logic for our ship so let's make this ship move so remember how I said in the previous tutorial that our fundamental logic of programming is basically if this then that or in game maker it would be if an event happens do a certain action so to make the ship move according to our input an event that we could use is the keyboard presses so we could create three events and put the different logic inside these events but another thing you can do is also use what's called the step event 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 be 60 frames per second well in game maker frames are basically called steps and so there are 60 steps in a second so this means that whatever card we put in here is going to be run 60 times per second and the benefit of putting all of them in a step event is that we can see all the logic at once so I'm just going to create some room and dock these panels and we'll make this a little bit larger and let's have a think so in plain English what we want to do is basically say if we press the let's go with the left key then we want to tilt the ship a certain direction so in code what we can do is use the if statement so if something which we put in these parenthesis then we use curly brackets to say what we want to do so anything in the curly brackets going to be performed if this is true I could say if 1 equals 1 that is true so it's going to run this code in the curly brackets but what we're tracking of course is the input so now at this stage I just want to say there is a lot to explore in game maker there's an entire language of GML but in these tutorials I'll pretty much only have time to go over the relevant functions and variables so if there's anything even in the interfaces that I don't discuss any buttons or anything feel free to hover over them look them up in the manual play around with the values it really is the best way to learn and a great place to start is the manual for example if I want to find out how to get the input I could come over to the scripting GML reference and I can have a look in the control functions keyboard input so this will tell us exactly how to get input from the keyboard so we can use this keyboard check so this keyboard check is checking if we're pressing the a button and there's a few different functions so we can check if a key is pressed if it's released or if it's just down right so if I'm pressing the key down I want it to keep spinning I don't want to do it just once which is what's going to happen if we use pressed so if we press the left key and we can see here that left key is VK left so if we put keyboard check the function is going to pop up and then inside these brackets so be careful of your brackets here we can put VK left so now we want to spin the ship and again in the manual there is a variable called image angle that we can change and this is to do with the rotation of the sprite and one important thing to note here is what direction is in game maker there right as it says zero degrees the zero degrees actually starts here just like it does on the unit circle and then the top is gonna be 90 degrees left is 180 and down is 270 so it is actually important how you've drawn the sprite so know how I've drawn the ship pointing to the right this is what we want because this means it's pointing zero degrees and then if we change the angle it's going to match exactly what the direction is make sure your shape is pointing to the right okay so what we want to do is change the value of image angle so basically we want to add to it we want to make it equal itself plus because remember we're going that way so plus let's just do five another way to write this is image angle plus equals five so it's equal to itself plus five and let's just copy this for the right except instead of adding we want to subtract all right so let's test that so press left and right and you can see that we're moving the ship okay now the movement so we're gonna do VK up generally the way that we make things move in game maker is changing their XY position in the room so you can see even in here that the XY position of objects changes as I move this over here is zero and over here it's increasing and the same actually goes for Y so the origin is actually up the top and it increases as we go down but to actually get the ship to move as if it's in outer space we want it to be using momentum and that actually involves calculating a bunch of vectors and I don't want to have to do a 30-minute math lecture here so we're just going to use a built in function motion at so this you can use middle click on this to bring up the documentation page if you want to have a read basically adds a certain amount of movement in a certain direction so the direction that we want to add the movement is just going to be the image angle and then the speed if we're pressing the up key for quite a few seconds that's gonna be running this curve because this is the step event 60 times per second so if we put a number like 1 that's actually going to be increasing the speed up to 60 in just one second so it's going to be going extremely fast so we actually don't want to add too much I'm gonna make it a very small number and feel free to play with these values if you want all of this is just how I have set this out there is literally infinity ways to code something they don't think I have a bunch of magic numbers and I am all-knowing I definitely am NOT this is just one way to do it okay let's test that so move and press forwards and we're moving okay that's great but you'll notice as we go outside the room we kind of disappear what we're gonna do is actually have the object wrap around the room when it goes outside and so we could set up logic here ourselves we could check if the x position of the player is outside the room so it could be is less than zero and its larger than 500 but again a life can be made easier if we use a built in function so there's one called move wrap and again if we bring up the documentation it tells you exactly what it does so it's gonna wrap an instance that leaves the room and you can actually just use it to horizontally or vertically wrap we're going to say that we did you birth and we can also set a margin so let's say true true and let's just set the margin to zero for now 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 edge and it looks a little bit clunky and that's because it is warping the player as soon as it's origin which is in the center reaches the edge of the screen and its nose is kind of appearing too far over too quickly what we could do is make sure the entire thing is over before we have it appear here so I'm going to set that margin to be half the size of the ship we can use a built-in variable called sprite width we could also just use sprite height because they're actually the same and divide that by two so if we do it now we can see it's a little smoother okay now our asteroids in the game went actually moving or doing anything and we just had that one kind of object so let's have it that when they created so in the create events of this event unlike the step event it runs once only once when the object is created and then never again this is a great place for us to do any kind of setup for the object so this is where you would declare variables and also perform once-off actions so for example right now I want to change its sprite index to be one of three things so this one this one or this one and there is a handy function called choose which is kind of like a random picker so whatever we put in here so one two three it will randomly choose from these and then spit out one of them but instead of numbers we're gonna put its price and what I'm gonna do because you can actually open these parentheses and have them open multiple lines and it can kind of help with readability because I don't have much space here I'm gonna say SPR asteroid small SPR asteroid medium and SPR asteroid huge okay cool so we have set the sprite index so if you ran that now you should see that it's sprite index is changing to be either small medium or large so another thing I want to do is as soon as it gets created choose a random direction and kind of head in that direction so there are a few other built-in variables you can use so the direction which is different than the image angle because the image angle is just about how its drawing but the direction is to do with the movement of the object so where the object is moving I'm gonna get it to choose a random number between 0 and 359 right so I'm not going to include 360 just because that's the same as zero degrees and I might also just do the same for the image angle just so all of the asteroids start off looking a little bit differently and even though we've set the direction we're actually not moving yet right we would have to change the speed so let's change the speed to be 1 and then because it's now moving just like the player objects I wanted to also do this wrapping let's just copy that and add it into the step event and maybe another thing we can do is to just have the asteroids slowly rotating so we could add to the image angle so image angle equals image angle plus one or you could just write image angle plus equals one that would be saying the same thing so now if we run the game there we go we've got our asteroids all wrapping around the room next time we're going to be working on some shooting and collisions I hope you enjoyed this tutorial and I will see you next time
Info
Channel: GameMaker
Views: 163,859
Rating: undefined out of 5
Keywords: YoYo, Games, YoYo Games, GameMaker, Game Maker Studio, GameMaker Studio, game, maker
Id: VIc8Kon5PFw
Channel Id: undefined
Length: 14min 25sec (865 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.