1

Here are some all the bugs im getting form breakout.c

/usr/include/stdlib.h:376:13: note: 'srand' declared here
extern void srand (unsigned int __seed) __THROW;
            ^
breakout.c:23:13: error: implicit declaration of function
      'time' is invalid in C99
      [-Werror,-Wimplicit-function-declaration]
    srand48(time(NULL));
            ^
breakout.c:25:40: error: use of undeclared identifier
      'HEIGHT'
    GWindow window = newGWindow(WIDTH, HEIGHT);
                                       ^
breakout.c:29:11: error: expected identifier or '('
    GOval = initBall(window);
          ^
breakout.c:31:20: error: implicit declaration of function
      'intScoreboard' is invalid in C99
      [-Werror,-Wimplicit-function-declaration]
    GLabel label = intScoreboard(window):
                   ^
breakout.c:31:12: error: incompatible integer to pointer
      conversion initializing 'GLabel' (aka 'void *') with an
      expression of type 'int' [-Werror,-Wint-conversion]
    GLabel label = intScoreboard(window):
           ^       ~~~~~~~~~~~~~~~~~~~~~
breakout.c:31:41: error: expected ';' at end of declaration
    GLabel label = intScoreboard(window):
                                        ^
                                        ;
breakout.c:48:14: error: use of undeclared identifier 'ball'
        move(ball, x_velocity, y_velocity);
             ^
breakout.c:52:24: error: implicit declaration of function
      'GetNextEvent' is invalid in C99
      [-Werror,-Wimplicit-function-declaration]
        GEvent event = GetNextEvent(MOUSE_EVENT);
                       ^
breakout.c:52:24: note: did you mean 'getNextEvent'?
/usr/include/spl/gevents.h:309:8: note: 'getNextEvent'
      declared here
GEvent getNextEvent(int mask);
       ^
breakout.c:52:16: error: incompatible integer to pointer
      conversion initializing 'GEvent' (aka 'struct GEventCDT
      *') with an expression of type 'int'
      [-Werror,-Wint-conversion]
        GEvent event = GetNextEvent(MOUSE_EVENT);
               ^       ~~~~~~~~~~~~~~~~~~~~~~~~~
breakout.c:59:51: error: use of undeclared identifier
      'paddle'
  ...double x = getX(event) - getWidth(paddle) / 2;
                                       ^
/usr/include/spl/generic.h:44:46: note: expanded from macro
      'getWidth'
#define getWidth(arg) getWidthGeneric(sizeof arg, arg)
                                             ^
breakout.c:61:29: error: use of undeclared identifier
      'paddle'
                setLocation(paddle, x, y);
                            ^
breakout.c:66:50: error: use of undeclared identifier 'ball'
        GObject object = detectCollision(window, ball);
                                                 ^
breakout.c:70:27: error: use of undeclared identifier
      'paddle'
            if (object == paddle)
                          ^
breakout.c:75:22: error: implicitly declaring library
      function 'strcmp' with type 'int (const char *, const
      char *)' [-Werror]
            else if (strcmp(getType(object), "GRect") == 0)
                     ^
breakout.c:75:22: note: please include the header <string.h>
      or explicitly provide a declaration for 'strcmp'
breakout.c:84:18: error: use of undeclared identifier 'ball'
        if (getX(ball) + getWidth(ball) >= getWidth(window))
                 ^
/usr/include/spl/generic.h:42:38: note: expanded from macro
      'getX'
#define getX(arg) getXGeneric(sizeof arg, arg)
                                     ^
breakout.c:84:35: error: use of undeclared identifier 'ball'
        if (getX(ball) + getWidth(ball) >= getWidth(window))
                                  ^
/usr/include/spl/generic.h:44:46: note: expanded from macro
      'getWidth'
#define getWidth(arg) getWidthGeneric(sizeof arg, arg)
                                             ^
breakout.c:89:18: error: use of undeclared identifier 'ball'
        if (getX(ball) <= 0)
                 ^
/usr/include/spl/generic.h:42:38: note: expanded from macro
      'getX'
#define getX(arg) getXGeneric(sizeof arg, arg)

Here is my code (this is a long one)

#include <spl/gevents.h>
#include <spl/gobjects.h>
#include <spl/gwindow.h>

#define HEIGHT6 00
#define WIDTH 400
#define Rows 5
#define Cols 10
#define Radius 10
#define Lives 3

void intBricks(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);
double x_velocity;
double y_velocity;

int main(void)
{
    srand48(time(NULL));

    GWindow window = newGWindow(WIDTH, HEIGHT);

    intBricks(window);

    GOval = initBall(window);

    GLabel label = intScoreboard(window):

    int bricks = Cols*Rows;

    int lives = Lives;

    int points = 0;

    waitForClick();

    x_velocity = 2;
    y_velocity = 4;

    while(Lives > 0 && bricks > 0)
    {
        updateScoreboard(window, label, points);

        move(ball, x_velocity, y_velocity);

        pause(10);

        GEvent event = GetNextEvent(MOUSE_EVENT);

        if (event != NULL)
        {
            if (getEventType(event) == MOUSE_MOVED)
            {
                // ensure paddle follows top cursor
                double x = getX(event) - getWidth(paddle) / 2;
                double y = 500;
                setLocation(paddle, x, y);
            }
        }


        GObject object = detectCollision(window, ball);

        if (object != NULL)
        {
            if (object == paddle)
            {
                y_velocity = -y_velocity;
            }

            else if (strcmp(getType(object), "GRect") == 0)
            {
                removeGWindow(window, object);
                y_velocity = -y_velocity;
                points++;
                bricks--;                
            }
        }

        if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
            x_velocity = -x_velocity;
        }

        if (getX(ball) <= 0)
        {
            x_velocity = -x_velocity;
        }

        if (getY(ball) <= 0)
        {
            y_velocity = -y_velocity;
        }

        if (getY(ball) + getHeight(ball) >= getHeight(window))
        {
            lives--;
            setLocation(ball, 190, 200);
            setLocation(paddle, 160, 500);
            waitForClick();
        }

    }

    if (bricks > 0)
    {
        GLabel game_over = newGLabel("YOU LOSE!");
        setFont(game_over, "SansSerif-70");
        setColor(game_over, "RED");
        add(window, game_over);
        setLocation(game_over, 15, 300);
    }
    else
    {
        GLabel game_over = newGLabel("YOU WIN!");
        setFont(game_over, "SansSerif-70");
        setColor(game_over, "GREEN");
        add(window, game_over);
        setLocation(game_over, 15, 300);
    }

    waitForClick();

    closeGWindow(window);
    return 0;
}

void initBricks(GWindow window)
{

    int brick_y = 50; 
    int row_space = 20; 

    for (int i = 0; i < Rows; i++) 
    {
        int brick_x = 2; 
        for (int j = 0; j < Cols; j++) /
        {
            GRect brick = newGRect(brick_x + 5, brick_y, 35, 10);

            if (i == 0)
            {
                setColor(brick, "PURPLE");
            }
            if (i == 1)
            {
                setColor(brick, "BLUE");
            }
            if (i == 2)
            {
                setColor(brick, "CYAN");
            }
            if (i == 3)
            {
                setColor(brick, "GREEN");
            }
            if (i == 4)
            {
                setColor(brick, "YELLOW"); 
            }

            setFilled(brick, true);
            add(window, brick);
            brick_x = brick_x + 39; 
        }
        brick_y = brick_y + 15;
    }
}

GOval initBall(GWindow window)
{
    GOval ball = newGOval(200 - 10, 200, 20, 20); 
    setColor(ball, "BLACK");
    setFilled(ball, true);
    add(window, ball);
    return ball;
}

