|
Best way to find the eleminated blocks? |
Recorder
Member #6,860
February 2006
|
I'm making a game where a rows of blocks rises from the bottom of the screen and you have to click on an area of 3 or more blocks of the same color and they disappear. You loose if a block reaches the top row. I'm sure you've all played a game like this. What I'm having touble with is finding the blocks in the array to be removed. Lets say x are blocks of the same color, if I click on one they all should disappear, but I'm not sure of a good way to do this. _ _ _ _ _ My only idea is: I'm not necessarily looking for code. Just wondering if ther is an easier way. Seems like this my be more than I need to do. |
Richard Phipps
Member #1,632
November 2001
|
Well, if all blocks move up at the same speed and are alligned in a grid then: 1. Draw those blocks to a temporary bitmap. |
Recorder
Member #6,860
February 2006
|
Thanks Richard, but that's not exactly what I'm having a problem with. I'm not sure how to find which blocks need to be removed based only on the position of the one block I clicked on. I can check if the blocks to the top, bottom, left, and right of the one I clicked on have the same color. But then I have to check the one surrounding those and so on. |
Richard Phipps
Member #1,632
November 2001
|
That's why you can use floodfill, it will mark all touching blocks of the same colour. |
Shawn Hargreaves
The Progenitor
April 2000
|
Or you can just do your own floodfill algorithm to avoid messing around with a temporary copy of the data. Very simple to do this recursively:
That will be terribly inefficient and use lots of stack space if the grid is very large, but I guess yours is only going to be 10x10 or at most maybe 30x30 or so? In which case this naiive implementation is no problem at all. To make it more efficient you can use your own stack structure rather than relying on recursion, and also change it to work in horizontal line spans rather than just single pixels (do a rapid scan left and right from each point, then only bother to floodfill into the line spans above and below that resulting span). I think Foley & Van Damme gives the standard algorithm for this, or you could look at how the Allegro implementation works. |
Carrus85
Member #2,633
August 2002
|
Recorder - This is easily solved using a recursive algorithm. Example:
Of course, this algorithm is basically the exact same thing as floodfill. (Beware, there are probabily syntax errors and such in that code, but I'm too lazy to fix them right now ) Edit: DOH! Beaten.
|
Recorder
Member #6,860
February 2006
|
Ooooh, I didn't realize floodfill would take care of the adjacent block too. Can you see the light bulb above my head. Thanks alot. I'm glad I asked. |
|