170 likes | 317 Views
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.
E N D
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 • 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”
if ( Greenfoot.isKeyDown(“left”) ) { //do something } • Every key has a name, left is the left arrow • “A” is the A-key
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”)
Crab Turns Left if (Greenfoot.isKeyDown(“left) ) { this.turn(-4); }
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.
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!!
Turn Crab Right • Add code to act method that will turn the crab right. • Compile and TEST!!
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
What should act look like? public void act() { this.checkKeypress(); this.move(); this.lookForWorm(); }
What should checkKeypress look lik? public void checkKeypress() { if(Greenfoot.isKeyDown(“left”) ) { this.turn(-4); } if (Greenfoot.isKeyDown(“right”) ) { this.turn(4); } }
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?
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.
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.
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?
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”);