 five of the Bouncing Ball Simulator Tutorial Series. Please note that I am recording this about two years after the original video is recorded because I'm getting the same question over and over again. So let's take a look at what we have here. And you can see we've got the balls. Now, they're not necessarily balls anymore, but we have the shapes bouncing and they're bouncing around. But what I've been asked to do is to make it so that the shapes actually bounce off of each other. And it's not super complicated to do, but there's a couple little things we need to keep in mind. So let's go ahead and take care of that. So what I'll do is I'm going to come down to my while true loop and I'm going to add another section down here at the bottom and I'm going to go ahead and make sure I indent again. Indent here. Okay, that's... I'm going to do spaces. And then... So what I want to do is check for collisions between the balls. Now, the trick to this one is that I'm going to have to compare ball 0 to ball 1, ball 0 to ball 2, ball 0 to ball 3, ball 0 to ball 4, all the way to the end. Then I have to compare ball 1 to ball 2 to ball 3, etc., then ball 2 to 3 to 4, then ball 3 to 4 to 5, etc., etc. So what I need is two different loops to do that. So I'm going to go ahead and do the following. I'm going to say 4i in range and I'm going to be starting at 0 and I'm going to be going to the length of balls. So that will tell me how many balls I have. And then the next loop, I'm going to use j because that's just the traditional thing in range. Now watch what I do here. This is the trick. i plus 1 and then also to length of balls. So what I'm doing is I'm going to be starting at 0 and then I'm going to be starting at 1. So 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. Then I come back around, then 1. This starts at 2, 3, 4, 5, 6, 7, 8, 9, 10. So to make it easy to understand, I'm going to do the following. I'm going to say ball 1 equals... Actually, I don't think I can do that, but let's skip that because I think that might cause more problems than it's worth. So what I need to do is check for collision. And to do that, I'm going to actually use the distance formula from the Python Turtle module. So watch what I do here. So if balls i dot distance balls j and watch your parentheses and watch your brackets here is less than 20 because each ball has 20 pixels width. So half that is 10. So 10 plus 10 is 20. So what I'm going to do here is I'm going to go ahead and try the following. Since we already have colors up here, I'm going to go ahead and do this. I'm going to change the color every time there's a collision. So watch what I do here. And I'm going to say balls i and balls j. And I'm going to copy that. So I don't feel like copying. Let's type it all in. And the reason I'm doing this is just to test and see if this part works. I'm going to get rid of this in a second. And this is something I often do. So let's go ahead and watch it and see what happens. Okay, so you can see how the colors are... When there's a collision, the colors are changing. Okay, so this is telling me that the collision is actually being registered. Which is kind of cool. I mean, I like that effect. But what we want to happen is we want the balls and the shapes to actually bounce off of each other. And now we're going to have to do this like perfect physics style. But basically what we're going to do is if there's a collision, we're going to swap the x and y values. Or the dx and dy values. Sorry about that. So what I'm going to do is I'm going to say temp dx equals balls i dot dx. Temp dy equals balls i dot dy. Then I'm going to say balls i dot dx equals balls j dot dx. Balls i dot dy equals balls j dot dy. And then I'm going to say balls... Yes, balls j dot dx equals balls... Equals temp dot dx. So basically just swapping values. This is a very common programming structure. Now those of you who know a little bit more about Python, yes, there's an easier way to do this in Python, but it doesn't work in every programming language. So I like to show this to my students, especially those that go on to study other languages. This is how it's normally done. The Python being Python has some cool shortcuts that I'll show you in a bit if I have some time. So let's go ahead and run this. And this should do it. That should be it. So basically what we're going to re-explain is I'm swapping the dx and dy of the balls. And this looks like a collision. You'll see what happens. So you can see how now they're hitting each other. They're bouncing around. And it's pretty realistic. It's realistic enough, I think, for our purposes. And it looks kind of cool. We can see how they're bouncing around. Yeah, I like this. It looks pretty cool. So that's it. That's the code that does it. You basically just iterate through the list. You need to do a nested loop. And again, we're going from zero to the length. And then this plus 1 because we don't want to compare it against itself because there will be a collision. And then we just set up a temporary dx, a temporary dy, and assign it and switch it out. Now, as I mentioned earlier, in Python, you can do it like this. It's a little bit shorter. I can do balls i dot dx and balls comma dx, sorry. Balls j comma dx equals balls j dot dx and balls i dot dx. And I can do the same thing for dy. So I'm just going to copy that, change it to dy. Again, most programming languages that I know of, at least I've used, you can't do this. But Python does let you do this. So it is a nice shortcut to know. So let me go ahead and test that. Just make sure it's working as expected. And you can see that the bouncing is working. So yeah, it's pretty cool. It gives you like a nice little bouncing effect. It looks like sometimes they're not quite, the collisions aren't registering, but it's probably just the distance isn't quite what you think it is. It looks cool. You can kind of see them bouncing around here. So that is that. So thanks to everybody that's been tuning in for several years. I finally got around to doing this. And thanks for the question. Subscribe if you're interested in more. Take care.