# My Breakout Clone is Insane

• 12-14-2005
Grantyt3
My Breakout Clone is Insane
This seems really strange to me. I've tried over and over to fix it myself, but I just can't see what the problem is. I'm making a breakout clone, you know where you control the paddle and you hit the ball and try to break all the blocks without the ball hitting the bottom of the screen. The problem is, when the ball hits one of the blocks, it destroys the block it hit, but it also destroys the block 1 row below and 10 columns to the left. These are the functions that have to do with displaying and destroying the blocks
Code:

```void TestBallBlock() {         for(int i = 0; i < 14; i++)         {                 bx = 43*i + 18;                 for(int k = 0; k < 9; k++)                 {                         by = 43*k + 18;                         if(blocks[k][i] == 1)                         {                         //Check if ball has collided with block                         if(DetectCollision(ball,ballx,bally,block,bx,by))                                         {                                                 KillFunction(1,k,i);                                         }                         }                 }         } } bool DetectCollision(BITMAP *A, int ax, int ay, BITMAP *B, int bx, int by) {         int aright = ax + A->w;         int abottom = ay + A->h;         int bright = bx + B->w;         int bbottom = by + B->h;     return !(aright < bx || bright < ax || abottom < by || bbottom < ay); } void DrawBlocks() {         //Draw all the blocks         for(int i = 0; i < 14; i++)         {                 bx = 43*i + 18;                 for(int k = 0; k < 9; k++)                 {                         by = 43*k + 18;                         if(blocks[k][i] == 1)                         {                                 draw_sprite(buffer,block,bx,by);                                                                                 }                 }         } } void KillFunction(int kf, int k, int i) { //Eventually there will be more of these once I make more types of blocks         switch (kf)         {         case 1:                 blocks[k][i] = 0;                 balldy = -balldy;                 break;         } }```
[/I]I know it's a lot to look over, but I would really appreciate it if someone would take the time to figure this out for me. It's probably something really obvious that I missed. Tell me if you have an idea or if you think I should give more information or code. Thanks!
• 12-14-2005
JaWiB
Not sure if this is your problem but this jumps out at me:
Code:

`if(blocks[k] == 1)`
Isn't blocks a two-dimensional array?
• 12-14-2005
Grantyt3
Oops, why didn't I catch that? That's definately messed up. I'll fix that and see if it makes a difference.
• 12-14-2005
Grantyt3
OH wait, it's not like that in the actual CPP file. I think it got taken out because it thought I was trying to italisize something. But it's not actually like that so that's not it.
• 12-14-2005
SlyMaelstrom
Mmmkay, well why don't you post the exact code so we don't have to debug blindly.
• 12-14-2005
Grantyt3
No, that is the exact code. I copy and pasted, but it took the [i] part out because it thought I was trying to italisize.
• 12-15-2005
dwks
You can post [i] as [i[u][/u]].
• 12-15-2005
Grantyt3
There, it's fixed.
• 12-15-2005
Daved
I don't see anything wrong. Are you sure that ballx and bally are always non-negative? Is blocks declared as int blocks[9][14] (normally you would have the outer loop with i iterate through the first dimension, so that might be a source of error)?

Does this always happen? Is it always 1 row down and 10 columns left? What about when it hits column 3, does that mean a block in column 7 is also killed, or does it have to be like 12 and 2? can you give examples of exactly which block gets hit and which two blocks get killed?

If you have access to a debugger, that would help you immensely. Put a breakpoint in the KillFunction to verify that it is only called once, and if it is called a second time, then that will help you see where the problem is. Or step through the DetectCollision method to find out when it is detecting a collision.
• 12-18-2005
Grantyt3
ballx and bally are always positive. I'm confused: You make an array like blocks[x][y] right? But don't you access the information like blocks[y][x]? This always happens. Actually, I counted wrong, it's 1 below and 9 to the left. If column 3 is hit, a block in column 12 gets destroyed(that is if you start counting the columns at 0) If block [10][5] is hit, then that block and [1][6] get destroyed.
• 12-18-2005
Grantyt3
Nevermind, I fixed it.