0

Would it be best to initiate the bricks by:

  • using GRect bricks = newGRect(x,y,width,height) (and would bricks be the best variable name?)

  • or, by drawing the rectangles using drawRect(window, x,y,width,height)?

The problem for me is that the first option seems to initialise the bricks best, but makes it almost impossible to recognise collisions with the ball later, upon which of course a brick must be removed. The second one doesn't work properly in my browser, as not all bricks show up. For example, the upper one and a half rows just don't appear. I know that this might probably not be a programming error, but it is still detrimental to the game.

1 Answer 1

2

I don't think drawRect() will work in this situation. You should create a brick like that

GRect brick = newGRect(x, y, width, height);

assuming x, y, width and height are declared and initialized correctly. For detecting collisions, the function detectCollision() that's already defined and ready to use by you takes in the GObject that you wanna check whether it collides with something as an argument, calls another function from the SPL namely getGObjectAt() which returns a pointer to a GObject if there's already a GObject (e.g., a GRect) at the specified x and y.

In short, you shouldn't care about the implementation of detectCollision() or getGObjectAt() (unless you're really curious), but rather, you should create your bricks (as GRects), initialize them and add them to the window. You should also know that if you passed the ball to detectCollision(), it will return a GObject (e.g., a GRect) in case there's a collision or NULL in case there's not.

You should then handle that by removing the brick (if the GObject returned by detectCollision() is a brick), increasing the score by 1, bouncing the ball off and so on.

For the bricks not all of them being shown, I assume either you forgot to add them to the window or the formula for placing them is not correct. If that's the case, remember that if you're creating them from left to right top to bottom then the x position of each brick should be > the x position of the previous brick by the width of the brick + the gap.

Similarly, the y position of each brick should be > the y position of the upper brick by the height of a brick + the gap. You should make sure that this formula applies correctly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .