Chapter 3 – Improving the Crab Game
3.1 - Adding Random Behavior: more complicated behaviors topics: random behavior, keyboard control, sound concepts: dot notation, random numbers, defining methods, comments We’ll use a Greenfoot Utility A Greenfoot utility is a method supplied by the Greenfoot environment, so the method starts with “Greenfoot.” in its name (“Dot-notation”). Greenfoot.getRandomNumber(limit) This will give us a random number between 0 and 20. The limit is --20 This method returns a random number that can be any value from zero up to but not including the given limit (so from zero to limit-1)
Comparison Operators An example: if (5 > 1) {move();} Comparison Operators are used to compare two numerical values. They always produce a definitive result, either true or false (i.e. they produce a single boolean value). Comparison Operators are often used as the condition within an if – else structure. An example: if (5 > 1) {move();}
Comparison Operators Note: a and b are numerical values < less than ex: if (a < b) { … <= less than or equal ex: if (a <= b) { … == equal ex: if (a == b) { … != not equal ex: if (a != b) { … >= greater than or equal ex: if (a >= b) { … > greater than ex: if (a > b) { …
Logical Operators An example: if (5 < 1 && 0 > 1) {move();} Logical Operators are used to logically link two boolean values. They always produce a definitive result, either true or false (i.e. they produce a single boolean value). Comparison Operators are often used as part of the condition in an if – else structure. An example: if (5 < 1 && 0 > 1) {move();}
Logical Operators Note: a and b are numerical values && Logical AND Ex: if (a < b && b < c) { … Note: both a < b and b < c must be true, or the entire condition evaluates to false || Logical OR Ex: if (a < b || b < c) { … Note: either a < b or b < c must be true, or the entire condition evaluates to false
Logical Operators (Truth Table) One way to visualize all possible sets of inputs -> outputs of a logical operation are Truth Tables. Inputs Results AND OR a b a && b a || b true false
What does it do. // This class define a crab. Code 3 What does it do? // This class define a crab. Code 3.1 from your textbook public void act( ) { if ( atWorldEdge( ) ) turn( 17 ); } if ( Greenfoot.getRandomNumber( 100 ) < 10 ) turn( 15 ); move( ); Gives you random numbers from 0 to 99 Note: The random number will be always <100 and <10 approx. 10% of the time
What does it do? Crabs tend to walk erratically, so this makes the behavior more realistic. But the crab always turns to the right. How can we make the crab turn either left or right randomly? 10% of the time the crab turns 15 degrees Clockwise (to the right)
What does it do? public void act( ) { if ( atWorldEdge( ) ) turn( 17 ); } if ( Greenfoot.getRandomNumber( 100 ) < 10 ) turn( Greenfoot.getRandomNumber( 46 ) ); move( ); Now it turns from 0 to 45 Degrees to the right
What does it do? public void act( ) { if ( atWorldEdge( ) ) turn( 17 ); } if ( Greenfoot.getRandomNumber( 100 ) < 10 ) turn( Greenfoot.getRandomNumber( 91 ) - 45 ); move( ); Now it turns from 0 to 90 minus 45 That equals 0-45 to 90-45 That equal -45 to 45
What does it do? All Crab Behavior is Independently Random Because Greenfoot.getRandomNumber () Returns a Different Random Number on Each Call
Adding Worms Right Click on the Animal Class and then Click on New subclass
3.2 - Adding worms Crabs like to eat worms We can add new actor classes to a Greenfoot Scenario New subclass from one of the exsiting actor classes. Add new class Worm (Subclass of class Animal)
Adding Worms Choose the worm image
Adding Lobsters Right Click on the Animal Class and then Click on New subclass
Animating Actors Worm facing right Worm facing left (Worm.png) (Worm2.png) Animation is Achieved by Switching Between the two or more Images
The Worm act() method (if-else) Keeps count of calls to Act() method private int counter = 0; private int imageNumber = 1; public void act() { counter = counter + 1; if (counter == 4) { if (imageNumber == 1) { // image 1 so switch to image 2 setImage("worm2.png"); imageNumber = 2; } else { // image 2 so switch to image 1 setImage("worm.png"); imageNumber = 1; } counter = 0; } } Keeps track of which image is showing
The Worm act() method (&&) Keeps count of calls to Act() method private int counter = 0; private int imageNumber = 1; public void act() { counter = counter + 1; if (counter == 4 && imageNumber == 1) { counter = 0; setImage("worm2.png"); imageNumber = 2; } if (counter == 4 && imageNumber == 2) { counter = 0; setImage("worm.png"); imageNumber = 1; } } Keeps track of which image is showing
The Worm act() method (comb.) Keeps count of calls to Act() method private int counter = 0; private int imageNumber = 1; public void act() { counter = counter + 1; if (counter == 4 && imageNumber == 1) { counter = 0; setImage("worm2.png"); imageNumber = 2; } else if (counter == 4 && imageNumber == 2) { counter = 0; setImage("worm.png"); imageNumber = 1; } } Keeps track of which image is showing
Eating worms Now we can add new behavior to the crab: When the crab runs into a worm, it eats it.
Crabs Ignore Worms
Crabs Eat Worms
3.4 - Creating new methods Adding new behavior to the crab – turning at the edge of the world, occasional random turn, and eating worms. Add new method called “lookForWorm”. The first four lines are a comment, which is ignored by computer. We have to call the method “ lookForWorm(); form act() method
Complete the source code after this restricting show below
3.5 - Adding a Lobsters Choose the lobster image
The Lobster methods – 3.5 – Adding a Lobster Copy the complete act method from the Crab class into the Lobster class. Also copy the complete lookForWorm, turnAtEdge and randomTurn methods. Change the Lobster code so that it looks for crabs, rather than worms. You can do that by changing every occurrence of “Worm” in the source code to “Crab”. For instance, where Worm.class is mentioned, change it to Crab.class. Also change the name lookForWorm to lookForCrab. Make sure to update your comments.
New Lobster act() method public void act() { turnAtEdge(); randomTurn(); move(); lookForCrab(); }
Did Crabs Eat all the Worms?
Adding more complicated behaviors: Keyboard control boolean Greenfoot.isKeyDown( String key ) Checks whether a given key is currently pressed down Greenfoot.isKeyDown("left") returns true if the left arrow key is currently pressed down. Greenfoot.isKeyDown("right") returns true if the right arrow key is currently pressed down.
Create checkKeypress() method for the Crab class
New Crab act() method public void act() { checkKeypress(); // no more random turns move(); lookForWorm(); }
New Lobster lookForCrab() method /* * Check whether we have stumbled upon a crab. * If we have, eat it. If not, do nothing. */ public void lookForCrab() { if ( canSee(Crab.class) ) eat(Crab.class); Greenfoot.stop(); } Ends the game
3.8 - Adding Sound to Crab class Another improvement to our game is the addition of sounds. Again, a method in the Greenfoot class helps us with this. /* * Check whether we have stumbled upon a worm. * If we have, eat it. If not, do nothing. */ public void lookForWorm() { if ( canSee(Worm.class) ) eat(Worm.class); Greenfoot.playSound(“slurp.wav"); } Plays Sound Effect
Adding Sound to Lobster class /* * Check whether we have stumbled upon a crab. * If we have, eat it. If not, do nothing. */ public void lookForCrab() { if ( canSee(Crab.class) ) eat(Crab.class); Greenfoot.playSound("au.wav"); Greenfoot.stop(); } Plays Sound Effect
Playing the Game The little-crab-4 version of this scenario shows the solution to this. It is a version of the project that includes all the functionality we have discussed so far: worms, lobsters, keyboard control, and sound (Figure 3.4).