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

2011/11/17

I need help please.

1
2
cphizzlemunizzle cphizzlemunizzle

2011/11/17

#
I am using little crab 5 from the book senarios. My dad, who is an instructor at my school, bet me that i cant do these and he would be right. I was wondering if anyone would be willing to explain to me how you do this. I am a novice programmer and i am taking my dads class next semster and i really wanna be a head of the ball game. 1. Automatically populate the world with 40 Worms and 3 Crabs, all randomly placed. 2. Set the initial worms eaten (on the Crabs) to a random number between 5 and 10. 3. Each time a Crabs move it uses 1/10 of a Worm worth of energy. 4. Once a Crab has reached zero Worms eaten, it dies and disappears from the world. 5. The Crabs shall move in a random pattern (without key controls) and eat Worms. 6. Let the Crabs move through the world and eat Worms. 7. Stop the game once all the Worms have been eaten or all the Crabs have died.
darkmist255 darkmist255

2011/11/17

#
Well I'll just do a quick answer here, tell me if I need to elaborate on some things. My syntax will probably NOT be correct for it all, so just use this as a reference. 1. make a variable (wormCount) and make a class called addWorms() that says something like: if(wormCount < 40) { getWorld().addObject(new Worm, Greenfoot.getRandomNumber(400), Greenfoot.getRandomNumber(400)) } Just place "addWorms();" in the Act() class. 2. Just do something like wormsEaten = (Greenfoot.getRandomNumber(5) + 5); 3. Make it so that when he eats a worm it does something like this: if(wormEat) { wormEnergy = (wormEnergy + 10); } then under the Move() method: move() { move(4); wormEnergy = (wormEnergy - 1); } 4. Call the method "checkDeath()" in the act() method: public void checkDeath() { if(wormEnergy =< 0) removeObject.this; <--- I forget the syntax for removing, that's close I think } 5. Just do something like Act() { if(Greenfoot.getRandomNumber(100) > 10) //about a 10% chance that it will change direction { setRotation(Greenfoot.getRandomNumber(359) } move(4) } 6. Just add the eatWorm() method to crab 7. In the eatWorm() and checkDeath() methods, make something like wormCount = (wormCount - 1) crabAlive = (crabCount - 1) then add a new method somewhere called checkLose() public void checkLose() { if(wormCount =< 0 || crabCount =< 0) // the || means "or" Greenfoot.stop(); } Hope that helps :D.
cphizzlemunizzle cphizzlemunizzle

2011/11/17

#
thank you very much. i have 1 problem though, the if statement that goes if(wormCount =< 0 || crabCount =< 0) it states it is an illegal start of type?
darkmist255 darkmist255

2011/11/17

#
I thought I was wrong when writing, and I was correct about being wrong. it should be: if(wormCount <= 0 || crabCount <= 0) I wrote "=<" where it should be"<=" Picky picky Java :D.
cphizzlemunizzle cphizzlemunizzle

2011/11/17

#
thank you very much after i put that what needs to go in the {} brackets in the if statement
cphizzlemunizzle cphizzlemunizzle

2011/11/17

#
actually the removeObject.this comes up as not a statement
darkmist255 darkmist255

2011/11/17

#
Try getWorld().removeObject.this;
bourne bourne

2011/11/17

#
getWorld().removeObject(this);
darkmist255 darkmist255

2011/11/17

#
Yay I was right :D. I guess, logically removeObject is in the world API right?
bourne bourne

2011/11/17

#
Yep that is why you have to call getWorld() assuming it's an Actor
Royalblue64 Royalblue64

2011/11/17

#
The first 6 items on the list are actually very simple, but the 7th one is EXTREMELY difficult. I'm taking a highschool class in Greenfoot and I cant even do that. Maybe you should leave that out, i dont know anyone who can do that. sorry. but if you want, i could get a working program that does everything else any post the code.
darkmist255 darkmist255

2011/11/17

#
Well... "Greenfoot.stop();" would stop it. 7th is pretty easy if you just mean stop. If you mean exit, well I think that would be difficult if not impossible. But dude, a highschool greenfoot class nice, wish I had that. :D
danpost danpost

2011/11/17

#
For number 7: put in the act() method of the world class 'if (getObjects(Worm.class) == null || getObjects(Crab.class) == null) Greenfoot.stop();'. And for number 2: wormsEaten = Greenfoot.getRandomNumber(6) + 5; (the random return will be between 0 and 5) I think as far as energy goes, I would set wormEnergy = 10 * wormsEaten immediately following the setting of wormsEaten; and with each worm eaten during the run of the scenario, increment wormsEaten and add ten (10) to wormEnergy; then, on each move you only need to subtract one from the energy and check for zero energy state.
darkmist255 darkmist255

2011/11/17

#
"if (getObjects(Worm.class) == null)" Didn't know you could do that :0. I do now, thanks!
delmar delmar

2011/11/17

#
I think you guys are mixing something up here. You're confusing "explain to me how you do this" with "giving me the solution". If you really "wanna be a head of the ball game" you need to be able to do more than copy down the code. How about explaining them one by one?
There are more replies on the next page.
1
2