Public Group

# Random Numbers and Arrays

This topic is 2763 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites

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?

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites

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.

##### Share on other sites
Thank you so very much for that. I finally get it. You sir are an excellent teacher.

##### Share on other sites
My final question is, would I use one array for all the blocks or a separate array for each?

##### Share on other sites

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. ^^

##### Share on other sites
One last question, how would I make a block drop randomly?

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
18
5. 5

• 33
• 13
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
632567
• Total Posts
3007109

×