How to code different colors?

Hi there, I am a very new to coding and I'm unsure how I can make a balloon (ellipse) be a different color of either red, blue or green every time the program is run. Here is a sample of what I am doing:

... Balloons randomBalloons() {

Balloons loon = new Balloons();

loon.x = random(50, 400);

loon.y = random(100, 200);

loon.dy = -3;

loon.diam = 60;

loon.fillColor = color(255, 0, 0) || color(0, 255, 0) || color(0, 0, 255);

//I cant seem to use the || OR to have it be red, green or blue

return loon;

any help will be appreciated thank you!

Tagged:

Answers

  • edited August 2017

    You've got some strange ideas about syntax going on here.

    The || operator is logical operator:

    if( x == 4 || y == 5 ) { 
    

    Here, the if statement's contents are only execute if x is 4 OR y is 5.

    What you want is to pick a random color from three colors. Since you have three colors to pick from, we enumerate the choices and then pick a random number - then use the color associated with that number:

    int r = int( random(3) );
    if( r == 0 ){
      fill( 255, 0, 0 );
    } else if ( r == 1 || r == 2 ){
      fill( 0,0,255 );
    }
    

    Here, the fill color is set to red if a 0 is the random number picked. otherwise it is set to blue if the random number picked was 1 or 2.

  • Thank you for the help!

Sign In or Register to comment.