Making your FIRST GAME #2 - GameMaker Studio 2 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right first game tutorial number two let's do this okay so in the last tutorial we made this player object and we made him move around by pressing the wasd keys but the game is pretty boring right now and that's because there's nothing to beat up so let's make some enemies so i'm going to make a new object call it obj enemy i'm going to make another sprite for it call it spr enemy so now i'm going to take 10 hours to make the most epic enemy sprite of all time okay so i made the enemy sprite and i'm going to center it like we did the other one make sure your sbr player is also centered so that it rotates and moves about this place that's the origin so just set it to middle center trust me it'll work out great okay moving on let's program the enemy to go towards the player so actually i'm going to start with a create event and the create event is an event that only runs one time when the object is created okay and it's a great place to initialize some variables so i'm going to write hp equals 3 and speed equals 1 for example so i just made two variables and set hp equal to 3 and speed equal to 1. cool now you can change these modify these however you want now we're going to go into the step event and make code so that the enemy goes towards the player so in the step event write direction equals point direction x y o j player dot x and obj player dot y so this sets the object's instance variable direction to be the direction from the enemy to the player now if we set the speed which is another instance variable equal to our defined variable spd then that sets the speed of the object to go many pixels per frame going in this direction so that's all good let's just go into the room place the enemy in there run it wait a bit and see that the enemy now comes towards us now it's a little hard for the player to win here because the enemy just catches up okay so we need to give the guy a chance and we can do that by giving the player a weapon so to make a weapon let's just make another object call obj weapon make another sprite for it caught spr weapon and i'm gonna make a very simple sprite for the weapon all right perfect there's the weapon i'm going to set the center to around here okay now we have our obj weapon with spr weapon attached to it so let's go ahead and add some code so i'm going to add a step event to set the weapons position relative to the player's position so you go in the step event now let's set the x of the weapon equal to the players x plus the player sprite with divided by two so this will essentially put the weapon just to the right of the player every frame and we'll set the y equal to the player's y cool now let's just put this in the game run it and perfect we now have a weapon that the player is holding but the player can't really aim the weapon or shoot the weapon so let's do those two things right now so in our weapon object we're going to set the image angle equal to point direction x y mouse x mouse y so this will make the image angle point to the mouse so basically rotating the sprite to point towards the mouse cool so now we can aim the weapon now we want the weapon to fire when the mouse is pressed so if mouse check button pressed and be left so if we're pressing or if we just pressed the left mouse button then let's make a bullet so we don't really have a bullet object yet so let's just do that right now create another object call it obj bullet make another sprite for it i'm gonna make it about let's see i don't know 16 by 16. that's about good perfect now i'm going to center it so that the object is centered about this low center of the image close out of that and go back to our bullet cool so let's actually make the weapon spawn the bullet right now so let's make a new variable actually called new bullet equal to instance create depth x y depth plus one obj bullet cool so that might look confusing but bear with me for a little new bullet is the name of a new variable and it's equal to the new object that's created using this command so this command actually returns an object and we store that object id in the variable new bullet so we have a new bullet now let's make the bullet move in the right direction so let's say new bullet dot direction equals image angle and new bullet.speed equal to i don't know three so with this code you can see that the gun fires bullets cool now the bullets don't hurt the enemy so let's just do that right now so let's have a new event in the bullet object called collision with the enemy so this event happens when the bullet collides with the enemy so if we go in here now when the bullet collides with the enemy what do we want to happen we want the enemy to lose one hp and the bullet to be destroyed so we will destroy the instance and remove one hp from the enemy so to do this to get the idea of the specific enemy we just hit we say other so other dot hp minus equals one okay and you don't need semicolons i just do it sometimes for random reasons but whatever play it again and you'll notice that the bullets hit the enemy but the enemy doesn't die remember we gave that enemy three hp well it actually deletes some of that hp when the bullet collides with it but we never tell the enemy to die or get destroyed so we can just do this simply in a command in the step event so if hp is less than or equal to 0 then instance destroy that's pretty simple so if our hp runs out if it is less than or equal to zero if we take three hits or more then we destroy the instance cool now if we play it we can see that three hits destroys the enemy cool so one thing i notice also is if we if we turn the gun this way it doesn't look right it just kind of rotates around this axis and it's not really as if we're pointing your gun to the left it looks like we're pointing the gun upside down and to the left so let's fix that real quick it's actually really simple so in the step event i can just say if uh i don't know mouse x is less than x then image y scale equals negative one and we'll just set the default image y scale equal to one cool so now if the mouse x is less than the gun x then it's going to flip the gun upside down see cool cool now looks way better so the last thing we're going to do in this tutorial is actually make the bullet spawn at the tip so if you'll notice the bullet doesn't actually spawn at the tip of the weapon it just kind of spawns right here and then goes out from it and you might like that you might not but what i actually like to do is spawn the bullet at the tip of the weapon right here so this has to use trigonometry but i'm sure you can do it so go to the weapon and when we make that new instance we have to specify different x and y coordinates for where to spawn the bullet so we should measure what the distance is from the origin to the middle of the exit of the gun so this is about so this is at x 16 and this is at x 54 so 16 minus or 54 minus 16 is i don't know i'm trying to make a tutorial and i can't do math in my head uh what is it like 38 all right okay so bar shaft length equals 38. so when i say var here i'm actually making a local variable like local to this event only so this variable can only be accessed in this event after this is mentioned and it's kind of confusing i was confused at first because i'm used to putting this at the beginning of like every variable but this is just use var for local variables in a script it saves on a little bit of memory i'm going to make two other variables called var spawn x and var spawn y so let's set spawn x equal to x plus d cosine of image angle times shaft length and i'll set y equal to y plus d sine of image angle times shaft length okay so this is actually a mistake that's minus um this is really confusing i don't really feel like explaining it but whatever i will anyway here we go uh spawn x is equal to the origin of the weapon plus the cosine of the image angle in degrees that's what the d cosine is if i just did this then the input i would provide here would have to be in radians but image angle is in degrees so i just use d cosine times shaft length which will give us the x position of the tip of the weapon and the same thing for y and we use minus here because y is inverted remember that and the sine of image angle is just kind of the same explanation so these give us the x and y of the tip of the gun so we'll just replace these with spawn x and spawn y cool now let's just try it out and you'll notice that the bullets now spawn at the tip of the gun look at that that's incredible all right i think i'm going to end the tutorial right here made a lot of progress it actually isn't too hard to make a really simple game like this so i hope you enjoyed this tutorial and i'll see you in the next one
Info
Channel: DelugeDrop
Views: 188
Rating: 5 out of 5
Keywords: gamemaker, studio, gms2, gms, tutorial, first, game, deluge, drop, delugedrop
Id: 7nGp7GiPZPc
Channel Id: undefined
Length: 14min 21sec (861 seconds)
Published: Fri Nov 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.