3

My detectCollision function detects collisions between ball and paddle, but not between ball and bricks. Bricks 'exist' in 'window' but they don't exist in main().

I thought detectCollision might be working for ball & paddle, and failing for ball & bricks, because ball's and paddle's instantiating functions return them to main, making them available to detectCollision, whereas brick's instantiating function returns nothing. (Within 5- and 10-iteration nested loops, I initialize a GObject, 'brick', and call add(window, brick).)

Based on the first answer to this question though, I'm discounting my return theory. And based on a comment in that answer, I'm concluding that my bricks don't really 'exist'. To explore that, I created a single, window-wide brick and included in main,

if (object == brick)
{
    // some code
}

The code did not compile because of an 'undeclared identifier' error triggered by 'brick'. The same thing happened when I tried move(brick, ...). Which makes perfect sense, because I didn't return it to main.

Without 50 brick-instantiating functions (and return values), or without using an address and pointer to an array of bricks, how can I make my bricks 'exist' in main for detectCollision?

6

Your first assumption was not wrong. The bricks are not returned to the main function, so you need to find another way to refer to them while the game is running.

The bricks do definitely exist as GRect objects attached to the window. 'detectCollision' will return an object which may be a brick, the paddle, the label, or whatever other object the ball is colliding with (or NULL if there is no collision).

It seems from the problem set spec that the expectation is that you need to inspect the type of object returned from detectCollision using getType. Refer to point #7 from the problem set specification, where the following code examples are given:

GObject object = detectCollision(window, ball);

...and

More generally, you can determine if that object is a GRect with:

if (strcmp(getType(object), "GRect") == 0)
{
    // TODO
}

This can be used to check if the object collided with is a GRect, and if it is not also the paddle, then it must be a brick, as those are the only two possibilities. A reference to paddle is already available in main.

That said, I personally found this rather cumbersome, and I wanted to implement my own collision detection, so I took a different approach. Not that I'm recommending you do this, but I took a risk and did one of the alternatives mentioned in your question - I changed the initBricks function to take a pointer to an array to populate with the brick objects. I did this knowing full well that the program could be rejected by the grading system, and that I might have been forced to do it the way the set intended. As it happens my answer was accepted, but there's no guarantee it would happen again.

1
  • 1
    as an aside, for cs50x, pset4 is autograded with a 1 if you submit a program, so it doesn't matter what approach you take, as long as you are personally satisfied that your program works.)
    – curiouskiwi
    Aug 3 '14 at 3:04

You must log in to answer this question.

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