Mail Archives: djgpp/2005/07/20/19:00:23
gary smith wrote:
>
> Help.
>
> I'm working on a breakout game and want to implement an exploding brick
> routine,
> where if one brick is hit by the ball, all the adjacent bricks will blow up,
> and all the bricks adjacent to the adjacent bricks will blow up, etc. Like
> a chain reaction.
>
> I have the brick positions stored in a structure and my collision detection
> routine gives me the number of the brick hit by the ball. I also have a
> structure holding the positions of each brick and weather it is enabled or
> not.
>
> I can easily work out which bricks are adjacent to the brick hit by the
> ball, it's when I try to work out which bricks are adjacent to the adjacent
> bricks etc, that I run into problems.
> The bricks are arranged on screen as shown below. They are numbered from 0
> to 220, with 13 bricks in each row. Is there a simple way of doing this.
>
> 0 1 2 3 4 5 6 7 8 9 10 11 12
> 13 14 15 16 17 18 19 20 21 22 23 24 25
> 26 27 28 29 30 31 32 33 34 35 36 37 38
> xxxxxxxxxxxx
> xxxxxxxxxxxx
> xxxxxxxxxxxx
> 208 209 xxxxxxxxxxxxxxxxxxxxxxxxxxxx220
Yup. Make a two dimensional array of bricks, where each brick has
a field called marked. Start from the originally struck brick, and
mark all its neighbors. When marking a neighbor, also mark its
neighbors. You mark neighbors by varying the x and y indices of a
brick by +1 or -1. Don't forget to check that such a position is
still in the array.
You can do it with one recursive procedure. You need to limit the
recursion depth to 2.
struct brick {
int marked;
};
struct brick wall[XDIM][YDIM];
/* initialize */
for (x = 0; x < XDIM; x++)
for (y = 0; y < YDIM; y++) wall[x][y].marked = 0;
Make the data format resemble the real thing. Things become much
clearer.
void markneighbors(int x, int y, int maxdepth)
{
int dx, dy;
if (maxdepth--) {
for (dx = -1; dx <= 1; dx++)
for (dy = -1; dy <= 1; dy++) {
if (/* check borders, do nothing if one passed */)
else {
wall[x + dx][y + dy].marked = 1;
if (dx || dy)
markneighbors(x + dx, y + dy, maxdepth);
}
}
}
}
--
Chuck F (cbfalconer AT yahoo DOT com) (cbfalconer AT worldnet DOT att DOT net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
- Raw text -