

This method makes the crab turn left (-4 degrees) or right (4 degrees) depending on the key that is pressed.Įnding the Game From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Create another method called checkKeyPress which is called from the act method. Exercise 3.19: Remove the Random Turning and the Turn at Edge coding. To keep a copy use Save a Copy As from the Scenario menu.
GREENFOOT CHAPTER 2 WINDOWS
From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Greenfoot automatically saves classes and scenarios when their windows are closed.

Greenfoot has method to check if a key on the keyboard has been pressed.Keyboard Control From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Does the crab manage to eat all the worms before it is caught by a lobster. Exercise 3.13: Copy the act method from the Crab class to the Lobster class replacing all references to Worm with Crab Exercise 3.14 Place a crab, three lobsters and many worms into the world. From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Use ifstatement to determine if crab sees worm – condition true then crab eats worm if ( canSee(Worm.class) ) Īdding Lobster Actor From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Lobsters like to eat crabs Exercise 3.10: Add new class (Lobster) to your scenario. Both methods require parameter that is a class i.e.First method checks if crab sees worm – bumps into worm.In Documentation view in the Animal class we see two methods: boolean canSee( clss) Returns true if we can see an object of class 'clss' right where we are void eat( clss) Try to eat an object of class 'clss'.What do you observe? What do the worms do? What happens when a crab meets a worm?Įating Worms From Introduction to Programming with Greenfoot by Michael Kölling Used with permission From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Exercise 3.7: Add some worms to your world. Do they all turn at the same time or independently? Why? From Introduction to Programming with Greenfoot by Michael Kölling Used with permissionĪdding New Actor (Worm) From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Worm is an animal (is a relationship) Class names in Java should always start with a capital letterĪdding New Actor (Worm) cont.

Exercise 3.6: Try running the scenario with multiple crabs in the world.
GREENFOOT CHAPTER 2 CODE
Exercise 3.5: Modify code so crab turns either right or left by up to 45 degrees.Exercise 3.4: Replace the turn(5) with random turn 45 between 0 and 44.Exercise 3.3: Experiment with different probabilities for turning.2 = 2 result is true != not equal to i.e. 2 > 33 result is false =greater than or equal to (not less than)i.e. Results in true or false greater than i.e.Relational Operators From Introduction to Programming with Greenfoot by Michael Kölling Used with permission getRandomNumber belongs to class Greenfoot.static int getRandomNumber(int limit).Methods belonging to classes have static in their method signature (definition).Methods Belong to Objects or Classes From Introduction to Programming with Greenfoot by Michael Kölling Used with permission Parameter 20 will randomly generate a number from 0 to 19 (one less than the parameter value).Greenfoot.getRandomNumber(20) – in Greenfoot class.

