Hi guys, I've made my own crab scenario but I've a problem. When my crab eats a burger, he's supposed to enter in a "godMode" for 5 seconds and he's supposed to be able to eat Lobsters during that time.
But in fact when he eats a burger he can't be eaten by Lobsters but he can't eat them, I don't know why.
Here's my code:
Thank you for helping me :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; private int wormsEaten; private int burgerEaten; private int lobsterEaten; public static int godMode; public int time = 15 ; /** * Create a crab and initialize its two images. */ public Crab() { image1 = new GreenfootImage( "crab.png" ); image2 = new GreenfootImage( "crab2.png" ); setImage(image1); wormsEaten = 0 ; burgerEaten = 0 ; lobsterEaten = 0 ; godMode = 0 ; } /** * Act - do whatever the crab wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkKeypress(); move(); lookForWorm(); switchImage(); lookForBurger(); lookForLift(); } /** * Alternate the crab's image between image1 and image2. */ public void switchImage() { if (getImage() == image1) { setImage(image2); } else { setImage(image1); } } /** * Check whether a control key on the keyboard has been pressed. * If it has, react accordingly. */ public void checkKeypress() { if (Greenfoot.isKeyDown( "left" )) { turn(- 10 ); } if (Greenfoot.isKeyDown( "right" )) { turn( 10 ); } } /** * Check whether we have stumbled upon a worm. * If we have, eat it. If not, do nothing. If we have * eaten eight worms, we win. */ public void lookForWorm() { if (canSee(Worm. class ) ) { eat(Worm. class ); getWorld().addObject( new Worm(), Greenfoot.getRandomNumber( 500 ) + 30 , Greenfoot.getRandomNumber( 500 ) + 30 ); Greenfoot.playSound( "slurp.wav" ); wormsEaten = wormsEaten + 1 ; if (wormsEaten == 5 ) { getWorld().addObject ( new Hamburger(), Greenfoot.getRandomNumber( 500 ) + 30 , Greenfoot.getRandomNumber( 500 ) + 30 ); wormsEaten = 0 ; } } } public void lookForBurger() { if (canSee(Hamburger. class )) { eat(Hamburger. class ); burgerEaten = burgerEaten + 1 ; godMode = 1 ; getWorld().addObject( new Timer(), 280 , 10 ); } } public void lookForLift() { if (canSee(Lift. class )) { Greenfoot.stop(); } } public void lookForLobster() { if (canSee(Lobster. class ) && godMode == 1 ) { eat(Lobster. class ); lobsterEaten = lobsterEaten + 1 ; if (lobsterEaten == 3 ) { getWorld().addObject ( new Lift (), 280 , 280 ); } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | public class Lobster extends Animal { /** * Do whatever lobsters do. */ public void act() { turnAtEdge(); randomTurn(); move(); lookForCrab(); } /** * Check whether we are at the edge of the world. If we are, turn a bit. * If not, do nothing. */ public void turnAtEdge() { if ( atWorldEdge() ) { turn( 17 ); } } /** * Randomly decide to turn from the current direction, or not. If we turn * turn a bit left or right by a random degree. */ public void randomTurn() { if (Greenfoot.getRandomNumber( 100 ) > 90 ) { turn(Greenfoot.getRandomNumber( 90 )- 45 ); } } /** * Try to pinch a crab. That is: check whether we have stumbled upon a crab. * If we have, remove the crab from the game, and stop the program running. */ public void lookForCrab() { if ( canSee(Crab. class ) && Crab.godMode == 0 ) { eat(Crab. class ); Greenfoot.playSound( "au.wav" ); Greenfoot.stop(); } } } |