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

2015/12/7

Randomly adding food to my world at random times.

Poland Poland

2015/12/7

#
I am trying to create a Snake game where you eat food and get bigger. I have this code for adding food to the world, but it won't update in real time. The snake will eat food, but new food won't appear in place.
public void addFood(){
        while(numberOfObjects() < 7){ //no more than 6 food items at a time (will probably be updated to include snake body)
            if(Greenfoot.getRandomNumber(100) < 20){ //adds cherries 20% of the time
                int x=Greenfoot.getRandomNumber(500);
                int y=Greenfoot.getRandomNumber(500);
                addObject(new cherry(), x, y);
            }
            if(Greenfoot.getRandomNumber(100) < 15){ //adds apples 15% of the time
                int x=Greenfoot.getRandomNumber(500);
                int y=Greenfoot.getRandomNumber(500);
                addObject(new apple(), x, y);
            }
            if(Greenfoot.getRandomNumber(100) < 10){ //adds pizza 10% of the time
                int x=Greenfoot.getRandomNumber(500);
                int y=Greenfoot.getRandomNumber(500);
                addObject(new pizza(), x, y);
            }
            if(Greenfoot.getRandomNumber(100) < 5){ //adds bread 5% of the time
                int x=Greenfoot.getRandomNumber(500);
                int y=Greenfoot.getRandomNumber(500);
                addObject(new bread(), x, y);
            }
        }
danpost danpost

2015/12/7

#
Maybe you forgot that when the snake grows, the number of actors in the world increases. So, the value of 'numberOfObjects' is not found to be decreased when the snake eats. You need to use a different condition. If there is no more than one food ever in the world at any time and all your classes, 'cherry', 'apple', 'pizza' and 'bread', subclass, maybe called 'food', then 'getObjects(food.class).size' would be an appropriate value to check. If not, then maybe 'numberOfObjects()-getObjects(snake.class)' would be.
Poland Poland

2015/12/8

#
Actually, I haven't gotten that far yet. My next problem would be getting the snake body (a seperate actor) to follow the snake head. All I have so far is the snake head and food classes programmed. This code creates 6 food items in the world at start-up. My problem is: it won't replenish itself, so I'm left without food after the snake eats it all. I need regenerating food, and this code should create that. I'm thinking a timer would help.
danpost danpost

2015/12/8

#
I have quite a few snake demos here on the site. You can either use 'snake demo' in the search bar or just go to my page and "Show all" scenarios and then search for them there.
Poland Poland

2015/12/8

#
It's for a class project. I'd rather not plagerize. But I suppose researching is fine.
danpost danpost

2015/12/8

#
Poland wrote...
It's for a class project. I'd rather not plagerize. But I suppose researching is fine.
I have come across other ways of making the snake body play "follow the leader" more recently which are actually a simplified way of doing it. Although the method behind the behavior is not an accurate representation, it still appears to behave similarly. One method involves creating a body part every time the head moves, giving that new body part a timer value to represent how long it will remain in the world. The more body parts you have, the higher the initial timer value.
You need to login to post a reply.