Java random color to blocks

1.7k Views Asked by At

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();
    }
}
2

There are 2 best solutions below

0
On

Assuming you want every line the same colour you just insert the random before applying to the new line. After some code-cleaning this would look like:

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

    Random rand = new Random();

    Color colour;
    for(int i=0; i<lines.length; i++){
       switch(rand.nextInt(4) + 1){
           case 1: colour = Color.red; break;
           case 2: colour = Color.yellow; break;
           case 3: colour = Color.green; break;
           case 4: colour = Color.blue; break;
           default: colour = new Color(n);
       } 
       lines[i] = new Line(BLOCKHEIGHT*i+i*spacing, colour);
       blockCount += lines[i].numberblocks;
       lines[i].fill();
    }
}

EDIT Although for even prettier code you should save the colours in an array (even safer than my switch), as Stultuske suggested in his answer.

0
On
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);

no matter what you do here, all your blocks 'll get the same color.

A few possible refactorings:

Don't work with hardcoded values for i, ... be more dynamic in your code:

private int getRandomIndex(){
  Random rand = new Random();
  return rand.nextInt(colors.length); // no need for + 
}

private Color[] colors = {Color.red, Color.yellow, Color.green, Color.blue};

This way, you don't need to add an if ... each time you want to add a color, just add it in the array. Not to mention, it shortens the code. You'll need to figure out how to add the Blockheight and such correctly, though.

// ....
  for ( int i = 0; i < lines.length; i++ ){
    lines[i] = new Line(BLOCKHEIGHT + " everything else you need here", colors[getRandomIndex()];
    }

The problem you have now, is that you use the same Color instance for all your lines.