Space Rocks! Godot Tutorial Part 11

KidsCanCode · Beginner ·🛠️ AI Tools & Apps ·9y ago

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 Learning to Code with Python: Lesson 1.1 - What is Programming?
Learning to Code with Python: Lesson 1.1 - What is Programming?
KidsCanCode
2 Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
KidsCanCode
3 Learning to Code with Python: Lesson 1.3 - Variables
Learning to Code with Python: Lesson 1.3 - Variables
KidsCanCode
4 Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
KidsCanCode
5 Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
KidsCanCode
6 Learning to Code with Python: Lesson 1.6 - Functions
Learning to Code with Python: Lesson 1.6 - Functions
KidsCanCode
7 Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
KidsCanCode
8 Learning to Code with Python: Lesson 1.8 - Number Guessing Game
Learning to Code with Python: Lesson 1.8 - Number Guessing Game
KidsCanCode
9 KidsCanCode - Patreon Intro Video
KidsCanCode - Patreon Intro Video
KidsCanCode
10 Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
KidsCanCode
11 Learning to Code with Python: Lesson 1.10 - Secret Codes
Learning to Code with Python: Lesson 1.10 - Secret Codes
KidsCanCode
12 Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
KidsCanCode
13 Learning to Code with Python: Lesson 2.2 Simple Animation
Learning to Code with Python: Lesson 2.2 Simple Animation
KidsCanCode
14 Learning to Code with Python: Lesson 2.3: Animating More Objects
Learning to Code with Python: Lesson 2.3: Animating More Objects
KidsCanCode
15 Learning to Code with Python: Lesson 2.4: More Fun with Animation
Learning to Code with Python: Lesson 2.4: More Fun with Animation
KidsCanCode
16 Extra: Setting up the Atom Editor for Python
Extra: Setting up the Atom Editor for Python
KidsCanCode
17 Game Development 1-1: Getting Started with Pygame
Game Development 1-1: Getting Started with Pygame
KidsCanCode
18 Game Development 1-2: Working with Sprites
Game Development 1-2: Working with Sprites
KidsCanCode
19 Game Development 1-3: More About Sprites
Game Development 1-3: More About Sprites
KidsCanCode
20 Pygame Shmup Part 1: Player Sprite and Controls
Pygame Shmup Part 1: Player Sprite and Controls
KidsCanCode
21 Pygame Shmup Part 2: Enemy Sprites
Pygame Shmup Part 2: Enemy Sprites
KidsCanCode
22 Pygame Shmup Part 3: Collisions (and Bullets!)
Pygame Shmup Part 3: Collisions (and Bullets!)
KidsCanCode
23 Pygame Shmup Part 4: Adding Graphics
Pygame Shmup Part 4: Adding Graphics
KidsCanCode
24 Pygame Shmup Part 5: Improved Collisions
Pygame Shmup Part 5: Improved Collisions
KidsCanCode
25 Pygame Shmup Part 6: Sprite Animation
Pygame Shmup Part 6: Sprite Animation
KidsCanCode
26 Pygame Shmup Part 7: Score (and Drawing Text)
Pygame Shmup Part 7: Score (and Drawing Text)
KidsCanCode
27 Pygame Shmup Part 8: Sound and Music
Pygame Shmup Part 8: Sound and Music
KidsCanCode
28 Pygame Shmup Part 9: Shields
Pygame Shmup Part 9: Shields
KidsCanCode
29 Pygame Shmup Part 10: Explosions
Pygame Shmup Part 10: Explosions
KidsCanCode
30 Pygame Shmup Part 11: Player Lives
Pygame Shmup Part 11: Player Lives
KidsCanCode
31 Pygame Shmup Part 12: Powerups
Pygame Shmup Part 12: Powerups
KidsCanCode
32 Pygame Shmup Part 13: Powerups (part 2)
Pygame Shmup Part 13: Powerups (part 2)
KidsCanCode
33 Pygame Shmup Part 14: Game Over Screen
Pygame Shmup Part 14: Game Over Screen
KidsCanCode
34 Pygame Platformer Part 1: Setting Up
Pygame Platformer Part 1: Setting Up
KidsCanCode
35 Pygame Platformer Part 2: Player Movement
Pygame Platformer Part 2: Player Movement
KidsCanCode
36 Pygame Platformer Part 3: Gravity and Platforms
Pygame Platformer Part 3: Gravity and Platforms
KidsCanCode
37 Pygame Platformer Part 4: Jumping
Pygame Platformer Part 4: Jumping
KidsCanCode
38 Pygame Platformer Part 5: Scrolling the Window
Pygame Platformer Part 5: Scrolling the Window
KidsCanCode
39 Pygame Platformer Part 6: Game Over
Pygame Platformer Part 6: Game Over
KidsCanCode
40 Pygame Platformer Part 7: Splash & End Screens
Pygame Platformer Part 7: Splash & End Screens
KidsCanCode
41 Pygame Platformer Part 8: Saving High Score
Pygame Platformer Part 8: Saving High Score
KidsCanCode
42 Pygame Platformer Part 9: Using Spritesheets
Pygame Platformer Part 9: Using Spritesheets
KidsCanCode
43 Pygame Platformer Part 10: Character Animation (part 1)
Pygame Platformer Part 10: Character Animation (part 1)
KidsCanCode
44 Pygame Platformer Part 11: Character Animation (part 2)
Pygame Platformer Part 11: Character Animation (part 2)
KidsCanCode
45 Pygame Platformer Part 12: Platform Graphics
Pygame Platformer Part 12: Platform Graphics
KidsCanCode
46 Pygame Platformer Part 13: Improved Jumping
Pygame Platformer Part 13: Improved Jumping
KidsCanCode
47 Pygame Platformer Part 14: Sound and Music
Pygame Platformer Part 14: Sound and Music
KidsCanCode
48 Pygame Platformer Part 15: Powerups
Pygame Platformer Part 15: Powerups
KidsCanCode
49 Pygame Platformer Part 16: Enemies
Pygame Platformer Part 16: Enemies
KidsCanCode
50 Pygame Platformer Part 17: Using Collision Masks
Pygame Platformer Part 17: Using Collision Masks
KidsCanCode
51 Pygame Platformer Part 18: Scrolling Background
Pygame Platformer Part 18: Scrolling Background
KidsCanCode
52 Pygame Platformer Part 19: Wrapping Up
Pygame Platformer Part 19: Wrapping Up
KidsCanCode
53 Gamedev In-depth Topics: 4-way vs. 8-way Movement
Gamedev In-depth Topics: 4-way vs. 8-way Movement
KidsCanCode
54 Gamedev In-depth Topics: Time-based vs. Frame-based Movement
Gamedev In-depth Topics: Time-based vs. Frame-based Movement
KidsCanCode
55 Gamedev In-depth Topics: Non-integer Movement
Gamedev In-depth Topics: Non-integer Movement
KidsCanCode
56 Tile-based game Part 1: Setting up
Tile-based game Part 1: Setting up
KidsCanCode
57 Tile-based game Part 2: Collisions and Tilemap
Tile-based game Part 2: Collisions and Tilemap
KidsCanCode
58 Tile-based game Part 3: Smooth Movement
Tile-based game Part 3: Smooth Movement
KidsCanCode
59 Tile-based game Part 4: Scrolling Map / Camera
Tile-based game Part 4: Scrolling Map / Camera
KidsCanCode
60 Tile-based game Part 5: Player Graphics
Tile-based game Part 5: Player Graphics
KidsCanCode

Related Reads

📰
Beyond 11,000: Navigating the Unified MCP Server Catalog That's Reshaping AI Tooling
Learn to navigate the Unified MCP Server Catalog with over 11,000 tools, reshaping AI tooling and streamlining development workflows
Dev.to · Robert Pelloni
📰
Could AI Save the English Major?
Explore how AI can enhance the English major curriculum and improve student learning outcomes
Medium · AI
📰
Six AI Tools Advancing Mental Health Care Access
Discover six AI tools revolutionizing mental health care access, enabling remote screenings and personalized support
Dev.to AI
📰
Runway Can’t Win on ‘Best Model’ Anymore. So It Stopped Trying To.
Runway launches Media Router, a tool that automatically selects the best image, shifting focus from 'best model' to practical solutions
Medium · AI
Up next
Clicky AI Telugu 🔥 Control Your Computer with Voice | Best Free AI Tool 2026
Withmesravani_
Watch →