I think I can get my random food generator to work (without creating an infinite loop) if I can get a loop to contstantly change a variable every couple of seconds or so. That way I can circle through my if statement without change it to a while and creating an infinite loop. I could also limit food production using and AND condition in my if statement.
Here's what I have so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public void addFood(){ if (snake.move= true ){ //This is supposed to be the condition for generating more food. It should create food only if there needs to be more food in the world (no more than 2 food items, but 4 is acceptable because of the way this code is made). I can't get this to be a while statement or it won't work right. while (Greenfoot.getRandomNumber( 100 ) < 20 ){ //This creates cherries 20% of the time int x=Greenfoot.getRandomNumber( 500 ); int y=Greenfoot.getRandomNumber( 500 ); addObject( new cherry(), x, y); } while (Greenfoot.getRandomNumber( 100 ) < 15 ){ //This creates apples 15% of the time int x=Greenfoot.getRandomNumber( 500 ); int y=Greenfoot.getRandomNumber( 500 ); addObject( new apple(), x, y); } while (Greenfoot.getRandomNumber( 100 ) < 10 ){ //This creates pizza 10% of the time int x=Greenfoot.getRandomNumber( 500 ); int y=Greenfoot.getRandomNumber( 500 ); addObject( new pizza(), x, y); } while (Greenfoot.getRandomNumber( 100 ) < 5 ){ //This creates bread 5% of the time int x=Greenfoot.getRandomNumber( 500 ); int y=Greenfoot.getRandomNumber( 500 ); addObject( new bread(), x, y); } } |