How to Make an Asteroids Game (Godot Engine)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're gonna start building a 2d asteroid style game at the end we're going to have a game that looks like this we have a top-down player character that can shoot asteroids and those asteroids will spawn smaller asteroids let's get started let's start building this game i have an empty project with some assets imported in let's start by creating the main scene i will call this scene simply world and this will be the scene that all the action takes place let's save this inside the rest folder and i'm going to go inside my project settings and do some changes we want to set the window size to 1280 by 720 and let's turn our stretch mode to 2d and our aspect ratio to keep the next thing i want to change is the default clear color which is under rendering environment i want to set this to be black because our game will be set in space and i don't want to create a color right or sprite node to create a background image i'm just going to use this default clear color as my background great let's save this and let's set this as the main scene and now we can start to create the different parts of our game the first thing that comes into mind is the player so let's start by creating the player character i'm going to create a couple of new folders characters and player so let's create a new scene this is going to be a kinematic body let's call this the player let's save it inside that folder that we just created this is going to have a sprite collision polygon 2d for now and we'll think about the other things later on when we build some of the functionality of the player as for the sprites let's choose this player ship texture here from our assets and we're actually going to scale this down to be 0.5 by 0.5 let's create a collision shape for this we're not trying to be perfect we just want to get the gist of it so it doesn't have to be symmetrical or anything because it's not going to be a the precision isn't going to be super super important this will do just fine okay great actually what i wanted to do was i want to before i create this collision polygon i want to rotate this 90 degrees so let's rotate the sprite by 90 degrees because this is going to make it easier to move the character later on in my script so now what we want to do is we can either rotate this collision shape as well which should be fine i'm not sure if this will be problematic later but we can also delete all of these this and create it again but i'm just going to leave it as it is now let's instance the player inside of the world scene let's place him in the middle let's run the game looks good and now we need to make this player character move to do that let's create a script on the player so what type of movement do we want to have for the player so the player is a spaceship right and we want the spaceship to accelerate in a certain direction and then we don't want it to instantly stop we want it to slowly lose its speed and we want the player to use the left and the right arrows to rotate the ship and when we press the up key or the back key we can move in the direction that the player chooses to move move in using the arrow keys left and right okay before we get into scripting let's create some input actions move forward move back strafe right and strafe left let's also create one for shooting as well i'm going to use the wsd keys you can also use the arrow keys but i just find it more convenient to use wsd ws and strafe right is going to be d strafe left is going to be a shooting i will use the space bar great let's go back inside the script and let's define the physics process function here i'm going to create some variables that i'm going to use to make the player move input vector this is going to be a vector 2. we need a velocity which is also going to be a vector 2. we need a variable for acceleration this will be an integer and let's set this to be let's actually turn this into an export variable let's set this to something like 300 and let's see let's start writing some code at the beginning of my function i want to get some input to my input vector i'm going to use the x property of it i'm going to get input get action strength move forward minus inputs get action strength move back let's set the velocity using this vector velocity.x equals let's actually set the whole vector velocity equals vector 2 input vector x times sets to be plus equals because we're going to accelerate instead of setting a fixed speed acceleration and for for the y let's give it a zero and finally let's call move and slide which is a built-in function for kinematic body and we need to give this a vector to and let's see if this works so if i press w my character just goes blazingly fast and if i press back my player goes back the problem here is we're not multiplying this with delta and this will make sure that it doesn't go crazy fast and looks like it's working let's clamp this value so so that we don't accelerate to a very very high speed so let's say velocity delta x equals clamp velocity x let's define a max speed [Applause] this can be like 500 so we want to clamp the velocity in between negative max speed and positive max speed because we can also go in the opposite direction okay we should do this for the y as well so let's clamp velocity.y in between the max speed and the negative max speed as well and now if we print the velocity i'm also going to set the velocity back to the return value of move and slide if we printed we should max out at 500 as you can see from the print statements grades we might wanna increase this acceleration because it seems to be a little too slow right now and this is more like it great okay now we want to make the player rotate using the a and the d keys in order to make the player rotate we can just use rotation property of the player and if i actually increment this rotation let's say by one every frame let's multiply that by delta you'll actually see that the player is rotating clockwise let's increment this by negative one and if so you will see that it's rotating counterclockwise we can use this fact with our input actions in order to give the player the ability to rotate so after we get input for move forward and move back let's also get some input for rotating i'm going to find some variables for this the first one is going to be rotation direction and this is going to be an integer and the next one is going to be rotation speed which i'm going to turn into an export variable this is actually going to be a float instead of an integer now that i remember let's set this to be 3.5 so the rotation direction variable is going to determine whether we're rotating towards the left or the right and the speed will determine the speed at which we rotate so at the start of every frame we're going to set the rotation direction to be zero after that we're gonna use the strafe right and strafe left input actions to set it to either positive one or negative one let's increment it instead of assigning so that if we press them both at the same time they end up becoming zero whether if we assign it if we press some both at the same time we're gonna get the last one okay now at the start of every frame we're setting the rotation direction to be zero and after that we're setting it to either one if we're pressing down straight right or negative one if we're pressing down strafe left so this means rotation direction can only be one negative one or zero at any given time so let's use this variable instead of this fixed number here to set the direction of the rotation let's also use that rotation speed variable here to change the speed of the rotation let's try this and as you can see i can use a and d in order to set the rotation of the player now we can rotate the player's sprite or the player object right but we're still moving horizontally so let's fix that what determines the way that we move it's the velocity vector right so we we need to change the velocity vector in order to go in a different direction let's go to line 19 and let's actually rotate this velocity vector by the player's rotation let's see what this looks like so i can rotate and if i press forward i can go in my desired direction we are a bit too fast actually so let's turn down the max speed to be like 300. now i can freely move which is exactly what we want yeah the acceleration should also come down to like 500 okay this is more like it there's still one more problem if i press forward and i take my finger off the key i'm still accelerating in the same amount we want to create a friction variable that we're going to use to interpolate the player speed to zero once the player stops giving the move forward input so let's create a new variable this is going to be a float we're going to call this friction weight and let's set this to be 0.1 for now so after setting the velocity let's check if the input vector dot x is zero if it is that means we're not pressing move forward or move back so we can start applying friction to our velocity this will be very simple we just need to set the velocity to it's linearly interpolated version we want to interpolate velocity down to velocity vector to zero and we want to use that friction weight variable to determine how fast we're going to interpolate down to vector to zero and this should work just fine let's print and say applying friction let's print out the velocity as well so as you can see if i move and stop moving i can see that i'm interpolating down to zero let's also do one more check here let's say and velocity isn't equal to vector 2 0 so that when we start the game we don't try to apply friction because we don't have any velocity there's one more optimization we want to do here we don't want to keep interpolating down when we reach a very very small number so let's check if for example velocity.x is less than or equal to 0.1 if this is the case let's just set the velocity.x to zero and we're going to do the same for y if velocity that y is less than or equal to 0.1 let's just set it to zero so that we don't bother interpolating to a very very very small number now this should work perfectly there's a slight problem we're getting some weird behavior and the problem is we're checking if velocity.x is less than or equal to 0.1 but remember this velocity.x can be negative as well so what we need to do is we need to get the absolute value of this and check for that instead of just checking for velocity at x or velocity.y and now this should work as expected and if you want the player to slide more you can play with this friction weight variable so if i set this to be like 0.01 which is a very small number the player will still stop but it's gonna take it more time so as you can see it's taking a while for the player to stop great let's get rid of this print statement here and we're basically done with our player character here we still need to implement shooting and other stuff but that's going to be a tutorial for another time thank you for watching make sure to subscribe leave a like and comment i will see you in the next one [Music] you
Info
Channel: Kaan Alpar
Views: 2,187
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, asteroids, asteroids game, godot asteroids tutorial, asteroids tutorial, how to make an asteroids game, how to make asteroids, asteroids game tutorial, game engine
Id: crd5SrzUsZ0
Channel Id: undefined
Length: 18min 47sec (1127 seconds)
Published: Sat Jan 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.