Sign in to follow this  

Random Numbers and Arrays

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
[quote name='jniblick' timestamp='1298248546' post='4776842']
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?
[/quote]

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 this post


Link to post
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.
[code]
private void RandomNum()
{
if (bloxFalling == false)
{
Random randnum = new Random();
bloxRand = randnum.Next(6);
}
}
[/code]

The code directly below this draws my block.
[code]
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;
}


}
[/code]

The code directly below this checks to see if my block has passed a certain point and, if it has, makes the block stop.
[code]
if (blox[bloxRand].BloxY > 338)
{
blox[bloxRand].BloxY = 338;
bloxFalling = false;
}
[/code]

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 this post


Link to post
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 this post


Link to 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 this post


Link to post
Share on other sites
[quote name='jniblick' timestamp='1298424922' post='4777790']
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?
[/quote]

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

At "array[i][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[i][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 this post


Link to post
Share on other sites
[quote name='jniblick' timestamp='1298473628' post='4777974']
My final question is, would I use one array for all the blocks or a separate array for each?
[/quote]

You'll need 2. One for the play area (where the blocks are falling) and one for the falling block itself. ^^

Share this post


Link to post
Share on other sites
[quote name='jniblick' timestamp='1298556693' post='4778437']
One last question, how would I make a block drop randomly?
[/quote]

Make a constant 3D array. The first dimension would be which block you want. EX (in C#, tell me if you're using something else):

[code]const bool[][][] BLOCKS = new bool[2][5][5](
{
{
{ false, false, true, false, false},
{ false, false, true, false, false},
{ false, false, true, false, false},
{ false, false, true, false, false},
{ false, false, true, false, false}
},


{
{ false, false, false, true, false},
{ false, false, false, true, false},
{ false, true, true, true, false},
{ false, true, false, false, false},
{ false, true, false, false, false}
}
}
);
[/code]

Obviously, you need to add more blocks to the list. Just change [2] to how ever many blocks you have.

Then, just make a random number from 0 to how ever many blocks you have minus 1. Copy that block to your falling block array.

[code]bool[][] fallingBlock = BLOCKS[random];[/code]

You'll need to to add the rotated block in too. I'd make the first half of the list one rotation, and the second half the other rotation. Generate a random number between 0 and the number of blocks there are (not counting rotations) Then when rotating, set fallingBlock = to BLOCKS[number of blocks + index of the random number earlier]

Tell me if you don't get something. ^^

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this