This site requires JavaScript, please enable it in your browser!
Greenfoot back
regba
regba wrote ...

2013/7/21

How to make a Crab each time it moves it uses 1/20 of a Worm worth of energy

1
2
3
4
regba regba

2013/7/21

#
I am new in programming. I have an assignment with 8 questions. I did the first four, by spending the last 48 hours by trying to figure it out, but I am clueless on the other four I should say the other 3. I really need help. I really did tried. I read similar questions in the forum, but it didn't help. Thank you very much. -Each time a Crab moves it uses 1/20 of a Worm worth of energy. -Once a Crab has reached zero Worms eaten, it dies and disappears from the world. -The Crabs shall move in a random pattern (without key controls) and eat Worms. (This I think I can do it) -Stop the game once all the Worms have been eaten or all the Crabs have died.
danpost danpost

2013/7/21

#
The easiest way is to give the crab 20 energy for each worm and remove 1 for each movement. Each time you remove 1, check for a zero value for dying. If no worms or no crabs in world, end game.
regba regba

2013/7/21

#
This is what I did Still do not work Need help. Thank you import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * *********************** REQUIREMENTS *************************************************** * * R3 -Set the initial worms eaten (on the Crabs) to a random number between 5 and 11. * R4 -If a Crab has more than 6 worms eaten, it cannot eat any more worms. * R5 -Each time a Crab moves it uses 1/20 of a Worm worth of energy. * R6 -Once a Crab has reached zero Worms eaten, it dies and disappears from the world. * R7 -The Crabs shall move in a random pattern (without key controls) and eat Worms. (I know this part) * R8 -Stop the game once all the Worms have been eaten or all the Crabs have died. * *******************************************************************************************/ /** * This class defines a crab. Crabs live on the beach. They like sand worms * (very yummy, especially the green ones). * * Version: 5 * * In this version, the crab behaves as before, but we add animation of the * image. */ public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; private int wormsEaten; //BA: this line satisfies requirement R3. private int moveCount = 0; /** * Create a crab and initialize its two images. */ public Crab() { image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png"); setImage(image1); wormsEaten = 0; wormsEaten= Greenfoot.getRandomNumber(7) +5; //this line satisfies requirement R3. Random number between 5-11. } /** * 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(); } /** * 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(-4); } if (Greenfoot.isKeyDown("right")) { turn(4); } } /** * 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) && wormsEaten < 7 ) //This code satisfies requirement R4 { eat(Worm.class); Greenfoot.playSound("slurp.wav"); wormsEaten = wormsEaten + 1; if (wormsEaten > 6) // This code satisfies requirement R4 { move(); //Greenfoot.playSound ("fanfare.wav"); } // if (wormsEaten == 8) { //Greenfoot.playSound("fanfare.wav"); //Greenfoot.stop(); } private void move() { move(1); // moveCount++; if (moveCount == 20) { moveCount = 0; // do whatever needs done } } } wormsEaten--; if (wormsEaten == 0) { getWorld().removeObject(this); } }
danpost danpost

2013/7/21

#
R3 takes what I said above out of the picture. Besides, it looks like you pretty much everything done. The last few statements (starting with 'wormsEaten--;') should be within the same 'if' block as 'moveCount = 0;', however (since this is something you want to do when the move count reaches 20).
moveCount = 0;
wormsEaten--;
if (wormsEaten == 0)
{
    getWorld().removeObject(this);
}
// final closing brackets here
regba regba

2013/7/21

#
Can you do a video tutorial with a similar exercise, because It seems like many people don't really get it. If you don't mind of course. That thing is tough! I'm still working on mine to see if it is going to work. Thank you.
regba regba

2013/7/21

#
So should I get rid under the public class Crab extends Animal of private int moveCount = 0; ?
Super_Hippo Super_Hippo

2013/7/21

#
Your crab starts with 5 to 11 "wormsEaten" which are like life points. If every move uses 1/20 of a Worm worth of energy of the crab, then it means that the crab loses one of these life points after 20 act circles, because the crab moves every frame. So what you have to do is that you decrease the life variable (wormsEaten) by one if the moveCounter reaches 20. Line 2 in danposts code. Your "private void move()" is inside "public void lookForWorm()", these should be separated. So actually you are almost done. Well, it is quite the same as danpost said, but for some reason, people sometimes understand it, if they hear it again and only a bit different.
regba regba

2013/7/21

#
Error Message move() in Crab cannot override move() in Animal Attempting to assign weaker access privileges; was public Here is the link for the error message: http://img829.imageshack.us/img829/2350/oykw.jpg
regba regba

