GameMaker #4 - Simple Player Movement & GML

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to the tech camp series on game maker studio 2. in the last video we created a room a room acts like a level in game maker we added to our room a load of objects starting drawing out the first level in this session we will be actually adding some code i'm going to close the level editor and go back to my workspace um car object is open at the moment don't need that just yet let's close that let's start with giving the player character some movement so if in the assets browser i'm going to look and find the player character object and open that and we can begin game maker is an event driven engine what that means is that every time a certain thing happens that triggers an event and first thing you see when you open up a object is this events window here if it doesn't pop up for you you can click events down here but you should see a window like that if you go ahead and click add event you'll see that there are lots of events available you've got the create event you've got the destroy event you've got the step event the gesture event the collision event and so on events get triggered when certain things happen so if we're going to be adding code to our player such that when the player presses the right key on the keyboard while we need to think of what event will be triggered then if a user is pressing the right key well that's going to be a key pressed event so i'm going to click add event and look at key pressed this is user pressing a key we've got options got pressing left right up and down these are the arrow keys control space enter you could have anything on the keypad any of the numbers any of the letters you could even have function keys or page up page down so on i'm gonna start with right as soon as i've added the key press right event it's opened up the code window remember that we chose the gml language rather than drag and drop in this window i'll be adding some code to get the player to move right now the first things we need to think about are kind of a very essential part of game maker and that is built in variables recall that a variable is a box that stores some value now game maker has a load of these that are built in that means that they are always present in the engine and a lot of them are also present for each object and an example of a built-in variable is the direction variable so what i'm going to do is in the code window i'm going to press enter a couple of times to give myself space you can ignore the green text on lines one and two for now but here i'm going to start typing out direction direction now as i was typing out that you should see that this sub window has popped up this is game maker trying to guess what you're writing and we can see here we've got a built-in variable called direction direction is a variable that handles which way an object will go next and it takes a value of degrees so they could go 180 degrees they could go 270 degrees it could go 45 degrees anything like that the direction i want to go from right is zero because game maker counts direction weird right is zero north is 90 left is 180 and down is 270. so if i want my player to go right i need to set the direction to be zero now game maker language is a javascript based language and it's got a couple of rules that is very much like javascript one of them is that commands like this where i'm setting direction equal to a becoming equal to zero this kind of thing ends with a semicolon now it's not essential to add that semicolon in in gml but it gives you good practice for the rest of the programming world other languages like javascript java and c will use that semicolon so even though it's not essential it's a good idea to include it to get into the right habits what we've done here is we've written our first line of code we have set the direction of the object equal to zero that means that the next direction that the object will move in is right however our object's not going to move at all and the reason why is we haven't told it what speed it should move so to do that we're going to update another built in variable this one is speed and as you can see as i typed out speed it shows me all the uh components of mate sorry of game maker that are well have speed in the name so we've got things like h speed image speed path speed room speed those are all extra ones the only one we want is the speed on its own this is the speed of the object we can set it to a number so we can say speed is now equal to so equals and choose a sensible number so i might say seven let's go at seven speed and again you don't need to add the semicolon but i recommend you do to get into the right habit direction equals zero speed equals seven okay with that written i should probably save my project i can see that it's currently unsaved at the moment because there's a little asterisk in the code window and there's a little asterisk in the top of the window title that means there are unsaved changes so i'm gonna just file save project there we are all nicely saved okay if i wanted to i could close the code window and there's my object my object now has an event and that event is when the key pressed uh is the right key well then we'll run this piece of code here so let's play the game and see if it works i'm going to click run it's over on the other windows let me drag that across here is our player let's press the right key on the keyboard to see what happens oh and there our player goes off the screen and into oblivion okay so a couple of takeaways from that first of all when i press the right key and let go of it the player didn't stop that could be useful if you're trying to make a game like pac-man but in our game i want it so that when you let go of the right key the player stops moving so let's go ahead and add that let's add an event that triggers when we let go of the right key so add event i'm not going to use key pressed because that's the key going down i'm going to use key up that's the key being released so when the key goes up and it's the right key i need to do something so this is open up the code window for key up and the right key and here i can do any bit of code for what happens when i want the player to slow down so easiest and cheapest way is to do speed equals zero semicolon all that does is it sets the speed of the player to be zero and it'll just the player will come to an instant stop you can get fancy and you can do slowing down and deceleration all that but that's extra stuff all we want to do today is the bear basics speed equals zero let's give this a test well let's save the game first of all file save project but yeah let's give this a test now bring in the game window okay when i press right play goes right when i let go of right the player stops try that again when i press right the player goes right when i let go they stop none of the other arrows work yet that's because we've not programmed them let's go ahead and do that now and then after we've done that we'll look at the problem of going straight through the walls okay to do the other ones basically all we have to do is copy key press right and key upright but changing the direction for each different direction we want to go in so let's do going up well i'm going to right click on our key press right event i'm going to duplicate that when you duplicate invent it asks you to choose a new event the one that you want to duplicate the code into well let's do key pressed up key press up well what do i want well direction up i believe is 90. let's do letting go of that so let's do the key up right this time duplicate that change that to key up up very confusing name i don't need to change any code because it's still setting that to zero so key up up is done key press up is done key up right is done key press right is done okay let's do uh key left now key pressed left that direction is 180 let's do key up left duplicate event key up left speed zero doesn't need to change finally let's do key press down key pressed down that direction is 270 and then let's do key up down very confusing but key up down speed 0 needs no changes okay let's click file let's click save and run the game okay when i move right and let go they stop when i move down and let go they stop when i move left and let go they stop when i move up they left can't stop i now have full control over my player okay let's fix the next problem in that the player can go straight through the walls i remember in the very first introductory video i made a plan for this game part of the plan was listing all the behaviors the objects should have and one of the behaviors of the player was not to was to collide with the walls and not go through them so we need another event that's all to do with collision if we look in our player object and click add event we can see that there is a collision event down in this menu this event gets triggered every time a collision happens with a certain object so in collision i'm going to select objects then i'm going to select the house object that's the thing that we want to act as our walls or obstacles so i've now added a collision with the house object and what this means is if i uh add some code in here this code will be triggered every time the player hits the house so what do you want to happen well we don't want the player to go through the house nice simple way of stopping that is setting the player's speed to sequel to zero again so as soon as the player touches the house their speed is now zero they can't go through it they can't advance into it any further so file save collision with house speed equals zero let's click play and see how that works okay let's go right ah i can't go through the houses now i'm trapped yeah there's no way i'm ghosting through walls anymore okay so that's problem fixed let's add uh the goal of the game now let's add the goal of the game to be that the player when they hit the shop goes to the next level so let's click add event we're going to have another collision this time when they collide with the shop object what do we want to do well if we want to go to the next level we probably need to use a function now functions are sort of little bundles of code you can call on that will do something for you and game maker has a load of these built in functions and one of the functions we can use is room underscore go to underscore next and as you see as i was typing out it showed it in the um in the code window any function ends with an open bracket and a closed bracket and of course this is the end of a uh sort of as javascript sentence or a gml sentence so finish it off with a semicolon room go to next and this code all it does is it loads up the next room or the next level i've added this in the collision with a shop object for the player character so when the player character hits the shop we should go to the next level let's file save let's well i was going to say let's test it but there's a small problem we don't actually have a shop in the level so i'm going to go to the level double click in the room section over on the assets browser and here i can add a shop into the level so i've done this before in a previous video but just as a recap find the object in the asset browser drag it in oh that's a little bit small remember i could resize it here or i could resize the sprite but just for laziness snakes i'm going to resize it here but there's now a shop in the level let's test it okay let's get this up let's navigate to the shop oh you can see it's gone all blurry that's one problem with resizing things in the uh editor there's another problem i can't actually get through this gap and this is why testing your game is so important because you might find some more little bugs like this so what i think is the problem is that the player is 128 pixels wide these blocks are also 128 pixels wide that doesn't give me much leeway for getting through there in fact i've got a margin of zero pixels so for now just to keep things simple we can make the player smaller if i were doing this in a more advanced capacity there are alternatives to making the player smaller but for now making the player smaller is going to be easier option so i'm going to go to my player sprite play a character let's resize that down to let's say 96 by 96 pixels that's a bit smaller there we are and now when i click play wait for it to build there we are it's loaded the player is smaller should be able to fit through that gap hopefully yeah i can fit through now what happens when i hit the shop oh dear we get an error so here's one type of error that might turn up it's important to be able to decipher these kind of errors and read what it actually says so that says there is an error in well it says the event shop object but also the player character object so that those first four lines aren't too useful to me but then the next line tells me what the actual problem is moving to next room after the last room ah what that means is there is no level two i'm trying to go to level two when it doesn't exist that's impossible after that line it tells me where the error was so it was in my player character object it was in the collision event and the collision with the shop object and the line of code that caused it was room go to next so that is one problem there now there are a couple of ways i could solve that i could create um a level two or i could get it to check whether there is a level uh next before going to it but these are all more advanced steps that you might need to explore on your own as we've got more things to do in this demonstration what we need to do in this demonstration next is get the cars moving up and down so i am finished with the player character for now so i'm going to click close player character i don't need the player character sprite anymore what i do need is car up object let's start with the car going up and here we are now to get the car moving i can do the exact same thing i did with the player character i could set the direction then set the speed but the question is what event to do it in i don't want to do it on a key press event because that would mean the player can control the cars which would be a little bit weird instead i'm going to do this on a create event the create event if i do add event create gets triggered when an object is first created so an object is first created when the room first loads so oncreate what do i want to do well i want to set the direction to up so direction equals 90 semicolon i also want to set the speed so i can set speed equals and i could put a number in here however if the cars move at the same speed maybe that might be a bit of a boring game maybe i want to uh sort of you know make things a little bit more difficult or more interesting maybe i decide i want a random speed so to get a random speed i can use another built-in function and i'm gonna just start typing out what i think the function might be called and we'll see if the code window tells me the built-in functions see if i'm guessing along the right lines so random oh yeah there's a couple of ones that uh to do of random there's random x random get seed random underscore range i think random range is probably the one i want because it had two numbers after it and that's kind of makes sense if i'm doing a random range i kind of need to give it the numbers it needs to go between so i'm saying speed equals random range one for the lower number comma to show i'm going to the next number and let's say the upper limit is five and of course the whole thing ends with a semicolon so now i'm setting the speed to be a random number between one and five and that will give our game a little bit more flair a little bit more interest than just having the cars go at a default speed okay let's click file save before going on to the next event i said that i wanted the cars to go up indefinitely so they just go up the screen and then up the screen and then up the screen and up the screen again and again and again a nice way i could do that is have it so that when they leave the room they return to their start position so let's click add event now in here we've got some options well let's pick one that might be triggered when we leave the room and the one you're looking for is over in other and the other menu we've got things like outside room intersect boundary game start game end path ended all sorts let's see if outside room works this outside room event what i think will happen is it'll be triggered when the object is outside the room now if we want to set the object to return to where it started i need to work on its position i need to reset its position and the position of any object in make code arcade is stored in two built-in variables the x variable and the y variable the x variable is the x position of the object so how far left or right the y position is how far up or down so if i wanted to reset the position of the uh car to be uh where it started well i need to set x to be equal to something um how do i find out the position of where it started tricky fortunately though there is a built-in variable for that there is a built-in variable called x start that gets stored when the game starts it stalls the starting position of an object the x start variable is the starting x position of the object in question so when i say x equals x start what i'm saying is update your position to now become your starting position that's x let's try that again with y y equals y start we also set the y position to be the starting y position and what this will do is hopefully it will have the effect of resetting the position of the car as soon as it leaves the room let's click save or file save either one and play and see if that works okay so we've got cars going up the screen and as soon as they leave the screen they jump back to where they started the car goes up you can tell by the different distances apart that they are choosing a random speed every time or in fact they are actually overlapping each other there so that's a a bug we can fix if we wanted to look at more advanced stuff but for now getting a little bit of movement pretty cool they don't actually do anything the cars we can fix that by adding a simple collision event to the player so that when the player collides with the car it either knocks off some health or resets the game or shows a you lose message anything we wanted like that okay let's just get the down cars working just as a recap of what we've done here so i'm going to open up the down car uh object editor i'm going to add event create when we create the car we want to set the direction equals down well down is 270 degrees so 270 semicolon we want the speed to be a random number between the range one and five semicolon let's add an outside room event outside room what do we want to do we want to set the x position equals this x starting position i'll say y equals the y start and um well let's just for the purposes of demonstration let's make a little mistake here actually let's say y equals nothing yeah it if i've forgotten a number somewhere you might get a an error like this if i'm not paying attention to that and i click run anyway something interesting will happen okay so down the bottom in the output i can see i've been given this message failed run program complete what this means is it's failed to run the program and then it says underneath for the details of why this build failed please review the whole log above and see your compile errors window well the log is quite long and quite full of gubbins it does say error here somewhere but what's most interesting is the compile errors tab and this lists all the errors that happen when you're compiling the game so this is where you'll find a lot of your coding mistakes so in object car down the outside room event at line five oh it says there's unexpected symbol so that's part of the problem but the major problem here is malformed assignment statement an assignment statement is any statement with equals in it's where you're resetting the value of one variable to be something else you're assigning that verb a variable a value well it's malformed which means we've made a mistake in that and yeah i had y equals nothing well let's change that back to y equals y start click play wait for it to build builds with no errors and now we've got cars moving down and yeah they're choosing a random speed most of the time okay that is it for this very brief demo game there is a lot of improvements to be made here things like actually colliding with the cars to make them dangerous there is possibility for smoother more satisfying movement with the character but that's a more advanced step hopefully this uh video tutorial series is giving you a nice leg up with the software and gives you opportunities to explore more in case that's being me i'm jack from technocamps hope that has been helpful in your journey with game maker studio two and uh good luck with it cheerio
Info
Channel: Technocamps
Views: 200
Rating: undefined out of 5
Keywords: Computer Science, Distance Learning, Home Education, Home Learning, Lockdown Learning, Teaching Resources, Free Resources, Free Activities, Kid's Activities, Activity Packs, Remote Learning, Computing, Programming, Science, Science at home, computing at home, STEM, Maths-Lovely Maths, Maths, School resources, Computing curriculum, WJEC, WJEC Digital Technology, Gamemaker, Gamemaker Studio 2, Tutorial
Id: bFBKQZAun8U
Channel Id: undefined
Length: 33min 8sec (1988 seconds)
Published: Fri May 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.