GRect initPaddle(GWindow window)
{
    GRect paddle = newGRect(200 - 35, 500, 70, 10);
    setColor(paddle, "7F00FF"); //purple
    setFilled(paddle, true);
    add(window, paddle);
    return paddle;
}

GLabel initScoreboard(GWindow window)
{
    GLabel label = newGLabel(" ");
    setFont(label, "SansSerif-50");
    setColor(label, "C0C0C0");
    add(window, label);
    setLocation(label, 185, 300);
    return label;
}

void updateScoreboard(GWindow window, GLabel label, int points)
{
    char s[12];
    sprintf(s, "%i", points);
    setLabel(label, s);

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

GObject detectCollision(GWindow window, GOval ball)
{
    double x = getX(ball);
    double y = getY(ball);

    GObject object;

    object = getGObjectAt(window, x, y);
    if (object != NULL)
    {
        return object;
    }

    object = getGObjectAt(window, x + 2 * Radius, y);
    if (object != NULL)
    {
        return object;
    }

    object = getGObjectAt(window, x, y + 2 * Radius);
    if (object != NULL)
    {
        return object;
    }

    object = getGObjectAt(window, x + 2 * Radius, y + 2 * Radius);
    if (object != NULL)
    {
        return object;
    }

    return NULL;
}       
3
  • Thanks for rewriting it.
    – i_am_david
    Jul 20, 2015 at 16:50
  • Honestly, you should read the errors before you ask. Many of the errors are easy to understand and simple to fix.
    – i_am_david
    Jul 20, 2015 at 17:24
  • I answered a few of them. Ask again after you have looked through the rest of the errors and tried to fix them.
    – i_am_david
    Jul 20, 2015 at 18:58

1 Answer 1

2
breakout.c:31:41: error: expected ';' at end of declaration
GLabel label = intScoreboard(window):
                                     ^
                                     ;

This error just means you need a semicolon.

breakout.c:23:13: error: implicit declaration of function
      'time' is invalid in C99
      [-Werror,-Wimplicit-function-declaration]
    srand48(time(NULL));
        ^

This means you did not include the correct header file: you need to include stdlib.h to use srand and drand.

breakout.c:75:22: error: implicitly declaring library
      function 'strcmp' with type 'int (const char *, const
      char *)' [-Werror]
            else if (strcmp(getType(object), "GRect") == 0)
                     ^
breakout.c:75:22: note: please include the header <string.h>
      or explicitly provide a declaration for 'strcmp'

This error means you need to include the header file string.h

breakout.c:52:24: error: implicit declaration of function
      'GetNextEvent' is invalid in C99
      [-Werror,-Wimplicit-function-declaration]
        GEvent event = GetNextEvent(MOUSE_EVENT);
                       ^
breakout.c:52:24: note: did you mean 'getNextEvent'?
/usr/include/spl/gevents.h:309:8: note: 'getNextEvent'
      declared here
GEvent getNextEvent(int mask);
       ^

This means you incorrectly spelled getNextEvent- capitalization matters.

From what I see, you didn't declare ball and paddle. You need to declare them.

breakout.c:25:40: error: use of undeclared identifier
      'HEIGHT'
    GWindow window = newGWindow(WIDTH, HEIGHT);
                                       ^

You made a typo at the top of the file. It should be easy to spot.

breakout.c:29:11: error: expected identifier or '('
    GOval = initBall(window);

You need a name for the GOval.

3
  • thanks, David, I fixed all of the bugs exept one, the ball/paddle thing. How would you delcare it?
    – Ani
    Jul 21, 2015 at 14:20
  • GOval ball; and GRect paddle; I think. I forgot pset3.
    – i_am_david
    Jul 21, 2015 at 17:34
  • Chris Gantidis could you tell me what you changed please?
    – i_am_david
    Jul 21, 2015 at 17:37

You must log in to answer this question.

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