Random Numbers and Arrays

Started by
11 comments, last by slynk 13 years, 1 month ago
I have set up a system in a Tetris styled game in which, the block that will fall is determined by a random number. The random number is passed into a variable and the variable inherits from an array containing all the necessary information for the variable. The block is then then set to fall down the the screen and when it reaches a certain point on the Y axis, stop. From there a new block will drop. However when the block stops it disappears and a new block falls. What can I do to keep the block on screen and make a new block fall?
Advertisement

I have set up a system in a Tetris styled game in which, the block that will fall is determined by a random number. The random number is passed into a variable and the variable inherits from an array containing all the necessary information for the variable. The block is then then set to fall down the the screen and when it reaches a certain point on the Y axis, stop. From there a new block will drop. However when the block stops it disappears and a new block falls. What can I do to keep the block on screen and make a new block fall?


There are so many ways to answer this but it'd be easier if we could see some code.

Some ideas:

Have an array of fallen blocks that you just copy to.


or

Don't view the game as a collection of odd shaped blocks, but of single blocks together. Have a grid for the game, made up of single blocks. It could be a 2D array of bool types. When the block can't go down due to hitting something, set the blocks to true.

| f f f f T |
| f f f f T |
| f f f T f |
| f f f T f |

See?
I'm going to post my source code below.

The code directly below this is simply meant to generate a random number.

private void RandomNum()
{
if (bloxFalling == false)
{
Random randnum = new Random();
bloxRand = randnum.Next(6);
}
}


The code directly below this draws my block.

private void DrawBlox()
{
bloxFalling = false;

if (bloxFalling == false)
{
spriteBatch.Draw(blox[bloxRand].bloxTexts, new Vector2(blox[bloxRand].BloxX, blox[bloxRand].BloxY), null, Color.White, 0, new Vector2(blox[bloxRand].bloxRotation, 0), 1, SpriteEffects.None, 0);
bloxFalling = true;
}


}


The code directly below this checks to see if my block has passed a certain point and, if it has, makes the block stop.

if (blox[bloxRand].BloxY > 338)
{
blox[bloxRand].BloxY = 338;
bloxFalling = false;
}


My problem is that the block disappears and a new one is drawn, rather than the original block staying as well as having a new block drawn.
You only ever draw one block at a time and when you finish dropping one block you don't store it anywhere to remember to keep drawing it. You replace the current block with a new block and start drawing that new block. slynk covered some possible techniques, although I think the first solution won't work for a Tetris clone very well when you want to start clearing lines. The second technique is the simplest. You will need to completely rethink how you do this though as you do several things that just won't work very well. Like using a texture to represent an entire block instead of having a 4x4 block being made of a set of squares that are either On or Off. I suggest looking at some tutorials online to help explain how you might implement tetris as it can get quite involved for a forum post.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Would someone please explain the concept of using an array to store my blocks. I'm trying to figure out how I would go about doing this, but I can't really grasp the idea of going from numbers or boolean variables to 2d images. Would I be using a spritebatch and storing the value in a variable?

Would someone please explain the concept of using an array to store my blocks. I'm trying to figure out how I would go about doing this, but I can't really grasp the idea of going from numbers or boolean variables to 2d images. Would I be using a spritebatch and storing the value in a variable?


Have a 2D array. Have an image for a single block. Loop through the array (for...i){ (for...j){

At "array[j] == true"

Draw the block image at:

new Vector2(i * image.Width + (X value for the left side of the grid), j * image.Height + (Y value for the top of the grid));

To test if a row has been filled:

loop through the array

At "array[j] == true"

increment a counter by one

At the end if:

counter == 5 (or how ever many blocks there are in a row) delete the row and move everything down.
Thank you so very much for that. I finally get it. You sir are an excellent teacher.:lol:
My final question is, would I use one array for all the blocks or a separate array for each?

My final question is, would I use one array for all the blocks or a separate array for each?


You'll need 2. One for the play area (where the blocks are falling) and one for the falling block itself. ^^
One last question, how would I make a block drop randomly?

This topic is closed to new replies.

Advertisement