2013/7/21

#
To super_hippo, I have separated private void move() but I still have an error. I have been working on that for a while now. I'd like to do teamviewer to share my screen so you can look at my work! private int moveCount = 0; private void move() <----error------- { move(1); // or a 'setLocation' statement moveCount++; if (moveCount == 20) { moveCount = 0; wormsEaten--; if (wormsEaten == 0) { getWorld().removeObject(this); } } } }
You can't make move() private after move() in Animal is public! If you're overriding a method, you must have the exact same modifier (that's what they're called) as the original method. Just change move to public.
regba regba

2013/7/21

#
That is the error below I have now after changing private to public. It worked for few seconds then here it is! java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:867) at Animal.canSee(Animal.java:87) at Crab.lookForWorm(Crab.java:102) at Crab.act(Crab.java:51) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
regba regba

2013/7/21

#
Here is the whole program: import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * * R1 -Automatically populate the world with 40 Worms randomly placed (using loops). * R2 -Automatically populate the world with 3 Crabs randomly placed( using loops). * R3 -Set the initial worms eaten (on the Crabs) to a random number between 5 and 11. * R4 -If a Crab has more than 6 worms eaten, it cannot eat any more worms. * R5 -Each time a Crab moves it uses 1/20 of a Worm worth of energy. * R6 -Once a Crab has reached zero Worms eaten, it dies and disappears from the world. * R7 -The Crabs shall move in a random pattern (without key controls) and eat Worms. * R8 -Stop the game once all the Worms have been eaten or all the Crabs have died. * */ /** * This class defines a crab. Crabs live on the beach. They like sand worms * (very yummy, especially the green ones). * * Version: 5 * * In this version, the crab behaves as before, but we add animation of the * image. */ public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; private int wormsEaten; // R3. /** * Create a crab and initialize its two images. */ public Crab() { image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png"); setImage(image1); wormsEaten = 0; wormsEaten= Greenfoot.getRandomNumber(7) +5; //R3 } /** * 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(); randomTurn();//R7 } public void randomTurn() { if (Greenfoot.getRandomNumber(100) < 10 ) // R7 { turn (Greenfoot.getRandomNumber(90)-45);//R7 } } /** * 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(-4); } if (Greenfoot.isKeyDown("right")) { turn(4); } } /** * 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) && wormsEaten < 7 ) // R4 { eat(Worm.class); Greenfoot.playSound("slurp.wav"); wormsEaten = wormsEaten + 1; if (wormsEaten > 6) // R4 { move(); //Greenfoot.playSound ("fanfare.wav"); } // if (wormsEaten == 8) { //Greenfoot.playSound("fanfare.wav"); //Greenfoot.stop(); } } } private int moveCount = 0; private void move() { move(1); // or a 'setLocation' statement moveCount++; if (moveCount == 20) { moveCount = 0; wormsEaten--; if (wormsEaten == 0) { getWorld().removeObject(this); } } } }
techgirl74 techgirl74

2013/7/21

#
cool pic, I feel like that baby right now. Sorry I can't help you. I am in the same boat!!
Your error happens when you haven't added the Actor to the world, or if you removed it and continued to do more. Make sure that you call any methods of the actor, while it's in the world, or make sure that removing the Actor is the last thing that happens (ex: removing the actor and then telling the actor to move, changed to telling the actor to move (or not at all) then removing the actor).
danpost danpost

2013/7/21

#
FlyingRabidUnicornPig wrote...
Your error happens when you haven't added the Actor to the world, or if you removed it and continued to do more. Make sure that you call any methods of the actor, while it's in the world, or make sure that removing the Actor is the last thing that happens (ex: removing the actor and then telling the actor to move, changed to telling the actor to move (or not at all) then removing the actor).
To be more to the point: An IllegalStateException error will occur if you try to use a method that require the actor to be in the world when, in fact, the actor is not in the world (either has not yet been added or has been removed from the world). These methods include, but are not limited to, 'getX()', getObjectAtOffset(int, int, Class)', 'getIntersectingObjects(Class)', 'getNeighbours(int, boolean, Class)', and 'move(int)'. Also, a NullPointerException will occur if you use 'getWorld().someWorldClassMethod()' if the actor has not been added or has been removed from the world. This does not however mean that you cannot still call methods and do things with the object while it is not in the world. Anything that does not require it to be in the world can still be processed (such as getting, altering, and changing its image or rotation),
There are more replies on the next page.
1
2
3
4