• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Bricks For Brick Breaker Game

Kadafi

Newbie
Hi, I am making a simple brick breaker game. I have made the ball and the paddle and it works fine. I now want to add bricks to the top of the screen, could someone guide me in the right direction to do this, could I make an arraylist?
or could someone advise me on how this can be done, note that i want the bricks to be shown in the game and dissapear on collision with the ball (i know how to do the collision part already just need to display bricks at the top).

Thanks
 
Are you drawing on a canvas?

You could do something like this.

Code:
/*
* 1. Create a Rect to represent each brick
*/
List<Rect> bricks = new ArrayList<Rect>();

bricks.add(new Rect(0, 0, 50, 20));
bricks.add(new Rect(55, 0, 105, 20));

/* ... And so on for each brick you want to create. */

/*
* 2. Draw each brick
*/
onDraw()
{
    for(int i=0 ; i<bricks.size() ; i++)
    {
        Rect r = bricks.get(i);
        canvas.drawRect(r, paint);
    }
        
}


/*
* 3. Collision Detection
Iterate over all bricks and calculate if the ball has hit. If yes, remove the brick from the list.
*/

for(int i=0 ; i<bricks.size() ; i++)
{
    Rect r = bricks.get(i);
    if(intersect(ball, r))
        bricks.remove(r);
}
 
thanks for such a detailed response.
I kind of got it working, the only problem i have is that the ball i am using is not a rect (and is a bitmap) so i have used my own intersection method which works most of the time but not always.

Also for the bricks i would like to use an image file rather than coloring in a rectangle, is this possible?

Thanks
 
Sure you can use a Bitmap graphics for the bricks as well.

The only difference here is that Bitmaps don't have their position stored with them. Remember, with Rects, you only had to tell the canvas to draw the rect and it was drawn at the position you specified in the Rect constructor.

With Bitmaps however, you always have to tell the canvas where to draw the bitmap: canvas.drawBitmap(bitmap, left, top, paint). A simple solution would be to save the bitmap graphics and the position where to draw the bitmap in its own class:

Code:
class Brick
{
    /* Where the brick will b drawn */
    int top;
    int left;

    /* What to draw */
    Bitmap bitmap;
}

/*
* Once you have created a ArrayList<Brick>, draw each brick
* in the list.
*/
onDraw()
{
    for(Brick b : bricks)
        canvas.drawBitmap(b.bitmap, b.left, b.top, paint);
}
 
Hi, thanks I want to set the bitmap to an image from the r.drawable folder, how can i do that, ive done it elsewhere but its not working for this?
Thanks
 
Hi, thanks for that.
I am trying to make the brick be printed a certain number of times across the screen, and then go to the next line, then the next etc to make a wall.
what is the best way to do this, the way i am trying it working but seems very complex.

also what does Brick b:Bricks exactly do(i thought it goes through each item up to the size of the array but) i only have 10 items in the arraylist but it is printing it many more times.

Thanks
 
For initializing the bricks, a double-loop is the way to go. Something like this:
Code:
List<Brick> bricks = new ArrayList<Brick>();
Bitmap b = BitmapFactory.decodeResource(resources, id);

for(int 5 = 0 ; x < 160 ; x+=30)
{
    for(int  y= 0 ; y < 140 ; y+= 15)
    {
        bricks.add(new Brick(bitmap, x, y));
    }
}

One loop increses the x-coordinate of the brick, the other increases the y-coordinate. This is done only one during the initialization. During drawing, a simple loop that iterates over all bricks is enough

Code:
for(Brick b: bricks)
{
    // draw it
}

or the equivalent
Code:
for(int i = 0 ; i < bricks.size() ; i++)
{
    Brick b = bricks.get(i);
    // draw it
}
 
Hi, thanks I am trying that and it makes perfect sense but for some reason, now I am only getting one brick being drawn eventhough I am adding many more to the array.

Thanks
 
its fine ive fixed it for now, really appreciate all your help. I am going to try the collision detection with the bricks now.
 
my collision is not working, i am using the same thing i have done for the ball and the paddle, and just changed the paddle bits to the relevent bits of the bricks, but i am not getting any collision detection.?
here is the code i am trying:

