1

I get a segmentation fault after x times i "die" (it varies) and if i decrement the lives each time the ball hits the bottom i get a segmentation fault after 1 time. It's very anoying and i can't find the error. I have tried multiple things. So can someone with a fresh mind take a look at my code and point out what i overlooked? Thank you. Here is the full codesource code

This is my main function:

int main(void) {

// seed pseudorandom number generator srand48(time(NULL));

// instantiate window
GWindow window = newGWindow(WIDTH, HEIGHT);

// instantiate bricks
initBricks(window);

// instantiate ball, centered in middle of window
GOval ball = initBall(window);

// instantiate paddle, centered at bottom of window
GRect paddle = initPaddle(window);

// instantiate scoreboard, centered in middle of window, just above ball
GLabel label = initScoreboard(window);

// number of bricks initially
int bricks = COLS * ROWS;

// number of lives initially
int lives = 3;

// number of points initially
int points = 0;

// inital velocity
double velocityX = drand48() * 3.25;
double velocityY = drand48() * 3.25;

// middle/place of ball when started
double x = (getWidth(window) - getWidth(ball)) / 2;
double y = (getHeight(window) - getHeight(ball)) / 2;
// keep playing until game over
while (lives > 0 && bricks > 0)
{
    // DONE 
    GEvent event = getNextEvent(MOUSE_EVENT);
    move(ball, velocityX, velocityY);

    // update points
    char s[3];
    sprintf(s, "%i", points);
    setLabel(label, s);

    // bounce off right edge of window
    if (getX(ball) + getWidth(ball) >= getWidth(window))
    {
        velocityX = -velocityX;
    }

    // bounce off left edge of window
    else if (getX(ball) <= 0)
    {
        velocityX = -velocityX;
    }
    // bounce off bottom edge of window
    if (getY(ball) + getWidth(ball) >= getHeight(window))
    {    
        GEvent click = getNextEvent(MOUSE_EVENT);

        if (event != NULL)
        {
            if (getEventType(click) == MOUSE_CLICKED)
            {
                setLocation(ball, x, y);
                //lives--;
            }
        }
    }

    // bounce off top edge of window
    else if (getY(ball) <= 0)
    {
        velocityY = -velocityY;
    }

    // detects a collison and stores it as object
    GObject object = detectCollision(window, ball);
    // bounce of paddle
    if (object == paddle)
    {
        velocityY = -velocityY;

    }

    if (object == NULL)
    {
        // Do nothing
    }
    else if (strcmp(getType(object), "GRect") == 0)
    {
        if (object != paddle)
        {
            removeGWindow(window, object);
            velocityY = -velocityY;
            points++;
        }
    }

    if (event != NULL)
    {
        if (getEventType(event) == MOUSE_MOVED)
        {
            double x = getX(event) - getWidth(paddle) / 2;
            double y = (getHeight(window) - getHeight(paddle)) / 1.25;
            setLocation(paddle, x, y);

        }
    }
    pause(10);
}

// wait for click before exiting
waitForClick();

// game over
closeGWindow(window);
return 0;

}

  • Can you share the backtrace you obtained? you can get it with gdb's help. invoke gdb like 'gdb breakout core' where breakout is your binary and core is the coredump created when your application crashes due to seg fault. once gdb loads both your binary and the core file, issue 'bt' command in gdb prompt – padfoot May 6 '15 at 9:40
0

Check your events. You declared two different events in two different parts of the code and made a little mess there (checking the wrong event). Try fixing that, and try again!

Also there is a smarter way to do this part of the code, that is, without else if.

if (object == NULL) { // Do nothing }

| improve this answer | |

You must log in to answer this question.

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