1 / 15

Variables and Inheritance A More Complex Example

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

tdoane
Download Presentation

Variables and Inheritance A More Complex Example

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Variables and InheritanceA 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. 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

  4. 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!

  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 isinitialized to 0.

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

  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

More Related