Thread: My Breakout Clone is Insane

  1. #1
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55

    Unhappy 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!
    Last edited by Grantyt3; 12-15-2005 at 03:50 PM.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    Oops, why didn't I catch that? That's definately messed up. I'll fix that and see if it makes a difference.

  4. #4
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    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.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Mmmkay, well why don't you post the exact code so we don't have to debug blindly.
    Sent from my iPadŽ

  6. #6
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    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.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can post [i] as [i[u][/u]].
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    There, it's fixed.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  10. #10
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    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.

  11. #11
    C++ Newbie
    Join Date
    Aug 2005
    Posts
    55
    Nevermind, I fixed it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my breakout clone
    By lambs4 in forum Game Programming
    Replies: 12
    Last Post: 09-03-2003, 02:16 PM
  2. Turman-Nate (BreakOut Clone) 0.01
    By cMADsc in forum Game Programming
    Replies: 5
    Last Post: 04-27-2003, 02:46 PM
  3. BreakOut clone problem
    By cMADsc in forum Game Programming
    Replies: 4
    Last Post: 01-20-2003, 02:24 PM
  4. My new and improved breakout clone!
    By Leeman_s in forum Game Programming
    Replies: 9
    Last Post: 01-14-2003, 06:43 PM
  5. Best breakout clone ever made.
    By funkydude9 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-15-2002, 10:25 PM