GameMaker: Studio 1.4 - Platformer Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to my new and improved platform game tutorial for game maker studio this video is going to teach you basically how to code the fundamental aspects of a platform game we've got moving jumping and a pixel perfect collision system it's all handled in about 40 lines of very easy to follow code which is all fully explained this system is super simple it's easy to follow and it should be the perfect starting point for any beginner coder who wants to get started making a solid 2d platformer game inside of game maker all you really need to know in order to be able to follow this tutorial is the very basics of using game maker studio so you should understand what sprite is one object is what are rumors and how to place and organize objects in rooms you should also understand the very basics of how events and actions interact with one another and if you don't understand anything about game maker you should go and watch my tutorial video on game maker basics and that'll bring you up to speed okay assuming you're okay with that you understand the basics of game maker let me explain what I've already set up so far I have a couple of sprites SPR underscore wall and SPR underscore player a couple of objects obj underscore player and obj underscore wall and I scattered those objects around the room the walls making up our little level and the little green box here is our player object in our one room at the moment we have mostly default settings the only thing I've changed in the default settings as I've set the speed of the room to be 60 making the game 60fps instead of 30 just to make it look a little bit smoother additionally it's worth making sure off for the purpose of this tutorial that your sprites I have their origin set to be in center like so my sprites I'm using the moment of both 32 by 32 rectangles they're really quite simple and so they're sent to origin is 16 that's 16 this just makes it so the object is being drawn from the center and not from the top-left and so we understand where our object is in virtual space at all times so the first thing we're going to do is just establish a few simple variables for our player object so go ahead and edit obj underscore player and add event we're going to add the create event so when the object is made we're going to go to control over in our actions and drag into the little code action into our actions box this is really the only action you ever need when you're working with code and I'm just going to give this a comment so that we know exactly what it is we're doing here and put the words initialize variables whenever you're working with code if you type two slashes like this you can the text turns green and it becomes non code it just becomes comment text that you can write write anything into and it won't point I won't affect your game in any way a special thing in game maker is if you add three slashes to align at the start of your code and if I click the tick now you'll see the very action itself as turned into initialize variable initialize variables there we go like that so then it even tells you in the action window exactly what that comment says and it can be useful just for organizing your code this is a little tip that I learned recently there not a lot of people know about so the first layer there we go we want to setup is graph y equals nine point two and not line of the semicolon a graph is going to represent gravity be careful not to use gravity before word as tempting as it might be used word gravity for gravity because that's a built in function for gravity and that's not what we're going to use like that if you set a number if you said like gravity itself to equal to or something like that then game maker will automatically use that gravity and it will move your object for you and it will will handle gravity all by itself but we don't want to do that we want to maintain complete control in the complete understanding of everything our game is doing so we're going to use our our own variables and we're going to code all the effects of gravity ourself so that we perfectly understand everything that's going on in the game the next variables we want to add RHS P equals zero and VSP equals zero these represent horizontal speed HSP and vertical speed VSP respectively that's set to zero at the moment because these are just going to be contained for our current horizontal speed and our current vertical speed at any given time so obviously at the beginning of the game they're going to start at the zero again don't use hate speed or key speed as these are built-in functions that will have their own effects in the code that you won't be able to directly see them and nebula as what we're going to create the next variable you need is called jump speed I'm going to set that to seven and that's just the amount of speed that's going to be added to our object in the up direction whenever we jump and the last variable we need is move speed and we're going to set that to four and that's going to be the amount of pixels per frame we move left and right whenever we press the left and right arrow keys simple as that so that's all the variables we need to establish so that's actually everything we need for the create event so you can go ahead and close that now on the next event we need and what the only other event we need is this step event as you may already know the step event action is contained in here will be carried out by the object every single frame of your game so go ahead and drag the little code icon into here it's all we need again and the very first thing we want here is we're going to get the players input so what we're going to do is we're going to find out what buttons are currently being pressed by the player in order to tell us whether or not we need to move or whether or not we need to jump X cetera so we're going to do this by basically assigning the results of keyboard checks to variables so every time we need to do this in the step event and on the create event because we need to be checking every frame so the very start of the frame what we're going to do first of all is checks a key underscore right that's just going to be the name of the variable to store whether or not the right arrow key is being pressed I'm going to stand out to be equal to keyboard check and you can see how that's turned a little like yellowy color because thou gained make a nose that is a function and if we open a bracket and you can see at the bottom here it's waiting for keyboard underscore check and in brackets key wants to know what key were looking for word being pressed and we're going to be looking for DK underscore right VK on the score right I believe we can not entirely show by believe ek stands for virtual keyboard this is just a constant that's built into game maker which you can see because it's turned red on the represents the right arrow key you can find out all of these like if you middle click on anything in code it will actually open up the help menu and it will bring you to the page related to that um whatever is you just clicked onto you can see I right clicked on this and it's brought the keyboard input page and you can see all the different constants VK left week a right hook down enter escaped space and so on for moving in different directions a common question that came up in previous tutorials is how to use other keys like WS a and D and so on so quickly now say you didn't want to use the right arrow key you could type in odd and in brackets what would right beyond wsad it's d so you can type that instead and or D that will look forward to see if the D button on your keyboard is being pressed but for now we're just going to stick with the arrow keys so VK underscore right so again just to sum up very quickly I know I've gone over this line quite a bit the key underscore right is just going to equal the result of checking to see whether or not our right arrow key is pressed and that's going to return either a 1 or a 0 so keep in mind now that after this line of code is executed by the game key underscore right is either equal to 0 or it's equal to 1 it's equal to 1 if you're pressing the button and 0 if you're not I keep that in mind it'll be important later so the next thing we want to do obviously is check the left arrow key now we're going to do this a little bit differently and you'll understand why a little bit later but no set key left to be - keyboard underscore check VK on the score of left semicolon now the reason I put that minus in there is you remember I told you before that this equals either not or 1 well that means this is either going to equal minus not which is of course still 0 or it's going to equal minus 1 because it's going to return the 0 or 1 from this and it's going to make it negative by because we've got that minus that so key right will either be 1 or 0 and key left will either be negative 1 or 0 and of course the only other key left we need to check is our jump button which we're going to make the spacebar for now and I say key underscore jump equals keyboard and the score check underscore press open bracket DK space so we're going to check the spacebar and what press does is it means it's going to check to see if the button is being pressed but it wasn't pressed the frame before which means if you've only just pressed the jump key so basically meaning we can't just hold the jump key down and unjam loads like you have to just distinctly press jump every single time you want to jump and that's the difference there there's also functions like released so you can tell when the boners let go of and so on but that's not important to us right now so now that we've got the player's inputs we know what bones are being pressed by the player at any point in time we can now react to inputs that's going to be this next section of code now we're going to set up a new variable here called move and we're going to set move to be equal to KI underscore left plus key underscore right and the line of the semicolon as always now if you remember before key left will either equal minus 1 or 0 and key right will either equal 1 or 0 so you're going to get a number here that either equals if say both keys are being pressed so key left is minus 1 and key is one added together that equals zero so doing this we're going to basically create a single variable that tells us what direction we want to move move will be minus one if just key left is being pressed it will be plus one if just P right is being our right arrow key is being pressed it will be zero if they're both being pressed and it will be zero if neither of them are being pressed so now that we know that to be true it's very very easy for us to now say what our horizontal speed should be so our variable from before is called HS p and HS p is going to be equal to move so either minus 1 0 or plus 1 multiplied by using asterisk asterisk representing multiply when you're working with code move speed and you'll remember we start our move speed before so now HSP is either going to equal minus 4 if we're pressing the left arrow key plus 4 if we're pressing the right arrow key or 0 if we're pressing neither or both then the only other thing we need to do is add our gravity so if we say but first of the important thing with gravity is to remember that if we keep accelerating with gravity we're going to keep getting faster and faster to the point where it gets ridiculous and things will break in you're moving hundreds of pixels a frame so we're going to first of all make sure that we haven't gone over a certain maximum with our vertical speed so I'm going to say if vertical VSP which is our variable for our vertical speed is less than um about 10 we'll say and so that means if it's less than 10 pixels of frame downwards because the higher VSP is the faster we're moving downwards and the lower it is like the more negative is the more we're moving going to be moving upwards so if it's less than 10 pixels of frame downwards VSP plus equals valve now remember what gravity is no point to and VSP will add nor point to to itself that's what plus equals does if as long as our gravity our vertical speed hasn't gone over ten so that's just a little safety check that and then that'll add our gravity okay so now we've got gravity affecting the player we've got the left and right arrow keys um setting up our variable and moving us the only other thing that we need to react to input wise is our jump button so I'm going to say I'm going to say to the game if we're on the ground and we're pressing the the jump button then add our jump speed to our vertical speed pretty much and how we're going to do that is first of all say if Open bracket place underscore meeting open another bracket X so our current x coordinate y plus one so our current y coordinate plus 1 so 1 pixel below our character sprite at the moment is where we're checking obj underscore wall so this is asking if the one pixel below our player object there is a wall meaning are we on the floor very distinctly I'm going to open some curly braces after that so if that is true and there is a floor below us we're going to carry out the stuff that's inside here it's only going to be one line of code its VSP equals key underscore tion x minus some speed now hopefully that's not too confusing and you can kind of work out the logic based on the same thing we did up here with HP so our vertical speed is going to be equal to our key jump variable which will be 1 if the spacebar has just been pressed or it'll be zero if the spacebar is either being held down or as as not only as not impressed so it's going to be 1 multiplied by negative jump speed you remember our jump speed is 7 so when we press the jump key our vertical speed is going to be set to minus 7 and otherwise um if we're on the ground and we're not pressing the junkie then the clock speed is going to be set to 0 which is what we want anyway because we're on the ground now what you may have noticed is that with setup HSP and vsp to receive the right the right numbers the moving is left and right four pixels one way four pixels the other or making this jump us seven pixels of frame upwards and gravity for pulling us back down again what we haven't actually done is apply those numbers to our object and meaningful way yet so if we type X and it you can see it's turned red recognizing that as an important part of our object plus equals HSB and y plus equals V SP it means after working all this out we're going to add our horizontal speed to our x coordinate and add our vertical speed to our y coordinate which will actually move the object however you'll notice if we run this now that we can move left and right an agency we fail at the beginning there but there's no actual proper collision going on here there's a kind of thing like work we're almost walking on the floor but it's not really it's not really there yet as you can see we can kind of fall out and through it and it's not really working the reason why that is is because we don't have anything in here that's actually checking for collision other than this one line here that's kind of setting or vertical speed to be zero if there is a floor below us but it's not properly handling our collision in terms of how our gravity is were so the next thing we need to do is actually set up collision and this is something that most people who are new to game development new to coding in general tend to get intuitively wrong so let me just take a moment now to explain how collision works now most beginners tend to see collision or how to handle collision as a little bit something like this they imagine that their object is moving around the around the pixel space and their game and the the logic they need to win the logic they need to create is to say okay when my object is colliding with the wall like this move it so it is no longer colliding or when I am colliding stop moving but they as you can see just by me demonstrating it moving it around once you're colliding it's already too late you've already done something that your character was not supposed to do and you've got to work about reversing it now it's possible to get into a situation where you like Durrett mid frame you've collided you move your character all the way back out of the wall like this and by the time the frame actually renders it looks like you've never collided with the wall it's possible to do that but it's not really an intuitive way of handling collision in games in my opinion the way I always handle collision in platformer games and really more or less any game actually is to say if I'm about to collide now as you know it's up in our code these horizontal speed and vertical speeds variables that have worked out how far we're about to move in the next frame so say and you know we're moving about four pixels of frame when we're moving left and right so say we're here with three pixels away from the wall and now if we're pressing right all of our code has worked out that we're about to move four pixels to the right so what we want to do instead for collision is say are we about to hit the wall to the alright so as for pixels to the right of us where we're about to move our current coordinates plus our current speed is that area free and the game will check here and say oh no it's not free and since it's not free what we do then is we set our horizontal speed to zero because we basically hit the wall but we also move ourselves as close as possible to the world because that speed still exists that horizontal speed of four still exists we just can't use all of it without hitting the wall so we're going to instead of using four of it here we would use three other and so we move one pixel at a time towards the wall until we can't move any further without hitting the wall and then we reduce our speed to zero and that causes us to have a pixel perfect collision with the wall and we do the same thing moving downwards and that more or less is how our collision system is going to work so thank you for bearing with me on that little detour what we want to do is basically establish these collision checks just before we actually do our we actually commit to our movement so we're going to do the horizontal ones first so let me write a comment for it horizontal collision just so we know what this section is going to be about so as I was saying we first of all we want to predict whether or not we're about to collide so I'm going to say a bracket place meeting the same line we used before to check whether or not we were on the ground x + h f no yes sorry X plus h SP comma Y comma obj underscore wall close bracket so as you can see we're going to check to see if our current x coordinate plus the amount we're about to move horizontally whether or not at that spot and that our current y coordinate there is a wall so are we about to collide horizontally open a pair braces it's going to be quite a bit more stuff in this one than what they usually what there wasn't that last on so if our code is needed inside these curly brackets then we know that we're about to collide so what do we do when we're about to collide we move as close as possible to the wall without actually hitting it so we're going to set up a while loop a while loop will carry on happening and it'll carry on executing a bunch of commands until the condition you give it is no longer true this condition is going to be not represented by an exclamation mark exclamation mark represents not in GameMaker code place anything X plus sign HSP close bracket comma Y comma obj underscore wall now as I said this while loop will carry on happening as long as this condition happens to be true oh but because we've put this knot symbol here it's kind of actually whether this condition is not true it will carry on going so while place meeting X plus sign Hesper now what sign does is will return either 1 or minus 1 depending on whether HSB is positive or negative this is just to make sure it works whether or not we're going left or we're going right so say we're moving to the right and we were about to hit a wall on our right HSP would be positive because by moving right and so it would be X plus 1 so it will be checking if one pixel to our right there is no wall not place meeting wall so while we're not hitting the wall X plus equal sign HSP so while one pixel in the direction that we were about to move we're not hitting a wall one pixel in that direction and it will keep doing that until one pixel to the right or one pixel to the left is that wall and it's about to hit the wall after it finds that it'll come out of this wire loop and it will go into here at which point all we need to do is say HSB equals zero so that we don't carry on moving any further and that's actually all we need for our horizontal collision code and make sure after that we you know after we've done all that it's safe to to move by however much our horizontal speed is if we're about to hit a wall it'll be zero and otherwise it'll be whatever it is appropriate to whatever buttons we pressed so we're more than we're more than okay to go ahead and commit to our movement now for vertical collision we basically need exactly the same thing so feel free to just copy and paste the entire chunk of code down here and we really just need to move all of these things to relate to our y coordinate instead of our but our horizontal coordinate or x coordinate even sorry so I'm going to remove plus saints HSP off this line and add VSP instead so y plus our current vertical speed and while we're not meeting a wall at our current x coordinate and our y coordinate plus sign VSP when while we're not meeting a wall there increase our y-coordinate by one in whichever direction and then set our vertical speed to be zero and that's really all there is to it and then y plus equal VSP commits our movement and I'll change this common here so this represents our vertical collision and that really is it so 36 lines of code and the staff at Ben and I think it was something like five lines of code six lines of code in the creative and that's everything you need and I'll run the game now to demonstrate to create a working platform as you can see we can jump around we can collide with walls it's all perfectly functional there's no real problems with the system at all and it's a pretty good starting point to get you going making a platform game hope that was useful for you guys and I'll catch you next time
Info
Channel: Shaun Spalding
Views: 1,080,344
Rating: undefined out of 5
Keywords: Game Maker (Video Game Engine), Game Development, Indie Games, Indie, Games, Tutorial, Information, Learning, Tutorial Series, Game Maker Studio, Steam, iOS, Android, HTML5, Windows, Making Games, How to make games, Basics, 1.4, 1.x
Id: IysShLIaosk
Channel Id: undefined
Length: 26min 22sec (1582 seconds)
Published: Fri Mar 21 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.