 Okay, welcome to simple pong and Python 3 for beginners part 10 And this is where we learn how to simplify our code because in the last video we added an extra ball we had a ball to and Ball to made us really have to double all of our code And as you probably realize by now coders do not like to do extra work. We don't like to double up We don't want to have duplicate code. It is a bad thing officially So what we want to do is figure out how to use the same code or should I say reuse the same code And then basically we can have one set of code that does everything for all for each ball What I'm going to actually end up doing is I'm going to end up adding four balls to this game and seeing what happens Okay, so let's go ahead and give this a shot. So let's take a look. We have a Ball and we have ball to What I'm going to do is I'm going to actually change the name of this ball to ball one So this is going to break the entire program, which is okay for now And then what I'm actually going to do with this is I'm going to make a list of balls So I'm going to say balls because there's more than one equals Ball one and ball two So remember each ball is a turtle object. It has all of these methods. It has all of these attributes Which we've given it so in this list of balls We've got a ball here and a ball here now I'm being very careful with my my vocabulary the words that I'm using We have a list of balls with more than one We have a ball and another ball that's going to come in handy down here. So what I need to do is Inside my loop. I'm going to do this for ball in balls When you see this structure you need to think to yourself, okay for each ball in that list I'm going to do this and so I'm gonna go ahead and move the ball. I Don't need ball to Because what it's going to do is it's going to start with ball one and that becomes the ball And it's going to do all the code and it's going to go again It's going to do ball to so each of these balls is going to get its turn So I'm going to go ahead and indent my borders. I'm going to indent all the code Oh, actually, maybe I'll just I'll delete the ball to code because I don't need it anymore That's probably maybe a better way to do it so I'm gonna go ahead and indent The border checking so that's inside this for ball and balls loop This is called a for each loop I'm gonna do the same thing for paddle and ball collisions Again, I don't need ball to anymore because that same code is going to take care of Ball one then ball two So I'm gonna come down to here And again now I don't have ball I don't have ball ball at ball to this part's going to be a little bit different. So let me go ahead and just See if it can hashtag that out. Yes. I use I'm like Mac I use command question mark to change all those to hashtags So I'm gonna go ahead and run it now that the AI is not going to work anymore But everything else should work if I did this correctly Use my arrows Okay, I missed the miss that Never get this game. Yeah, go ahead and test the right player. Okay test the left player Okay, so I think it's working basically as we have intended so Let's review what I did there I Created the balls. I called the first one ball one and ball two Then I made a list of the balls And then down here in my wild true loop note the indentation where the for loop is all this code And it says for each ball in balls. So it starts out with ball one So move ball one blah blah blah blah, so ball becomes ball one and then ball becomes ball two So it kind of replaces it So then do all those things that we did before for each ball now what I can do now What's great about this code is that I could go ahead and take this And I go ahead and make a ball three. I can make a ball four So let's go ahead and do ball three Let's go ahead and do ball four and why don't we change the numbers here a little bit? Let's let's make this Let's do let's do one and one make these ones a little slower and we'll do Let's see what we got here. So we've got negative two and negative two negative two and two And so the bottom should be positive. So let's make that positive one and let's make this negative one Okay, and then what I gotta do is just put them into my loop So you can see using this method I could have 50 balls there what wouldn't matter It's not going to change anything so the only code I need to do is create the balls and then I can just go ahead and Put them into the list and now my code's done. It does it's gonna do exactly what I wanted it to do Let's go ahead and run it test it with Okay, you see Yeah, let's go ahead and give it. Let's go get different colors. That's uh, blue. Let's do yellow And let's do red Why don't we go ahead and make this one always go ahead and make this one green have fun with it So save and I'm gonna go ahead and run it again So Okay, so you see how the score is going in and out. It's doing kind of exactly what we expected So this is kind of a melee version of the game again, I'm sure why keeps speeding up and slowing down probably made a mistake somewhere, but That's kind of a demo here. So I want to close that It's basically doing what we want it to do except for now the AI doesn't work So this is actually a really really Kind of interesting thing. I teach AP computer science and this is one of the standard kind of algorithms that we use in in in there to with with lists and with array lists and things like that so what I need to do is I need to find the closest ball to the Paddle so I need to figure out which paddle has the highest X coordinate So I'm gonna say I'm gonna call this called closest ball equals balls zero So I'm gonna just assume That the closest ball is The first ball on the list Now watch what I do here for ball in balls if ball dot X core is Greater than the closest ball Dot X core. Don't forget your parentheses Then the closest Ball Now is the current ball. Let me explain that one more time. We've got four balls in the list So we're gonna just say okay probably the let's say for example The first ball is is the closest ball. We'll just use that as our example and then we check again all The balls actually this is a little inefficient I should start from the second ball, but we'll just keep this the code very easy here So for each ball now if the balls X coordinate is greater than the current closest ball That tells us that that ball is closest So this will go through every single ball and tell us which one is closest So all I have to do here now is I can actually just get rid of this code. I only need One set of code I'm gonna go ahead and delete that Make sure everything's lined up and this actually This actually has to go back here because I don't want to do this until It's decided the closest ball first So first we're gonna decide the closest ball Which is what this section does and then we're gonna decide sorry Then the paddle is gonna move To intercept the closest ball So there we go oops, I forgot to see I'm gonna say that So again, I only want to compare paddle B to the closest ball. Let's go ahead and test it and run it and start Now you can see how it's going after the green ball Okay, now it's gonna shoot up to the yellow ball Still following the yellow ball. Okay now it's coming down the red ball So you see it's actually pretty good, especially because it can kind of cheat compared to the player I can still move things up and down Okay, and that is that that is how our AI player follows the closest ball Which is quite interesting when you think about it. And so you can see here how if we use a loop We can add as many balls as we like we can have one ball. We have two balls. We have three. We have four as many as we add This part here will take care of every single one of those balls Pretty pretty cool. Okay. I'm gonna stop there. Have a good one subscribe for updates