Make a Game in GameMaker | Bounce or Break!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
greetings and welcome my name is Aaron Craig and today we're going to be creating an awesome little game called Bounce or break using the physics engine inside of game maker you can see it right here you're going to be able to shoot a ball at different objects and it's going to be able to bounce around and it's going to have a lot of life to the movement it's going to feel very real because we're using a physics engine it's going to be a total game so that you can play you can keep track of your score but when one of these objects gets up too high the game resets and you can try again if that sounds exciting let's go ahead and dive in you'll be able to follow along with gml or gml visual make sure that you download the project because it's going to have all the Sprites and sounds that you'll be needing to follow along with let's do a quick overview of this project we're going to have the Sprites for each of the objects the arrow that we'll use and the background and we also have the sound that we'll be playing whenever this ball hits an object object or a wall and of course we have the default room inside of here so to show you the basics of the physics world and engine inside of game maker let's go ahead and get just a ball bouncing on a wall just so that you can get familiar with it so let's go up to objects and I'm going to click on this plus icon the create asset and I'm going to click on objects and I'm going to tick it up to two because we actually want a ball and a wall so I'm going to double click on object one and I'm going to rename this o ball and then I'm going to scroll down and I'm going to rename object two to o wall now we've already got Sprites for each of these so I can just drag the spr ball onto this object and I can do the same thing for the wall do spr wall so so the physics engine that's built into game maker has to be manually ticked for every object and every room that you want to use it in because it is not the default option when making a game so every object is going to have this little box that says uses physics we need to click on that and then we have a little window that we can open up by clicking this physics button right here and this is going to open up all the properties that we can control about this object so things like density restitution the Collision group that it's in linear damping angular damping and friction along with a couple of options here and even modifying the Collision shape itself now there's a lot that we can go into but essentially each one of these options is going to change how this object is interpreted in the world by how bouncy It Is by how heavy it is and each one of these you can dig deeper into for now let's do a really quick overview so that you know why we're changing the numbers on some of these the density of an object is how heavy it weighs but this is distributed over the size of the shape of the object what that means is this ball that's 32x 32 pixels is going to be very dense if we give this a high number because it's a very small object that same number would make a huge object actually be very light maybe like a balloon so the number the strength the amount of density that we have right here depends on the size of our object and in this case we're going to set the density of this ball to 75 that might seem like a big number but don't worry it's going to look and feel really great next is the rest of subtitution and you can think of this number as how bouncy you want this object to be the bigger the number the bouncier it's going to be in this case we're going to set it to 0.75 and we'll set the other objects to make it bounce more when it hits them as well once we Define their properties Collision group just means that you can set every group of objects to a specific and unique ID we don't need to worry about this at all for this project linear and angular damping are very similar you can think of linear damping as the air in the world all of those particles that as something is moving through it it's actually hitting them and slowing down over time angular damping is that same thing except for rotating objects so that when you want something to eventually stop rotating that angular damping it controls how quickly that will happen and the Very last one is friction which is just how much something is going to slow down when it's hitting it and moving on it so for linear damping let's go ahead and change that to .25 and for friction let's change it to 0.1 these bottom three we don't need to worry about the sensor the starting awake and kinematic just leave it the way that it is lastly let's look at the modify Collision shape because there are several different shapes that you can have available and if we look at this one we can actually see that this is the wrong size so I'm going to zoom in by pressing the magnifying glass here and you can see that this is actually a square or a box in this case and if we click on this dropdown menu you've got three options a circle a box and a convex shape and I'm going to show you what each of these shapes does for this one let's go ahead and click on Circle and you can see immediately that it changes from a square into a circle and it has a radius and it picks the correct size based on the Sprite that's there so that's everything that we need to do to get our ball ready to actually fly and move around inside of the world so I'm going to close the physics tab here then I'm going to scroll down and for o wall we need to do this same kind of thing but it's going to be a little different because this wall in our world and the point objects we don't actually want to fall down as the game is being played they need to stay in one spot so there's a trick for that I'm going to click on uses physics open the physics Tab and I'm going to set the density to zero this makes it infinitely dense which just means it is not going to move ever nothing can ever make this thing move when colliding with it now we have controls to move it which will do but if you want something to just stay there but still be able to be interacted with setting the density to zero is what you need to do now let's go through and alter the properties here for restitution let's put this to 75 so that when the ball hits it it gives it some of its bounciness and will'll change the friction to zero because we don't want the ball to actually slow down at all when it hits this wall and lastly let's take a look at the Collision shape we saw before that it chooses Box by default and in this case it's chosen box and set the size correctly so we don't need to worry about it let's go ahead and close the physics Windows completely and double click on room one to open it up now let's place the ball somewhere in the middle let's take that wall and let's click on it click on a corner and drag it out to be a little bit larger now we'll be be able to see the ball actually hit this and bounce on it before we do that though we must set the room properties itself so remember every object must be ticked and set to use physics and every room you wanted in must also use the physics engine so by clicking on the room it will open up the inspector over here on the left click on room physics near the bottom and you see that you can enable physics with the checkbox right here then you you can control things like the gravity X and the gravity Y in this case we're going to set gravity y to 125 so it's going to be pulling down with that much force if you have things that are heavier or lighter in your room this is going to treat them and pull them down in a different way for each object in this case our ball will be pulled down but our wall which is a zero density is going to stay exactly where it is so let's go ahead and we're going to change the target from GX games to test and I'm going to press play and we'll be able to see the physics engine working immediately right here and there we go the very first test of the physics engine in game maker and you can start to see that with no code and just setting a couple of numbers you can actually get a game up and going now we want to actually turn this into a game that we can play so let's go ahead and start adding some objects and some code into them to be able to control them so let's jump back over to our workspace click on objects and then I'm going to click on the plus icon and I'm going to click objects and I'm going to type the number six because we need six more objects here for the rest of the project let's go ahead and make them right now click on Create and now we have all of them that we need right here and we'll rename them and add them to the right spot as we move along so first let's click on object three this one we're going to call O controller this object is going to be the arrow that actually moves around and then spawns the ball and shoots it out when we click on it so that's pretty useful next let's rightclick on objects and I'm going to click on create group here because I want to keep this project organized as we make it and as you might want to expand it as you get finished with it and want to try out some new things so I'm going to click create group and I'm going to call this group points and then I'm going to click on object four hold shift and click on object 8 to select all of them at once a lot faster this way and then drag them into this group this is where all of the point objects are going to live so let's put them in here now for o controller before we jump into the points let's go ahead and assign that Arrow to it and this one does not need to use physics so we don't need to check this at all now let's go over to object 4 and this one I'm going to call o Circle and I'm going to drag the spr circle onto it this one is going to need physics so let's go ahead and click that open physics and these properties are going to be the same for all of the point objects that we put in here so we just need to change density to zero and restitution to 0.5 and we have to make sure that the Collision shapes are correct so let's click on this and you can see this is a box which is no good so let's click Circle and we're all set I'll go ahead and close this out object five I'll double click on and this one is going to be called o diamond and then we'll drag the spr diamond onto it and for this one we need to click uses physics change the physics to zero and the restitution to 0.5 and for this one let's go to the Collision shape now a diamond is just a box turned but game maker doesn't know that and this box is not it doesn't have any way to turn like this so instead of a box or a circle we're actually going to click on convex shape take a look at this graphic here and you'll see the difference between a convex and a concave shape which you'll be tempted to create as you start making physics games a convex shape for game maker can have up to eight points and all of those points have to be going outward in a concave shape some of those points Bend in kind of like a Pac-Man and Game Maker does not natively support a concave shape in another part to this series I'll show you how you can use concave shapes in game maker without too much hassle so we are going to actually add these points manually I'm going to go ahead and zoom in and I'm going to click on the top of the diamond and you just get it kind of lined up and it'll be perfectly fine I'm going to click over here and that should be good then I'm going to click down here and you'll see that it makes a triangle now we need to add one more point to this end and there we go that is our convex diamond shape that we want to use right there and we're all set so I'm going to go ahead and close that and click on our next object object 6 let's go ahead and name this one o square and we're going to drag the square shape onto it click uses physics click on the physics tab we're going to change the density to Z restitution is 0.5 and it uses Box by default so the square is all set let's close that last point that we're going to have is going to be the triangle so we'll call this o triangle drag this onto here it uses physics and we have to set the density to zero and the restitution to 0.5 we need to modify this Collision because a triangle is not a box or a circle so we're going to click click on convex shape zoom in and we can start these points anywhere that we want but we're going to click right here here and here and it makes that triangle for us so that's the really nice thing about making these custom shapes it really is pretty simple let's go ahead and close this out and that's all the points that we're going to need the last object that we're going to use we'll go ahead and name it right now is going to be called o Point Parent and we're going to talk about parents and inheritance because we want to have multiple Point objects inside of this game but we don't want to have to repeat our code over and over so we're going to put nearly all of our code inside of this one point parent object and we'll give that code to all of its children and it'll be really quick and easy so first up let's go ahead and get the ability to aim and shoot and create a ball up and running so let's click on o controller and we're going to add a step event to follow along in gml look to your left to follow along in gml visual look to your right in the step event we're going to set our image angle to the value of what we get back from the function Point direction so we pass in where we're at where our Mouse is and then we can set our image angle correctly okay now that we've got the image angle set properly let's go ahead and add another event this one is going to be the draw GUI or guey event for this event we're going to draw the score but anytime we draw we also want to make sure we set the color properly so we're going to set it to White and then we'll draw the score of the player with a caption of score right before that and next up we're going to use the global left pressed event to spawn the ball and shoot it out where it's supposed to go so let's go to add event it's going to be under Mouse all the way at the bottom Global and Global left pressed there's a lot going on here so let's break it down line by line the first thing we're doing is checking that no other ball exists and if that's true we're then going to create a new ball object right at the arrow we're going to save that in a temporary variable called ball then we're going to get the point Direction so where we want this ball to actually be flying towards with the function Point direction then we're going to create a couple of local variables called X force and Y Force and these two variables we're going to multiply against a special function called length dur both for x and for y and this is going to give us the length and direction of how much force we're actually going to shoot this ball out with and the last thing we do then is finally apply that Force to the ball and we use a special physics function because we're inside of a physics World here called physics apply impulse this function immediately applies that force that we set up with our variables X force and Y force and puts it onto our ball and is going to send it flying exactly where we want it to go for the gml visual users be sure that you use that little arrow and you check that this function physics apply impulse doesn't apply to self but actually applies to the ball object okay that's everything we need to do to be able to shoot the ball and test this out so let's jump back over to room one and because the code requires there isn't a ball let's go ahead and delete this one and to set our room up right so that we can actually see it and it looks a little bit cooler let's go ahead and go to background and I'm going to click on no Sprite go into the Sprites folder and I'm going to choose the background and this is going to be the size of the game that we're creating so let's go and quickly change a few more options here let's click on room one so that we can see the properties over here in the inspector and we're going to change the width of the room to 480 pixels and we're going to change the height to 720 so this is going to be the size of the room that we have this is a pretty common or actually the most common resolution for phones and that's kind of what we're making this game for so let's go down to viewports and cameras and we need to enable viewports go to viewport zero click visible and now we're going to change the properties to those same ones we set the room to so the camera is going to be 480 by 720 the viewport properties are are going to be 480 by 720 and once we've done that our camera and our room are all set up the way we need them to be so let's go back and click on instances and let's drag our wall in here and let's drag our arrow and put it right here for now and let's click F5 to run our game and now our Arrow we can actually click and there's our ball and it moves and it bounces just the way we want to make this a little cooler to be able to actually shoot multiple balls at once we need this ball to be destroyed when it leaves the room but first let's get this Arrow lined up so let's click on the arrow and if you try to move it you'll notice that it snaps to the grid well if you hold the control key you can undo that snapping for the object you're moving and let's drag the arrow so that it's right here the base of it is right there at the tippy top and that is going to to make it look and feel much much better then let's click on o ball and let's add a new event and this event will trigger when the ball is outside of the room and destroy it as simple as that it's under the other events the very first one outside of room for this all we need to do is destroy the instance with the function instance destroy and now we can test this out let's run our game one more time and if we shoot the ball we can shoot it as soon as it leaves and now you can see we can kind of shoot the ball wherever we want on this and it will bounce wonderfully okay now that we've got that working let's go ahead and tackle the points and getting them moving up the screen as we play and this is going to be simpler than it seems because we're going to use that parent object we created so let's jump over to Point Parent and a parent is a way for us to share code among objects and be able to use other functions inside of game maker to be able to find all of those children whenever we want to we're going to use this functionality and it's going to save us so much time and energy and this system is great for anything like multiple groups of enemies or multiple objects in your game that can be interacted with or anything along those lines so this will save you time down the road if if you learn it and master it now in our Point Parent let's go ahead and start with the create event let's add it here so we're going to set points equal to two plus our score / 10 so that it changes over the course of the game and finally so that it's a nice round number we're going to use the function round on the variable points next let's add the step of event at event step for this event we're going to always be asking the question has the player lost by one of the point objects getting too high in the level so we're going to check if the Y of any point object is less than 90 which is just above the border of our game and if it is we're going to show the message game over we're going to reset the score to zero and and we're going to restart the game with the function game restart next let's go ahead and add a draw event this one's not draw goey it's just the draw and anytime we add the draw event we also have to add the draw self otherwise the object doesn't even draw its own Sprite so we'll add the draw self event first so we don't forget it then we're going to take care of coloring and setting the text for when we actually draw it at the end so we're going to set the color to to Black the horizontal or H alignment to Center the vertical or V align to middle and finally we're going to draw the text from this object at its X and Y and we're going to draw points the text value and lastly what we're going to do is add a collision event with the ball so let's go to add event and it's going to be Collision objects with the ball object in the Collision we're going to subtract one from points because there's been a hit then we're going to play the sound effect bounce and we're going to play it at only a quarter of its initial volume because the sound effect is a little loud then we're going to check to see if there are any points left so if points is less than or equal to zero which means that it has no points we're going to destroy this object and we're going to increase the score by one now we don't ever add the parent object itself into the room but we do have to set each of these children to be Children of the parent so there's two ways to do this the first one is in the parent object itself we can click on parent and we can click on this plus to add a child and we can navigate to wherever one of these is so in this case if I click on o Circle that is now a CH of this one the other thing you can do is click on the object itself go into parent and up at the top instead of under children under parent you can choose the parent now watch what happens when we do this there are zero events in this diamond object but as soon as we navigate into our group and select the point parent object voila all of those events automatically get added inside of here they're gray out and they have this little icon here to indicate that it's coming from the parent now it's possible to override it but for this game we don't need to do that this is going to take care of the logic for all of our Point objects and it's awesome so we just need to select square and triangle go to parent and we can drag the parent here as well to assign it so let's do that there go to Triangle click on parent and drag that parent there as well and now if we return to the point parent object we can see all four of these Point objects these shapes are children so if you don't have that make sure you add them here you can drag them here to add a child if you didn't do that already and once we've got this set up our game is almost complete so let's go into our room and let's let's go ahead and take this wall and I'm going to go ahead and delete it for now and let's just put a few of these objects inside of our game now we don't have the logic having them move up but everything else is set in place so let's go ahead and run their game and we'll see that when we shoot they're actually going to go down in health now that ball went up and out and it died so let's shoot again and voila we can shoot and it can bounce and it can hit these and that is the primary logic and the mechanics of our game taken care of now this ball bouncing out of the room and dying instantly that's frustrating so let's take our wall object I'm just going to click on it here and then I'm going to basically paint it as an outline for our level so starting in the top left I'm just going to hold control and then I'm just just going to drag all the way down to right around there and this will make it that every single time the ball hits one of these it just bounces right back now one of the things you can do is you could add one wall object and drag this here but this can do something kind of funky to physics based games when we want to start doing a special kind of collision so I'm going to Nyx that and I'm just going to paint each individual wall Square onto here there we go then we want to do the same up here so I'm going to paint at the very top and once we've done that I can Center fit our room and now our game is this close to being done so we can click on this and now the ball bounces throughout the whole room and it looks and plays perfectly the last thing we need to do is actually have these Point objects move up and every single time the ball dies and to do that we're going to use the event we already have so let's jump back to our ball object and inside of here where we destroy it let's add in that logic to actually move up all of the point objects and we can do that because we have the inheritance with a parent already set up to make it really simple so because of the point parent we only have to apply this code to that one object and it will apply to all of its children so the one line of code we need to apply is subtracting 100 from its physics position Y and the very last thing to do is to just spawn more blocks as they move up so when we move them up we'll also spawn more so to do that we're going to actually take the code that we've got already and in between destroying the ball and moving the them all up we are also going to create a new one so we're going to use the choose function and action block here to choose from all of the different objects that we have available for the points objects we're going to save that in a variable called new points and then we will use that variable along with the variable we're going to create called point x to choose where along the X AIS to create this object and then we can call instance create with new points and point x so every time that this happens it will create a new object randomly chosen along a new Point randomly chosen so it feels Dynamic and random every single time we play and that's it let's go ahead and run our game and you'll see that we can shoot we can destroy objects when the ball goes down it makes a new one they all float up and we have a beautiful fun physics game so that's it I hope you enjoyed this project and be sure to check out ways to even extend this further and take your physics knowledge and Game maker to the next level leave a comment share what you enjoyed and what your going to do with this project to make it even better I'd love to hear about it but as I like to say keep making keep learning and I'll talk to you later
Info
Channel: GameMaker
Views: 9,467
Rating: undefined out of 5
Keywords: How to use physics in gamemaker, GameMakerEngine, GameMaker, Game Physics, gravity in GameMaker, GameMaker Beginner Tutorial, Bounce or Break part 1, GameMaker Studio 2, bounce or break, How games use gravity, GmaeMaker, Game Maker, Game Dev, Game Development, Video Games, Indie games, pixel art game engine, Learn to make games, Make a game from scratch, GML Visual, GML Code, Tutorial, Fast, Easy, Full game tutorial, GameMaker Tutorial, beginner full game tutorial
Id: iL8pgudKeeo
Channel Id: undefined
Length: 31min 40sec (1900 seconds)
Published: Fri Jan 12 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.