1 / 11

Wrapping Up Crabs and Greenfoot

Wrapping Up Crabs and Greenfoot. Mrs. C. Furman. Count How Many Worms Crab Has Eaten…. Lets decide that if Crab eats 8 worms it wins the game. What will we need???. What We Need To Track Worms?. An instance variable to store the current count of worms eaten;

lowri
Download Presentation

Wrapping Up Crabs and Greenfoot

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. Wrapping Up Crabs and Greenfoot Mrs. C. Furman

  2. Count How Many Worms Crab Has Eaten… • Lets decide that if Crab eats 8 worms it wins the game. • What will we need???

  3. What We Need To Track Worms? • An instance variable to store the current count of worms eaten; • An assignment that initializes this variable to 0 at the beginning; • Code to increment our count each time we eat a worm; • Code that checks whether we have eaten eight worms, and stops the game and plays a winning sound.

  4. Instance Data • Where to put the instance data? • What should we name it? • What should the type be? • public or private?

  5. Add instance data • Add an instance variable in the same place as the GreenfootImage instance data • private int wormsEaten;

  6. Initialize Instance Data • Where do we initialize the instance data? • Write a statement that will initialize wormsEaten to 0

  7. Add to the Constructor • Hopefully you decided to initialize the instance data in the constructor. • Add this line to the Crab constructor… • wormsEaten = 0;

  8. Counting Worms Eaten • Where does crab eat worms? • Add an 1 to wormsEaten when Crab eats a worm.

  9. Modify lookForWorm method • Add the following line to the lookForWorm method so that it will add 1 when a worm is eaten public void lookForworm () { if (this.canSee(Worm.class) ){ this.eat(Worm.class); wormsEaten = wormsEaten + 1; } }

  10. Stopping at 8 • Where should we check for 8? • What should this if statement look for?

  11. public void lookForWorm() { if (this.canSee(Worm.class) ) { this.eat(Worm.class); Greenfoot.playSound(“slurp.wav”); wormsEaten = wormsEaten + 1; if (wormsEaten == 8) { Greenfoot.playSound(“fanfare.wav”); Greenfoot.stop(); } } }

More Related