I am faced with a very strange problem on PSET 4 (Breakout.c)... The following is my code (main portion). All is well - the ball moves and bounces off the paddle and off the walls (not yet the bricks) - when the strcmp portion of the code is omitted:
else if (strcmp(getType(object), "GRect") == 0)
{
removeGWindow(window, object);
}
However, once the above is added (see below code), the whole program hanged - pausing at the initialised screen - i.e. ball is no longer moving, paddle no longer moving, etc. Why is that so???
double xvelocity = drand48() + 1.5;
double yvelocity = drand48() + 1.5;
// keep playing until game over
while (lives > 0 && bricks > 0)
{
// TODO
GEvent event = getNextEvent(MOUSE_EVENT);
GObject object = detectCollision(window, ball);
// bounce ball at an angle
move(ball, xvelocity, yvelocity);
If (getY (ball) + getWidth (ball) >= getHeight (window))
yvelocity = -yvelocity;
else if (getY(ball) <= 0)
yvelocity = -yvelocity;
if (getX(ball) + getWidth(ball) >= getWidth(window))
xvelocity = -xvelocity;
else if (getX(ball) <= 0)
xvelocity = -xvelocity;
if (object == paddle)
{
xvelocity = -xvelocity;
yvelocity = -yvelocity;
pause(10);
}
else if (strcmp(getType(object), "GRect") == 0)
{
removeGWindow(window, object);
}
//paddle movement
if (event != NULL)
{
if (getEventType(event) == MOUSE_MOVED)
{
double x = getX(event) - getWidth(paddle)/2;
double y = getY(paddle);
setLocation(paddle, x, y);
}
}
pause(10);
}