0

The code below is not completed yet, but I am running into one major problem. When I first had my code typed in, I did not write in the main function and I got the ball moving and the paddle going etc. but when I put all my "action code" into the main function, it starts to not work and it says segmentation fault when I run it. But I have all my return NULL changed to return "object" already. Does anyone know what I'm doing wrong here? :(

//
// breakout.c
//
// Computer Science 50
// Problem Set 3
//

// standard libraries
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// Stanford Portable Library
#include <spl/gevents.h>
#include <spl/gobjects.h>
#include <spl/gwindow.h>

// height and width of game's window in pixels
#define HEIGHT 600
#define WIDTH 400

// number of rows of bricks
#define ROWS 5

// number of columns of bricks
#define COLS 10

// radius of ball in pixels
#define RADIUS 10

// lives
#define LIVES 3

// prototypes
void initBricks(GWindow window);
GOval initBall(GWindow window);
GRect initPaddle(GWindow window);
GLabel initScoreboard(GWindow window);
void updateScoreboard(GWindow window, GLabel label, int points);
GObject detectCollision(GWindow window, GOval ball);

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 = LIVES;

// number of points initially
int points = 0;

// keep playing until game over
while (lives > 0 && bricks > 0)
{
    // TODO

   waitForClick();



    double velocity = 2.0;


        move(ball, velocity,0);
        if (getX (ball) + getWidth (ball) >= getWidth(window))
        {
        velocity = - velocity;
        }
        else if (getX (ball) <=0)
        {
        velocity = - velocity;
        }
        pause(10);



    GEvent event = getNextEvent(MOUSE_EVENT);

    if (event != NULL)
    {
        if(getEventType(event) == MOUSE_MOVED)
        {
            double x = getX(event) - getWidth(paddle);
            double y = getY(event) - getWidth(paddle);
            setLocation(paddle,x,530);

        }
}


    GObject object = detectCollision(window, ball);
    if (object == paddle)
{
// TODO
}
if (strcmp(getType(object), "GRect") == 0)
{
// TODO
}
}

// wait for click before exiting
waitForClick();

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

/**
* Initializes window with a grid of bricks.
*/
void initBricks(GWindow window)
{
// TODO

int setRectx = 5;
int setRecty = 37;

for ( int i = 0; i < ROWS; i++)
{

for ( int n = 0; n < COLS; n++)
{
GRect bricks = newGRect(setRectx,setRecty,36,10);
setColor(bricks,"BLUE");
setFilled(bricks,true);
setRectx = setRectx + 38;
add (window, bricks);
}
setRectx = 5;
setRecty = setRecty + 20;


}

}

/**
* Instantiates ball in center of window.  Returns ball.
*/
GOval initBall(GWindow window)
{
// TODO

GOval ball = newGOval(200,300,20,20);

add (window, ball);


return ball;

}

/**
* Instantiates paddle in bottom-middle of window.
*/
GRect initPaddle(GWindow window) 
{
// TODO 
GRect paddle = newGRect(200,530,36,10);
setColor(paddle,"BLACK");
setFilled(paddle,true);

add(window, paddle);

return paddle;
}

/**
* Instantiates, configures, and returns label for scoreboard.
*/
GLabel initScoreboard(GWindow window)
{
// TODO
return NULL;
}

/**
* Updates scoreboard's label, keeping it centered in window.
*/
void updateScoreboard(GWindow window, GLabel label, int points)
{
// update label
char s[12];
sprintf(s, "%i", points);
setLabel(label, s);

// center label in window
double x = (getWidth(window) - getWidth(label)) / 2;
double y = (getHeight(window) - getHeight(label)) / 2;
setLocation(label, x, y);
}

/**
* Detects whether ball has collided with some object in window
* by checking the four corners of its bounding box (which are
* outside the ball's GOval, and so the ball can't collide with
* itself).  Returns object if so, else NULL.
*/
GObject detectCollision(GWindow window, GOval ball)
{
// ball's location
double x = getX(ball);
double y = getY(ball);

// for checking for collisions
GObject object;

// check for collision at ball's top-left corner
object = getGObjectAt(window, x, y);
if (object != NULL)
{
    return object;
}

// check for collision at ball's top-right corner
object = getGObjectAt(window, x + 2 * RADIUS, y);
if (object != NULL)
{
    return object;
}

// check for collision at ball's bottom-left corner
object = getGObjectAt(window, x, y + 2 * RADIUS);
if (object != NULL)
{
    return object;
}

// check for collision at ball's bottom-right corner
object = getGObjectAt(window, x + 2 * RADIUS, y + 2 * RADIUS);
if (object != NULL)
{
    return object;
}

// no collision
return NULL;
}
0

1) Why do you have a waitForClick() inside of your game loop? You do realize that it will cause for nothing to happen at all unless you keep on clicking all the time, don't you? :)

2) You call collisionDetection and pass it to "object". Then you do this:

if (strcmp(getType(object), "GRect") == 0)

What happens when there's no collision? What does collision return in that case? You're not checking for that and that's the most likely cause for segmentation fault.

You must log in to answer this question.

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