We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello Community! I am trying to create a gameboard for a small game I started making, but I hav encountered a problem, I couldn't solve yet. The gameboard ground consists of rectangles, filled with images ( 38x21 ). I want each rectangle to be filled with a randomized image, which is then saved and the same board redrawn in the methode draw() . So far it is always filling the rectangles with always new randomized images, which makes the gameboard look all weird (constant color changes). Here is the code I use to draw it:
draw() {
for (int j = 0; j < 21; j ++) {
squarePositionY[j] = squarePositionYVariable;
squarePositionYVariable = squarePositionYVariable + 48;
squarePositionXVariable = 48;
for (int i = 0; i < 38; i++) {
squarePositionX[i] = squarePositionXVariable;
squarePositionXVariable = squarePositionXVariable + 48;
groundimagerandom = random(0, 100);
if ( groundimagerandom > 40) {
image(groundLevelOne, squarePositionX[i], squarePositionY[j]);
} else if ( groundimagerandom < 41) {
image(groundLevelOneRotten, squarePositionX[i], squarePositionY[j]);
}
}
}
}
Thanks for a short answer!
Answers
The draw method draws a single frame. So in your draw method you are creating a random number with
groundimagerandom = random(0, 100);
EVERY frame.
groundimagerandom should be calculated once in setup.
See this topic for more info on setup and draw
If I have
groundimagerandom = random(0, 100);
calculated only once in setup(), the ground will not look weird, but only have one colour and not different ones, like it is supposed to (see my code).Then you will need to remember more than one number! In fact, you will need to remember which image each grid rectangle is using. Here is a simple example of how to do this using different colors:
you will have to store the random value for each tile.
alternatively, keep a count, incrementing it in the inner loop and use randomSeed(count) before each call to random(0, 100);