Thread: Problem with Segmentation Fault with breakout game problem in c.

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    2

    Problem with Segmentation Fault with breakout game problem in c.

    I am having problem with using the variable 'paddle' which inside of the while loop gives a segmentation error. Please help me on this. This is my first post here, so if I am not following guidelines for this forum, please do suggest and help me. I have uploaded the file here. Thanks

    Code:
    //
    // breakout.c
    // ?????????????????????  Line 60 and 94 have some explanations on the problem I am facing
    // This is an implementation of breakout game. I am having problem with moving paddle (at bottom middle) with mouse for this game.
    // This might not work on your PC as it is using some external libraries.
    //
    
    // standard libraries
    #define _XOPEN_SOURCE
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    // Stanford Portable Library
    #include "gevents.h"
    #include "gobjects.h"
    #include "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);
    
        //
        
        //  ###################### Problematic variable or zone. The paddle variable below actually initializes (as I can see in the
        // graphic or output) but when I call it in a while loop for updating it's coordinates with mouse's coordinates, segmentation
        // fault occurs.
        
        //      instantiate paddle, centered at bottom of window
        GRect paddle = initPaddle(window);
        
        // ############ Below codes work well. #########################################
    
        // 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
            GEvent event = getNextEvent(MOUSE_EVENT);
            if (event != NULL)
            {
                // if the event was movement
                if (getEventType(event) == MOUSE_MOVED)
                {
                // getX() and getY() give x and y coordinates of event, here mouse position.
                    double x = getX(event);
                    double y = getY(event);
           
           // ??????????????????#################  Problem starts with the below line. I need to add 
           // or update (x,y) coordinates of paddle variable. But, whenever I add this variable here, 
           //it says 'segmentation fault'. Somwehow 'paddle' variable doesn't work here and I have no clue why???????
           //????????????????????????? #####################################################c
           
           
           
           
                    printf ("%f/n",getX(paddle));
                   // setLocation(paddle, x, y);
                }
            }
        }
    
        // 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 row=3;
        for(int i=0;i<10;i++)
        {
            GRect rect = newGRect(row,60,35,15);
            setFilled(rect, true);
            setColor(rect,"RED");
            add(window,rect);
            row+=40;
        }
        
        int row2=3;
        for(int i=0;i<10;i++)
        {
            GRect rect = newGRect(row2,80,35,15);
            setFilled(rect, true);
            setColor(rect,"YELLOW");
            add(window,rect);
            row2+=40;
        }
          int row3=3;
        for(int i=0;i<10;i++)
        {
            GRect rect = newGRect(row3,100,35,15);
            setFilled(rect, true);
            setColor(rect,"ORANGE");
            add(window,rect);
            row3+=40;
        }
          int row4=3;
        for(int i=0;i<10;i++)
        {
            GRect rect = newGRect(row4,120,35,15);
            setFilled(rect, true);
            setColor(rect,"CYAN");
            add(window,rect);
            row4+=40;
        }
          int row5=3;
        for(int i=0;i<10;i++)
        {
            GRect rect = newGRect(row5,140,35,15);
            setFilled(rect, true);
            setColor(rect,"PINK");
            add(window,rect);
            row5+=40;
        }
         
     
    }
    
    /**
     * Instantiates ball in center of window.  Returns ball.
     */
    GOval initBall(GWindow window)
    {
        // TODO
        GOval circle = newGOval(190, 310, 20, 20);
        setColor(circle, "BLACK");
        setFilled(circle, true);
        add(window, circle);
        return NULL;
    }
    
    /**
     * Instantiates paddle in bottom-middle of window.
     */
    GRect initPaddle(GWindow window)
    {
        // TODO
        GRect rect = newGRect(160,550,80,15);
        setFilled(rect, true);
        setColor(rect,"BLACK");
        add(window,rect);
        return NULL;
    }
    
    /**
     * 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;
    }
    *** inlined by salem ***
    Attached Files Attached Files
    Last edited by Salem; 02-09-2014 at 01:09 AM. Reason: Add code

  2. #2

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > // instantiate paddle, centered at bottom of window
    > GRect paddle = initPaddle(window);

    Now look here
    Code:
    GRect initPaddle(GWindow window)
    {
        // TODO
        GRect rect = newGRect(160,550,80,15);
        setFilled(rect, true);
        setColor(rect,"BLACK");
        add(window,rect);
        return NULL;   //!!<< What is this doing?
    }
    Perhaps you should return the rect you actually created?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    2
    Oh, thanks. I just changed return value from NULL to rect and it worked. Thanks a lot!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with segmentation fault
    By Bacondeluxe in forum C Programming
    Replies: 6
    Last Post: 09-30-2012, 09:15 PM
  2. MPI Problem gives segmentation fault
    By confused_coder in forum C Programming
    Replies: 6
    Last Post: 09-07-2009, 09:34 PM
  3. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  4. problem with segmentation fault
    By cBegginer in forum C Programming
    Replies: 13
    Last Post: 05-15-2005, 11:51 AM
  5. Segmentation fault problem
    By robsmith in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 05:33 PM