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

2018/10/21

Help with Snake Game

ananyagarg ananyagarg

2018/10/21

#
I am making a snake game on Greenfoot but with two types of food: blueberries and cherries. Cherries add only 5 points while blueberries add 10. I am trying to add a blueberry in the world for every 5 cherries eaten, but I am not sure how to implement that in Greenfoot. I have different classes for Blueberries and Cherries. This is the code in the eatFood method in the Snake head class:
public void eatFood(){
        if(isTouching(Cherries.class)){
            removeTouching(Cherries.class);
            cherriesEaten++;
            MyWorld world = (MyWorld)getWorld();
            Score score = world.getScore();
            score.addScore();
             if (cherriesEaten > 5){
                world.addBlueberries();
            }
            else{
                world.addCherries();
            }
        }
        if (isTouching(Blueberries.class)){
            removeTouching(Blueberries.class);
            blueberriesEaten++;
            MyWorld world = (MyWorld)getWorld();
            world.addBlueberries();
        }
    }
Any help would be appreciated!
danpost danpost

2018/10/21

#
The condition on line 8 does not match up with what you said needs to happen. That is "greater than 5" is not the same as "equal to a multiple of 5" (which is what you really want).
ananyagarg ananyagarg

2018/10/21

#
danpost wrote...
The condition on line 8 does not match up with what you said needs to happen. That is "greater than 5" is not the same as "equal to a multiple of 5" (which is what you really want).
How would you write a multiple of 5 in code? Would it be easier to use a for loop instead?
danpost danpost

2018/10/21

#
ananyagarg wrote...
How would you write a multiple of 5 in code? Would it be easier to use a for loop instead?
A for loop would be a terrible choice. What is the difference between a number that is a multiple of 5 and one that is not? -- the remainder being zero after dividing by 5. So if cherriesEaten "mod 5" ('%5') is equivalent to zero, add a blueberry.
ananyagarg ananyagarg

2018/10/21

#
danpost wrote...
ananyagarg wrote...
How would you write a multiple of 5 in code? Would it be easier to use a for loop instead?
A for loop would be a terrible choice. What is the difference between a number that is a multiple of 5 and one that is not? -- the remainder after dividing by 5. So if cherriesEaten "mod 5" ('%5') is equivalent to zero, add a blueberry.
Hi, thank you for the help. If you use the code above, it only works for the first 5 cherries but then doesn't continue to add cherries after the first blueberry is eaten
danpost danpost

2018/10/21

#
ananyagarg wrote...
If you use the code above, it only works for the first 5 cherries but then doesn't continue to add cherries after the first blueberry is eaten
Line 19 should add a cherry -- not a blueberry.
You need to login to post a reply.