0

Just as the title says. When I remove the function to make the scoreboard the ball moves past the middle of the screen.

I've tried using draw() and sendToBack() on the label thinking the ball would avoid collision with it. Also tried changing the return type to NULL instead of label. running out of ideas on what the problem could be.

Here's the code. removed some functions here like initbricks() and initpaddle()

// 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>
#include <spl/ginteractors.h>

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

// height and width of the paddle
#define PADDLEWIDTH (1.0/6.0) * getWidth(window)
#define PADDLEHEIGHT (1.0/50.0) * getHeight(window)

//width and height of the ball
#define BALLWIDTH 20
#define BALLHEIGHT 20

// 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);
double centerobject(double left_top, double right_bottom, double width_height);

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 lives initially
    int lives = LIVES;

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

    // number of points initially
    int points = 0;

    // sets the speed of the ball
    double velocityx = 1.0;
    double velocityy = 2.0;

    // waits for a mouse click before beginning the game
    waitForClick();

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

        move(ball, 0, velocityy);


        if(detectCollision(window, ball) != NULL)
        {
            velocityy = -velocityy;
            move(ball, 0, velocityy);
        }

        pause(1);

    }
    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}    
/**
 * Instantiates, configures, and returns label for scoreboard.
 */
GLabel initScoreboard(GWindow window)
{
    // Used to align the Scoreboard.
    double left = 0;
    double right = WIDTH;
    // Blah blah.
    GLabel sarah = newGLabel("0");   

    setFont(sarah,"SansSerif-50");
    setColor(sarah, "LIGHT_GRAY");
    // You know the drill.    
    double x = centerobject(left, right, getWidth(sarah));
    double y = getHeight(window) * 4/10;
    setLocation(sarah, x, y);
    add(window, sarah);
    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;
}
1

I think this has to do with your code for detecting collisions. As of now, the only check performed is if a collision with something occurred or not. There is no differentiation between colliding with the paddle, a brick, the wall, etc. Someone else can correct me if I'm wrong, but a label is still an object.

7
  • KernelRutgers is exactly right. You need to do more than detect that you hit an object. Think about what you want to detect. Do the objects that the ball hits have any attributes in common, like maybe their shape?
    – Cliff B
    Apr 10 '15 at 17:45
  • I notice that the objects I want to collide with aren't labels. I think everything from GOvals to GLabels are inscribed within a rectangle so I don't think I can use their shape to fix this problem.
    – Stalris
    Apr 10 '15 at 18:39
  • Okay...so you want to (1) detect if a collision happened, and (2) take action on those collisions if the ball hit a brick or paddle, but not anything else. You're almost there, maybe structuring it in pseudocode first, and then coding each block of pseudocode into C chunk by chunk will get you through. Apr 10 '15 at 23:14
  • in an earlier attempt I tried doing something like if(object == GLabel){ return NULL;} and it didn't work. Didn't think I had to change the prototype to also pass in objects-to-avoid but I guess that's the answer. It is supposed to be that right? Add a third argument to detectCollision() that takes in a label and use if(object == label) instead of GLabel?
    – Stalris
    Apr 11 '15 at 0:51
  • 1
    @Stalris You shouldn't need to re-write the function call or add arguments. The function was already written for us for the most part. Point 7 in the PSet specification covers the "collision detection" aspect of breakout... Once it comes time to add a GLabel to your game (for its scoreboard), you can similarly determine if that object is GLabel, in which case it might be a collision you want to ignore. if (strcmp(getType(object), "GLabel") == 0) { // TODO } Apr 11 '15 at 2:29

You must log in to answer this question.

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