Full transcript of “App Lab (Code.org) 🚀 Space Shooter Game 🚀 Tutorial”. Click a timestamp to open the video at that moment. Need another video? Use the YouTube transcript generator.
0:00 hello in this tutorial we're going to learn how to use code orgs Apple AB to create a space shooter style game I'm going to demonstrate how this works we're gonna hit start our timer will start counting up we hit enter to fire a shot the enemies will speed up and then they will move back and forth across the screen to make them harder to hit in this case we've set it up in such a way that their speed will change as they move left and right across the screen once we destroy all the enemies the timer will stop our goal is to destroy all enemies in the shortest amount of time so I'm on my code org projects page
0:51 I'm going to create a new project an app lab project now I'm going to rename it so it's easy to find I mean hit rename I'm gonna call it space shooter and then I'm gonna hit save so it saves the name the graphics I'm gonna use are from an artist called Morgan 3d I downloaded them from open game art org if you'd like more information on how to find good game art on the web click on the video link in the upper right hand corner of this screen and go back to my space shooter game now I'm going to drag an image here this is going to be my ship I'm gonna change the ID to image underscore shift this is a good naming convention for app lab where I have the object type and then
1:37 underscore then the name of the object now I'm gonna go to image choose I'm gonna upload a file now I've already cut out the ship's I want to use this was the original file I downloaded it has multiple kinds of ships on it it's important when you cut them out or crop them that you have as little white border as possible because even though you won't be able to see that white border on a background that will be involved in collision checking so I'm going to select hero chip I'm gonna select choose now you see this has a white border when I imported it no it didn't from the original image and that's because we're trying to force a rectangular image into a square size so I'm gonna have to resize it and make sure I get rid of the right white border
2:24 so I'm going to click and drag make it a little smaller and you see now the white borders gone because I've got it in its original rectangular shape next I'm going to create an enemy so I'm going to drag image I'm going to call this one image underscore enemy underscore one I'm going to choose the image I'm going to upload another file I'm going to choose one of these ships I'm going to select choose now this is way too big for what I want the smaller we make these ships the harder they'll be to hit so we're going to resize it both so it's smaller and there's no white border on the top and the bottom and there we go that looks like a good size now I'm
3:10 going to duplicate it so I select it again I duplicate it and I'm going to change this name to image underscore enemy underscore to duplicated again image underscore enemy underscore three duplicated a final time image underscore enemy underscore four and you can make as many or as few of these as you want now I'm going to move them around I'm gonna click you see a little hand icon I want to make sure these are all lined up on the x-axis because I'm going to have them be moving in sync so we see let's see this one is at an x value of 210 this one will move it to an x value to 10 move it down a little bit this one
3:58 will move it about here and get the x value to 210 and finally this one will put right about there and get the x value to 210 so they're all lined up on the x axis next I want to create something for my ammo we want to make the object we're gonna be shooting out small again to make it challenging to hit the things it was too big it'll hit them even accidentally so I'm gonna make another image we'll put it right in front of the ship in this case I just created a 10 by ten pixel image in paint it's something you can do very simply so I'm gonna call this image underscore energy this is gonna be an energy bolt I'm gonna select shoes I'm gonna upload a file this is my
4:44 fireball open it choose so there's way too big in it by default forces it into 100 by 100 so I'm just gonna change this one manually to a 10 by 10 sometimes if you try to make it too small you can't really do it by clicking and dragging the size so you have to manually select it and then go in and change the width in the height so if we want to make these ships really small you know I could only make it so small by clicking and dragging but then if I want to make it even smaller than that I could make it like 10 by 10 but I'm gonna put this back to a 30 by 40 and there it's back to the original size so I've got my ammo in this case it's a little hard to click
5:32 and drag it because it's so small to another location so I'm just gonna click on it to select it and then I can change the X and y position manually so let's make it a little further over and a little further down doesn't really matter where this starts out at there we go the final thing we need to do is create a button that's going to start the program so we're gonna drag the button there we're gonna call it button underscore start and the text is going to be under text start now I'm going to change the color I'm gonna give it a blue background and there we go and we
6:18 can move this around click and drag it wherever we want so now we've set up all the objects in the interface we need and now we're gonna go start looking at the code our next step is we're going to create controls to move the player ship up and down so we're gonna start by changing the name of the screen so we clicked on the screen right now it's screen 1 I'm going to - screen underscore game now we have to write some code so we're gonna go to code and then we're going to go to UI controls and we're gonna grab an on event block now you can see it's drag-and-drop but we are working in the text mode we can always switch back and forth even when we're working in text mode sometimes it's easier to drag things in and let it convert it to text
7:04 okay so the on event we want is we want when screen game so this means it's gonna have to be in screen game the event we're looking for is key down so that means any key is pressed and then we have this anonymous function here anonymous function is a function without any name and this anonymous function runs every time there is a key down when we're in the screen game so next what we have to do is we have to get the current location of the ship on the x and y axis now the location it takes is the upper left-hand corner of the image so right about there that's the location it's going to be dealing with we're going to declare a local variable so we're gonna say ver this is a local variable since
7:52 it's declared inside a function it'll only exist inside each run of this function we're going to call it ship X and we're going to set it equal to get X position which is a function that's built into a blab and we want to the x position we want to get it from the ID of this ship the ID of the ship here is image underscore ship it's important to put it in quotation marks since this is a literal strength so we're gonna have a semicolon there let me save ver ship why let's say equals get Y position and same thing in quotation marks image underscore ship so now we've got the current X&Y location
8:37 of the ship where it sits right now so now what we have to do is we have to decide how far we're going to move it so I'm going to create another local variable on there called ver distance and I'm going to set it equal to 12 now we can change this if we want for now we're gonna go with 12 if you want it to be more or less that's fine now we have to check which button is being pressed because this anonymous function will be activated any time any key is prepped so we'll start by tracking if the up arrow key is pressed and we do that by creating an if-then parenthesis we say event dot key double equals then a string literal up with a capital u now
9:23 if this boolean expression evaluates to true we're gonna do whatever is inside these curly brackets here and all we want to do is we want to change the Y value we want to decrease it by the amount of distance so we're going to say ship y equals ship Y minus distance now the reason we're subtracting is on a computer screen the upper left-hand corner is 0 0 and the y increases as we go down and the X increases as we go to the right so as if it moves up its Y value is actually decreasing we're gonna do something similar with the down we're gonna say give event dot key equals down
10:15 we're gonna say ship y equals ship y plus distance what this does is it takes the old value in the ship Y variable adds whatever is in the distance variable in this case 12 and that becomes the new value inside the ship Y variable sometimes it's easier to think of a single equal sign as like a left pointing arrow so this here on the right hand side is calculated and that data goes into the variable on the left hand side now we've got the new Y value calculated so we actually have to set the image at this new location the X is going to be the same we just had to take it so we can send it back we're going to use a function called set position this
11:02 is another function that's built into a plan now you see a blabs helping us out it's telling us what argument Sydney so it's saying it's the ID the new x value the new Y value the width and height we can actually use this just by sending the idea in the X and the y and we're gonna do that because we don't want to change the width to the height so the ID is the string literal image underscore ship and since the string literal we have it in quotation mark so we'll say image underscore ship then we have a comma and it tells us it's waiting for our argument for the X so we're gonna say ship X which isn't going to be a change because the ship never moves on the x-axis and then we're going to say the final one which is ship Y so we'll send the updated y value to set
11:48 position so it'll move the ship will end this line with a semicolon and let's try running it and see if it works so we're gonna hit run we're gonna move it up we're gonna move it down so our ship moves next what we're going to do is we're going to create a function that can wrap an object around to the other side of the screen when it's halfway off the bottom or halfway off the top we're going to make a function that we can use either on our ship or on any of the enemies we're gonna hit f11 to make this full screen to get a little more work space here so now I'm gonna go I'm gonna make a function I'm gonna call it vertical wrap around and then I'm going to put a parameter called object and
12:34 this parameter is going to take in a value that's going to be the ID for whatever object we want to wrap around by the way if you want a wraparound function that will work either vertically or horizontally check the link in the upper light right-hand corner of the screen and it'll explain how to do that so we are going to create some curly brackets and we're going to declare a local variable we'll call it object X because it'll be the x value of whatever object ID is in that object variable we'll say equals get X position and then we'll put the argument object
13:21 now the reason we aren't putting this in quotation marks is we don't want literally the word object we want whatever string is inside object so if we're dealing with a string literal we put quotation marks around it if we were dealing with a variable that holds a string literal or another type of data then we don't put quotation marks around it next we're going to create a local variable object Y and we'll set equal to get Y position and I'll get the Y position of whatever object ID is in the variable object the next thing we need to do is we need to get the height of our ship and the reason for that is is we want to make sure it's halfway off the screen either on the top of the
14:07 bottom before we wrap it up and we need to know the height of the object to calculate whether it's halfway off the screen so we're gonna declare a local variable called object height and we're gonna get the height by using the get property function now we can get all sorts of properties with the get property function if you want to see some examples just click on see examples there the ID of the object we're dealing with it'll be in the variable object and then the property we want is height and we will put this in quotation marks we'll hit a semicolon to end that now what we're going to do is we're going to check it to see of the ship or any other object is half way off the screen on the top edge so we'll say if object Y which
14:54 is the Y value of whatever objects were dealing with and we'll say less than zero - object height divided by two so if it's halfway off the screen its Y value is actually going to be less than zero because its origin is gonna be right about there if that's the case we are going to set object y equal to 450 - object height divided by two and what that does is that'll move it back around the other side this is 450 on the y-axis there and we only want it
15:40 halfway off so we want the origin to be half of the height minus 450 so it'll actually put it right about there if we're dealing with this ship now we're gonna do an else--if what an else--if does is it only checks the boolean expression if the if previous to it or the else if previous to it is false so if this is true we don't even have to check the else if because it can't be on the top edge and the bottom edge at the same time we're going to say else if object Y is greater than 450 - object height divided by 2 so there's going to
16:27 check to see if it's going off the bottom edge and if it is if it is we're going to set the variable object y equal to 0 - object height divided by 2 so we've calculated what the new object Y should be however we actually have to write the code to send whatever object it is to that new Y value so we're going to use the command set position we're gonna pass the ID of whatever is in the object parameter we're gonna send object X which will be unchanged from this function and we're gonna send object Y which will potentially be changed from this function if either this or this
17:14 we're true now this function is not going to get called automatically we want this function to be called whenever the key down is pressed so we're gonna go to the end of this anonymous function and we're gonna call vertical wrap around and in this case we want to send the ID for the ship so it's gonna say image underscore ship we'll have a semicolon to end that line let's run it and see if it works so we'll say run we can move it up and it moves to the bar part of the screen and we'll move down and it moves back up to the top part of the screen and we're going to use the same function later to have all our enemies wrap around when they hit the top or the bottom - next we're going to
18:01 create some global variables global variables are different than local variables in several ways first global variables are declared outside of any function this gives them a the advantage of lasting the whole lifetime of the program and they are accessible from every function in the program I'm in my first one I'm gonna create it's called gun loaded and we'll start off by setting this true we can only fire one bullet at a time and we can only fire if the value of gun loaded is true next we're going to create a variable enemies moving left all our enemies will be moving in the same direction we'll start off with a false value when they are moving left this will be true next we're
18:48 gonna create a variable game started and we'll set that equal to false they'll become true once the game is started next we're gonna create a variable called enemies remaining this is going to have a number value that's 4 if you have more or less enemies you'll start it out as whatever number of enemies you have on your screen next we're going to create a variable called start time this is going to allow us to keep track of the start time so we can calculate the current time we're not going to initialize this and finally we're going to create a variable called elapsed time and this is going to keep track of how much time has passed overall again we are not going to initialize this value we'll initialize it later as a rule we
19:36 declare global variables at the top of the program to make it easier for other programmers to find them and understand your code next we're going to create a function that allows us to fire our energy bolt from our spaceship so let's go down to the bottom of our code we're going to create a new function and we're going to call it function fire first thing we have to check is if the gun is loaded you see if it's not loaded we're not going to allow it to fire so we'll say give gun loaded this is equivalent to writing gun loaded double equals true however with boolean values it's usually easier just to write it like this we'll create a set of curly
20:25 brackets and the code inside the curly bracket we'll execute it if the gun is loaded first thing we do is set gun loaded equal to false because if we'd aspired it it's no longer loaded we have to find the FIPS current location so we can move the energy bolt there so we'll save their ship x equals get X position and then we want the x position of the ship I'm going to create another local variable ship Y we'll set that equal to get Y position again the ID of the ship which is image underscore ship now we have to figure out the ship's height and
21:11 width because we want to make sure it's in front of the ship so the x and y position are only going to give us the values of this corner here so we got to move it over here and we got to know the width for that and we need to move it halfway down so we got to know the height so it looks like it comes right out of the nose of the fit so we'll save their ship height equals get property we want to get the property of the ship and the property we want to get is the height next we want to get the width so we'll say their ship width equals get property again from the ship so we'll
21:56 put the ship's ID in this case we want the width property now we have to figure out what the size of the energy bolt is so that we can make sure we calculate it so it comes right out of the center so we're gonna say their energy bolt height equals get property image underscore energy and we want the height now we've got to
22:42 calculate where we want the energy bolt to go so we're going to save their energy bolt X and that'll be ship X plus ship width which is going to calculate the ship X there and then the width is going to add on there so we'll actually have the x value of right about there which is where we want it now we'll have to calculate the Y value we want to put it at so Y energy bolt y equals ship y plus ship height divided by 2 and then minus energy bolt height divided by 2
23:33 and this is going to get it just right in front of the nose finally we've got these values calculated so we got to move the energy bolt to the calculated position so we'll say set position image energy and we want to move it to the calculated X position so that's energy bolt X and then move it also to the calculated Y position which is energy bolt Y so now what we have to do is we have to create a command to call fire every time we press Enter we're going to go back up to our button down event listener and right here we're gonna say
24:20 if event dot P equals enter create a curly bracket and we are going to call the function fire let's run this and see if it works it's not going to be very impressive yet because it's just going to move the energy bolt right in front of the ship will reset it and run it again let's move the ship we hit enter and then it goes right to the nose of the ship later on we'll learn how to actually make it move so that we can get it out of the screen and then reload it our next step is to create an event listener for the start button and what that'll do is it'll put a few things in their proper places then it'll start a
25:05 timed loop that will eventually be used to move things across the screen and check for collisions so the easiest way to create an event listener for the button is to go to design click on start go to events and click on under click insert and show code so down here at the bottom we have our event listener for when the button is clicked we're gonna get rid of this line that was automatically generated it just outputs button start click to the console now we're going to create an if statement that checks if the game has already started which we don't want to start to get more than one so we say if I'm going to say not and we're gonna check the variable game started so if the game has not started we're gonna do everything that's in these two curly brackets the
25:53 first thing we want to do is we want to set game started to true which the game has started now next thing we want to do is we want to move the start button off the screen so it's not in the way so we're gonna say set position then the ID of the button is button underscore start will set it to an exposition of 100 and then to a Y position of 500 so it's going to be off the bottom of the screen where we can't see it next we're going to initialize the global variable start time so we're gonna say start time equals get time now get time Returns what's called unix time which is a number of milliseconds since january
26:40 first 1970 and that number is always increasing as we move on into the future now we're going to create a time loop that's going to run over and over again so we're going to go to controls we're gonna scroll down until we see time gloop we're gonna drag it over to the bottom of this if statement now right now this time the loop is defaulted to run once every thousand milliseconds or once every second we want to have it run more frequently than that so it's not choppy so we're gonna have it run once every 50 milliseconds or 20 times per second we've got an anonymous function here so everything in the anonymous function will run 20 times per second we
27:26 aren't going to do anything with this now but we're going to come back to this a little later once we have some other functions to call now we're going to work on creating a timer for this program so we have to have a label up here that's going to display the time so we're gonna go to design we're going to drag a label up into the upper left hand corner want to make sure it's wide enough so we can display the time we're gonna click and drag this out and we're gonna click on it again we're gonna go to properties we're gonna call it label underscore time the text we're just gonna set at zero point zero to start out with now we're going to go write the
28:12 code to control the label so we're going to go back to code we're gonna go to the bottom of our code here and we're gonna create a function called update time so the first thing we need to do is we need to create a variable that's going to store the current new next time so we're gonna say their current time and we're gonna set it equal to get time which is gonna give us the number of milliseconds since January 1st 1970 right at this moment so now we got to calculate the elapsed time elapsed time is a global variable so we're just going to set it elapsed time equals current time minus now we need to convert it to seconds
28:59 because it's in milliseconds right now so we're gonna do that by saying elapsed time equals elapsed time divided by a thousand because there's a thousand milliseconds in a second as it is we're going to have three digits past the decimal point for this game we only want it to one digit past the decimal point so we'll convert it to tenths of a second precision we'll say two fixed and then we want one digit of precision past the decimal point finally we have to set the value of elapsed time to that label there so we're gonna say set number and we're gonna say label underscore time
29:46 and we want to pass it elapsed time so anytime this function is called it's going to calculate the difference between now and the original time and send that value to that label so we got to make sure we call update time so we're going to call it from the time bloop so we're gonna say update time and every time this time loop runs update time will be called let's try it and see if this works we're gonna reset it run it hit start and there we go it's calculating the time and we're eventually going to make this stop once we've killed all our enemies the next block of code we're going to write is going to handle the energy bolt so we're gonna make it always moving to the right and we're gonna make it disappear when
30:32 the game starts so let's go down to the bottom let's start a new block of code we'll call it function move energy bolt I love our curly brackets we're going to first get the energy bolts current location so we're going to make a local variable energy bolt X and we're gonna get the x position of image energy we're going to do the same thing with the Y position their energy Boult y equals get Y position image underscore energy now we have to create
31:20 a local variable that's going to control the speed of the energy bolt we'll save air energy bolt speed now if you want you could set this to a constant value like 20 I like to make it speed up as it moves to the right so what I do is I say energy bolt X divided by 10 that way as the x value gets higher the energy bolt is actually speeding up and you can tweak this how everyone now we're going to calculate the updated location of the energy bolt so we're gonna say energy bolt x equals energy bolt X plus energy bolt speed now we're going to move the
32:07 energy bolt to the new location so we're going to say set position want to set the position of image energy and we want to set it to the new energy bolt X and the energy bolt Y will be unchanged now we have to figure out how to reload it now we don't want it to be accessible again too quickly otherwise it makes a game too easy because you can rapid-fire so I usually wait until it's quite a bit past the edge of the screen the edge of the screen is X of 320 I wait till it gets till about 800 which would be about here you can't see it there but the program still keeping cracked and that gives us a little delay before we can fire again so I'm going to say if energy
32:54 bolt X is greater than 800 then we're gonna change gun loaded equal to true now if you want there to be a longer delay you can set this higher if you want there to be further joy you can set it lower also bear in mind the faster the energy bolt is moving the quicker it's going to get to this location now we're going to go back up to our event listener four button start once the game is started we want to move the energy bolt off the screen so we can't see it so right down here we're going to say set position image energy and then we're
33:43 going to move it to an X of 400 and a Y of 100 so I'll be off the screen where we can't see it we'll add the semicolon to the end of this line now we're going to call our new move energy bolt function from inside the timed loop so we're gonna go inside the time loop we're gonna say move energy bolt and then every 50 milliseconds it's gonna run move energy bolt which is gonna advance the energy bolt a little bit to the right now we're gonna try it out to see it working so we'll reset and run our program again we're gonna hit start so the first thing it did was move the energy bolt off the screen here it's actually continuing to advance the energy bolt along the x axis where we can't see it I'm gonna hit enter it fires the energy bolt once it gets far enough along the x axis it's going to be
34:29 reloadable so we can hit enter again and it'll fire again if I hit enter too often it's not going to actually fire it until it gets farther enough along the x axis now what we're going to do is we're going to write a function to move the enemies around and they're going to move down and then they're going to move to left and right so they kind of dodge let's go back to full screen mode let's go down to the bottom we're gonna create a function called move enemy and then we'll have a parameter name because there's different enemies and we'll pass the ID to this parameter when we call move enemy okay so we need to get the enemy x and y value so that'll be the
35:15 upper left-hand corner we're going to create a local variable enemy AK will set it equal to get X position and we'll get the x position of whatever ID is in the name parameter it'll make a local variable enemy Y and set equal to get Y of whatever ID is in the name parameter now we got to decide how far to the left and how far to the right we want the enemies to be swinging back and forth so we're going to say they're min X and I want it to go about to here so that X is let's say about a hundred and three it may be different on yours I don't want
36:00 it actually hitting my players Shep will say 103 then we're gonna say their max x equals let's see we don't want to be ships going off the screen so we'll maybe set the max to 280 so it'll only swing back that far now we're gonna calculate the distance from the max and that's going to help us determine the speed because we're gonna have them speed up and slow down depending on how far they are from the max we're going to say their distance from max equals max X minus enemy X and
36:49 remember enemy X is whatever the current X value is of the enemy now we're going to create a variable for the vertical speeds we're gonna say their vertical speed if you would prefer to set to a constant value you can I find it's harder if they change how fast they're moving vertically down so I base that on how far over to the left they are so I say vertical speed is equal to distance from Max divided by 10 and then I add on 20 and you can tweak this however you want then we calculate the horizontal speed and I have them speed up horizontally and horizontally is left and right depending on how long the program's been running so I said to
37:35 their horizontal speed now if you want to set this to a constant value you can what I do is I have it speed up until it hits a certain time and then it doesn't go any faster because otherwise they could get too fast so I'm going to use a function called math.min so I'm going to put in two values in here and it's going to choose whichever is the smallest one so I'm going to say either elapsed time divided by 2 or 15 so it's going to speed up until a elapsed time divided by 2 equals 15 and then it's just going to go to 15 be 15 will be the smallest now we're going to update the Y location so we're gonna say enemy y equals enemy y
38:26 plus vertical speed now horizontal is a little bit more difficult because it can either be moving to the left or right so we've got to switch it up depending on whether it hits our boundaries so we're going to say if enemy X and that's the current enemy X location is greater than max X then we want to set enemy's moving left to true because that means if we've hit too far to the right we want to start at moving left you may notice that I'm not using curly brackets after this if if you only have one line of code to be part of an if and else if and else a 4 or a while you don't need to use curly
39:11 brackets you can just indent it if you have more than one line of code that you want to be part of it you must use curly brackets now on the other hand if enemy X is less than min X that means we've moved too far to the left so we want to set enemy's moving left to false so we can have them move right now we're gonna check is enemy's moving left true or false if enemy's moving left is true we remember just saying enemy's moving left if there's a boolean value in there is the same as saying enemy's moving left double equals true it's just a shorter
39:58 way to write it we will say enemy x equals enemy x - horizontal speed and on the other hand if enemies moving less left is not true we're going to say L will say enemy x equals enemy x + horizontal speed so now we've got the new x value and the new y value calculated so we need to move them to those new locations we'll say set position whatever ID is in the name parameter and we're gonna set that to the new enemy X and the new enemy y the final thing we want to do is we want to make it called a vertical
40:43 wraparound that way of our enemy hits the bottom they come back around on the top so we're gonna call vertical wrap-around then we're gonna pass it whatever ID is in the name parameter so now we just got a call function move enemy from our timed loop so every kind of time loop runs it'll move e to the enemy so we're gonna go up we're gonna say move enemy will start off by sending it the ID for image underscore enemy underscore one with a semicolon there then we're gonna copy and then paste this three times and we're gonna change this to two three four so it'll update
41:30 the enemy's location every 50 milliseconds let's run it and see it working so we're gonna hit run we're gonna start so the enemies are moving now they hit the edge really switch directions they keep moving and they keep going back and forth next we're going to create a function that detects collisions between the energy bolt and each of the enemies and then handles those collisions in this video I'm just going to explain the basics of the math and logic but for a more detailed explanation please click on the link to the video in the upper right hand corner of this screen we're going to create a new function called collision detection and it's going to
42:15 have a parameter called object that's going to have the ID of each of these four enemies so let's put in our curly brackets we're gonna start by getting the location of the energy bolt which is going to give us a location in the upper left-hand corner so we'll say there energy bolt x equals get X position then image energy and we'll do the same thing there energy bolt y equals get Y position image underscore energy now what we need to do
43:04 is we need to get the X&Y value of whatever object we're dealing with so the ID for the object will be inside the object parameter so we'll save there object x equals get X position object then there object y equals get Y position object now we actually need to know more than just the X&Y value of the upper left-hand corner because we need to know if they're colliding overall so we need to be able to calculate this corner this corner and this corner so we can do that we can calculate this lower left-hand corner by knowing the upper left-hand X Y value
43:49 and knowing the height and we can calculate this one by knowing the upper left hand coordinate and the width and we can calculate this one by knowing the upper left hand coordinate and the width and the height then we're going to do the same thing with each of these enemy objects so we can are they colliding on top of each other now we need to calculate the width and the height so we're going to save air energy bolt width equals get property want the property of the image underscore energy and the property we want is the width then their energy bolt
44:39 height equals get property image energy and then height let's close this out so we have a little more room to see our code we can always open the tool box again if we need to now let's get the width and the height of whatever objects ID is in the parameter object so we'll say their object width equals get property object we don't have quotation marks around the object because we're not getting the property of something called object we're getting the property of something that is stored inside the
45:26 variable object so we want the width then their object height equals get property object and then height now what we need to do is we need to see are they overlapping on the X and then we'll check to see if they're overlapping on the Y so we're going to say if energy bolt X plus energy bolt width
46:11 is greater than or equal to object X and so we have double ampersand for and energy bolt X is less than or equal to object X plus object width so that means they're colliding on the x axis now we're going to make a nested if that's going to check if they're colliding on the y axis so we'll say if energy bolt y plus energy bolt height is greater than
46:58 or equal to object Y and the double ampersand again those are right over the seven key energy bolt bolt Y is less than or equal to object Y plus object height so if both of these if statements are true that means it's colliding on the x axis colliding on the Y act so the energy bolt is overlapping whichever of these enemy ships that has an ID inside the object parameter so what do we want to do we want to first check if it's already hidden because we're gonna hide it once it's destroyed
47:44 and we don't want to double count it if it's already been hidden so we'll say if not and remember not which is an exclamation point always reverses a boolean value makes true false and false true so we want to say get proper key object and we want to see if it's hidden so if it is not hidden it's going to return false and this is going to reverse that to true and it was not hidden that means we can destroy it so the first thing we want to do if we're destroying it is we want to hide it we're gonna say hide element object and then we're going to decrease by one the global variable enemies remaining
48:30 because we have one less enemy to destroy will say enemies remaining - - now that's shorthand for enemies remaining equals enemies remaining - one it just takes one off the current value and sets it as a new value by the way these two slashes tell the computer to ignore everything after them on this line so we use this to put comments and notes to other programmers notes to ourselves so we've destroyed the enemy now we got to see if there's any more enemies that need to be destroyed if there isn't we're gonna end the game will say if enemy's remaining is less
49:16 than or equal to zero and if it is we're going to do two things first we're going to stop our time to loop and most importantly that's going to stop the timer from running and we're going to hide our energy bolts we'll say hide element image energy now this isn't going to run automatically we're going to need to call it from the timed loop so we're going to scroll up and we're going to say collision detection and we want to pass it the first ID of the first object and we're going to copy and
50:05 paste this three times four three okay so now that we'll call it for each of them it'll check if the energy bold is colliding if it is it'll destroy that fit and it will decrease the value in the enemies remaining variable by one so let's play this and see how it works moving my character down and up I hit enter to fire alright hit one hit another miss that one usually the fewer
50:50 there are the harder it is to hit them they're also kind of speeding up now alright I got those last two with one shot so now I did it in eighteen point six seconds now I'm gonna make a little tweak to make it a little more difficult I'm gonna go to design I'm gonna click on this one I'm gonna make it a little smaller so I'm gonna make my width 25 I'm gonna make my height 35 and we can see there's not really a problem with the whiteboard or you always got a double check make sure there's not a white border outside of the image I'm gonna do that with these ones too 25 and 35 25 and 35 you all are probably gonna end up using different images so you're gonna have to tweak these numbers a
51:37 little differently I'm going to go back to my code and I'm going to speed up my enemies just a little bit you can do whatever you want so I'm going to go to my move enemy and instead of dividing my vertical speed distance from max divided by 10 I'm gonna divide it by 9 and my horizontal speed I'm gonna let it go as high as 16 instead of 15 so now my enemies will move a little faster so you can tweak these speeds however you want you can have change how much the enemies move in and out I can take the min x and Max X so once you've got yours completed
52:24 and you want to share it you can click on the share and even copy and paste this we like to copy and paste it in the comments so other people can see I also have some other videos on some other programs in a pleb and other languages you can find the links here or on my channel have a great day you