[MUSIC] So we saw a simple example of object-oriented programming and building a basic class to encapsulate some data and define some methods on that data. Now here, in this video, I want to show you a more complicated example. I'm going to show you something where it would be much more difficult to write in this style that we've been using so far. So hopefully this will allow you to appreciate the value of using classes in object-oriented programming. So that even if we're using them in a very simple way in some of the projects here, you'll see why we're headed in this direction. So with that, I want to walk you through a more complicated example. All right, when you use classes and object-oriented programming for simple structures, it may seem like overkill or a lot of effort for not a lot of benefit. It really start to shine when you start to write more complex programs. So I want to give you a feel for a slightly more complicated program. We've been showing many examples with balls bouncing around or flying around on the screen. I want to show you how I can do some interesting things by simply turning it into a class, all right? So here's my ball class right here, okay? Remember, I have the word class, I named it Ball with a capital B, and then I have my corn, and then I have a couple of methods. The first is the initialize under bar, under bar, init. I want to take a radius for the ball, a color that the ball is going to be, and a domain, what is a domain? Well, in this context, a domain is sort of the area that the ball is going to fly around in, okay, or bounce around in rather, right? And so I'm just going to store those away in fields, okay? Radius, color, and domain. And then I want to do some additional initialization. I want to start at some position and I want to start at a random position. So I'm going to ask the domain, please give me, which itself is a class, we will cover in a second. Please give me a random position inside the domain somewhere and this is my radius. So make sure that I'm actually entirely inside, okay? And the domain doesn't necessarily need to know I'm a ball, but I'm going to use a radius to define a circle that encapsulates my object, and with a ball that's somewhat obvious, okay? Then I'm going to pick a random velocity. My velocity's going to have an X and Y component, as we've been having in many of our examples. I'm going to just pick a random number between zero and one, and add 0.1 to it, so I've got a random number from 0.1 to 1.1, from the X direction, and 0.1 to 1.1 in the Y direction. So now my ball has a radius, a color, a domain in which it's bouncing around, a position inside that domain, and a random velocity. Okay, I'm going to have an update and a draw function, okay? What does the update function do? Well first, I add the velocity in. And we've seen this before as we had our ball moving across the screen, that each time I update, I add the correct velocity component of the velocity to the correct component of the position. Then, I'm going to add this extra bit of code here. Okay, the domain is going to have a method called inside that returns true or false, okay? Then I'm going to pass it my current position and my current radius. And so it's going to be able to tell me, are you inside the domain or are you hitting the edge? And if you're not inside, I'm going to reflect or bounce, okay? And here's the code for reflection. And I don't really care what this does, let's just ignore it for now, okay? There's some math here and I encourage you to look at it on your own, but I'm going to ignore it, right? And then finally, draw. Draw is actually pretty simple. Right? I take a canvas and I draw a circle in that canvas with the given color of the ball. So now I've created this abstraction. I have a ball, right? It keeps track of its all its own properties. It keeps track of its radius, its color, what the domain it's sitting in is, where its position is, what its velocity is, okay? And so now I can just use this abstraction. Okay, so I create some kind of domain so I'm going to create a rectangular domain. And then I create a ball and I tell it that it's inside this field which is this rectangular domain. And then I have a draw handler for my frame which simply updates the ball which adds the velocity, it doesn't need bouncing if it needs to. I draw the field which is just going to draw a box or whatever that shows you where the field is so you can see it. And then I'm going to draw the ball in that field. So let's watch this, okay? So here we go. My ball is moving and you just saw it bounce. Okay, it's moving pretty slowly, but it's going to hit the side wall here and it's going to bounce. And here we go again. Okay, so that's my ball, right? Now, there's some stuff going on in the domain, so let's go back up here and look at my domain. My domain has initializer and my rectangular domain just takes the width and height, because it's going to be a rectangle. Right, and play. You can see the red outline of the rectangle in the rectangular domain. Okay. It has this function inside which returns true if the bounding ball or bounding circle with the center and radius is completely inside the domain. False otherwise, it has a normal function that gives you the normal vector which helps me figure out how to do the reflection properly. It has a function that gives you a random position, somewhere inside the bounding box and it has a draw function. Okay, I'm going to ignore how all these work. All right, you have the code, you can take a look at it. You can think about it on your own. I do want to make the following point though, I also defined a circular domain, okay? So, if I do the following, you should do this yourself to convince you that I'm not playing any tricks on you. If I simply uncomment this line and comment that line out, right? I have not changed any code in the ball. The ball behaves exactly the same as it did before. I simply now have created a circular domain instead of a rectangular domain. And everything is going to work exactly as it did before. And here, you can see my ball bouncing inside the circle. Okay? Just take a minute to think about that. Okay, bouncing inside the circle. I change nothing about the ball class. I built the ball class in a way that it took a domain. I don't even know how the domain is implemented, I didn't uncomment any of that. I simply know the domain has certain methods. Two methods, inside and normal, okay? Using those two methods, the ball can effectively bounce inside any domain that you want as long as those methods are implemented correctly in the domain. I didn't have to change a line of the ball code. And this is the true power of objectory in your programming. Okay, I can take a class, understand its interface, what methods implemented and just use it. You can swap it out with new implementations that do different things. And they can continue to use it, all right? If you didn't use object-oriented programming. If you wanted your ball to start bouncing around inside of a circle and see it bouncing inside of a rectangle, we start having to change code all over the place. You would likely have to change code that relates to the ball, code it here. So it becomes much harder to design your programming pieces, okay? Now we're not going to implement something quite this hard this week. The Blackjack program with the objects and abstractions that we're going to build are much simpler. I just wanted to wet your appetite and get you to understand why we use this kind of abstraction. I hope this example's given you a feel for the power of object-oriented programming. This is still a relatively basic example. But by separating things out into classes and clearly defining the interfaces to those classes, I was able to separate the behaviors of the ball and the behaviors of the domain. And this allowed me to swap out domains, but by allowing the ball to continue to behave exactly as before. And instead of bouncing around in a rectangle, it bounced around in a circle. And I didn't have to change a line of code in the ball class. This is a hugely powerful concept. So hopefully, you now see why we try to organize programs in this way. And we're going to talk about how we might organize this week's mini project using classes, next.