so i am trying to generate a random number of worms, choosing between 8-10 (so 8,9, & 10) when the world starts.
Here is my code I am using so far. Although I feel like "static int numWorms = Greenfoot.getRandomNumber(minWorms-maxWorms); is incorrect, from it not working.
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 | public class CrabWorld extends World { static int xDimension = 800 ; // X Dimension of our World static int yDimension = 800 ; // Y Dimension of our World static int minWorms = 8 ; static int maxWorms = 10 ; static int numWorms = Greenfoot.getRandomNumber(minWorms - maxWorms); /** * Create the crab world (the beach). Our world has a size * of xDimension x yDimension cells, where every cell is just 1 pixel. */ public CrabWorld() { super (xDimension, yDimension, 1 ); // Populuate with a Crab in the the center of my world int crabXPosition = xDimension/ 2 ; int crabYPosition = yDimension/ 2 ; // Add my crab populateCrab(crabXPosition, crabYPosition); // Add some worms populateWorms(numWorms); } /** * Place a crab in the world at xPosition and yPosition */ private void populateCrab( int xPosition, int yPosition) { addObject( new Crab(), xPosition, yPosition); } /** * Place specified number of worms randomly in the world */ private void populateWorms( int numberOfWorms) { int loopNumber = 0 ; while (loopNumber < numberOfWorms) { // add a worm to the world, at a random location addObject( new Worm(), Greenfoot.getRandomNumber(xDimension), Greenfoot.getRandomNumber(yDimension)); loopNumber++; } } } |