lundi 30 avril 2018

How do I randomise specific colours on Processing

I am looking for some help to randomise specific colours on Processing using the Array Function. For example I only want the colours to be a specific red, blue, yellow and green. How to I ensure that when each single letter bounces off the wall, one of the colours appears on that specific letter?

Below is my code, really appreciate the help.

String word = "bounce";
char[] letters;
ArrayList<Letter> letterObjects;
boolean ballFall = false;
PFont font;
color randColour = color (0);
Letter l;

void setup () {
    size (500, 500);
    pixelDensity(displayDensity());
    textAlign(CENTER);
    textSize(30);
    letters = word.toCharArray();
    letterObjects = new ArrayList<Letter>();
    font = createFont("AvenirNext-Medium", 50);
    textFont(font);

    //iterate over the letter array
    //for each letter create a new object
    //add this object to an ArrayLost
    for (int i = 0; i<letters.length; i++) {
        char currentLetter = letters[i];
        float currentPosition = i * 30;
        letterObjects.add(new Letter(currentLetter, 180 + currentPosition, height/2));
    }
}

void draw () {
    background(255);

    for (Letter l : letterObjects) {
        l.display();
    }

    if (ballFall == true) {
        for (Letter l : letterObjects) {
            l.move();
            l.bounce();
        }
    }
}

void mouseClicked() {
    ballFall = true;
}

Letter class

class Letter {
    char character;
    float x, y;
    float xSpeed, ySpeed;
    float distance;

    Letter(char _c, float _x, float _y) {
        character = _c;
        x = _x;
        y = _y;

        //x = random(width);
        //y = random(height);
        xSpeed = random(1, 3);
        ySpeed = random(1, 3);
    }

    void move () {
        x += xSpeed*2;
        y += ySpeed*2;
    }

    void bounce () {
        if (x<0 || x>width) {
            xSpeed *=-1;
            randColour = color (random(255), random (255), random(255));
        }

        if (y<0 || y>height) {
            ySpeed *=-1;
            randColour = color (random(255), random (255), random(255));
        }
    }

    void display () {
        fill(randColour);
        text(character, x, y);
    }
}




Aucun commentaire:

Enregistrer un commentaire