I want to add 25 objects at random locations. They need to be in the range of Greenfoot.getRandomNumber(5) + 2.
for(int i=0; i < 25; i++) { int randX = Greenfoot.getRandomNumber(5) + 2; getWorld().addObject(whateverObjectYouNeedToAdd, randX, whateverY); }
int[][] objsGrid = { {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5} }; for (int loopCt = 0; loopCt < 5; loopCt++) { for (int myCt = 0; myCt < 5; myCt++) { int x = Greenfoot.getRandomNumber(5); int y = Greenfoot.getRandomNumber(5); while (objsGrid[x][y] != 5) { x = Greenfoot.getRandomNumber(5); y = Greenfoot.getRandomNumber(5); } objsGrid[x][y] -= myCt + 1; } } for (int y = 0; y < 5; y++) { for (int x = 0; x < 5; x++) { switch(objsGrid[x][y]) { case 0: addObject(new obj1(), x + 2, y + 2); break; case 1: addObject(new obj2(), x + 2, y + 2); break;
public void act() { timeUntilIShouldAddRandomObjects--; if(timeUntilIShouldAddRandomObjects == 0) { addRandomObjects(); } //whatever your original act() method had }
for(int i=0; i<numOfRows; i++) { for(int j=0; j<numOfCols; j++) { ObjectToAdd objToAdd = new ObjectToAdd(); getWorld().addObject(objToAdd, startX + i*spaceBetweenObjects, startY + j*spaceBetweenObjects); int rand = Greenfoot.getRandomNumber(amountOfVariation); objToAdd.setLocation(objToAdd.getX() + rand, objToAdd.getY() + rand); } }
int[] maxObjs = { 4, 5, 6, 3, 7}; int[][] objNums = {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5}, {5, 5, 5, 5, 5} }; for (int myObj = 0; myObj < 5; myObj++) { for (int myCt = 0; myCt < maxObjs[myObj]; myCt++) { int x = Greenfoot.getRandomNumber(5); int y = Greenfoot.getRandomNumber(5); while (objNum[x][y] != -1) { x = Greenfoot.getRandomNumber(5); y = Greenfoot.getRandomNumber(5); } objsGrid[x][y] = myCt; } }
if(l < 2) { cw.addObject(new Light(),ints[l]*64,ints2[l]*64); ints[l] = 0; ints2[l] = 0; } else if(l < 8) { cw.addObject(new Wire(),ints[l]*64,ints2[l]*64); ints[l] = 0; ints2[l] = 0; } else if(l < 12) { cw.addObject(new Resistor(),ints[l]*64,ints2[l]*64); ints[l] = 0; ints2[l] = 0; } else if(l < 15) { cw.addObject(new Diode(),ints[l]*64,ints2[l]*64); ints[l] = 0; ints2[l] = 0; } else if(l <= 25) { cw.addObject(new EmptyCircuitArea(),ints[l]*64,ints2[l]*64); ints[l] = 0; ints2[l] = 0; }