Reputation: 1461
I'm trying to develop my first XNA game by my own hands, just looking the source of tutorials and thinking my own implementations and solutions. Currently, I'm making a bubble shooter game, and I'm drawing the bubbles in their respective position.
The thing is, I have implemented two types of bubbles. The program chooses which type to draw though a random generator (0 or 1 means blue or red), and depending on the result, the selected type is drawn into the screen. This approach is not working, and I have exhausting my search resources. The code is the following
for (int colBubCounter = 0; colBubCounter < maxVerticalBubNumber / 2; colBubCounter++)
{
for (int rowBubCounter = 0; rowBubCounter < maxHorizontBubNumber; rowBubCounter++)
{
Rectangle bubbleDrawRectangle = new Rectangle(initDrawCoordX, initDrawCoordY, bubbleWidth, bubbleHeight);
//Randomizamos el tipo de burbujar a dibujar ( 0 = blue, 1 = red)
bubbleType = randomGenerator.Next(0, 1);
if (bubbleType == 0) spriteBatch.Draw(blueBubbleSprite, bubbleDrawRectangle, Color.White);
else if (bubbleType == 1) spriteBatch.Draw(redBubbleSprite, bubbleDrawRectangle, Color.White);
//Cada vez que dibujamos uno, corremos la coordenada a dibujar en el otro ciclo en 10 pixeles
initDrawCoordX += bubbleWidth;
}
initDrawCoordX = 0;
initDrawCoordY += bubbleHeight;
}
With
System.Random randomGenerator = new System.Random();
I'm not using classes or anything more than raw code, because I'm taking the incremental step development, once this is ready, I'll do the same thing with classes and other fancy thing.
Thanks for your help, and please let me know if I did something wrong in this question, it's my first here in StackOverflow. :)
Upvotes: 1
Views: 339
Reputation: 35227
The randomGenerator.Next(0, 1);
will always return 0, since maxValue (upper) bound is exclusive. You need to use randomGenerator.Next(0, 2)
to make zeroes and ones.
Upvotes: 1
Reputation: 14163
You are using the random method Next() with 2 overloads telling it the range is between 0 and 1 meaning it will always return 0
this is because the upper boundry (in this case 1) is excluse, as in its from 0 to 1 but not including 1... so its from 0 to 0.
try bubbleType = randomGenerator.Next(2);
or bubbleType = randomGenerator.Next(0, 2);
and a hint for the random class, try to make it class level, and use only 1 object to get a more random number generation (creating a new object often tends to give the same numbers more often as its pseudo random numbers)
Upvotes: 1