X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f Message-ID: <42DED06A.31FA52BD@yahoo.com> From: CBFalconer Organization: Ched Research http://cbfalconer.home.att.net X-Mailer: Mozilla 4.75 [en] (Win98; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.os.msdos.djgpp Subject: Re: Breakout Game - Exploding brick logic. References: <42dea833$0$3580$ed2619ec AT ptn-nntp-reader01 DOT plus DOT net> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 75 Date: Wed, 20 Jul 2005 22:53:33 GMT NNTP-Posting-Host: 12.76.134.177 X-Complaints-To: abuse AT worldnet DOT att DOT net X-Trace: bgtnsc05-news.ops.worldnet.att.net 1121900013 12.76.134.177 (Wed, 20 Jul 2005 22:53:33 GMT) NNTP-Posting-Date: Wed, 20 Jul 2005 22:53:33 GMT To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com 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. USE worldnet address!