Breakout game - Exploding brick logic

This is a discussion on Breakout game - Exploding brick logic within the Java-Games forums in Other Technologies category; 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 ...

Go Back   Application Development Forum > Other Technologies > Java-Games

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 07-20-2005, 03:42 PM
gary smith
Guest
 
Default Breakout game - Exploding brick logic

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


Reply With Quote
  #2  
Old 07-20-2005, 04:30 PM
Nathan Mates
Guest
 
Default Re: Breakout game - Exploding brick logic

In article <42dea928$0$2381$ed2619ec@ptn-nntp-reader03.plus.net>,
gary smith <garymeganemma@hotmail.com> wrote:
>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 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.


Easy - do it recursively. Do something like this:

void ExplodeBrickAt(int x, int y)
{
if(!GetBrick(x,y))
return; // No brick here. Do nothing
if(!GetBrickExplodable(x,y))
return; // Brick can't explode. Do nothing
if(GetBrickExploding(x,y))
return; // This brick is already exploding. Do nothing

// If we got here, this brick can explode. Do so, set off neighbors

// Set flag that this is exploding
SetBrickExploding(x, y);

// Now, blow up neighbors.
if(x > 0)
ExplodeBrickAt(x - 1, y); // blow up left neighbor
if(x < BOARD_WIDTH)
ExplodeBrickAt(x + 1, y); // blow up right neighbor
if(y > 0)
ExplodeBrickAt(x, y - 1); // blow up upper neighbor
if(y < BOARD_HEIGHT)
ExplodeBrickAt(x, y + 1); // blow up lower neighbor
}

You should be able to figure out what the other functions I
mentioned but don't have a body do.

Nathan Mates

--
<*> Nathan Mates - personal webpage http://www.visi.com/~nathan/
# Programmer at Pandemic Studios -- http://www.pandemicstudios.com/
# NOT speaking for Pandemic Studios. "Care not what the neighbors
# think. What are the facts, and to how many decimal places?" -R.A. Heinlein
Reply With Quote
  #3  
Old 07-21-2005, 06:14 AM
Gerry Quinn
Guest
 
Default Re: Breakout game - Exploding brick logic

In article <42dea928$0$2381$ed2619ec@ptn-nntp-reader03.plus.net>,
garymeganemma@hotmail.com says...

> 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.


Basically you use a version of the same algorithm that is used for the
Dijkstra 'wavefront' pathfinding algorithm (one reason for knowing this
as well as A*, it has a number of related algorithms).

At each stage you have a list of bricks you have just found are
exloding, and you have some sort of marker that can tell whether bricks
exist and if so have been marked as exploding. At the start, the list
of 'found' bricks is just the original brick.

After you find the list of 'new' bricks, you repeat the process using
that, until there are no new bricks.

You can get a nice effect by setting a timer (or by carrying out the
algorithm in stages) so that there is a visible wait before each set of
bricks explode. This is an advantage of doing things non-recursively:

Call at intervals shorter than it takes an explosion to complete:

For all currently exploding bricks:
Set all adjacent normal bricks to exploding

That's it! Call it every frame/interval, if there are no exploding
bricks it will do nothing. Usually the interval will be quite small,
so the explosion will have swept across all connected bricks long
before the ball gets back up, but this is optional.

- Gerry Quinn


















Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 12:44 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.