0

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); 
       }
  • 1
    I am not sure but i think maybe you should check too if the object is NULL in the elseif. – wallek876 Dec 13 '14 at 7:04
4

You need to add

if(object != NULL)

before your

if (object == paddle)

Your program stops because of "Segmentation Fault". The Detect-Collision function returns a NULL if no object is detected. You cannot compare a NULL with anything.

| improve this answer | |
  • yup! Thanks a lot :) – Happytreat Dec 13 '14 at 23:28

You must log in to answer this question.

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