this is used to call the method (from where i am updating the game physics)
Code:
 [B][COLOR=#7F0055][FONT=Consolas]for[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas]([/FONT][/COLOR][B][COLOR=#7F0055][FONT=Consolas]int[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas] i=0; i<[/FONT][/COLOR][COLOR=#0000C0][FONT=Consolas]arrBricks[/FONT][/COLOR][COLOR=black][FONT=Consolas].size(); i++){[/FONT][/COLOR]
      [B][COLOR=#7F0055][FONT=Consolas]if[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas](Brick_Collide([/FONT][/COLOR][COLOR=#0000C0][FONT=Consolas]arrBricks[/FONT][/COLOR][COLOR=black][FONT=Consolas], i) == 1){[/FONT][/COLOR]
      [COLOR=#0000C0][FONT=Consolas]ballDY[/FONT][/COLOR][COLOR=black][FONT=Consolas] = -[/FONT][/COLOR][COLOR=#0000C0][FONT=Consolas]ballDY[/FONT][/COLOR][COLOR=black][FONT=Consolas];    // to reverse the ball direction upon collision[/FONT][/COLOR]
      arrBricks.remove(i);
      [COLOR=black][FONT=Consolas]}[/FONT][/COLOR]
  [COLOR=black][FONT=Consolas]}[/FONT][/COLOR]
this is the method i am using to check if there is a collision (note that i couldnt seem to access the width and height of the bitmap in the array so i just used the width and height of the bitmap that was added to the array for each object as it is the same)

Code:
  [B][COLOR=#7F0055][FONT=Consolas]int[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas] Brick_Collide(ArrayList<Brick> arrBricks, [/FONT][/COLOR][B][COLOR=#7F0055][FONT=Consolas]int[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas] i) {[/FONT][/COLOR]
  
  [B][COLOR=#7F0055][FONT=Consolas]int[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas] left1, left2;[/FONT][/COLOR]
  [B][COLOR=#7F0055][FONT=Consolas]int[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas] right1, right2;[/FONT][/COLOR]
  [B][COLOR=#7F0055][FONT=Consolas]int[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas] top1, top2;[/FONT][/COLOR]
  [B][COLOR=#7F0055][FONT=Consolas]int[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas] bottom1, bottom2;[/FONT][/COLOR]
  
  [COLOR=black][FONT=Consolas]left1 = ([/FONT][/COLOR][B][COLOR=#7F0055][FONT=Consolas]int[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas])[/FONT][/COLOR][COLOR=#0000C0][FONT=Consolas]ballX[/FONT][/COLOR][COLOR=black][FONT=Consolas];[/FONT][/COLOR]
  [COLOR=black][FONT=Consolas]left2 = arrBricks.get(i).[/FONT][/COLOR][COLOR=#0000C0][FONT=Consolas]brickBitmapX[/FONT][/COLOR][COLOR=black][FONT=Consolas];[/FONT][/COLOR]
  [COLOR=black][FONT=Consolas]right1 = ([/FONT][/COLOR][B][COLOR=#7F0055][FONT=Consolas]int[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas])[/FONT][/COLOR][COLOR=#0000C0][FONT=Consolas]ballX[/FONT][/COLOR][COLOR=black][FONT=Consolas] + [/FONT][/COLOR][COLOR=#0000C0][FONT=Consolas]ball[/FONT][/COLOR][COLOR=black][FONT=Consolas].getWidth();[/FONT][/COLOR]
  [COLOR=black][FONT=Consolas]right2 = arrBricks.get(i).[/FONT][/COLOR][COLOR=#0000C0][FONT=Consolas]brickBitmapX[/FONT][/COLOR][COLOR=black][FONT=Consolas] + [/FONT][/COLOR][COLOR=#0000C0][FONT=Consolas]brickBitmap[/FONT][/COLOR][COLOR=black][FONT=Consolas].getWidth();[/FONT][/COLOR]
  [COLOR=black][FONT=Consolas]top1 = ([/FONT][/COLOR][B][COLOR=#7F0055][FONT=Consolas]int[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas])[/FONT][/COLOR][COLOR=#0000C0][FONT=Consolas]ballY[/FONT][/COLOR][COLOR=black][FONT=Consolas];[/FONT][/COLOR]
  [COLOR=black][FONT=Consolas]top2 = arrBricks.get(i).[/FONT][/COLOR][COLOR=#0000C0][FONT=Consolas]brickBitmapY[/FONT][/COLOR][COLOR=black][FONT=Consolas];[/FONT][/COLOR]
  [COLOR=black][FONT=Consolas]bottom1 = ([/FONT][/COLOR][B][COLOR=#7F0055][FONT=Consolas]int[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas])[/FONT][/COLOR][COLOR=#0000C0][FONT=Consolas]ballY[/FONT][/COLOR][COLOR=black][FONT=Consolas] + [/FONT][/COLOR][COLOR=#0000C0][FONT=Consolas]ball[/FONT][/COLOR][COLOR=black][FONT=Consolas].getHeight();[/FONT][/COLOR]
  [COLOR=black][FONT=Consolas]bottom2 = arrBricks.get(i).[/FONT][/COLOR][COLOR=#0000C0][FONT=Consolas]brickBitmapY[/FONT][/COLOR][COLOR=black][FONT=Consolas] + [/FONT][/COLOR][COLOR=#0000C0][FONT=Consolas]brickBitmap[/FONT][/COLOR][COLOR=black][FONT=Consolas].getHeight();[/FONT][/COLOR]
  
  [B][COLOR=#7F0055][FONT=Consolas]if[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas] (bottom1 < top2) [/FONT][/COLOR][B][COLOR=#7F0055][FONT=Consolas]return[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas](0);[/FONT][/COLOR]
  [B][COLOR=#7F0055][FONT=Consolas]if[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas] (top1 > bottom2) [/FONT][/COLOR][B][COLOR=#7F0055][FONT=Consolas]return[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas](0);[/FONT][/COLOR]
  
  [B][COLOR=#7F0055][FONT=Consolas]if[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas] (right1 < left2) [/FONT][/COLOR][B][COLOR=#7F0055][FONT=Consolas]return[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas](0);[/FONT][/COLOR]
  [B][COLOR=#7F0055][FONT=Consolas]if[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas] (left1 > right2) [/FONT][/COLOR][B][COLOR=#7F0055][FONT=Consolas]return[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas](0);[/FONT][/COLOR]
  
  [B][COLOR=#7F0055][FONT=Consolas]return[/FONT][/COLOR][/B][COLOR=black][FONT=Consolas](1);[/FONT][/COLOR]
  [COLOR=black][FONT=Consolas]       };[/FONT][/COLOR]
i dont understand why i am not getting any collision, when the same method is working for my ball and paddle collision.
thanks


i just realised that i am only getting collision on the first brick that is made? but nothing on any other brick.
 
One problem I see is that, when looping though your bricks and checking for collision, you are starting at the front and then removing from the front. You will only ever get the proper check for the first brick because every time you remove a brick from the list, the order of the bricks shift down one place and your iterator becomes defunct.

Try this:
Code:
for(int i = arrBricks.size() - 1; i >= 0; --i)
{
      if(Brick_Collide(arrBricks, i) == 1)
      {
             ballDY = -ballDY;    // to reverse the ball direction upon collision
             arrBricks.remove(i); 
       }
}
[/code]
 
Hi, thanks ive tried that but its not working, when the first brick is collided with all bricks are removed.

could it be due to the what i am sending into the collision detection method, should i do it a different way.

----------
for testing i put in a break into the code like:
Code:
        for(int i = arrayBricks.size() - 1; i >= 0; --i)
        {
              if(Brick_Collide(arrayBricks, i) == 1)
              {
                     mBallDY = -mBallDY;    // to reverse the ball direction upon collision
                     arrayBricks.remove(i);
                     updateScore(1000);
                     break;
              }
        }
and now i notice that when the first brick is hit, the last brick on screen is removed.
also the score is only updated when the first brick on screen is hit.
this shows that it is not recognising any of the other bricks on the screen and only the first one.
I dont understand why it is only doing anything when the first brick is hit.
thanks
 
One of the best things you can do when developing an algorithm is to step through your code, keeping track of all relevant variables. This will tell you if there is a mistake, and where that mistake is if there is one.

I don't have any paper near me to write stuff down, so I tried to doing it in my head and, although it's possible things got confused in my head, I believe that things are getting messed up inside your function. Try just passing the single instance of brick for that loop iteration to the function and not the arraylist and the iterative value.
 
Actually, I thought about it some more and the problem is that you are altering the list inside the loop to begin with. When you do this, the iterater becomes all messed up.

The way I would do this is to make a boolean data member inside your Brick class (initialized to true) and then if the collision occurs, set that data member to false, but leave it in the list.

then, in your onDraw method, check to make sure a particular brick's data member is true before drawing it.
 
Hi, thanks, i tired that but its still not working.
For some reason it is still only detecting collision with the first brick and none others?

Thanks
 
hello, sorry to hijack your thread but i'm also doing something very similar and having trouble drawing the bricks, I've tried the solutions provided but wondered if you could elaborate on what you've done to get the bricks to be visible, i'm a beginner in android and was looking for some help.
Thanks
 
Back
Top Bottom