How To Make a Space Shooter in 20 Minutes (Godot Engine)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to create a space shooter game just like this one in 20 minutes using the godot game engine so without further ado let's get into it i created an empty project and now i'm gonna start my timer to start this challenge okay so the first thing i'm gonna do is i'm gonna go into my project settings and set my window dimensions to 540 to 900 it's gonna be 16 by nine but reversed because this is going to be like a mobile game instead of a desktop game i'm gonna set my orientation to portrait and my stretch mode to 2d and my aspect to keep so that it looks the same in different sizes now let's create the main scene which is going to be called world this is going to be the scene that all the action takes place i'm going to set this as the main scene and as you can see my dimensions are just like a mobile game now let's import some assets i'm gonna create a new folder called assets and i'm gonna i'm gonna drag all of these assets maybe not this one and this one this one into my folder great they're all here let's create the background it's going to be a sprite called i'm gonna use this image but if i resize this it's scaling and i don't want that so i'm gonna go into the import settings and turn on repeat let's turn it on let's bring it in and let's turn off centered and let's turn on region so that i can specify a width and a height and it's it's tiling and not scaling that's what i want great now i'm gonna create the player character this is gonna be an area 2d it's called player let's actually turn this into its own scene create a folder called characters another one called player let's save it here and this is gonna have sprites and collision shape let's actually have a collision polygon instead of a shape i'm going to use this image right here but if you look at it inside of the world here it's a bit too large and that's why i'm going to scale the sprite down to about point 75 let's define collision shape i'm not going to try and be perfect i just want to be in the ballpark okay great now let's add a script and make this guy move going to define a speed variable this is going to be an integer let's say 300 and inside of the physics process i'm going to use the speed variable and i'm also going to define an input vector which i'm going to use to get define which direction my player is currently trying to move at let's create some input actions move right move left move up move down let's also define one for shooting these are going to be wasd d for right a for left w for up down for s shoot is going to be space okay now like i said i'm going to use this input vector in order to define which direction the player is moving at so let's say input vector dot x equals input dot get oops input dot get action strength move right let's subtract same thing but left this is going to be for horizontal movement let's do the same for vertical but change these parameters okay great now i have the direction and now i'm going to use it to increment my global position it's a global position plus equals input vector times speed times delta if i did everything right this should allow me to move my player yep it's working great now let's implement the simple enemy i'm only gonna have a single enemy in this game because i don't want to take too long creating different kinds of enemies this is also going to be an area 2d that's called enemy save it characters get a new folder called enemy and it's going to be much like the player it's going to have a sprite let's just use a regular old collision shape for this one we don't really care that much i'm gonna use this enemy black sprite and i'm also gonna scale this down to about 0.7 let's create a new collision shape let's make this guy circle it's not going to be perfect but it works let's add a script to this and the only thing we want this guy to do is move down so i'm going to define the speed let's actually export this let's say 150 and inside of the physics process i'm gonna say global position dot y plus equals speed times delta multiplied by delta so that stays the same on different devices on different frame rates and this should work right now as you can see it's moving down and now i have a player and i have an enemy that i can spawn that's gonna go down so now i need to spawn that enemy that's going to be the job of the enemy spawner so let's create a new scene this can be a 2d scene that's called enemy spawner let's save this inside of the enemies folder let's add a script to this this is gonna have five different positions that we're gonna use to spawn an enemy randomly so let's create another node 2d let's call this spawn positions at the end and let's add five position to these let's call this spawn position one two three four five the first one is going to be adds so our width is 540 we're gonna have five different ones and i'm gonna make these guys start ads i think 70 was the number i wanted so let's see first one 70 the next one 170 third one 200 fourth one 370 the last one is going to be 470. okay yeah that looks good that looks about right and finally i'm gonna add a timer to this class and call this spawn timer this is gonna define how far between enemies gets spawned it's gonna be a second by default let's say two seconds actually second and a half seems right i'm gonna make this auto start and we're gonna go back to our script let's define a variable called spam positions it's gonna be null by default and inside of the ready method we're gonna get the spawn positions from the spawn positions node using the get children method and now we need to make this class spawn a random enemy so let's actually get the enemy packed scene it's called enemy it's pre-loaded should be here okay and let now let's create a function called spawn enemy enemy equals enemy.instance let's add it as a child and we also need to set its position so that's why we created all these spawn positions we're gonna get a random spawn position and we're going to assign the enemy's position to that let's check our time time is left okay so at ready let's randomize so that we get a different one different random number each time and after before we create the enemy let's get a random index i'm gonna do that by using the random eye method modulo the size of the spawn positions array positions okay now after we add the enemy actually before let's set its global position to spawn positions index dot global position positions okay if i can stop making that mistake and now let's connect the timeout method of the spawn timer and each time the timer times out let's call spawn enemy and this should be good to go let's test it so each time the timer goes off an enemy is spawned at a random position let's see if it's random by testing it again looks random enough to me let's add this class to the world scene let's bring it back and now we should have almost playable game we have the player moving around we have the enemies spawning now let's implement the collisions between the player and the enemies and shooting lasers okay so the player let's quickly define some physics layers first one is going to be environment player and enemies let's also say player laser and enemy laser great now let's put the player on the player layer and it's gonna look for the environment the enemies and the enemy laser and the enemy should be on the enemies layer it's gonna look for environment player and player laser okay we added enemy laser but enemies aren't gonna have lasers so that was kind of like a habit shield thing i did but they're not gonna have lasers okay now let's inside of the player let's create a method called take damage and let's also define a variable for the hb set of three so the same for the enemy hp let's set one take damage damage let's say hp minus equals damage and if hp is less than or equal to zero let's free this enemy so it dies so the same for the player and now let's connect the area entered signal of the player and let's say if area is in group enemies let's take damage let's actually actually not take damage let's say area that take damage one so that when we collide with an enemy it dies and let's do the same for the enemy and the enemy collides with with let's see when the enemy collides with with an area and if that area is player let's say area that take damage only one okay let's add this class name keyword here so that our code inside of the enemy works let's also put the enemy inside of the enemies group so that the code inside the player works now these two should be able to collide and the enemy will die and touches the player and the player will also die when it touches three enemies okay now let's implement the shooting i'm going to create a new class this is going to be it's also going to be a an area 2d it's going to be called player laser let's save it inside of a new folder called projectiles this is gonna have a sprite and a collision shape 2d okay let's set its sprites to the player laser sprite that we have here and let's set its collision shape to a new capsule shape let's put this guy on the player laser layer and it's going to look for the environment and the enemies let's add a script and let's connect its area entered signal and this is going to check if area is in group enemies let's make the area take some damage we could actually check if like we could actually do some defensive programming and check if the area has a method called take damage but i'm not gonna do that because i'm running out of time let's make the area take a single damage okay now we need to make the player shoot this thing to for it to actually work let's create a position 2d that's called muzzle this is going to be the point the laser gets instantiated on let's bring it about here let's get it get a reference to it inside of our script let's create a new function call it shoot laser let's add some scripts signals i should say signal spawn laser application and when we try to shoot a laser we're gonna spawn we're gonna emit a signal called spawn laser and we're gonna pass the position of the muzzle and we're actually gonna spawn the laser inside of the world world script let's get the laser seam and let's actually connect the player's spawn laser method three minutes left to here and let's spawn a new laser whenever this signal is emitted let's add it as a child and let's set its position to location so now inside of the player script we can add a new input statement saying if input is action just pressed shoot we can say shoot laser let's try this oops what's going on we spawned another player for some reason okay let me check did i make a mistake yes i actually loaded the player scene instead of the player laser should be inside of the projectiles folder all right okay that was funny okay i'm still making a mistake let's delete this characters there okay we're trying to save okay now it's working but the laser isn't moving duh i should have should have seen that coming actually okay let's actually make the laser move now so you need to define speed let's say a thousand lasers should be fast and let's inside of the physics process let's say global position dot y equals negative speed plus equals negative speed times delta okay this should make it move is that okay and now we can actually kill enemies let's actually make the laser disappear when it enters an enemy and finally if i have if i can make it in time i want to add some score add a label say score okay i don't know if i'm going to have enough time let's say score equals 99 add a variable up here call it score zero by default and whenever an enemy dies let's make it emit a signal okay this is actually going to be tough because my enemies are being spawned inside the enemy's spawner if i wanted to add a score i would have to emit connect the enemies signal here oh boy i'm not going to make it in time but let's just do it anyway we're going to be a little bit above 20 minutes but i think it's fine let's add a line here saying enemy dot connect enemy died to self i can i actually do get free currency score okay let's see if this is going to work i'm trying to connect to a method inside of the world script let's create a method called score let's say score plus equals 10 and let's say score the text wait text should be text oh wait i need a typo there okay score.text equals oops the time's up but like i said we're gonna be over just a little bit let's say score plus string score okay let's see if this works okay at first it's going to be 999 but if i score great it doesn't change didn't work let's see why let's take a look at the enemy spawner okay so the new signal here called add score and this should work now and we need to inside of the world script we need to connect this signal it's connected to the score function that we created and now let's also set the score to zero start 999 now hopefully this is going to work still doesn't work it's making me angry let's see why okay oh man i'm so i'm so angry right now because i didn't emit the enemy died signal inside of here which is a dire mistake the first solution we had should have worked if i did this correctly let's see let's emit this and now this should work properly okay it's working we have a game that we can play it took us a little bit over 20 minutes but i think that's fine it's not the previous game but it works and this is going to be it for this video if you liked it make sure to leave a like subscribe and leave a comment if you want to give me some feedback if you want to support me you can buy my course on godot and also make sure to join my email list if you want to get notified of my future endeavors thank you for watching and i will see you in the next one [Music] you
Info
Channel: Kaan Alpar
Views: 6,567
Rating: undefined out of 5
Keywords: godot, godot tutorial, kaan alpar, learn godot, godot 2d, indie game, indie game development, game development, game dev, gamedev, game developer, learn game development, learn gamedev, godot game engine, godot engine, godot mobile game, godot 2d game, godot engine tutorial, space shooter, space shooter tutorial, godot space shooter, space shooter in godot, space shooter game
Id: qd0UTOQ_la8
Channel Id: undefined
Length: 23min 4sec (1384 seconds)
Published: Wed Dec 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.