 Hey, welcome to code.org app lab space invaders part two making an enemy with Tokyo EdTech. That's me. So let's go and see what concepts we're going to be looking at. So the concepts today are location X and Y movement DX and DY and border checking. If you remember back to the first video, this is part two. These are the same coding concepts. There's nothing really new that we're going to be doing today. Okay. So, which is cool. So we're just going to be applying things that we've already done. And something that I'm really trying to get across to my students is to understand what we're doing and to try to, you know, use what we've already know to do the next thing. Let's go to the main screen. And here is what we have so far from the last time. So if I run this, I hit start, takes me to the main screen. I push the right arrow goes right, I push the left arrow, it goes left. And this is going to represent our player. The player is drawn on a canvas, so it's called canvas game, CNV game, and we have a couple of buttons here. And I'm using player X and player Y and player DX. I don't have player DY because the player doesn't move up and down. So what I'm going to be doing in this program is I'm going to be showing you how to add one enemy. Okay. So I'm taking this kind of simple, taking the simple route, I should say, just because again, my students are beginners, and I want them to kind of just get the basic ideas down. So if I want to put something on the screen, I'm going to think about what did I do last time? Okay. So last time I created player X, player Y, DX, and in the case of the invaders, they're going to drop down. So I might make a DY, but actually I'm going to deal with that a little bit differently probably in this particular case. And in my loop, my main loop, so we're going to ignore this because this part's done. Okay. I've already got the start button, left button, right button, that's done. I don't need to deal with that anymore. But I'm going to go to my loop, and I made this 20 milliseconds, so it's 50 frames a second. I don't know if you can even keep up, but you can play with the numbers and see how that goes. So we clear the canvas, move the player, it's kind of the physics side of things. Check for borders, and then we draw the player. So in this case, we're going to be doing basically the same thing. Think about that. If you're thinking about space invaders, the enemy, we're going to start the enemy up here somewhere. Enemy's going to come across, it hits the border. Instead of stopping, it's going to drop down and reverse direction. Drop down, reverse direction, drop down, reverse direction. Then we get to the bottom. We have to do something, but we won't deal with that part today. So this should be pretty straightforward. So let's go ahead and get started. I'm going to start with my variables, and I'm going to do enemy x, and let's start the enemy 35, 30, so 35, and enemy y, 30, and let's see where enemy dx, enemy dx, and we'll make the enemy move a little slower than the player. The player moves at four, so we'll make the enemies move at two, see what happens there. So we've created the variables. You notice we've got these little triangles, it says enemy x is defined, but it's not called in your program, so we've got to go ahead and call that. So I'm going to go down here, and I'm just going to kind of keep things together. So this says move player, so I'm going to go ahead and pull that in. Notice there's no var here. We only use var the first time we create it. And then down here we're going to do enemy x equals enemy x plus enemy dx. And again, nothing new. I'm just basically copying what I did here and adding the stuff for the enemy. And then down here I'm going to draw the enemy. So let's go ahead and do that, which I'm going to show the text. And now I find it easier just to copy and paste, but if you make a mistake then it's hard to get back into block mode. And I'm going to make the enemies red for now. And of course it's going to go at enemy x, enemy y, and I'm going to make the enemies a little smaller. Let's try 10 and see what happens. So if I've done my code correctly I should be able to hit show blocks and we'll see the block code. So let's go ahead and run that and see what happens. Okay, so there's my enemy. Now the enemy is going right off the screen, so let's hit reset. So again, we're just going to be doing the exact same thing except with a little bit of a twist here. So I'm going to go ahead and show text. And I'm going to just go ahead and copy this. So what I'm going to do is I'm going to do enemy x is greater than 300. Same thing. I'm just copying. I've already got this working. I don't need to reinvent the wheel. The only difference here is that I'm not stopping it at the border. I'm reversing it. So to do that I do the following. Enemy dx times equals negative 1. What that does is if dx is positive, 1 times negative 1, or 2 times negative 1 is negative 2, it'll start moving to the left. Negative 2 times negative 1 is positive 2. It'll start moving to the right. So I'm going to do the same thing here. And then don't forget, this is a little different. You know how that red box disappeared now that I coded it correctly? So let's go ahead and hit show blocks. You can see what that kind of looks like here. So we've got the enemy on the right border and the enemy on the left border. Now you see here how there's a little bit of a mistake. I typed that wrong. So again, everything should line up. Everything should look exactly kind of the same. So let's go ahead and restart this and see what happens. Just run it and start. It's going to hit the right border and it's going to bounce off. Now you can see how the collision is a little bit off just because the size is different. Okay. Now again, if you've played Space Invaders, you know that this should be dropping down. That's the one thing that we missed. So what I'm going to do is I'm going to go ahead and when it hits the border, I'm going to go ahead and change the D, change the Y. So enemy Y equals enemy Y. And remember it is positive going down. So I have to do plus in this case. Let's try 20. I don't know if that's the right number, but I think it's probably close to what we want. And enemy Y equals enemy Y plus 20. Okay, so let's go ahead and run it and start. And what we should see is the enemy dropping down, coming across, dropping down. At this point, I know it's basically working how I want it to. I can still move my player around. Now the player is pretty big. You know, this is something we might want to adjust the size, but we can leave it the way it is for now. And you notice how it's a little, the player is faster, which is good. So we want to try and figure out how to deal with that. So I'm going to hit reset. So that is it. That's really all we got to do for the enemy. Now, of course, I'm not dealing with the fact that when it hits the bottom, the game is over. That's that's a different lesson. That's for a different day. But what I want you to get the concepts early, what you think about here is we've got some working code. We've already got a player on the screen. So we know we need an X, we know we need a Y, we need a DX. So we made that for the enemy here. Then we also have to move the enemy, just like we move the player, and it's the same code. We're just copying it and changing the name. With the borders, we still have to look for the borders. So that's X, not Y. But in this case, we're not stopping at the border. We're reversing direction. That's what that does. And we're also jumping down. In this case, I chose 20 pixels. You can choose whatever number works for you. And that will bring the enemy down. And then we also need to draw the enemy. So it's pretty straightforward. Again, if you understood what happened in the first video, this in the second video should be pretty clear. So we used X and Y for the location, DX, and we actually used DY. We actually only used DX, so I kind of cheated there a little bit. And then we did a little bit of border checking. So that is that. Yeah, thanks for watching and keep on coding.