150 likes | 180 Views
Variables and Inheritance A More Complex Example. Alice. A steerable car. The task is to create a car-steering simulation. To steer the car, the user presses keys to control: moving the car forward or backward turning the wheels right or left
E N D
A steerable car • The task is to create a car-steering simulation. To steer the car, the user presses keys to control: • moving the car forward or backward • turning the wheels right or left • Of course, as the wheels are turned (right or left) and the car is moving forward (or backward), the car should steer right or left.
right Turn front wheels right left Turn front wheels left An intuitive storyboard • forward • Move car forward • Rotate wheels forward • backup • Move car backward • Rotate wheels backwards
Problem • The problem is the actions are not coordinated. • leftturns the wheels left, but • forwardrotates the wheels forward while the entire car is moving forward (Of course, right and backUp are opposites of the above) • This is an "accident looking for a place to happen." The animation is going to look like the car has a broken axel!
Solution • When the car is moving, we need to coordinate the left-right turning action (steering) with the forward or backward movement. • But, to coordinate the actions, we have to (somehow) remember how far the wheels have been turned to the left or right – the direction in which the wheels are turned. • In other words, we need to know the state of the front wheels of the car object!
Adding State • To keep track of the amount of turn on the front wheels (the direction), we can use a linear scale, where positive values indicate the degree of turn to the right and negative values to the left. -10 0 10 left right • Create a class-level variable named direction • start with direction at 0 (straight ahead) • right-arrow increases direction • left-arrow decreases direction
Create new variable 1) direction is declared to be a number type. 2) Its value isinitialized to 0.
Event: right arrow right: Increment direction Event: left arrow left: Decrement direction Revised plan right and left do not turn the wheels! Event: up arrow forward: Do together corvette move forward 1 meter corvette turn right/left based on direction • Event: down arrow • backup: • Do together • corvette move backward 1 meter • corvette turn right/left based on direction
Demo • Ch10Lec1bSteeringCorvette • Concept illustrated in this example • A class-level variable can be used to track the state of an object in an interactive animation, where the user's actions change the state.
right Q: Why do we use an If statement? A: To limit the amount the user can direct the car to turn right – real cars have limits, too.
left Q: Why is a negative 10 used as a limit in the If statement for this method?
forward Q: Why is the corvette's direction multiplied by 2 and divided by 360? Q: What happens if the value of direction is less than 0?
backup What is the difference between this method and forward?
Assignment • Chapter 10 Section 1, pages 257-261
Lab • Chapter 10 Lecture 1 Lab