Sign in to follow this  

Tetris Help!

This topic is 1110 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

So I'm writing a Tetris game, mostly for practice, and I have come to a wall. Let me start by saying I'm not using any frameworks, I'm doing this all in C# using Visual Studio and I know I'm probably not doing this the best way however i am determined to get this finished before I start doing more research and using a framework. 
 
So my Form has a panel in it that I have put a picture box for every possible Tetris block. They all start with the visibility flag set to false, and I have two arrays I have created, one that is for the gameMap and the other is for the current tile in play, as follows
 

int[,] gameMap = new int[,] {

 

{ 0, 0, 0, 0, 0 },

{ 0, 0, 0, 0, 0 },

{ 0, 0, 0, 0, 0 },

{ 0, 0, 0, 0, 0 },

{ 0, 0, 0, 0, 0 } }

 

int[,] currentTile = new int [,] {

 

{ 0, 0, 0, 0, 0},

{ 0, 0, 0, 0, 0},

{ 0, 0, 0, 0, 0},

{ 0, 0, 0, 0, 0},

{ 0, 0, 0, 0, 0} }

 

This has been slightly simplified, but you get the idea. I have an array index for every picture box and I have a timer running that is constantly checking the gameMap and currentTile arrays to see if any of the positions are set to 1 and if they are then I set the tile that maps to this index to visible = true;

 

The problem I am having is dropping the tiles. I have set up another timer to run in the background and drop the till in the current tile array, and this is where I am having an issue. I had written a line of code that is a for loop inside of a for loop looking for an index set to 1 and it sets this to 0 and sets the index below it to 1(ie if its currentTile [0,5], it sets this to 0 and sets currentTile[1,5] to 1). I quickly figured out that this runs through the whole loop and ends up sending the tile to the bottom of the board at the end of the for loop.

 

I had tried a few more ideas to get it to drop the 1's in the array down 1 level, but nothing I have tried has worked. Does anyone have any ideas, or will it just not work this way? I don't want to try this any other way at the moment until I get a little more comfortable with the ins and outs of classes. Any help would be appreciated! 

Share this post


Link to post
Share on other sites

This is not directly a sorting problem. When copying rectangular areas (Edit: inplace), the loop direction has to account for the copy direction. If you want to move your tiles one row down, the loop should start at the last row and count backwards.

 

 

PS: If you really need sorting, don't implement a bubble sort, use the readily availble sorting functions (Array.Sort, List<T>.Sort or LINQ's OrderBy).

Edited by unbird

Share this post


Link to post
Share on other sites


If you want to move your tiles one row down, the loop should start at the last row and count backwards.

 

Yes! I should have stuck with the KISS principle. Thanks! Stuff like this is exactly why I'm practicing and forcing myself to take on these problems. The more I practice and work on projects, the better I will get at not making such easy mistakes. 

 


If you really need sorting, don't implement a bubble sort, use the readily availble sorting functions (Array.Sort, List.Sort or LINQ's OrderBy).

 

I do not feel the need for a sort for this game, I cannot think of any benefit it will have to how I have this implemented. And I do know, and have used, the Array.Sort method before. 

 

Thanks again for the help!

Share this post


Link to post
Share on other sites

This topic is 1110 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