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

2018/4/3

Bubbles

danteinferno danteinferno

2018/4/3

#
Hello i need to populate the first 6 rows of my world with 19 random color bubbles each row. The bubbles should be red, green, yellow or blue. I have no idea how to do it so any help its well appreciated. The bubbles must have a radius of 10, thus a diameter of 20
danpost danpost

2018/4/3

#
Your world is foreign to us. Show code with attempt to build rows of balloons.
danteinferno danteinferno

2018/4/3

#
danpost wrote...
Your world is foreign to us. Show code with attempt to build rows of balloons.
public class MyWorld extends World
{

 
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 400x650 cells with a cell size of 1x1 pixels.
        super(400, 650, 1);
          
    }
    
    
    
}
danteinferno danteinferno

2018/4/3

#
danpost wrote...
Your world is foreign to us. Show code with attempt to build rows of balloons.
I don't have any idea that is why i dont have any attempt. I feel its with two loops but I don't know how to materialize my idea
danteinferno danteinferno

2018/4/3

#
danpost wrote...
Your world is foreign to us. Show code with attempt to build rows of balloons.
Now i have this
public class Bubble extends Actor
{
  
    
    //  a constant indicating the intended radius of a bubble
    public static final int RADIUS = 10;
    
    // YOU WILL LIKELY WANT TO ADD METHODS (INCLUDING CONSTRUCTORS) HERE
    public Bubble()
    {
        blueBubble();
        redBubble();
        greenBubble();
        yellowBubble();
    }    
    
    private void blueBubble()
    {
        int diameter = (int) (2*RADIUS);
        GreenfootImage img = new GreenfootImage(diameter, diameter);
        img.setColor(Color.BLUE);
        img.fillOval(0,0 , diameter, diameter);
        setImage(img);
    }    
    
    private void redBubble()
    {
        int diameter = (int) (2*RADIUS);
        GreenfootImage img = new GreenfootImage(diameter, diameter);
        img.setColor(Color.RED);
        img.fillOval(0,0 , diameter, diameter);
        setImage(img);
    }
    
    private void greenBubble()
    {
        int diameter = (int) (2*RADIUS);
        GreenfootImage img = new GreenfootImage(diameter, diameter);
        img.setColor(Color.GREEN);
        img.fillOval(0,0 , diameter, diameter);
        setImage(img);
    }
    
    private void yellowBubble()
    {
        int diameter = (int) (2*RADIUS);
        GreenfootImage img = new GreenfootImage(diameter, diameter);
        img.setColor(Color.YELLOW);
        img.fillOval(0,0 , diameter, diameter);
        setImage(img);
    }
Vercility Vercility

2018/4/3

#
Use 2 for loops Their loop-variables are x and y starting at where ever your row edges are (assuming u fill the whole world they start at 0). They run to the edges of the rows (e. G World edges) and increase x/y by length or width divided by how many balloons there should be. Note that insufficient space and/or too large balloons will cause them to intersect Generate a random number in the inner loop and create either of the balloons depending on the number at the Current x/y values (which are incremented by the for loops) Edit : I don't know whether greenfoots image coordinates are the upper left corner or the center, if it's the center you can't start at 0 but at diameter/2
danteinferno danteinferno

2018/4/3

#
Vercility wrote...
Use 2 for loops Their loop-variables are x and y starting at where ever your row edges are (assuming u fill the whole world they start at 0). They run to the edges of the rows (e. G World edges) and increase x/y by length or width divided by how many balloons there should be. Note that insufficient space and/or too large balloons will cause them to intersect Generate a random number in the inner loop and create either of the balloons depending on the number at the Current x/y values (which are incremented by the for loops) Edit : I don't know whether greenfoots image coordinates are the upper left corner or the center, if it's the center you can't start at 0 but at diameter/2
Where should I put these loops?
danpost danpost

2018/4/3

#
I do not know where you got that code for the Bubble class; but, you will always create yellow bubbles with it (no other color). The following will produce random colored bubbles:
import greenfoot.*;

public class Bubble extends Actor
{
    private static final int RADIUS = 10;
    private Color[] COLORS = { Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW };
    
    public Bubble()
    {
        int diameter = 2*RADIUS;
        GreenfootImage image = new GreenfootImage(diameter, diameter);
        image.setColor(COLORS[Greenfoot.getRandomNumber(4)];
        image.fillOval(0, 0, diameter, diameter);
        setImage(image);
    }
}
With this, all you need to do in your world class is add the bubbles into the world (using a double for loop).
danteinferno danteinferno

2018/4/3

#
danpost wrote...
I do not know where you got that code for the Bubble class; but, you will always create yellow bubbles with it (no other color). The following will produce random colored bubbles:
import greenfoot.*;

public class Bubble extends Actor
{
    private static final int RADIUS = 10;
    private Color[] COLORS = { Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW };
    
    public Bubble()
    {
        int diameter = 2*RADIUS;
        GreenfootImage image = new GreenfootImage(diameter, diameter);
        image.setColor(COLORS[Greenfoot.getRandomNumber(4)];
        image.fillOval(0, 0, diameter, diameter);
        setImage(image);
    }
}
With this, all you need to do in your world class is add the bubbles into the world (using a double for loop).
int numberOfActors = 19; //
        int spacing = 19; // numberOfActors;
        for (int i = 0; i < numberOfActors; i++)
        {
           addObject(new Bubble(), Greenfoot.getRandomNumber(200), 280);
        }
I currently have this but some of the bubbles are on top of each other
danteinferno danteinferno

2018/4/3

#
Now I have this. It compiles but does not run
int numberOfActors = 19; //
        double incrementor = 400/19;
        for (int i = 0; i < numberOfActors; incrementor = incrementor)
        {
           addObject(new Bubble(),(int)incrementor, 40);
        }
danpost danpost

2018/4/3

#
danteinferno wrote...
Now I have this. It compiles but does not run << Code Omitted >>
"incrementor = incrementor" (line 3) does not change its value. I was about to post the following when I noticed your last post:
danteinferno wrote...
<< Code Omitted >> I currently have this but some of the bubbles are on top of each other
That is because you are placing them randomly along the horizontal. You added a spacing variable; but, its value is (1) not used; and (2) not large enough. To evenly space the bubbles, you would divide the world into nineteen vertical strips and place a balloon on each strip. The width of a strip can be used to center a bubble on a strip, but should not be used for the gap between bubbles (although it may seem logical to do so). After dividing the world into these strips, there could be a partial strip left over. Its width should be distributed among the other strips to even things out. The way to accomplish this is to multiply the width of the world by the number of the actor currently being place and then dividing by the number of actors to get where the strip for that actor starts at:
int numberOfActors = 19;
int offset = getWidth()/numberOfActors/2; // distance from edge to middle of a strip
for (int i=0; i<numberOfActors; i++)
{
    addObject(new Bubble(), getWidth()*i/numberOfActors+offset, 40);
}
You need to login to post a reply.