1 / 17

Keyboard Control

Keyboard Control. Mrs. C. Furman August 26, 2010. Can You Control Crab??. Greenfoot has a method to tell whether or not a key is being pressed: static boolean isKeyDown(String key) What is the return type? What is the method name? What is the parameter?. String.

merrill
Télécharger la présentation

Keyboard Control

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. Keyboard Control Mrs. C. Furman August 26, 2010

  2. Can You Control Crab?? • Greenfoot has a method to tell whether or not a key is being pressed: static boolean isKeyDown(String key) What is the return type? What is the method name? What is the parameter?

  3. String • Strings are characters (letters, numbers, symbols) that are put together to form words or sentences. • String literals: Strings enclosed in double quotes. • “This is a String”

  4. if ( Greenfoot.isKeyDown(“left”) ) { //do something } • Every key has a name, left is the left arrow • “A” is the A-key

  5. Static methods • Static methods are invoked (called) through their class name, in this case the class name is Greenfoot • That is why we do Greenfoot.isKeyDown(“left”)

  6. Crab Turns Left if (Greenfoot.isKeyDown(“left) ) { this.turn(-4); }

  7. Replacing Random Turn and Turn at Edge Code… • Remove the random turning code from the crab • Remove the code from the crab that does the turn at the edge of the world.

  8. Add Code to act method • Add code into the crab’s act method that makes the crab turn left whenever the left arrow key is pressed. • Compile and TEST!!

  9. Turn Crab Right • Add code to act method that will turn the crab right. • Compile and TEST!!

  10. Put turning in Methods • Make a method called checkKeypress • What should the return type be? • Copy the codes for turning left and right from act and put them into your new method. • Call checkKeypress in your act method

  11. What should act look like? public void act() { this.checkKeypress(); this.move(); this.lookForWorm(); }

  12. What should checkKeypress look lik? public void checkKeypress() { if(Greenfoot.isKeyDown(“left”) ) { this.turn(-4); } if (Greenfoot.isKeyDown(“right”) ) { this.turn(4); } }

  13. Ending the Game • Choose Greenfoot Class Documentation from the help menu. • Select the Greenfoot class. • In Method summary find a method that stops the execution of the running scenario. • What is this method called? • Are there parameters? • What is the return type?

  14. static void stop() • Where can we put a call to this method so that it will stop the execution when the lobster eats the crab? • Greenfoot.stop(); • Try putting the line of code in, and run. • If it doesn’t work, try somewhere else.

  15. Where did we put the code? • The crab is removed and the game should stop when the Lobster eats the crab. • Put Greenfoot.stop() line in the Lobsters lookForCrab method, after it “eats” crab.

  16. Add Sound • Look at the Greenfoot documentation and find the method that is used to play sound. • What is its name? • What parameters does it expect?

  17. static void playSound(String file) • There are 2 sound files included in the crab scenario. • slurp.wav • au.wav • When crab eats worm, call Greenfoot.playSound (“slurp.wav”); • When lobster eats crab, call Greenfoot.playSound (“au.wav”);

More Related