- Home /
Shifting blocks to the right to remove empty spaces - C#
In the block matching game that I am creating I currently have the blocks falling upon elimination and because of this, there's a lot of leftover blocks and some empty spaces that could be filled with blocks and cause more matches to be available. How can I get the game to shift specific blocks over to the right in order to fill those spaces? I'll post the code I have currently that drops them down, now I just need them to shift to the right. I've also attached a screenshot of what's happening in the grid so that you can see the openings that the game is creating
public void applyGravity() {
// Go through every column
for (int y = 0; y < 10; y++) {
// Track empty positions in the colum
int lastEmptyPos = 0;
// Go through every row, starting from the bottom
for (int x = 0; x < 10; x++) {
// Check to see that the current block is not empty
if (grid[x, y].renderer.material.mainTexture != null) {
// Check for gaps
if (lastEmptyPos < x) {
// Drop the blocks down
grid[lastEmptyPos, y].renderer.material.mainTexture = grid[x, y].renderer.material.mainTexture;
grid[x, y].renderer.material.mainTexture = null;
}
// Since we have a block (regardless if we moved it), the tower must be +1 higher
lastEmptyPos++;
}
}
}
}
Clarify please - you want all blocks in all rows to move to the right if there's white space/empty to the right? Example say the red block in col 3, row0 should shift to the right? Should it then shift again because there are two white spaces? When it moves, the green block will now have white space to its right, should it shift? etc
What you described, yes, that is basically what I want. I want my loop to go through all the columns and rows and see if there's emptiness on the right and just "push" everything over to the right. So, for example, (from the right) the 1 red piece and 2 blue blocks would shift over to the right, etc.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Round Planets and Movement on them 2 Answers
Change Block Position In Block Matching Game C# 2 Answers
Multiple Cars not working 1 Answer
How to destroy blocks that are matching in color C# 2 Answers