random colours that don't update?

I’m a complete beginner with monogame, and am making a breakout style game, with blocks that are random colours. Here’s my code in the draw function:

Random rand = new Random();
spriteBatch.Draw(testRect, new Rectangle(x * tileWidth, y * tileHeight, tileWidth, tileHeight), blockColours[rand.Next(0,blockColours.Length)]);

However when I do this, the blocks flash different colours because they are picking another random colour. How can I get them to stay the same.

You need to set the random colors one time at the start of the level. By putting it in the draw method you’re telling Monogame that every time you draw the image on the screen (which for 60Hz would be 60 times a second) to pick a new color.

So in your blockColors you need to have that match the same size and layout of your tiles.

Make blockColors a two dimensional array blockColors[,] with the maximum number of X and Y elements you need.

Would be:


spriteBatch.Draw(testRect, new Rectangle(x * tileWidth, y * tileHeight, tileWidth, tileHeight), blockColours[x,y]);

I would find a way to keep that as a feature for when someone is scoring high or clearing blocks fast!

1 Like