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.