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

2021/2/7

Can I add objects to the world with a for() loop?

BlueHand BlueHand

2021/2/7

#
I have a bouncing ball program that also has the balls change color. On initial start up, there are 5 balls with a random number. Even-numbered balls start as green and odd-numbered balls must randomly be assigned a color that's not green. Balls must be assigned a random rotation between 0 and 360 degrees and must be added at random locations. Is this possible?
rocket770 rocket770

2021/2/7

#
Not sure how your ball class works with colour, but here's how it would probably go.
for(int i = 0; i <=5;i++){
     int rand = greenfoot.getRandomNumber(10)
     if(rand % 2 == 0) { // we got an even number
          // add greenball or whatever 
     } else if (rand % 2 == 1) { // we got an odd number
         // add otherball 
     }
}
Depending on how you've set up your ball class, changing its Color and Rotation could be done in a few ways. Your constructor could look like this however
 public Ball(int rotation, Color color){
        GreenfootImage img = new GreenfootImage(50,50);
        img.fillOval(0,0,img.getWidth(), img.getHeight());
        img.setColor(color)
        img.setRotation(rotation)
        setImage(img);
    }
Although, having a perfect circle means the rotation wont actually effect what it looks like. With this adding a ball would be as simple as:
getWorld().addObject(new Ball(Greenfoot.getRandomNumber(360), Color.GREEN),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight());
danpost danpost

2021/2/7

#
Actually, no parameters are required for the Ball constructor. Rotation and color can be determined in the constructor itself. The Ball class will need a method for setting a new color anyway, which the constructor can call.
danpost danpost

2021/2/7

#
rocket770 wrote...
GreenfootImage img = new GreenfootImage(50,50);
img.fillOval(0,0,img.getWidth(), img.getHeight());
img.setColor(color)
Color needs set prior to fill operation. Lines 2 and 3 need swapped.
having a perfect circle means the rotation wont actually effect what it looks like.
True. However, forward direction is determined by rotation.
BlueHand BlueHand

2021/2/16

#
If I use images for my ball colors, like: setImage("ball"+imgNum+".png"); How much will the code change? (the green balls are called ball2)
danpost danpost

2021/2/16

#
BlueHand wrote...
If I use images for my ball colors, like: setImage("ball"+imgNum+".png"); How much will the code change? (the green balls are called ball2)
It would only change the details -- not the general format.
BlueHand BlueHand

2021/2/20

#
So what can I do? So far my even and odd balls are the same. I'm not sure how to make it so that even balls will show the ball2 image and the odd balls show other images (ball1, ball3, ball4, ball5, ball6, and ball7)
for(int i = 1; i <= 5; i++)
        {
            int rand = Greenfoot.getRandomNumber(10);
            if(rand % 2 == 0) { // we got an even number
                Ball ball = new Ball(Greenfoot.getRandomNumber(360));
                addObject(ball,Greenfoot.getRandomNumber(520),Greenfoot.getRandomNumber(320));
            } else if (rand % 2 == 1) { // we got an odd number
                Ball ball = new Ball(Greenfoot.getRandomNumber(360));
                addObject(ball,Greenfoot.getRandomNumber(520),Greenfoot.getRandomNumber(320)); 
            }
        }
danpost danpost

2021/2/20

#
To add balls, you only need this:
for (int i=0; i<5; i++)
{
    int x = Greenfoot.getRandomNumber(520);
    int y = Greenfoot.getRandomNumber(360);
    addObject(new Ball(), x, y) ;
}
The ball constructor can take care of everything else:
public Ball()
{
    turn(Greenfoot.getRandomNumber(360));
    int rand = Greenfoot.getRandomNumber(10);
    if (rand%2 == 0) setImage(ball2);
    else
    {
        GreenfootImage[] images = { ball1, ball3, ball4, ball5, ball6, ball7 };
        setImage(images[Greenfoot.getRandomNumber[images.length]);
    }
}
BlueHand BlueHand

2021/2/21

#
Thanks, but I notice that something is up with the last parentheses of:
setImage(images[Greenfoot.getRandomNumber[images.length]);
It says " ']' expected "
danpost danpost

2021/2/21

#
BlueHand wrote...
Thanks, but I notice that something is up with the last parentheses of:
setImage(images[Greenfoot.getRandomNumber[images.length]);
It says " ']' expected "
Correction:
setImage(images[Greenfoot.getRandomNumber(images.length)]);
You need to login to post a reply.