0

I have looked through many similar questions and some seemed to be the same as my problem but the solutions did not work. Basically, as the title says, the game plays but when bricks are removed, points are not added; and when the ball hits the bottom of the window, the number of lives are not reduced so I can just keep playing. I am not sure but I think they are part of the same problem. As always, any help is greatly appreciated!!!

//
// 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;

waitForClick();
//double velX = (drand48() + 1.0) - drand48();
double velX = drand48();
double velY = 4.0;

// keep playing until game over    
while (lives > 0 && bricks > 0)
{
    // TODO        
    while (true)
    {
        GEvent event = getNextEvent(MOUSE_EVENT);

        // if we heard one
        if (event != NULL)
        {
            // if the event was movement
            if (getEventType(event) == MOUSE_MOVED)
            {
                // ensure circle follows top cursor
                double x = getX(event) - getWidth(paddle) / 2;
                setLocation(paddle, x, 540);
            }
        }
        move(ball, velX, velY);
        pause(10);

        if (getX(ball) <= 0 || getX(ball) + getWidth(ball) >= getWidth(window))
        {
            velX = - velX;
        }

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

        else if (getY(ball) + getHeight(ball) >= getHeight(window))
        {
            lives--;
            setLocation(ball, 190, 290);
            waitForClick();
        }

        GObject object = detectCollision(window, ball);

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

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

// wait for click before exiting
waitForClick();

// game over
closeGWindow(window);
return 0;
}
/**
 * Initializes window with a grid of bricks.
 */
void initBricks(GWindow window)
{
// TODO
for (int r = 0; r < ROWS; r++)
{
    double pos_x = 2;
    double pos_y = 50;

    if (r < 1)
    {
        for (int c = 0; c < COLS; c++)
        {
            GRect bricks = newGRect(pos_x, pos_y + r * 15, 36, 10);
            setColor(bricks, "RED");
            setFilled(bricks, true);
            pos_x = pos_x + 40;
            add(window, bricks);
        }
    }
    else if (r < 2)
    {
        for (int c = 0; c < COLS; c++)
        {
            GRect bricks = newGRect(pos_x, pos_y + r * 15, 36, 10);
            setColor(bricks, "ORANGE");
            setFilled(bricks, true);
            pos_x = pos_x + 40;
            add(window, bricks);
        }
    }
    else if (r < 3)
    {
        for (int c = 0; c < COLS; c++)
        {
            GRect bricks = newGRect(pos_x, pos_y + r * 15, 36, 10);
            setColor(bricks, "YELLOW");
            setFilled(bricks, true);
            pos_x = pos_x + 40;
            add(window, bricks);
        }
    }
    else if (r < 4)
    {
        for (int c = 0; c < COLS; c++)
        {
            GRect bricks = newGRect(pos_x, pos_y + r * 15, 36, 10);
            setColor(bricks, "GREEN");
            setFilled(bricks, true);
            pos_x = pos_x + 40;
            add(window, bricks);
        }
    }
    else if (r < 5)
    {
        for (int c = 0; c < COLS; c++)
        {
            GRect bricks = newGRect(pos_x, pos_y + r * 15, 36, 10);
            setColor(bricks, "BLUE");
            setFilled(bricks, true);
            pos_x = pos_x + 40;
            add(window, bricks);
        }
    }
}
}

/**
 * Instantiates ball in center of window.  Returns ball.
 */
GOval initBall(GWindow window)
{
// TODO
GOval ball = newGOval(190, 290, 2 * RADIUS, 2 * RADIUS);
setColor(ball, "BLACK");
setFilled(ball, true);
add(window, ball);
return ball;
}

/**
 * Instantiates paddle in bottom-middle of window.
 */
GRect initPaddle(GWindow window)
{
// TODO
GRect paddle = newGRect(170, 540, 60, 10);
setColor(paddle, "BLACK");
setFilled(paddle, true);
add(window, paddle);
return paddle;
}

/**
 * Instantiates, configures, and returns label for scoreboard.
 */
GLabel initScoreboard(GWindow window)
{
// TODO
GLabel label = newGLabel("0");
setFont(label, "SansSerif-36");
setColor(label, "GRAY");
double x = (getWidth(window) - getWidth(label)) / 2;
double y = (getHeight(window) - getHeight(label)) / 2;
setLocation(label, x, y);
add(window, label);
return label;
}

/**
 * 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

The problem is simple. While you are updating the values in points and lives, you don't actually do anything with them.

For the score, you update points, but you never call updateScoreboard(), so it never gets displayed.

The lives is a slightly different issue. Your outer while loop appears to cause the program to stop when lives hits 0. The problem is that you created a forever loop with while (true) just inside the outer loop. The effect of this is that the outer loop starts it's first pass, starts the inner infinite loop and never comes back to test the value of lives to see if it should stop - the outer loop never completes a first pass.

There may be other problems - it was causing problems in my appliance - but this should get you going.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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