Welcome to class, it's great to have you here. In this lecture I'm going to walk you through a modest size example of using a single class to create some particle objects that we'll use in a simulation. Now, the purpose for this lecture is to get you more experience in working with kind of an object-oriented style of programming. my experience in watching students try to actually implement Blackjack is those that have previous programming experience but no OO experience actually struggle in this project. It kind of likens it to a lot like this process of learning how to drive on the left hand side of the road after you've spent your entire life driving on the right hand side of the road. My wife and I flew into England and rented a car and we drove out of Heathrow Airport. And for the first 30 minutes, as I drove around and drove around my wife's favorite words were, oh, my God, there's a curb, watch out [SOUND], we go over the curve. Now, eventually, I have figured it out. And for those of you that are struggling, yes, you will figure it out too, just be patient. For those who have never programmed before, you actually have a little bit advantage here, this part actually seems strange to you that will be something new that you are learning. what I am going to do in my example is I am going to walk through building this little simulator. And I'm going to do it the wrong way, and you'll see where I go wrong. And then I'll do it the right way. And hopefully from that experience of seeing it done both ways, you'll kind of get a little more insight about how you should write OO programs. Okay, let's go to work. Okay, let's take a look at the example that I've built for this lecture. So I've written a fair bit of code here. the, the examples designed to spawn a collection of particles on the canvas and allow those particles to diffuse across this canvas in the same way that maybe molecules of perfume would diffuse across a room. Kind of the critical thing I've built here is I've built a particle class. The particle class has four methods attached to it. There's an initializer, it takes the object itself, the position and a color. A move method that actually moves the particle and takes it's, the object itself and an offset. A draw method that takes the object and a canvas. And a string method that takes the object, and returns back a string that kind of encodes the data inside the object. Now, one thing to note here is I've used code folding, to hide the implementation of these four methods. So we're going to build our, our simulation without understanding or knowing how we've implemented these particular methods. We're going to just trust that they work correctly. And again, I'll do it one of two ways. I'll do kind of an incorrect way, where I don't trust what's going on, and I'll do it a correct way where I trust it. And then at the end of the lecture, I'll go through and show you how I implemented each of these methods. And we'll talk a little bit about how to kind of use this same approach to finish Blackjack. All right, let's work with our code and implement our particle simulator. To do that, I'm going to spawn a list of particles and then I'm going to actually manipulate them inside the draw handler, make them move and draw them. Now, we've already implemented the particle class, so really this should be pretty easy to do. Let's focus on just first creating some particles. I've got some code written. I have a list that I'm ready to put my particles into. I want to create ten of them. So, lets do that. Lets make a particle p and hmm, I don't know how do I build a particle. You know, lets build a tube, maybe that'll work. So a tube that we're going to have to start is I guess the position and the color. So, let's put the particle at the middle of the canvas. That seems like a good idea. And, then get a color for it. And, I've actually got a list of possible colors up here already called color list. Now, remember, there's this handy dandy function called random.choice that will actually pick out the random element from the list. So, let's use random.choice and apply it to color list. Then I'll probably need to append that particle to my particle list. We can use the method append. And let's just print out the particle just to check our work real quick before I proceed. Remember, always check things as we go. Looks pretty good actually. Here we go. We got the position, we got a color. build ten of them. Gotta pass, passes the smell test. let's go up and do something with them now inside the draw handler. So for each part P and particle list, what do I need to do? We need to make it move. We need to move it around. Well, you know here's this method move. Let's use that method move on my particle and see if we can get that to work. So how do we apply a method to an object? We say object.method, and we have to give it something. And notice here that this object is actually corresponds to the first parameter here inside our move method. So we want to make it move in some offsets. So we can do that. I've already got some code waiting to be used. We can say random.choice of a list of directions that I've already predefined. We'll call, it's called direction list. So, hopefully this will take each of the particles and move it in some random direction. So let's fire that code up real quick. Hmm, error. Let's take a close look. Line 39. That's just the line we just typed in. It says attribute error. Tuple object has no attribute move. When you see an attribute error, that means that something went wrong with one of your objects, or one of your classes. ÂÂ here it's telling us that p is a tuple and that we can't use the method move on a tuple. That probably makes sense because the method actually move was defined for particles. Let's go and do one more thing just to check and make sure this is exactly right. Let's go back down here where we printed out the particle let's also print out the type of p. So we thought we created a particle, and we printed it out. It looked okay. Let's print out what Python thinks it's type is. Well, there's the thing we printed out but there's it says class [UNKNOWN]. So that's a touple. It's not a particle. So, we messed up. So give me a second, we'll go back and fix this. Let's clean up our first cut here implimenting our particle simulation. The trouble was that we used a two pole to try to represent a particle. Python didn't like that. So what can we do to fix this? Well, it's things not at all lost. What we really need to do is we need to use the method init to create a particle. if we use that method, well, actually build something that's a particle and then hopefully the move method will work. So how do we actually call this init method? Well, it's not too difficult. We simply use class name as particle. And we give it. Well, we give it the parameters, position and color. In this particular example, since actually you're creating the particles, cell doesn't actually appear. We don't have anything on this side. So notice I said particle. I gave a position, and I gave it a color. now let's just try running the code and see what happens. looking better. So we have, oh, how, where'd this come from? this came from the str method. Whenever we said print an object, Python looks for the corresponding str method for that, for that class. When you say print, it calls the STR method. And so on the side our STR method, as we'll see in a few minutes, it actually prints out this data for the particle in a very nice format. notice the type of the thing says now it's actually a particle. So, let's finish it off. Let's see if we can go through and actually complete our particle simulator. So we move the particle. All we need to do now is actually draw the particle. So how do we draw a particle? Well, we got a draw method. Let's just trust that our, our draw method works. So we call it by saying object.method. We need to give it the parameter, which is going to be the canvas. And let's fire it up and see what happens. Hey, looks pretty good. You've got some little circles bouncing around here. They're essentially particles diffusing across the canvas. let's go back and try with 100 just for the heck of it and I'll kind of wrap up this segment. So that looks reasonable. So, the moral of the story on this segment are two things. First, when you see an attribute error, go back and make sure that the object you're trying to apply the method to have compatible types. In this example, we had to use a particle method on a particle object. the second thing is if you're unsure about the type of an object, just use the type function to actually have Python tell you what the type of an object is. If you do those two things, it will help you avoid some of these errors. So let's finish up now and we'll talk about how we implemented the particle class, and we'll talk a little bit about how to apply some of this knowledge to Black Jack. OK, let's take a look at the implementation of the particle class. So I've unfolded all the method definitions here. So it's very straightforward. Simple, simple code. we've initialized, have two fields inside a particle. We have a position field and a color field initialized such. offset. Well, this is exactly kind of like what you saw in the column, basically a call to draw a circle here to draw the particle. And kind of the last interesting thing is this is our STR method. So it goes through and takes the various fields, converts them to strings, and then, just returns back a big long string of all the information about the object, kind of smooshed together. Inside Blackjack, you actually have to work with three classes; you have to work with the hand class, a card class, and a deck class. here again, on the card class, you have to implement hand and deck. one, kind of thing, that's going to help you build Blackjack, is if you focus on getting the implementations of the various classes correct, before you proceed onto the rest of the game. So this, kind of, leads you to, kind of, an interesting question, which is how do I know they're correct? Okay. Well we're going to provide you in black jack testing templates that allow you to check to see if the implementations of the three classes are working correctly. So I've actually built a testing template for the particle class and here's how we use it. We're going to take our implentation of the particle class, I'm going to a copy it, and I'm going to go to the testing template. So here's the testing template, you've probably seen this, if you done the practice exercises. It says, post your code, for the car-, particle class, here, so I posted it. And, notice what happens after that, because I have a bunch of, Python statements, that create particles and print out. Kind of what the particle looks like, what it's type is. I apply some of the methods to it. And then I had down here the expected output. So, if our implementation of article class is working, you would expect whenever you run this code to actually see output that looks like this. So let's do that. And it says we've got a particle position 20, 20 that's great. And a color red, that's great. the type of the particle is indefinitely indeed a particle. Those match. And in fact we can just go through here and check that the output here matches the output here. Kind of. For reference this is very similar to what you might see in some classes that are machine graded. this is basically is a testing regime here. And, it produces some output. And what happens is the machine graded classes. Typically the computer goes through and does this check to make sure these two things agree. for the games that we're building, it's not going to always be possible to kind of compare textural output, to make sure your game works correctly. But here, for Blackjack, we're going to provide you with three testing templates. One for a card, one for hands, one for a deck. Take your code, test it with these testing templates. Do not proceed with implementing Blackjack until you pass these testing templates. If you don't, you're going to have all kinds of errors when you try to use your incorrectly implemented methods for cards, hands, and decks. Hopefully this lecture gave you a few survival skills to make it through blackjack. my best piece of advice for fishing Blackjack is follow the mini project development process. Use the testing tempos that we provide for cars and hands and decks. implement it so that you can play black jack using the console before you move on to doing black jack on the canvas. if you break it down into a step by step process black jack is not too difficult to implement. feel free if you have questions to send your code to the codeclinicinteractivepython@online.rice.edu. We'll be glad to help.