 Okay, welcome to Space Invaders Part 12. Again, this part of the series is being recorded five years after the original series. I'm here to kind of fix some of the mistakes I made years and years ago and to kind of address a lot of the questions that I've gotten over the years on this particular tutorial which has proven to be extremely popular. I think actually Pong is better, but I'm glad people are tuning in. So let's get going. So let's take a look at what we have right now. And it's working. In the last video, fix the player so that it moves like Space Invaders did continuously instead of a little bit at a time. As you can see, I'm not very good at this game. Yeah, okay, so that's what we have. But this is very unlike the original Space Invaders. There should be rows of aliens here in the program instead of just a few aliens. So let's go ahead and fix that. Unfortunately, it's not a difficult fix, but it takes a little bit of math, a little bit of basic math. Now we're also gonna learn, once we start putting some more aliens on the screen, it's gonna slow down. So we'll learn how to speed up the program as we go along. So let's take care of the aliens first. So here we've got where we create the enemies. And what I wanna do is I'm gonna create 30 enemies instead of five, okay? Because the original game had a ton of enemies up at the top. So this part of the code is still gonna work. It's still gonna create the number of enemies that we need. And you can see here what it does is it's gonna put them in a random location. So if I run this, this is not what we want. We're gonna have a ton of aliens. Okay, and actually it looks a little bit more like the original program just because they kind of moved weird like that because the system was so slow back then. But anyway, let's get rid of that. So what I wanna do is, actually I should have kept that on the screen, is what I'm gonna do is I'm gonna start here and I'm gonna move across and I'm gonna put 10 aliens here. I'm gonna put 10 aliens here and 10 aliens here. And I'm gonna do that with a little bit of loops and a little bit of mathematics. Okay, so now I've already done this, tested it in another program. So I have the numbers, pretty much how I like them. So what I'm doing is I'm doing enemy start x. So that's the starting coordinate of the first enemy here on top left. And I used minus 200 and I have enemy start y and that was 250 is what I had decided on. And then I got a key track of which enemy I'm putting on the screen and you'll see why in a second, okay? So again, I played around with the numbers. I found some numbers that I liked. You can go ahead and play around with them as you like. Something to consider, notice I type very long variable names because it makes my code easier to read. I could call this ESX, ESY and EN and I see all kinds of weird combinations but it really is really helpful if like me you're coming back five years later to try to understand what your code is doing. So what I'm gonna do is instead of x equals a random number, watch what I do here. Enemy start x, okay? So for the first enemy, it's gonna be minus 200. And then I'm gonna put plus 50 times enemy number. So what's gonna happen? The first enemy is actually number zero. So the first enemy is minus 200. Plus 50 times zero is zero. And then when it comes around again, the second enemy, which is gonna be number one, it's gonna be enemy start x plus 50 times one. So it's gonna move over and move over and move over and move over, okay? And so then when I'm here, I'm gonna say y equals, where's that, enemy start y. Now, I'm gonna run this and it's not done yet. So I'm gonna run it and show you what happens. Nothing, okay, because I missed something, all right? So I put them all on top of each other. So what I gotta do is at the end of this, I have to say enemy number plus equals one. So I gotta increment the enemy number each time, okay? So I'm gonna go ahead and run that again. And so you should see them form a row. Now you can see they went off the screen. So yeah, and then it's gonna come down. So we gotta do 10, then come down, do 10, then come down, okay? So you see there's a few problems with the code. So what I wanna do is I'm gonna say updates the enemy number, the number, okay? So if the enemy number equals 10, so remember, every 10 enemies I wanna drop down, okay? I set enemy, actually I don't need to do that. Basically enemy start y minus equals 50. So every 10, every 10 aliens, it's gonna drop down. Actually I do need this right now, I realize it. Enemy start x minus equals 200. And now I gotta move everything over to, actually no, I don't need to do that. I can leave that like that, sorry. And then I say enemy number equals zero, okay? So what I'm doing is I'm saying zero, actually up to nine, hits 10, come back to zero, all the way up, come back to zero, all the way over. I think this is, oh, and then I have to, here, I have to do, I don't have to do that, that's fine. I think that's all I need to do. It was a bit more, it took me a while to figure that out actually. Okay, there we go. And it's a little bit off-centered. So why don't we change that, sorry, that's bugging me. So we say enemy start x equals minus, let's try minus 225, let's see if that does it. And again, this is the process, you know, it's, yeah, that's much better. Okay, look at that, I like that. Okay, so we can see we've got the aliens and they are going across the screen. Actually it looks a little bit like the old school space invaders, just the way it's going slow. Now you see how things have slowed down, but that's okay. Now let's see if they reverse. Okay, so they are reversing and we can leave it at that. I had, oh, that, I forgot about that, I don't like that. Here's the other thing, I don't want this alien to come back on the screen like that. So I think that's probably the next thing I need to do. And interesting, all right, that's working. So this is a little bit different than how I had practiced. But anyway, so why don't we do that, let's fix that. So I'm gonna come down to, okay, if there's a collision between the bullet and the enemy, you set the bullet. Okay, what we do here is the enemy is, how can I put it, the enemy is set to a random position. Okay, so the easiest thing to do at this point would be to move the enemy like off the screen somehow and to just so we don't see it anymore. So what I'm gonna do is I'm gonna set the enemy into the center, so zero, and I'm gonna put it like way off the screen for now, like 5,000. Let's make it 10,000 just to be safe. So we never see it again. Now, this again, this isn't an ideal way of doing it, but based on what I wrote originally, this is probably the easiest. So let's run that and see if it works. I might have some problems later with that, but let's save that for later, cause we saw a few other things. All right, so we can watch this go super-duper slow. Ooh, I did it, really? Okay, I missed them all. Geez, let's try that again. I think I'm gonna miss them again. Oh my gosh, it's harder than it looks. Yeah, I'll tell you what. Why don't we go ahead and speed it up first and then we can go from there? So the reason that it is running slowly is just the way that the turtle module works. It updates the screen at its own kind of pace, and since we have a lot of objects on it, it's updating when we don't need things to be updated, et cetera, et cetera. So what we're gonna do is we're gonna use something called Win Tracer. So up here, when we set up the screen, we're gonna put WN.tracer0, and what that does is it shuts off all of the screen updates. So if you run the program now, this is what you're gonna see. Yeah, nothing. Okay, so, and it might lock up. Terminate, okay. All right, so what you have to do is inside the while true loop, way down here, you're gonna put Win.update, no zero, just nothing. Now I'm gonna run it, and now watch what happens. Watch carefully. Okay, so you can see how things now are moving just super fast, which is obviously not quite what we want. Okay, so we sped up the program and aliens are running off the screen. So what we're gonna do is basically just change the speed now. So for example, on my computer, now your computer might be different. So on my computer, we set the, we're gonna set the enemy speed to, I'm gonna try and set that to 0.2. On my computer, it's gotta be pretty low number. Your computer might be slower, it might be faster. So this number could be very, very small based on the speed of your computer. The bullet speed, I'm gonna set that to, I'm gonna try five. And the player, I'll try like minus three and plus three. It's kind of the relative speeds of everything. Maybe the bullet should be faster. Let's make it seven. So let's try it. Okay. Yeah, that's a bit more like Space Invaders was back in the day. Okay, so now keep in mind, all of the aliens are at the top of the screen. Now I don't know, this is probably gonna cause a problem later. I know how to fix it, but let's get to that bridge. We'll cross that bridge when we get to it. So, and it's still reversing, which is good. And again, some of the structures, you know, the computer, like the programming structures I used back then, five years ago, I didn't particularly, I wouldn't particularly do now, because I know more, but they worked, which is all, you know, it's always a good result. So again, with the speed, you just wanna kind of play around and find the numbers that work on your computer. But the relatively speaking, the bullet's gonna be about two, two and a half times faster than the player. And the enemies are gonna be about, about a 10th of the speed of the player. In this case, it's about .06, but you get the idea. So that's it. That's it for this one. So we have now have multiple invaders in multiple rows, which is what Space Invaders is supposed to look like. And we have been able to speed up the program using Win Tracer. And again, zero shuts off all updates and lets us control when we update it. And so what we wanna do is update it each time through the loop. And then that gives us a much smoother animation, a much faster animation as well. Okay, so yeah, so there you go. That's part 12. Part 13 will be coming, maybe not today, but definitely will be coming soon. So stay tuned.