The Idea Behind Collision:
There's a function namely detectCollision() that's already written
for you and ready to use. This function, when it's called, checks
whether any of the sides of the ball collides with any GObject
(e.g., GRect). If so, it returns that GObject immediately.
The simple idea behind collision is that whether any of the points on
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 ...
Just for the record, I had the same problem of breakout getting "laggy" after running (in the appliance) for a few seconds. Staff's implementation would run properly, so it wasn't strictly a hardware (or a VM) issue.
I found this useful answer in /r/cs50, that, basically, states that the pause() statement is mandatory for the program to run properly.
Here is a bit of psuedocode to help you define detectCollision:
If object is not equal to NULL then check if:
object == paddle, if it is, flip the y-velocity accordingly
else if, object == GRect (which in this case is a brick) then:
flip the y-velocity, remove the object (removeGWindow), update the points and update the scoreboard (UpdateScoreboard)
What type have you initialized the variable "collision" to be? If you initialized it as a string, that could be the issue.
Make sure you are initializing it as:
GObject collision = detectCollision(window, ball);
On the main routine, you have a call to a function that detect collisions.
The ball is the only moving object.
What you need to do is checking if the ball "touches" an object or the walls.
Detecting wall hitting is easy: you only need to compare the position of the ball with the border of the window, and invert the xvelocity in case the position is on the ...
You're on the right track, but there's an additional issue going on.
Think of scope this way: a variable only exists within the curly braces that immediately surround it. So, when you declare them outside of the while loop, the variables exist and persist inside of all of main. When you declare them inside the while loop's curly braces, they only exist ...
Whoops, sorry guys. Just realized that for the latter situation, when the code goes through the If statement then loops back to the top of the while statement, the velocities get assigned back to their original values, i.e. they won't go negative, while for the former situation allows the velocity to go negative.
Your code isn't reversing the direction after bouncing off a wall, it's setting a specific direction. Look at the following:
if (getX(ball) <= 0)
xvelocity = drand48() + 3;
xvelocity = -xvelocity;
The initial velocities are always set to drand48() + 3. Each time you detect a collision with a wall, you reinitialize the x or y velocity and ...
The function detectCollision(...) has a specific purpose - to detect whether the ball has collided with another object, and, if it has, to return the identifier of that object. Since this function is used to detect collisions with multiple objects, you should expand it to detect collisions on all 4 corners.
You are attempting to change the velocity of the ...