8 – Car Object

So here I’ll show you how to interact with a car object that can keep track of its own state. First, you’ll see some import statements and some code that we’ll go through in detail soon. But before we dive into the code that makes up an object, we’ll learn how to interact with it. So next you’ll see some initial variables. First a world, and then some variables that should look familiar, and initial position and velocity. This time, the position is in two dimensions, Y and X, and the velocity is also broken down into vertical and horizontal components VY and VX. I’ll make sure to run both of these, then to create a car which I’ll name Carla. I have to say car_dot_car and pass in some initial state. So I’m passing in some YX position and velocity. And lastly I’m passing in a world which is just a 2D array, and I’ll print out Carla’s initial state. You’ll notice that I can access Carla’s state by saying Carla_dot_state, and we see that the initial position is at zero zero, and the initial velocity is zero one, which means it has some horizontal velocity to the right. Now this car object, Carla, has some visualization code too. And we can see where the car is at any given point using the display world function. I can say Carla_dot_display_world, and when I run this code, I’ll see the location of Carla and the size of the 2D world. Now, what else can Carla do? Well, we can tell Carla to move by saying Carla_dot_move, and Carla will move one grid cell in the direction of the initial velocity, which in this case is towards the right. Let’s test this movement by displaying the world again, and we can see that Carla has moved one space to the right. After this movement, we can also track the change in Carla’s state, and I can print out its value using Carla_dot_state again. And we can see that the state has actually updated. The position has moved one grid cell to the right, and the velocity remains constant. So somehow Carla is keeping track of her own state. And what if we move a couple more times? Let’s track the change in state, and we see that we’ve moved two more to the right, zero_comma_three. And we can see the movement on a map of the world by calling Carla_dot_display_world once more. So we know Carla’s state updates automatically. Carla can also turn left. So let’s tell Carla to turn left and move one space. And let’s display the world. We can see that Carla turns left at this spot and actually circles around the world. And if we print out Carla’s state by saying Carla_state is string Carla_dot_state, we see that both the position and the velocity have changed. Our position is three_three, which is reflected in our grid, and VY is now negative_one and VX is zero. This means that there is no horizontal velocity, there’s only a negative vertical velocity, which means Carla’s moving up our grid world. Now moving and turning left are all Carla can do for now, but we’ll soon see how to add to our list of capabilities.

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