Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables and Inheritance A More Complex Example Alice.

Similar presentations


Presentation on theme: "Variables and Inheritance A More Complex Example Alice."— Presentation transcript:

1 Variables and Inheritance A More Complex Example Alice

2 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.

3 An intuitive storyboard right Turn front wheels right left Turn front wheels left backup Move car backward Rotate wheels backwards forward Move car forward Rotate wheels forward

4 Problem The problem is the actions are not coordinated. left turns the wheels left, but forward rotates 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!

5 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!

6 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

7 Create new variable 1) direction is declared to be a number type. 2) Its value is initialized to 0.

8 Revised plan Event: right arrow right: Increment direction Event: left arrow left: Decrement direction Event: down arrow backup: Do together corvette move backward 1 meter corvette turn right/left based on direction 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

9 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.

10 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.

11 left Q: Why is a negative 10 used as a limit in the If statement for this method?

12 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?

13 backup What is the difference between this method and forward?

14 Assignment Chapter 10 Section 1, pages 257-261

15 Lab Chapter 10 Lecture 1 Lab


Download ppt "Variables and Inheritance A More Complex Example Alice."

Similar presentations


Ads by Google