Space Rocks! Godot Tutorial Part 11
Skills:
AI Pair Programming50%
Key Takeaways
Continues building a space-themed game using Godot Game Engine, exploring solutions and refactoring code
Full Transcript
welcome back this is part 11 of space rocks the Godot engine game tutorial where we're making an asteroid style game last time we started making an enemy spaceship and this time we're gonna add it to the main scene and we're gonna make it shoot at the player alright let's get started now that we have our enemies set up with their paths we're gonna make them spawn in the main scene so over here I've added an enemy timer to the main scene and in the code we are just loading the reference to that enemy timer so what we want to do is we want to do a couple of things we also need to read we also need a preload the actual enemies scene so that we have a way to spawn it or instance it so we'll grab an immediacy and path and paste that in there so what we're gonna do is every time this enemy timer ticks down we're going to instance one of these enemies but we're gonna want to be able to vary the spawn rate as the player gets to higher levels more enemies spawning that kind of thing so what I'm gonna do is when we start a new level okay we're going to take the enemy timer and we're gonna stop it so might've been running from the what the last level might have you know only had a couple seconds left I don't want the enemy to spawn based on the timing from the previous level so we're gonna stop it and then we're going to set the time that we want to wait so we do set wait time and this will set how long we want it to wait well I'm gonna do for right now I'm just going to do a Rand range between 20 and 40 seconds after we get this working we'll come back in here and we'll make these variables so that we can set them her level and then we started so that way we can make sure that we are working and then will respawn it again later or will change how it responds later so we're just going to take that enemy timer and we're going to connect the timeout signal and this is going to create one other instance one of these enemies so we instance it we're gonna add it as a child and we're going to set the wait time actually I'll just copy this from up here we're gonna take this wait time and we're just gonna restart it again so we're gonna reset this value again and we're gonna start it alright so if we hit play now we should see the enemy spawning after a certain amount of time right I gave it a little bit of time because we don't want it to be popping up right away nor do we want it to be very frequent all right these are gonna be things that come out every once in a while and hassle you and make it more difficult for you to stay alive so 30 seconds or so goes by here okay and there it is okay so just spawn it's following whatever path we told it to follow but we have a small problem and I noticed this while I was testing before I started making the video if we look at in the debugger you can look at the remote inspector wolf which will show you the scene tree as it exists right now here's the main scene there's the enemy why is the enemy still there it should have be spawned right there we just spawned another one so we're having a problem where we are not we're having a problem where we are not actually deleting the enemy when it goes off screen like it's supposed to so here's our enemy scene we had made this visibility notifier and just said that when it exits the screen it should queue free but that is not working even though when we reach the end of the offset here we are off the screen and so I played around with it some and I'm not quite sure yet I need to do some more testing but I suspect this may be a bug in the visibility notifier when it's a child of a of a path fellow 2d it's not detecting the transition to going off-screen but in the meantime I think the easiest way to solve this is to we're gonna get rid of the visibility notifier so I'm just gonna get rid of that function and I'm gonna delete the node okay and instead in the script here in the process when we're when we're moving we're just going to check that if the on the follow we're going to get the unit off step which just measures from the beginning of the path at zero to the end of the path at one and if our offset is greater than one then we know we have reached the end and we will do the Q free and that will solve our problem the enemy instances will go away and be deleted when they go off the screen so our next step is to get that enemy to shoot at the player and so I've made this enemy bullet seen with it's just an area 2d and a sprite and a collision just like we made our player bullet but I did also find in the as far as the art goes I found this this pretty cool texture that has all sorts of cool glowing looking laser beams I thought these looked really nice and glowy so I decided to use them instead of the ones that were in the Kenny art pack so this comes as a single texture with all of the individual beams in it so when you drop this on your bullet you don't want to use the whole image so you use region here you set region and then you can go in here and draw a box around which part of the texture you want to use for your sprite okay so there's the enemy bullet so now we need to just talk about how we're going to make it move we're going to obviously instance it at the location of the enemy and it's going to travel in the direction of the player and here's the script for that it's very simple it's pretty much based on how we did the player bullet we have the visibility notifier freeing it when it goes off the screen and other than that we're just moving at a steady speed in a straight line at whatever direction we said to start at so back to our enemy I've added a bullet container which is just a basic node that's going to hold all the bullets and the shoot timer which is going to control how quickly the enemy fires so if we go over to our script I've just added a couple of variables to refer to those and we're gonna use that to make things happen now for the shoot timer we're probably going to want to vary this the amount of time between well it's also based on level so I'm just gonna set the wait time to about say 1.5 seconds for now and this is going vary by level is we will change that in a little bit to when we start to configure how things vary by level in general so we'll just say start and we'll connect the timeout signal and this is going to shoot and actually I'm going to call this shoot one because I'm going to define a function that shoots one bullet you'll see why in a few minutes I have some other things I want to do too so we need to calculate what direction that bullet is going to fly in so we need to get the global position of our selves and we need to subtract the target well what is the target we haven't set that so when we so when we first start we don't have a target all right so we're gonna have a variable called target we're gonna need to set that when we instance the bullet or actually we need to set that when we instance the enemy we're gonna set that and then we're gonna usually set it to the player because that's what we want the enemies to shoot at but we'll get the global position of that then we instance a bullet add it as a child of the bullet container and then we use the start at the bullets start at method to set its angle to whatever the angle of that vector was towards the player and we started so location is going to be the global position of the enemy so how do we set the target well we need to set that when we instance the enemy over in the main so back over here and the main script when we added the enemy here we're just gonna say the enemy's target is the player and if we check that out and it skipped ahead a little bit just so that we can see the enemy when it comes on screen and you have to wait the whole time sorry for the testing on the videos I probably should have set the spawn time lower just so that we could see the enemy pop out quickly just to see it happening all right there it is okay there comes a bullet it comes a bullet they are flying towards where the player is but not very fast so they're pretty easy to dodge and that's okay we can obviously adjust that the enemy bullets have a speed here we can increase or decrease this right we can also the other thing I wanted to do was also have a function where we can where I want to try this out I should say I don't know for sure if this is what I want to do so I'm just going to try it this way and I will probably Clete will probably clean up this code later if it turns out to be something that we like and want to keep doing I just want to try it out a little bit at the moment so what I'm gonna do is make a different function where it shoots a spread of bullets so for this we're gonna do the same thing we're gonna find the direction that we needed to go in but then we're going to have a little loop here where we add to the angle by a certain amount so I'm gonna just add and subtract 0.1 radians from the from the direction and then we instance it like we did here but when we do start at we take the angle and we add we just add that a okay so let's just call that shoot three now when the timer runs out and in the interest of testing I'm going to go over here and change the spawn time to a low number so that we will see it come out pretty quick okay so here comes our like there we go so there you see the spread of bullets coming out that's not too bad obviously we get some sound in there that'll be better and of course we want them to do something when they run into the player of course okay so I went ahead and did that I just added a sound player and I'm playing the enemy Laser sound when the enemy shoots again I'm not going over the details of that because we've already done that before in other nodes but now we at least have a little bit of a sound so we're gonna leave that as it is now for the for this video and in the next one we will start to figure out how to make the enemy bullets damage the player the player bullets damage to the enemy the player might run into the enemy we got to deal with that and the other thing is also I also want I was thinking of another type of weapon that the enemy might have which would be a kind of homing missile slash torpedo kind of thing that will not quite move as fast but it will follow the player and create a big explosion so you want to get away from it before it gets near you alright thanks for watching and I'll see you next time you
Original Description
This is a complete end-to-end game development tutorial using the Godot Game Engine. We're going to be making a version of the classic game "Asteroids". We're making it as we go, so there will be bugs, mistakes, etc. - but this is how real development works, exploring possible solutions, refactoring (this means going back and changing code you already wrote). I hope you enjoy it and learn something.
Code for each part can be found here: https://github.com/kidscancode/godot_tutorials/tree/master/Space_Rocks_tutorial
Support me on Patreon: http://patreon.com/kidscancode
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from KidsCanCode · KidsCanCode · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Learning to Code with Python: Lesson 1.1 - What is Programming?
KidsCanCode
Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
KidsCanCode
Learning to Code with Python: Lesson 1.3 - Variables
KidsCanCode
Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
KidsCanCode
Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
KidsCanCode
Learning to Code with Python: Lesson 1.6 - Functions
KidsCanCode
Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
KidsCanCode
Learning to Code with Python: Lesson 1.8 - Number Guessing Game
KidsCanCode
KidsCanCode - Patreon Intro Video
KidsCanCode
Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
KidsCanCode
Learning to Code with Python: Lesson 1.10 - Secret Codes
KidsCanCode
Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
KidsCanCode
Learning to Code with Python: Lesson 2.2 Simple Animation
KidsCanCode
Learning to Code with Python: Lesson 2.3: Animating More Objects
KidsCanCode
Learning to Code with Python: Lesson 2.4: More Fun with Animation
KidsCanCode
Extra: Setting up the Atom Editor for Python
KidsCanCode
Game Development 1-1: Getting Started with Pygame
KidsCanCode
Game Development 1-2: Working with Sprites
KidsCanCode
Game Development 1-3: More About Sprites
KidsCanCode
Pygame Shmup Part 1: Player Sprite and Controls
KidsCanCode
Pygame Shmup Part 2: Enemy Sprites
KidsCanCode
Pygame Shmup Part 3: Collisions (and Bullets!)
KidsCanCode
Pygame Shmup Part 4: Adding Graphics
KidsCanCode
Pygame Shmup Part 5: Improved Collisions
KidsCanCode
Pygame Shmup Part 6: Sprite Animation
KidsCanCode
Pygame Shmup Part 7: Score (and Drawing Text)
KidsCanCode
Pygame Shmup Part 8: Sound and Music
KidsCanCode
Pygame Shmup Part 9: Shields
KidsCanCode
Pygame Shmup Part 10: Explosions
KidsCanCode
Pygame Shmup Part 11: Player Lives
KidsCanCode
Pygame Shmup Part 12: Powerups
KidsCanCode
Pygame Shmup Part 13: Powerups (part 2)
KidsCanCode
Pygame Shmup Part 14: Game Over Screen
KidsCanCode
Pygame Platformer Part 1: Setting Up
KidsCanCode
Pygame Platformer Part 2: Player Movement
KidsCanCode
Pygame Platformer Part 3: Gravity and Platforms
KidsCanCode
Pygame Platformer Part 4: Jumping
KidsCanCode
Pygame Platformer Part 5: Scrolling the Window
KidsCanCode
Pygame Platformer Part 6: Game Over
KidsCanCode
Pygame Platformer Part 7: Splash & End Screens
KidsCanCode
Pygame Platformer Part 8: Saving High Score
KidsCanCode
Pygame Platformer Part 9: Using Spritesheets
KidsCanCode
Pygame Platformer Part 10: Character Animation (part 1)
KidsCanCode
Pygame Platformer Part 11: Character Animation (part 2)
KidsCanCode
Pygame Platformer Part 12: Platform Graphics
KidsCanCode
Pygame Platformer Part 13: Improved Jumping
KidsCanCode
Pygame Platformer Part 14: Sound and Music
KidsCanCode
Pygame Platformer Part 15: Powerups
KidsCanCode
Pygame Platformer Part 16: Enemies
KidsCanCode
Pygame Platformer Part 17: Using Collision Masks
KidsCanCode
Pygame Platformer Part 18: Scrolling Background
KidsCanCode
Pygame Platformer Part 19: Wrapping Up
KidsCanCode
Gamedev In-depth Topics: 4-way vs. 8-way Movement
KidsCanCode
Gamedev In-depth Topics: Time-based vs. Frame-based Movement
KidsCanCode
Gamedev In-depth Topics: Non-integer Movement
KidsCanCode
Tile-based game Part 1: Setting up
KidsCanCode
Tile-based game Part 2: Collisions and Tilemap
KidsCanCode
Tile-based game Part 3: Smooth Movement
KidsCanCode
Tile-based game Part 4: Scrolling Map / Camera
KidsCanCode
Tile-based game Part 5: Player Graphics
KidsCanCode
More on: AI Pair Programming
View skill →Related Reads
🎓
Tutor Explanation
DeepCamp AI