vendredi 26 février 2016

Java random color to blocks

So I am programming the game breakout in Java and I already got an array with bricks, but I want to give the brick (or rows of bricks) random colors. I have the possibility of 4 different colors; red, green, blue, yellow. But with my code shown below, I only get the different colors when I re-open my window and game. Can someone help me to give random colors to the blocks?

public void prepareBlocks() {
    int spacing = Breakout.BLOCKSPACING_Y;

    Random rand = new Random();
    int  n = rand.nextInt(4) + 1;
    Color colour = new Color(n);

    if (n==1){
        colour = Color.red;
    } if (n==2){
        colour = Color.yellow;
    } if (n==3){
        colour = Color.green;
    } if (n==4){
        colour = Color.blue;
    }

    lines[0] = new Line(0, colour);
    lines[1] = new Line(BLOCKHEIGHT+spacing, colour);
    lines[2] = new Line(BLOCKHEIGHT*2+2*spacing, colour);
    lines[3] = new Line(BLOCKHEIGHT*3+3*spacing, colour);
    lines[4] = new Line(BLOCKHEIGHT*4+4*spacing, colour);
    lines[5] = new Line(BLOCKHEIGHT*5+5*spacing, colour);

    for(int i = 0; i<lines.length; i++) {
        blockCount += lines[i].numberblocks;
        lines[i].fill();
    }
}




Aucun commentaire:

Enregistrer un commentaire