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

2013/1/1

How do you make worms appear only some of the time?

1
2
LonelyCreeper LonelyCreeper

2013/1/1

#
I'm using the crab 5 scenario and was asked to modify the game. I am having some confusion with the populate world methods. For the project I need to make 10-20 worms appear at random locations in the beginning of the game. I also need to create a new class called red worms and make them appear one by one only 10% of the time at random locations. Do these have something to do with the populate methods? How do I add these? Please help!!!
danpost danpost

2013/1/1

#
If the red worms are only to "appear one by one", then this cannot be something to do with the populate methods because the populate method is called by the world constructor and it is run only once, at the time the world is instantiated.
LonelyCreeper LonelyCreeper

2013/1/1

#
Alright thanks for the tip. How can this be done though?
danpost danpost

2013/1/1

#
What method can you add that will be run multiple times?
LonelyCreeper LonelyCreeper

2013/1/1

#
I could add an addWorms() method. If that could work.
danpost danpost

2013/1/1

#
Still, it would have to be put in the world class and called from the 'act' method.
LonelyCreeper LonelyCreeper

2013/1/1

#
So what should I put in there?
danpost danpost

2013/1/1

#
You will need the following things: 1) Greenfoot.getRandomNumber(int) 2) new redWorm() 3) addObject(Actor, int, int) 4) 10% (which is 10 one-hundreths: 10/100); meaning you will need a '10' and a '100' 5) an 'if' statement 6) getWidth() and getHeight() (the dimensions of the world)
LonelyCreeper LonelyCreeper

2013/1/1

#
Thanks, but i dont quite get how the dimensions of the world could go into use here. And how do I make the worms come in one by one? (im new to greenfoot so i don't really know how to make things work.)
danpost danpost

2013/1/1

#
Well, the worms have to be placed in the world at random locations; which means a random x-coordinate and a random y-coordinate; and those coordinates must lay within world bounds. The 'if' statement (restricting the addition of worms using random chance of 10 percent of the time) will be executed once every act cycle; however, the rate of redWorm population will be much more than you would expect. A timer would probably be in order to prevent population explosion. With you being new and all, I feel this might be getting a little beyond you (about adding a timer). I am now wondering if you may have mis-understood the objectives in redWorm populating (or maybe I mis-understood). Is it that a redWorm is to appear for, say, 4 seconds and then not appear for 40 seconds; then appear again somewhere else for another 4 seconds -- something like that? I mean, that would considered appearing one by one ten percent of the time; would it not?
LonelyCreeper LonelyCreeper

2013/1/1

#
There is actually a timer that counts down from 3 minutes that I forgot to consider. So I guess that means a red worm appears every 18 seconds...
danpost danpost

2013/1/1

#
I would think they would have said 'every 18 seconds' instead of '10% of the time', if that were the case (but, what do I know!).
LonelyCreeper LonelyCreeper

2013/1/1

#
Oh well. Thank you so much for the help! Really appreciate t!
danpost danpost

2013/1/1

#
LonelyCreeper wrote...
I also need to create a new class called red worms and make them appear one by one only 10% of the time at random locations.
This statement is so ambiguous, it is hard to say what is wanted. I mean, it could even be interpreted as 90% of the time it should appear at a set location instead of a random one (which I am sure is not meant). The phrase 'only 10% of the time' could be applied to (1) chance of adding a redWorm (2) chance of appearing one by one (3) amount of time made visible, or (4) chance of random locating. Four can be eliminated immediately cause it would not make sense to use a specific location to place the redWorm 90 percent of the time. Two, likewise, can be eliminated as I doubt you would want to add more than one at a time. That leaves One and Three. If One is what was meant, then I would think that you would be adding a worm (whether red or not) occasionally, and then 10 percent of the time it should be a red one. If Three is what was meant, then would each one be visible for 18 seconds? would the total time for all redWorm objects total 18 seconds? there is too much unknown. In conclusion, One seems to be the most plausable, but the objectives are not explicit enough to be certain of this, either. If you are to add a worm back into the world each time a worm is eaten, then, definitely, One would be the case; and there would be a 10% chance that the new worm added to the world would be a red one.
LonelyCreeper LonelyCreeper

2013/1/1

#
It is pretty vague isn't it. Just to make it clearer, I'll just rephrase what the instructions were supposed to mean: every 18 seconds, a new red worm is to appear in the world at a random place. It just stays there, and new red worms just keep adding. I was a bit confused too, but this hopefully clears things up.
There are more replies on the next page.
1
2