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

2020/5/8

How do I spawn apples randomly?

Ahmed780 Ahmed780

2020/5/8

#
75% of chance to get a green apple and 25% of chance to get a yellow apple (appleCategory 1 for green apple, appleCategory 2 for yellow Apple).
private void prepare()
    {
        Bowl bowl =  new  Bowl();
        addObject(bowl, 400, 500);
        bowl.setLocation(400, 540);
        /* Generate apples for 20 screens*/
        int i = 0;
        while (i < 12000) {
            int xPos = 40 + 60 * Greenfoot.getRandomNumber(10);
            /* get random number 1 or 2 for adding apples (appleCategory 1 for Green Apple, appleCategory 2 for Yellow Apple)*/
            int appleCategory = Greenfoot.getRandomNumber(2) + 1;
            addObject( new  Apple(appleCategory), xPos,  - i);
            i = i + 60;
        }

Apple calss *****************************************************************************************************
public class Apple extends SimulationActor{
    /* (World, Actor, GreenfootImage, Greenfoot and MouseInfo)*/
    public int appleColor = 0;

    /**
     * 
     */
    public Apple(int v_appleColor)
    {
        setRotation(Greenfoot.getRandomNumber(720));
        if (v_appleColor == 1) {
            setImage("greenApple.png");
            appleColor = 1;
        }
        else if (v_appleColor == 2) {
            setImage("yellowApple.png");
            appleColor = 2;
        }
        if (Greenfoot.getRandomNumber(1) == 1) {
            setImage("greenApple.png");
        }
       
    }

    /**
     * 
     */
    public void act()
    {
        super.act();
        if (getY() > 630) {
            position.setY(position.getY() + 300.0);
        }
    }
}

danpost danpost

2020/5/8

#
Ahmed780 wrote...
How do I spawn apples randomly? 75% of chance to get a green apple and 25% of chance to get a yellow apple (appleCategory 1 for green apple, appleCategory 2 for yellow Apple) << Code Omitted >>
In general, for a 25% chance that something will happen, the following process would be needed:
int pctChance = Greenfoot.getRaandomNumber(100);
boolean willHaapen =  pctChance < 25;
Ahmed780 Ahmed780

2020/5/8

#
Is this correct?
 int i = 0;
while (i < 12000) {
            int xPos = 40 + 60 * Greenfoot.getRandomNumber(100);
            /* get random number 1 or 2 for adding apples (appleCategory 1 for Green Apple, appleCategory 2 for Yellow Apple)*/
            int appleCategory = Greenfoot.getRandomNumber(2)< 25;
danpost danpost

2020/5/8

#
Ahmed780 wrote...
Is this correct? << Code Omitted >>
In your last post, you have '60' times some random number up to '100', making a value up to '6000 for an x-coordinate (that part did not need any attention; so I do not know why you messed with it. Another thing I see is that you are placing the apples at negative valued y-coordinates by using '-i' in the addObject line. I am guessing your world is 800x600. If so, I think this is what you need:
int xPos = 40+Greenfoot.getRandomNumber(720);
addObject(new Apple(2-Greenfoot.getRandomNumber(100)/75), xPos, i/10);
You need to login to post a reply.