9 – Nd113 C2 L3 20 L Adding Color V2

So, we’re back in our car.py file, and let’s see how we can actually modify this car object and add a color variable. Inside our class code, we can see that there’s a default color for our car. Red represented by the character r. So, how do you think we might customize this? Well, just like with our state variables in our world, we can pass it in in our initialization parameters. To do this in the usual way, we just add an extra parameter to our init function. I’ll call this parameter color. I can also specify a default value for color, equal to red, equal to the character r. This color equals r means that, if and only if, a person does not specify a color parameter when a car object is created, it will by default be red. None of our other variables have a default value. Then, we also have to change this one other line of code. Instead of saying self.color equals r, we say self.color equals color, and this is the past end variable here. So, now, our init function initializes the state of our car, gives our car a 2D world to traverse, and it specifies the color of a car. All that’s left to do is to test this out in executable code. So, let’s go into a new notebook. Here are the usual import statements where we import our car class file. Here are our usual import statements where we import our car class file. Next, I’ll create car objects. Here, I’m defining the initial parameters, and I’m creating Carla in the same way as I usually do. Then, I’ll define some new initial parameters and they’ll create a new car object. This time, I’ll name the car Jeannette. I’ll passion an initial state, a world and a color, and it’ll say Jeanette equals car.car, and pass in our initial variables. As you can see. I’m passing in our initial state variables, position2 and velocity2. I’m passing in the same world, and I’m also passing in a color, and specifying that Jeanette has a yellow color with the character y, as our last past end invariable. Order is important here. This should match the order in our car class file. So, now, I should have two cars, Carla and Jeanette, and I’ll run this cell of the notebook. Now, if you get an error at this step, make sure you saved your car class file after you’ve changed it, and restart your kernel by clicking kernel, and restart, and clear output. So, since I didn’t get any errors, my next step will be to write some move code for both of these cars. This is just for fun and visualization. First, I’m going to move Carla. Telling Carla to move, and turn left, and move again. Then, I’m going to display Carla’s world. We can see Carla’s movement, moving forward, then left, and looping around the world. We see that color is red. Red is the default color since we didn’t specify a color when we created Carla. Let’s scroll back down. Next, I’m going to move Jeannette, turning left, and moving, and turning left and moving some more and I’ll display Jeannette’s world. We can see that Jeanette start off at a different point, and looped around, creating a different path. We see that Janette is yellow, including the path. So, this is pretty cool. Adding more variables like a car color can be just a couple of lines of code. This is also the case with functions. You can add their definitions to a class and then be able to access them.

%d 블로거가 이것을 좋아합니다: