Adding random behavior

advertisement
Programming One
Greenfoot – Chapter Three – Improving the Crab – more sophisticated programming
TOPICS
o Students will implement random behavior, keyboard control, and sound into their Greenfoot
program.
CONCEPTS
o dot notations, random numbers, defining methods, and comments
Adding random behavior
In our programs previously our objects basically walked straight and turn at the edge of the world. We
can change that by adding random behavior to make more realistic. In this section we will have the crab
randomly turn at various points in the execution of the program.
The Greenfoot environment has a method written to produce random numbers. The method is called
getRandomNumber, it expects a parameter ( the ( ) part of the method), that will specify the limit of the
number. The method will then produce a random number between 0 and the limit number provided in the
parameter. ------ Example ---- Greenfoot.getRandomNumber (20) -- Actually will give us numbers 0 to 19,
because the number20 is actually the limit number.
The notation used in the method call is called dot notation. Greenfoot.getRandomNumber( ) --- see
Greenfoot has provided us with the random method, so we have to call it. This method is not a part of the
class Crab or Animal, but provided by Greenfoot, so we have to call it that way.
Methods that belong to the class itself are marked with the keyword static at the beginning of the
method signature. Static means it does not change.
Note the grey box on page 29 at the very top ---When using the if statement, we use symbols or operators
when writing the control expression….the operators are defined below…
< …………………… less than
>……………. greater than
<=…………………less than or equal to >=…………greater than or equal to
== equal to
!=…….not equal tol
The source code in Code block 3.1 on page 29 will have the crab turn a little in 10 percent of its steps
if( Greenfoot.getRandomNumber(100) < 10)
(
turn(5);
}
1
ADDING WORMS
In this section you will be creating a new subclass of the class Animal. You will be creating the subclass
Worm.
EATING WORMS
We will now see if our crab can eat the worms. The crab will eat the worm but it has to see it first. It
can only see it when it runs right into it. We use methods defined in the Animal class. In the source code in
code block 3.2 on page 33 we use if statements and method calls to make our crabs eat the worms. Some of
the classes use Boolean return types. Remember this will return a value of true or false.
CREATING NEW METHODS
In this section we will make our program more readable. We are dividing our program up into more
methods. At the top of the methods you will see lines of code…
/**
Check whether..
If we have…
*/
The above our comments or notes the programmer writes in the program. These lines of code are ignored by
the compiler or environment. They are strictly there for notes and to make the program more readable.
Remember we have to call our method to make it execute. Take a look at the code structure in code
block 3.3. By us dividing up our program it did not change what the program will do, but made it more
readable.
We continue to break our program up into more sections by splitting up our methods.
ADDING A LOBSTER
We add a new subclass Lobster to make our program more interesting. Again we are making our
program more interesting and breaking it up into more methods and subclasses.
KEYBOARD CONTROL
We now add in behavior to make it more like a game. We add keyboard control to give the user
interaction. Greenfoot has a method called isKeyDown
static boolean isKeyDown(String key)
2
Note the return type is Boolean so it will return a true or false value. The parameter has the word
String in it. This means it is expecting a word in “ “ . Every key on the keyboard has a name. The left cursor
key is called “left.”
if(Greenfoot.isKeyDown(“left”) )
{
//do nothing
}
if(Greenfoot.isKeyDown(“left”) )
{
turn(-4);
}
ADDING SOUND
We can call a Greenfoot method that will help us to play a sound.
Greenfoot.playSound(“Slurp.wav”);
playSound is the name of the method we are calling from Greenfoot environment and the parameter expects a
string – see “Slupe.wav”
3
Download