Example #1
0
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;
    
    // velocity of ball
    // TODO use drand for velocityX
    double velocityX = drand48() * 3.0;
    double velocityY = 3.0;

    // keep playing until game over
    waitForClick();    
    while (lives > 0 && bricks > 0)
    {
        // check for mouse event
        GEvent event = getNextEvent(MOUSE_EVENT);


        updateScoreboard(window, label, points);
        // set velocity of ball
        move(ball, velocityX, velocityY);

        // detect collision
        GObject collision = detectCollision(window, ball);

        if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
            velocityX = -velocityX;
        }
        // bounce off left edge of window
        else if (getX(ball) <= 0)
        {
            velocityX = -velocityX;
        }
        else if (getY(ball) + getHeight(ball) >= getHeight(window))
        {
            lives -= 1;
            waitForClick();
            setLocation(ball, 190, 290);
            move(ball, velocityX, -velocityY);
        }
        else if (getY(ball) <= 0)
        {
            velocityY = -velocityY;
        }
        else if (collision != NULL)
        {
            if (collision == paddle)
            {
                velocityY = -velocityY;
            }
            else if (strcmp(getType(collision), "GRect") == 0)
            {
                // TODO
                velocityY = -velocityY;
                removeGWindow(window, collision);
                points += 1;
                bricks -= 1;
            }
        }
        pause(10);
        


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

    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #2
0
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 scoreboard = initScoreboard(window);

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

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;

    // initial ball velocity
    double h_velocity = 1.0;
    double v_velocity;

    // make sure our v_velocity isn't too slow to start with
    do
    {
        v_velocity = drand48();
    }
    while (v_velocity < 0.5);

    waitForClick();

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

        // check if there's been a collision...
        GObject collided_obj = detectCollision(window, ball);

        // if we got one
        if (event != NULL)
        {
            // if the event was movement
            if (getEventType(event) == MOUSE_MOVED)
            {
                // move the paddle along the x axis
                double x = getX(event) - getWidth(paddle) / 2;
                double y = getY(paddle);
                setLocation(paddle, x, y);
            }
        }

        // move the ball
        move(ball, h_velocity, v_velocity);

        // bounce off either left or right edge of window
        if (getX(ball) + getWidth(ball) >= getWidth(window)
                || getX(ball) <= 0)
        {
            h_velocity = -h_velocity;
        }

        // bounce off the top edge of window
        if (getY(ball) <= 0)
        {
            v_velocity = -v_velocity;
        }

        // lose a life if the ball moves off the bottom of the window
        if (getY(ball) + getWidth(ball) >= getHeight(window))
        {

            // remove the ball, and remove a life
            removeGWindow(window, ball);
            lives--;

            // if we've run out of lives, don't recreate the ball
            if (lives > 0)
            {
                ball = initBall(window);
                waitForClick();
            }
        }

        if (collided_obj == NULL)
        {
            // do nothing if there hasn't been a collision
        }
        else if (collided_obj == paddle)
        {
            // bounce ball off of paddle
            if (v_velocity > 0)
                v_velocity = -v_velocity;
        }
        else if (strcmp(getType(collided_obj), "GRect") == 0)
        {
            // if it isn't the paddle, and it's a GRect, it must be a brick!
            // remove the brick
            removeGWindow(window, collided_obj);

            // reverse the ball's vertical velocity
            v_velocity = -v_velocity;

            // add to the player's score
            points++;

            // reduce the number of bricks left
            bricks--;

            updateScoreboard(window, scoreboard, points);
        }

        // slow down a tiny bit..
        pause(2);
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #3
0
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;

    // velocity of the ball
    double ball_vx = (drand48() - 0.5) * 5;
    double ball_vy = 3;

    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // Player movement
        GEvent e = getNextEvent(MOUSE_EVENT);
        if (e != NULL)
        {
            int x = getX(e) - getWidth(paddle) / 2;
            setLocation(paddle, x, getY(paddle));
        }

        // Game Logic
        GObject object = detectCollision(window, ball);
        if (object != NULL && !(strcmp(getType(object), "GRect")) &&
            object != paddle)
        {
             removeGWindow(window, object);
             ball_vy = ball_vy * -1;
             bricks -= 1;
             points += 1;
             updateScoreboard(window, label, points);
        }

        if (getY(ball) + 2 * RADIUS >= getHeight(window))
        {
            lives -= 1;
            removeGWindow(window, ball);
            ball = initBall(window);
            if (lives)
                waitForClick();
        }

        if (getY(ball) <= 0 || object == paddle)
            ball_vy = ball_vy * -1;

        if (getX(ball) <= 0 || getX(ball) + 2 * RADIUS >= getWidth(window))
            ball_vx = ball_vx * -1;

        // Rendering
        move(ball, ball_vx, ball_vy);
        pause(10);
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #4
0
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;
    updateScoreboard(window, label, points);

    // Initial parameters
    double xvelocity = drand48()*0.09;
    double yvelocity = 0.09;
    waitForClick();
    
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // Moves the paddle with the mouse
        GEvent event = getNextEvent(MOUSE_EVENT);
        
        if (event != NULL)
        {
            if (getEventType(event) == MOUSE_MOVED)
            {
                double x = getX(event) - getWidth(paddle) / 2;
                setLocation(paddle, x, HEIGHT - (HEIGHT / 7));
            }
        } 
        // Moves the ball and bounces it of the edges       
        move(ball, xvelocity, yvelocity);
       
        if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
            xvelocity = -xvelocity;
        }    
        if (getX(ball) <= 0)
        {
            xvelocity = -xvelocity;
        }
        if (getY(ball) <= 0)
        {
            yvelocity = -yvelocity;
        }
        
        //Detects if player losses a life
        if (getY(ball) + getHeight(ball) >= getHeight(window))
        {
            lives = lives - 1;
            waitForClick();
            removeGWindow(window, ball);
            ball = initBall(window);
        }
       
        // Detects collision
         
        GObject object = detectCollision(window, ball);
        
        if (object != NULL)
        {
            if (strcmp(getType(object), "GRect") == 0)
            {
                yvelocity = -yvelocity;
                if (object != paddle)
                {
                    removeGWindow(window, object);
                    bricks = bricks - 1;
                    points = points + 1;
                    updateScoreboard(window, label, points);
                }
            }
        }
    }
    
    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #5
0
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);
    add(window, ball);

    // instantiate paddle, centered at bottom of window
    GRect paddle = initPaddle(window);
    add(window, paddle);

    // instantiate scoreboard, centered in middle of window, just above ball
    GLabel label = initScoreboard(window);
    add(window, label);
    setLocation(label, WIDTH / 2, HEIGHT / 2);

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

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;
    
    // let scoreboard show up at the beginning of the game
    updateScoreboard(window, label, points);
    
    // declare once useful variables for game loop
    double mouse_x, paddle_x;
    double ball_x, ball_y;
    
    // start the ball with random x and y velocity (scaled down by const)
    double ball_x_velocity = fmod(drand48() * BALL_VELOCITY, BALL_VELOCITY);
    double ball_y_velocity = 0.1;
    
    // wait for click before entering the game loop
    waitForClick(); 
  
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // move the ball  
        move(ball, ball_x_velocity, ball_y_velocity);
        
        // check user input (mouse movements)
        GEvent event = getNextEvent(MOUSE_EVENT);
        if (event != NULL && getEventType(event) == MOUSE_MOVED)
        {
            // set paddle x to be the paddle center aligned to the x mouse event 
            // keep paddle y constant (don't move through the y axis)
            mouse_x = getX(event);
            paddle_x = mouse_x - PADDLE_WIDTH / 2;
            // check mouse event x to keep paddle in the canvas
            if (mouse_x <= PADDLE_WIDTH / 2)
                paddle_x = 0;
            else if (mouse_x >= WIDTH - PADDLE_WIDTH / 2)
                paddle_x = WIDTH - PADDLE_WIDTH;
            // update paddle position
            setLocation(paddle, paddle_x, PADDLE_BOTTOM_OFFSET);    
        }
        
        // store x and y for ball and prevent further functions calls
        ball_x = getX(ball);
        ball_y = getY(ball);
        
        // bouncing checks
        // top window bouncing
        if (ball_y <= 0) 
            ball_y_velocity = -ball_y_velocity;
        // bottom window bouncing
        else if (ball_y + BALL_DIMENSION >= HEIGHT)
        {
            // spawn ball from the center
            setLocation(ball, WIDTH / 2 - BALL_RADIUS, HEIGHT / 2 - BALL_RADIUS);
            // center the paddle
            setLocation(paddle, WIDTH / 2 - PADDLE_WIDTH / 2, 
                        PADDLE_BOTTOM_OFFSET);
            // pause 1 sec, update lives and starting ball velocity to random 
            pause(1000);
            ball_x_velocity = fmod(drand48() * BALL_VELOCITY, BALL_VELOCITY);
            lives--;
        }    
        // left and right bouncing
        if (ball_x + BALL_DIMENSION >= WIDTH || ball_x <= 0)
            ball_x_velocity = -ball_x_velocity;
            
        // check for collisions
        GObject collided_obj = detectCollision(window, ball);
        if (collided_obj != NULL)
        {
            // check for rectangular object type
            if (strcmp(getType(collided_obj), "GRect") == 0)
            {   
                // ball hit the paddle
                if (collided_obj == paddle)
                {  
                    ball_y_velocity = -ball_y_velocity;
                    // prevent paddle-ball overlapping
                    setLocation(ball, ball_x, 
                                PADDLE_BOTTOM_OFFSET - BALL_RADIUS * 2);
                }
                // ball hit a brick: remove it, update game status variable
                else if (collided_obj != paddle)
                {
                    ball_y_velocity = -ball_y_velocity;
                    removeGWindow(window, collided_obj);
                    points++; 
                    bricks++;
                    updateScoreboard(window, label, points); 
                }
            }
        }   
    }
    
    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
int main(void)
{
    // seed pseudorandom number generator
    srand48(time(NULL));

    // instantiate window
    GWindow window = newGWindow(WIDTH, HEIGHT);

    // instantiate bricks
    GRect bricks_obj[ROWS][COLS];
    initBricks(window, bricks_obj);
    
    // Two dimensional array to detect if there are bricks in a certain position
    bool check_bricks[ROWS][COLS];
    initCheckBricks(check_bricks);

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

    // get a velocity for the ball
    double velocity = drand48() + 0.9;

    // waits for a mouse's click to start the game as is in the staff's implementation
    waitForClick();

    // The side that ball must go
    double side = 1.5;

    // object: variable to detect collisions 
    GObject object;

    // index to access brick's elements
    int i, j;

    // keep track of old velocity
    double old_vel = velocity;

    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // TODO
        GEvent event = getNextEvent(MOUSE_EVENT);
        move(ball, velocity, side);

         // if we heard one
         if (event != NULL)
         {
             // if the event was movement
             if (getEventType(event) == MOUSE_MOVED)
             {
                 // ensure the paddlee follows the cursor
                 double x = getX(event) - getWidth(paddle) / 2;
                 double y = HEIGHT - 50 - (PADDLE_H / 2);
                 setLocation(paddle, x, y);
             }
         }
         // bounce off right edge of window
         if (getX(ball) + getWidth(ball) >= getWidth(window))
         {
             velocity = -velocity;
         }
        // bounce off left edge of window
         else if (getX(ball) <= 0)
         {
             velocity = -velocity;
         }
         if (getY(ball) + getHeight(ball) >= getHeight(window))
         {
             --lives;
             if (lives == 0) {
                 break;
             }
             else {
                waitForClick();
                setLocation(ball, WIDTH / 2 - (getWidth(ball) / 2), HEIGHT / 2);
             }
          }
         // bounce off left edge of window
        else if (getY(ball) <= 0)
        {
            side = -side;  
        }
        object = detectCollision(window, ball);
        if (object == paddle)
        {
            side = -side;
            velocity = (velocity < 0) ? -old_vel : old_vel;
        }
       else {
           for (i = 0; i < ROWS; ++i) {
                for (j = 0; j < COLS; ++j) { 
                    if (object == bricks_obj[i][j] && check_bricks[i][j] == true) {
                        side = -side;
                        velocity = (velocity < 0) ? -0.75 : 0.75;
                        ++points;
                        removeGWindow(window, bricks_obj[i][j]);
                        check_bricks[i][j] = false;
                        updateScoreboard(window, label, points);
                    }
                }
            }
        } 
        if (points == bricks) {
            break;
        }
        // linger before moving again
        pause(5);
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #7
0
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;
    
    // velocity of ball
    double velocityX = drand48();
    double velocityY = 3.0;

    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // Keeps the paddle on the same x coord. as the mouse
        GEvent event = getNextEvent(MOUSE_EVENT);
        if (event != NULL)
        {
            if (getEventType(event) == MOUSE_MOVED)
            {
                double x = getX(event) - getWidth(paddle) / 2;
                setLocation(paddle, x, HEIGHT - 100);
            }
        }
        
        // Makes the ball move
        move(ball, velocityX, velocityY);
        if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
            velocityX = -velocityX;
        }
        else if (getX(ball) <= 0)
        {
            velocityX = -velocityX;
        }
        
        if (getY(ball) + getHeight(ball) >= getHeight(window) && lives > 0)
        {   
            GEvent event = getNextEvent(MOUSE_EVENT);                
            if (event != NULL)
            {
                if (getEventType(event) == MOUSE_CLICKED)
                {
                    ball = initBall(window);
                    move(ball, velocityX, velocityY);
                    lives--;
                }
            }
        }
        else if (getY(ball) <= 0)
        {
            velocityY = -velocityY;
        }
        
        GObject object = detectCollision(window, ball);
        if (strcmp(getType(object), "GRect") == 0 && object != paddle)
        {
            velocityY = -velocityY;
            removeGWindow(window, object);
            bricks--;
            points++;
            updateScoreboard(window, label, points);
        }
        else if (strcmp(getType(object), "GRect") == 0)
        {
            velocityY = -velocityY;
        }

        pause(10);
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #8
0
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;

    double velocity_x = drand48()+1;
    double velocity_y = drand48()+1;
    // keep playing until game over
    waitForClick();
    while (lives > 0 && bricks > 0)
    {
        // TODO
        // check for mouse event
        GEvent event = getNextEvent(MOUSE_EVENT);
        move(ball,velocity_x,velocity_y);
        // 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);
                if(x > 0)
                {
                   setLocation(paddle, x, 500);
                }
            }
        }
       GObject object = detectCollision(window,ball);
       if(object != NULL)
       {
            if(object == paddle)
            {
                    //velocity_y = -velocity_y;
                    velocity_y = -drand48()-2;
                    
            }
            else if(strcmp(getType(object), "GRect") == 0)
            {
                //velocity_y = (-velocity_y+drand48());
                velocity_y = drand48()+2;
                removeGWindow(window, object);
                points++;
                bricks--;
                updateScoreboard( window,  label,  points);
            }
       }

       if(getY(ball)+RADIUS*2 >= HEIGHT )
       {
           if(lives > 1)
           {
                waitForClick();    
                lives -= 1;
                setLocation(ball, WIDTH/2 - RADIUS, HEIGHT/2 - RADIUS);
           }
           else
           {
                lives -= 1;
           }
     
       }
       else if(getY(ball) <= 0)
       {
           velocity_y = -velocity_y;
                
       }
       if(((getX(ball) + getWidth(ball)) >= WIDTH) || (getX(ball) <= 0) )
       {
           velocity_x = -velocity_x;
                
       }
       pause(5);
           
  }
    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #9
0
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;

    // initialize velocity variable as random number between 0.0 and 1.0 times the constant 2
    double hvelocity = drand48() * .9;
    
    double vvelocity = -0.5; //drand48() + .1  * 5;
    
    waitForClick();

    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // follow mouse forever
        // check for mouse event
        GEvent event = getNextEvent(MOUSE_EVENT);

        // if we heard one
        if (event != NULL)
        {
            // if the event was movement
            if (getEventType(event) == MOUSE_MOVED)
            {
                // ensure paddle follows cursor 
                // TODO Fix paddle so it doesn't fall outside window
                double x = getX(event) - (getWidth(paddle) / 2);
                double y = getY(paddle); 
                setLocation(paddle, x, y);
            }
        }
 
        // move ball
        move(ball, hvelocity, vvelocity);

        // bounce off right edge of window
        if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
            hvelocity = -hvelocity;
        }

        // bounce off left edge of window
        else if (getX(ball) <= 0)
        {
            hvelocity = -hvelocity;
        }
        
        // bounce off bottom edge of window (not sure why?)
        else if (getY(ball) + getHeight(ball) >= getHeight(window))
        {
            lives--;
            setLocation(ball, WIDTH/2 - RADIUS/2, (getHeight(window)/2));
            setLocation(paddle, .5*WIDTH - .5*PADDLEWIDTH, .8*HEIGHT);
            waitForClick();
            hvelocity = drand48() * .01;
            vvelocity = -.05; //drand48() + .1  * 5;
        }
        
        // bounce off top edge of window
        else if (getY(ball) <= 0)
        {
            vvelocity = -vvelocity;
        }

        GObject collisionObject = detectCollision(window, ball);
        
        if (collisionObject != NULL && (strcmp(getType(collisionObject), "GRect") == 0))
        {
           vvelocity = -vvelocity;
           
           
           if (collisionObject != paddle)
           {
            removeGWindow(window, collisionObject);
            
            bricks--;
            points++;
            updateScoreboard(window, label, points);
            
           }
         } 
           
        /** Why do I get a segmentation fault by doing this?--code above works fine, but I am 
        just curious why I get an segmentation fault error by splitting this check into two 
        separate 'if' statements instead of one as I did above
        // TODO
        
        if (collisionObject == paddle)
        {
            vvelocity = -vvelocity;
        }
        
        if (strcmp(getType(collisionObject), "GRect") == 0)
        {
            vvelocity = -vvelocity;
        }
        
        */
        
     
    }
   

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #10
0
int main(int argc, char* argv[])
{
    // 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);
    
    // instantiate LivesBoard, centered in middle of window, above Scoreboard.
    GLabel liveslabel = initLivesBoard(window);

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

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;
    
    // ball movement 
    double ball_vertical = 2.0;   
    double ball_horizontal = 2 * drand48();
    
    bool godmode = false;
    if (argc > 2)
    {
        return 1;
    }
    // detect god mode    
    if ((argc == 2) && (strcmp(argv[1], "GOD") == 0))
    {
        godmode = true;
    }
    
    waitForClick();

    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // call detectCollision func to check for collisions
        GObject object = detectCollision(window, ball);
        
        // ball collision with paddle
        if (object == paddle)
        {
            ball_vertical = -ball_vertical;
        }
        
        // detect collision with bricks
        if (object != NULL)
        {
        
            if (strcmp(getType(object), "GRect") == 0 && object != paddle)
            {
                removeGWindow(window, object);
                ball_vertical = -ball_vertical;
                points++;
                bricks--;
                updateScoreboard(window, label, points);
                if (bricks == 0)
                {
                    printf("YOU WON! :)\n");
                }
            }
        }
        
        // ball collision with bottom edge
        if (getY(ball) >= getHeight(window))
        {
            lives--;
            updateLivesboard(window, liveslabel, lives);
            setLocation(ball, BALL_X, BALL_Y);
            setLocation(paddle, PADDLE_X, PADDLE_Y);
            waitForClick();
            
            if (lives == 0)
            {
                printf("GAME OVER :(\n");
            }
        }
        // if godmode is on, set paddle-x equal to ball-x
        if (godmode == true)
        {
            setLocation(paddle, getX(ball) + (BALL_WIDTH / 2) - (PADDLE_WIDTH / 2), PADDLE_Y);
        }
        else
        {       
            // check for mouse event
            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 cursor on x-axis
                    double x = getX(event) - getWidth(paddle) / 2;
                    setLocation(paddle, x, PADDLE_Y);
                }
            }
        }   
        
        move(ball, ball_horizontal, ball_vertical);
        
        // bounce off left and right
        if (getX(ball) + getWidth(ball) >= getWidth(window) || getX(ball) <= 0)
        {
            ball_horizontal = -ball_horizontal;
        }
        // bounce off top
        else if (getY(ball) <= 0)
        {
            ball_vertical = -ball_vertical;
        }
        
        pause(7);
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #11
0
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)
    {
        // SCORE
        updateScoreboard(window,label,points);  
        
        // BALL
        /* BOUNCING */
        move(ball, x_velocity, y_velocity);
        pause(10);
        
        // MOUSE EVENT
        GEvent event = getNextEvent(MOUSE_EVENT);
        if (event != NULL)
        {
            /* if mouse was moved */
            if (getEventType(event) == MOUSE_MOVED)
            {
                /*move paddle were mouse goes */
                double x = getX(event) - getWidth(paddle) / 2;
                double y = 400;
                setLocation(paddle, x, y);
            }
        }
        
        /* Collision */
        /* ball touching paddle */
        GObject object = detectCollision(window, ball);
        if (object != NULL)
        {
            if (object == paddle)
            {
                y_velocity = -y_velocity;
            }
            else if (object != paddle)
            {
                if (strcmp(getType(object), "GRect") == 0)
                {
                    removeGWindow(window,object);
                    y_velocity = -y_velocity;
                    points++;
                    bricks--;
                }
            }
        }
        
        /* ball touching wall on the right */
        if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
            x_velocity = -x_velocity;
        }
        /* ball touching wall on the left */
        if (getX(ball) <= 0)
        {
            x_velocity = -x_velocity;
        }
        /* ball touching wall on the top */
        if (getY(ball) <= 0)
        {
            y_velocity = -y_velocity;
        }
        /* ball touching wall on the bottom */
        if (getY(ball) + getHeight(ball) >= getHeight(window))
        {
            setLocation(ball,190,300);
            setLocation(paddle, 190, 400);
            lives--;
            waitForClick();
        }
    }
      
    //  END MESSAGE
    if (bricks > 0)
    {
        GLabel end = newGLabel("GAME OVER!");
        setFont(end,"SandSerif-50");
        setColor(end, "BLUE");
        add(window,end);
        setLocation(end,25,300);
    }
    else if (bricks == 0)
    {
        GLabel end = newGLabel("WINNER!");
        setFont(end,"SandSerif-50");
        setColor(end, "BLUE");
        add(window,end);
        setLocation(end,25,300);
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #12
0
File: breakout.c Project: trvvss/C
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;
    
    x_velocity=1.5;
    y_velocity=3.0;

    // keep playing until game over
    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)
            {
                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-65");
        setColor(game_over, "ORANGE");
        add(window,game_over);
        setLocation(game_over,15,300);
    }
    else
    {
        GLabel game_over=newGLabel("You Win!");
        setFont(game_over, "SansSerif-65");
        setColor(game_over, "BLUE");
        add(window, game_over);
        setLocation(game_over,15,300);
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #13
0
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;
    
    //velocity
    double velocityx = drand48() * 2;
    double velocityy = 2.0;
    
    //update scoreboard
    updateScoreboard(window, label, points);
                    
    
    int count = 1, lala = 0;
    
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // TODO
        
        //move paddle
        GEvent event = getNextEvent(MOUSE_EVENT);
        
        if(event != NULL)
        {
            if (getEventType(event) == MOUSE_MOVED)
            {
                double x = getX(event) - getWidth(paddle)/2 ;
                setLocation(paddle, x, 550);
            }
        } 
        
        //move ball
        move(ball, velocityx, velocityy);
            
        if(getX(ball) + getWidth(ball) >= getWidth(window))
            velocityx = -velocityx;
            
        else if(getX(ball) <= 0)
            velocityx = -velocityx;
            
        else if(getY(ball) <= 0)
            velocityy = -velocityy;
            
         pause(10);
         
         //detect collision
         GObject object = detectCollision(window, ball);
         
         if (object != NULL)
         {
            if(strcmp(getType(object), "GLabel") == 0 && object != NULL)
            {
                continue;
            }
         
            if(strcmp(getType(object), "GRect") == 0)
            {
                if (object == paddle)
                    velocityy = -velocityy;
                    
                else
                {
                    removeGWindow(window,object);
                    velocityy = -velocityy;
                    count++;

                    if (count > 25)
                    {
                        points += 2;
                        updateScoreboard(window, label, points);
                        if(lala == 0)
                        {
                            velocityx *= 2;
                            velocityy *= 2;
                        }
                        lala = 1;
                    }
                        
                    else if (count >= 25 && count < 45)
                    {
                        points += 3;
                        updateScoreboard(window, label, points);
                        if(lala == 1)
                        {
                            velocityx *= 2;
                            velocityy *= 2;
                        }
                        lala = 0;
                    }
                    
                    else if (count >= 45 && count < 50)
                    {
                        points += 4;
                        updateScoreboard(window, label, points);
                        if(lala == 0)
                        {
                            velocityx *= 2;
                            velocityy *= 2;
                        }
                        lala = 1;
                    }
                        
                    else
                    {
                        points++; 
                        updateScoreboard(window, label, points);
                    }
                }
            }
         }
         
         //loose life for dropping the ball
         if(getY(ball) + getHeight(ball) >= getHeight(window))
         {
            lives--;
            points -= 5;
            //waitForClick();
            setLocation(ball, 192, 342);
            setLocation(paddle, 160, 550);
            waitForClick();
            continue;
         }
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #14
0
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 = 50;

    // keep playing until game over
    double bdx = drand48()/10;
    double bdy = drand48()/10;
    while (lives > 0 && bricks > 0)
    {
        // TODO
       //move paddle

    
        // check for mouse event
        GEvent event = getNextEvent(MOUSE_EVENT);

        // if we heard one
        if (event != NULL)
        {
            // if the event was movement
           if (getEventType(event) == MOUSE_MOVED)
            {
            double x;
                // ensure circle follows top cursor
                x = getX(event)-getWidth(paddle)/2;
                if (getX(event)>=400-getWidth(paddle)/2)
                x = 400-getWidth(paddle);
                if (getX(event)<=getWidth(paddle)/2)
                x = 0;
                
                  
                setLocation(paddle, x, 580);
            }
         }
    
    // Move ball
    move(ball,bdx,bdy);
        // bounce off edges
        // left or right
        if ((getX(ball) + getWidth(ball) >= WIDTH) || getX(ball) <= 0)
            bdx = -bdx;
        // top
        else if ( getY(ball) <= 0)
            bdy = -bdy;
        else if ( (getY(ball) + getHeight(ball) >= HEIGHT))
            {
              lives--;
             setLocation(ball, 190, 290); 
             waitForClick();  
            }
           
  
  
  GObject object = detectCollision(window, ball);
        
        if (object!=NULL && object!=label)
            bdy = -bdy;
     
        
         if (object!=NULL && object!=paddle && object!=label)
              {
                points--;
                removeGWindow(window, object);
              }
              
  GLabel initScoreboard(GWindow window);
  
  updateScoreboard(window, label, points);
         
                
                
    }      

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #15
0
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;

    double x_velocity = getRandomVelocity();
    double y_velocity = 0.05;

    GObject collision;

    char label_text[20];
    snprintf(label_text, 20, "Score: %d", points);
    setLabel(label, label_text);

    waitForClick();

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

        move(ball, x_velocity, y_velocity);

        collision = detectCollision(window, ball);

        if (getX(ball) + getWidth(ball) >= getWidth(window))
            x_velocity *= -1;
        else if (getX(ball) <= 0)
            x_velocity *= -1;

        if (collision == paddle)
            y_velocity *= -1;
        else if (collision != NULL && strcmp(getType(collision), "GRect") == 0) {
            bricks--;
            points++;
            snprintf(label_text, 10, "Score: %d", points);
            setLabel(label, label_text);
            y_velocity *= -1;
            removeGWindow(window, collision);
        }

        if (getY(ball) + getHeight(ball) >= getHeight(window)) {
            lives--;
            if (lives == 0)
                break;
            setLocation(ball, (WIDTH - RADIUS)/2, (HEIGHT - RADIUS)/2);
            x_velocity = getRandomVelocity();
            waitForClick();
        } else if (getY(ball) <= 0)
            y_velocity *= -1;

        GEvent event = getNextEvent(MOUSE_EVENT);

        if (event != NULL) {
            if (getEventType(event) == MOUSE_MOVED) {
                double x = getX(event) - PADDLE_WIDTH/2;
                double y = getY(paddle);
                if (x >= 0 && x + PADDLE_WIDTH <= WIDTH)
                    setLocation(paddle, x, y);
            }
        }
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #16
0
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;

    // initial velocities
    double x_velocity = drand48() + 1.5;
    double y_velocity = 2.0;

    waitForClick();

    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // Check for next mouse event
        GEvent event = getNextEvent(MOUSE_EVENT);

        if (event != NULL)
        {
            if (getEventType(event) == MOUSE_MOVED)
            {
                // Calculate paddle's new x and y co-ordinates
                double x = getX(event) - PADDLE_WIDTH / 2;
                double y = getY(paddle);

                // Ensure paddle doesn't go off screen
                if (x > (WIDTH - PADDLE_WIDTH))
                {
                    x = (WIDTH - PADDLE_WIDTH);
                }
                else if (x < 0)
                {
                    x = 0;
                }

                // Update paddle location
                setLocation(paddle, x, y);
            }
        }

        // Move the ball
        move(ball, x_velocity, y_velocity);

        // Check if the ball hit right edge
        if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
            x_velocity = -x_velocity;
        }
        // Or the left edge
        else if (getX(ball) <= 0)
        {
            x_velocity = -x_velocity;
        }

        GObject object = detectCollision(window, ball);

        // Check if the ball hit the paddle
        if (object == paddle)
        {
            y_velocity = -y_velocity;
        }
        // Or one of the bricks
        else if (strcmp(getType(object), "GRect") == 0)
        {
            removeGWindow(window, object);
            bricks -= 1;
            points += 1;
            updateScoreboard(window, label, points);
            y_velocity = -y_velocity;
        }
        // Or the bottom edge of the screen
        else if (getY(ball) + getHeight(ball) >= getHeight(window))
        {
            lives -= 1;
            waitForClick();
            removeGWindow(window, ball);
            ball = initBall(window);
        }
        // Or the top edge
        else if (getY(ball) <= 0)
        {
            y_velocity = -y_velocity;
        }

        // Pause for 10 ms
        pause(10);
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #17
0
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;
    
    // ball speed set to random
    double velocity_x = drand48() * 4;
    double velocity_y = 3.0;
    
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // check for mouse event
        GEvent event = getNextEvent(MOUSE_EVENT);
        // if we heard one
        if (event != NULL)
        {
            // if the event was movement
            if (getEventType(event) == MOUSE_MOVED)
            {
                // ensure paddle follows top cursor
                double x = getX(event) - getWidth(paddle) / 2;
                double y = 500;
                setLocation(paddle, x, y);
            }
        }   
        // moving the ball
        move(ball, velocity_x, velocity_y);

        // bounce off right edge of window
        if (getX(ball) + getWidth(ball) > getWidth(window))
        {
            velocity_x = -velocity_x;
        }
        // bounce off left edge of window
        else if (getX(ball) <= 0)
        {
            velocity_x = -velocity_x;
        } 
        // bounce off top edge
        if (getY(ball) <= 0)
        {
            velocity_y = -velocity_y;
        }
        // collision with paddle
        GObject object = detectCollision(window, ball);
        if (object == paddle)
        {
            velocity_y = -velocity_y;
        }
        // break some bricks
        if (object != NULL)
        if ((strcmp(getType(object), "GRect") == 0) && (object != paddle))    
        {
            points++;
            removeGWindow(window, object);
            velocity_y = -velocity_y;
            char s[12];
            sprintf (s,"%i", points);
            setLabel(label, s);
            
            if (points == 50)
            {
                break;
            }

        }
      
        // bounce off bottom edge
        if (getY(ball) + getHeight(ball) >= getHeight(window))
        {
            lives--;
            removeGWindow(window, ball);
            waitForClick();
            ball = initBall(window);  
        }
       
        // linger before moving again
        pause(10);
    }

    waitForClick();
     
    // game over
    //if points is equal to the number of bricks you win
    closeGWindow(window);
    return 0;
}
Example #18
0
int main(void)
{
    // seed pseudorandom number generator
    srand48(time(NULL));

    // instantiate window
    GWindow window = newGWindow(WIDTH, HEIGHT);

    // instantiate bricks
    initBricks(window);
    double bdx = BALL_DX;
    double bdy = BALL_DY(BALL_DX);

    // 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
    GObject collisionObject;
    waitForClick();

    while (lives > 0 && bricks > 0)
    {
        GEvent eMouse = getNextEvent(MOUSE_EVENT);
        if (eMouse != NULL)
        {
            if (getEventType(eMouse) == MOUSE_MOVED)
            {
                int x = getX(eMouse) - getWidth(paddle) / 2;
                setLocation(paddle, x, PADDLE_Y);
            }
            
        }
              
        move(ball,bdx,bdy);
        if((getX(ball) + getWidth(ball) >= WIDTH || getX(ball) <= 0))
        {
            bdx *= -1;
        }
        else if(getY(ball) <= 0)
        {
            bdy *= -1;
        }
        else if ((getY(ball) + getHeight(ball) >= HEIGHT))
        {
            lives--;
            setLocation(ball, (WIDTH/2) - RADIUS, (HEIGHT / 2) - RADIUS);
            setLocation(paddle,PADDLE_X, PADDLE_Y);
            bdx = BALL_DX;
            bdy = BALL_DY(BALL_DX);
            waitForClick();
        }

        collisionObject = detectCollision(window, ball);

        if (collisionObject != NULL && strcmp(getType(collisionObject), "GRect") == 0)
        {
            bdy *= -1;
            if (collisionObject != paddle)
            {
                removeGWindow(window, collisionObject);
                bricks--;
                points++;
                updateScoreboard(window, label, points);
                printf("bricks: %d points: %d\n", bricks, points);
            }
        }
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #19
0
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;

   //initialize velocity
   double xvelocity = drand48();
   double yvelocity = drand48(); 
   
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
    
    //start moving ball
    move(ball, xvelocity, yvelocity);
    
    //set scoreboard
    updateScoreboard(window, label, points);
    
    //bounce off left and right edges of window
    if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
          xvelocity = -xvelocity;
        }
    else if (getX(ball) <= 0)
        {
          xvelocity = -xvelocity;
        }
    pause(1);
    
    //stop if touching bottom
    if(getY(ball) + getHeight(ball) >= getHeight(window))
      {
        lives--;
        setLocation(ball, 185, (paddleY/2));
        setLocation(paddle,175, paddleY);
        waitForClick();
      }
    //bounce off top wall   
    else if (getY(ball) <= 0)
      {
        yvelocity = -yvelocity;
      }
    
    
    //detect collision
    GObject object = detectCollision(window, ball);
    
    if(object != NULL)
    {
    //Bounce if rectangle    
      if(strcmp(getType(object), "GRect") == 0)
      {
        yvelocity = -yvelocity;
        
        //if brick, remove it reset add points, lower bricks
        if(object != paddle)
          {
          removeGWindow(window, object);
          bricks--;
          points++;
          }
        else if(object == paddle)
          {
          if(getY(ball) + getHeight(ball) > getY(paddle) + getHeight(paddle))
            {
            lives--;
            setLocation(ball, 185, (paddleY/2));
            setLocation(paddle,175, paddleY);
            waitForClick();
            }
          else if(getY(ball) + getHeight(ball) <= getY(paddle))
            {
            yvelocity = -yvelocity;
            }
            
          }    
      }
       
      //ignore scoreboard   
      else if (strcmp(getType(object), "GLabel") == 0)
      {
      ;
      }
    }
      
        //check for mouse movement
        GEvent event = getNextEvent(MOUSE_EVENT);
        
        //if mouse moves
        if(event != NULL)
          {
           
          //verify event was mouse movement
          if(getEventType(event) == MOUSE_MOVED)           
            {
            //Follow mouse along x-axis
            double x = getX(event) - getWidth(paddle) / 2;
            double y = paddleY;
            setLocation(paddle, x, y);    
            }
            
          }       
     }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #20
0
int main(int argc, char* argv[])
{
    // 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;

    // horizontal & vertical velocity
    double xVelocity = (drand48() - .5) * 4;
    double yVelocity = 2;

    // check for GODMODE
    bool godMode = false;
    if (argc == 2)
    {
        if (strcmp(argv[1], "GOD") == 0)
        {
            godMode = true;
        }    

    }


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


        GObject object = detectCollision(window, ball);
        if (object != NULL)
        {
            // determine behavior based on collided object
            if (object == paddle)
            {
                // bounce ball back up and change bounce angle based on collision location with paddle
                yVelocity = -yVelocity;
                xVelocity = ((getX(ball) + RADIUS) - (getX(paddle) + getWidth(paddle) / 2)) / 10;
            }
            else if (object != label) 
            {
                // adds points based on which row brick is from
                points += ROWS - (getY(object) - 30) / (BRICK_HEIGHT + BRICK_SEP);
            
                removeGWindow(window, detectCollision(window, ball));
                yVelocity = -yVelocity;

                // decreases paddle size slowly based on remaining bricks, minimum of half original size
                if (getWidth(paddle) > PADDLE_WIDTH / 2)
                {    
                    setSize(paddle, PADDLE_WIDTH * (( 2.0 * (ROWS * COLS) - points) / (2.0 * (ROWS * COLS))), PADDLE_HEIGHT);
                }
                updateScoreboard(window, label, points); 
                bricks--; 
            }

        }
    
        move(ball, xVelocity, yVelocity);
        
        pause(10);

        // check for collision with edges
        if (getX(ball) + getWidth(ball) >= WIDTH || getX(ball) <= 0)
        {
            xVelocity = -xVelocity;
        }        

        if (getY(ball) <= 0)
        {
            yVelocity = -yVelocity;
        }        
    
        // reset ball and reduce lives if ball touches bottom
        if (getY(ball) + getHeight(ball) >= HEIGHT)
        {
            lives--;
            waitForClick();
            setLocation(ball, WIDTH / 2, HEIGHT / 2);
        }        
        
        // if god mode is not active, move paddle with mouse, else paddle moves automagically
        if (godMode == false)
        {
            // move paddle with mouse
            GEvent event = getNextEvent(MOUSE_EVENT);
            if (event != NULL)
            {
                if (getEventType(event) == MOUSE_MOVED)
                {
                    setLocation(paddle, getX(event) - (PADDLE_WIDTH / 2), PADDLE_OFFSET);
                }
            }
        }
        else
        {
            // drand used to encourage variety in ball angle to finish game faster
            setLocation(paddle, (getX(ball) + RADIUS) - (getWidth(paddle) / 2) - (drand48() - .5) * 9, PADDLE_OFFSET);
        }
        
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #21
0
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;
    
    // initial velocity of ball in the x and y direction
    double xVelocity = ((1.0 * drand48()) + 2.0);
    double yVelocity = ((1.0 * drand48()) + 2.0);
    
    waitForClick();

    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {   
		// sets ball in motion
        move(ball, xVelocity, yVelocity);
        
        // bounce off right edge of window
        if (getX(ball) + getWidth(ball) >= WIDTH)
        
            xVelocity = -xVelocity;

        // bounce off left edge of window
        else if (getX(ball) <= 0)

            xVelocity = -xVelocity;
        
        // hit bottom edge bottom edge and reset, lose life
        else if (getY(ball) + getWidth(ball) >= HEIGHT)
        {
		    removeGWindow(window, ball);
            ball = initBall(window);
            lives--;
            waitForClick();
        }    
        // bounce off top edge of window    
        else if (getY(ball) <= 0)
        
            yVelocity = -yVelocity;
            
        // linger before moving again
        pause(10);
             
    	// creates variable to control paddle movement 
        GEvent paddleMovement = getNextEvent(MOUSE_EVENT);
        
        if (paddleMovement != NULL)
        {
            // if the event was movement 
            if (getEventType(paddleMovement) == MOUSE_MOVED)
            {
                // check to make sure the entire paddle stays within 
                // the confines of the game window
                if (getX(paddleMovement) >= (PWIDTH / 2) &&\
                    getX(paddleMovement) <= WIDTH - (PWIDTH/2))
            	{
            	    double x = getX(paddleMovement) - (PWIDTH/2);
                    setLocation(paddle, x, 550);
                }
            }
        }
        
        // detects if ball collides with another GObject in game window
    	GObject object = detectCollision(window, ball);
        
        if (object == NULL)
        {
            continue;   
        }
        
        else if (object == label)
        
            continue;
            
        else if (object == paddle)
        {
            yVelocity = -yVelocity;
        }
        
        else
        {
            yVelocity = -yVelocity;
            bricks--;
            points++;
            removeGWindow(window, object);
            updateScoreboard(window, label, points);
        }
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #22
0
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;

    // wait for click before starting
    waitForClick();
    
    // ball speed
    double xspeed =drand48() + 1.5;
    double yspeed = 2.5;

    // keep playing game until these conditions are met
    while (lives > 0 && bricks > 0)
    {        
        // Scoreboard
        updateScoreboard(window, label, points);
        
        // move ball
        move(ball, xspeed, yspeed);

        pause(10);
        
        // listen for mouse event
        GEvent event = getNextEvent(MOUSE_EVENT);
        
        // validation for an event
        if (event != NULL)
        {
            if (getEventType(event) == MOUSE_MOVED)
            {
                // make sure paddle follows the mouse
                double x = getX(event) - getWidth(paddle) / 2;
                double y = HEIGHT - 100;
                setLocation(paddle, x, y);
            }
        }
        
        
        GObject object = detectCollision(window, ball);
        
        if (object != NULL)
        {
            // when ball strucks the paddle
            if (object == paddle)
            {
                yspeed = -yspeed;
            }
            
            // keep track of stats 
            else if (strcmp(getType(object), "GRect") == 0)
            {
                removeGWindow(window, object);
                yspeed = -yspeed;
                points = points + 1;
                bricks = bricks - 1;                
            }
        }
        
        
        // when ball hits left wall
        if (getX(ball) <= 0)
        {
            xspeed = -xspeed;
        }
        
        // when the ball hits the top wall.
        if (getY(ball) <= 0)
        {
            yspeed = -yspeed;
        }
   
        // when ball hits the right wall
        if (getX(ball) + getWidth(ball) >= 400)
        {
            xspeed = -xspeed;
        }
        
        // bottom wall below paddle
        if (getY(ball) + getHeight(ball) >= 600)
        {
            lives = lives - 1;
            // reset
            setLocation(ball, WIDTH / 2 - RADIUS, HEIGHT / 2 + RADIUS);
            setLocation(paddle, 160, 500);
            waitForClick();
        }
      
        // end of game, winner!
        if (points == 25)
        {
            GLabel win = newGLabel("Good Job! You win!");
            setFont(win, "SansSerif-36");
            setColor(win, "BLUE");
            setLocation(win, 15, 300);
            add(window, win);
       
        }
        
    }

   
  
   
    waitForClick();


    closeGWindow(window);
    return 0;
}
Example #23
0
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;

    double drand48(void);
    double xVelo = drand48() + 1;
    double yVelo = drand48() + 2;

    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        move(ball, xVelo, yVelo);

        pause(10);

        GObject object = detectCollision(window, ball);

        if(object != NULL)
        {
            if (object == paddle)
            {
                yVelo = -yVelo;
            }
            else if(strcmp(getType(object), "GRect") == 0 && object != paddle)
            {
                removeGWindow(window, object);
                points++;
                bricks--;
                updateScoreboard(window, label, points);
                yVelo = -yVelo;
            }
            else if(strcmp(getType(object), "GLabel") == 0)
            {
                continue;
            }
        }

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

        else if(getY(ball) <= 0)
        {
            yVelo = -yVelo;
        }
        else if (getX(ball) + getWidth(ball) >= getWidth(window) || getX(ball) <= 0)
        {
            xVelo = -xVelo;
        }
        else if(getY(ball) > HEIGHT)
        {
            lives--;
            setLocation(ball, (WIDTH/2), (HEIGHT/2));
            setLocation(paddle, 172, 525);
            waitForClick();
        }


        GEvent event = getNextEvent(MOUSE_EVENT);

        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;
                double y = 525;
                setLocation(paddle, x, y);
            }
        }

    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #24
0
int main(int argc, char* argv[])
{
    // 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);
    // add ball to window
    add(window, ball);

    // instantiate paddle, centered at bottom of window
    GRect paddle = initPaddle(window);
    // add paddle to window
    add(window, paddle);

    // instantiate scoreboard, centered in middle of window, just above ball
    GLabel label = initScoreboard(window);
    // add label to window
    add(window, label);
    
    // declare laser
    GRect laser = NULL;

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

    // number of points initially
    int points = 0;
    
    // ball velocity
    double velocity_x = drand48() * 2 + 3;
    double velocity_y = drand48() * 2 + 3;
    bool ballMove = false;
    
    // GOD mode on
    bool God = false;
    if (argc == 2)
    {
        if (strcmp(argv[1], "GOD") == 0)
        {
            God = true;
        }
    }
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // check for mouse event
        GEvent event = getNextEvent(MOUSE_EVENT);
        
        // when clicked, ball starts to move
        if (event != NULL)
        {   
            // if the event was moved
            if (getEventType(event) == MOUSE_CLICKED)
            {
                ballMove = true;
            }
        }
        
        // ball movement
        if (ballMove == true)
        {
            move(ball, velocity_x, velocity_y);
            // when ball hits the left edge
            if (getX(ball) <= 0)
            {
                velocity_x *= -1;
            }
            // when ball hits the right edge
            else if (getX(ball) + getWidth(ball) >= getWidth(window))
            {
                velocity_x *= -1;
            }
            // when ball hits the top edge
            else if (getY(ball) <= 0)
            {
                velocity_y *= -1;
            }
            // when ball hit the bottom edge
            else if (getY(ball) >= getHeight(window))
            {
                lives -= 1;
                ballMove = false;
                setLocation(ball, WIDTH / 2, HEIGHT / 2);
            }
            pause(10);
        }
        
        // if God mode is on, paddle x-position follows ball
        if (God == true)
        {
             double y = getY(paddle);
             setLocation(paddle, getX(ball) + RADIUS - PADDLEWIDTH / 2.0, y);
        }
        // else if God mode is off, paddle follow mouse movement
        else
        {
            // if we heard one
            if (event != NULL)
            {
                // if the event was moved
                if (getEventType(event) == MOUSE_MOVED && ballMove == true)
                {
                    double x = getX(event) - PADDLEWIDTH / 2;
                    double y = getY(paddle);
                    // ensure paddle doesn't go out of window
                    if (getX(event) <= 0 + PADDLEWIDTH / 2)
                    {
                        setLocation(paddle, 0, y);    
                    }
                    else if (getX(event)>= getWidth(window) - PADDLEWIDTH / 2)
                    {
                        setLocation(paddle, getWidth(window) - PADDLEWIDTH, y);
                    }
                    else
                    {
                        setLocation(paddle, x, y);
                    }
                }
            }
        }
        
        // during gameplay, when mouse is clicked, shoot laser
        if (event != NULL)
        {
            if (ballMove == true && getEventType(event) == MOUSE_CLICKED && laser == NULL)
            {
                // instantiate laser
                laser = newGRect(getX(paddle) + PADDLEWIDTH / 2 - LASERWIDTH / 2, getY(paddle) - LASERHEIGHT, LASERWIDTH, LASERHEIGHT);
                setColor(laser, "RED");
                setFilled(laser, true);
                add(window, laser);
                
            }
        }
        
        // if laser exists
        if (laser != NULL)
        {
            // detect collision of laser with object
            GObject object2 = detectCollision2(window, laser);
            if (object2 != NULL)
            {
                // if laser hits brick
                if (strcmp(getType(object2), "GRect") == 0 && object2 != paddle)
                {
                    // remove laser and brick hit
                    GObject laserObj = getGObjectAt(window, getX(laser), getY(laser));
                    removeGWindow(window, laserObj);
                    laser = NULL;
                    removeGWindow(window, object2);
                    //  bricks higher in the game’s grid are worth more points than are bricks lower in the game’s grid
                    int brickheight = (100 - GAP * (ROWS + 1)) / ROWS;
                    for (int k = 0; k < ROWS; k++)
                    {
                        if (getY(object2) == 50 + (GAP * (k + 1)) + (brickheight * k))
                        {
                            points += ROWS - k;
                            break;
                        }
                    }
                    updateScoreboard(window, label, points);
                    // decrease paddle width
                    bricks -= 1;
                    setSize(paddle, PADDLEWIDTH - PADDLEWIDTH * 2 / 4.0 * (((ROWS * COLS) - bricks) / (float)(ROWS * COLS)), PADDLEHEIGHT);
                    // increase velocity of ball
                    velocity_x += 0.1;
                    velocity_y += 0.1;
                }
                // else if laser hits ball
                else if (object2 == ball)
                {
                    GObject laserObj = getGObjectAt(window, getX(laser), getY(laser));
                    removeGWindow(window, laserObj);
                    laser = NULL;
                    break;
                }
                // else if laser over top edge
                else if (getY(laser) + LASERHEIGHT >= 0)
                {
                    GObject laserObj = getGObjectAt(window, getX(laser), getY(laser));
                    removeGWindow(window, laserObj);
                    laser = NULL;
                }
            }
            else
            {
                move(laser, 0, -velocity_laser);
            }
        }
        
        // detect collision of ball with object
        GObject object = detectCollision(window, ball);
        if (object != NULL)
        {
            // if ball collide with paddle
            if (object == paddle)
            {
                velocity_y *= -1;
            }
            // if ball collide with GRect object other than paddle (i.e. bricks)
            else if (strcmp(getType(object), "GRect") == 0)
            {
                velocity_y *= -1;
                removeGWindow(window, object);
                //  bricks higher in the game’s grid are worth more points than are bricks lower in the game’s grid
                int brickheight = (100 - GAP * (ROWS + 1)) / ROWS;
                for (int k = 0; k < ROWS; k++)
                {
                    if (getY(object) == 50 + (GAP * (k + 1)) + (brickheight * k))
                    {
                        points += ROWS - k;
                        break;
                    }
                }
                updateScoreboard(window, label, points);
                // paddle width decrease
                bricks -= 1;
                setSize(paddle, PADDLEWIDTH - PADDLEWIDTH * 2 / 4.0 * (((ROWS * COLS) - bricks) / (float)(ROWS * COLS)), PADDLEHEIGHT);
                // velocity of ball increases
                velocity_x += 0.1;
                velocity_y += 0.1;
            }
        }
        // when no more live or all bricks are broken
        if (lives == 0 || bricks == 0)
        {
            break;
        }
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #25
0
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;

    // set velocity
    double randMagnitude = (drand48() + 1) / 40;
    int direction = (rand() % 2) * 2 - 1;
    double velocity_x = direction * randMagnitude;
    double velocity_y = 1;
    
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        GEvent event = getNextEvent(MOUSE_EVENT);
        
        if(event != NULL)
        {
            if(getEventType(event) == MOUSE_MOVED)
            {
                double paddle_x = getX(event) - getWidth(paddle);
                double paddle_y = getY(paddle);
                
                setLocation(paddle, paddle_x, paddle_y);
            }
        }
        
        move(ball, velocity_x, velocity_y);
        
        if(getX(ball) + getWidth(ball) >= getWidth(window))
            reverse(&velocity_x);
        else if(getX(ball) <= 0)
            reverse(&velocity_x);
        else if(getY(ball) <= 0)
            reverse(&velocity_y);
        else if(getY(ball) + getHeight(ball) >= getHeight(window))
        {
            lives--;
            setLocation(ball, (WIDTH - RADIUS) /2, (HEIGHT - 100) / 2);
            setLocation(paddle, (WIDTH - PADDLE_WIDTH) /2, HEIGHT - 50);
            waitForClick();
        }
        
        GObject collision = detectCollision(window, ball);
        
        if(collision != NULL && strcmp(getType(collision), "GRect") == 0)
        {
            reverse(&velocity_y);
            
            if(collision != paddle)
            {
                removeGWindow(window, collision);
                bricks--;
                points++;
                updateScoreboard(window, label, points);
            }
        }
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
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;

    double paddleLength = 60;
    double x, y, velocityx ,velocityy;
    velocityx = velocityy = 2;
    
        
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // Check beyong paddle position
        if (getY(ball) + getWidth(ball) >= getHeight(window)) 
        { 
            
            // if ball does not touch the bat but bottom of window
            //creates new ball object and deduct lives
            removeGWindow(window, ball); 
            freeGObject(ball); 
            lives--;
            
            if (lives <= 0)
            {
                break;
            } 
            
            initBall(window); 
            
            waitForClick();
        }
       
       // Move paddle along with mouse
       GEvent event = getNextEvent(MOUSE_EVENT);
       if (event != NULL) 
       { 
       
            if (getEventType(event) == MOUSE_MOVED) 
            { 
                double x = getX(event); 
               
                // Paddle should not go outside of the window
                if(x + getWidth(paddle) > getWidth(window))
                    x = (getWidth(window)- getWidth(paddle));
                    
                double y = getHeight(window) - 50 - getHeight(paddle);
                setLocation(paddle, x,y); 
             }
       } 
       
       // Accelerate x cordinates of the ball i.e. left and right
       
       if (getX(ball) + getWidth(ball) >= getWidth(window))
       {
            //printf("Inseide if getX(ball) event\n");
            velocityx = -velocityx;
       }
       else if (getX(ball) <= 0)
       {
            //printf("Else if getX(ball) event\n");
            velocityx = -velocityx;
       }
       //printf("After if getX(ball) event\n");
       
        
        // Acceleration control check for top of the window
        
       if (getY(ball) <= 0) 
       {
            velocityy = -velocityy;                        
       }
       
       // Acceleration control if ball collided with paddle or brick
       
       GObject object = detectCollision (window, ball); 
       
       if (object!= NULL) 
       { 
            // Acceleration control if ball collided with paddle
            if (object == paddle) 
            {
                velocityy = -velocityy; 
                move(ball, velocityx, velocityy);
            }   
                    
            // Acceleration, ScoreBoard,  brick control if ball collided with brick
            if (strcmp(getType(object), "GRect") == 0 && getY(ball) < HEIGHT / 2) 
            { 
                //ScoreBoard
                points ++; 
                updateScoreboard(window, label, points); 
                
                //bricks
                removeGWindow(window,object); 
                bricks = bricks - 1;
                
                velocityy = -velocityy;
            
                if (bricks < 0)
                {
                    break;
                }
            } 
        }
        
        move(ball, velocityx, velocityy);
        
        //
        pause (8);
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #27
0
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;
    
    // set velocity
    double x_velocity = drand48() + VELOCITY;
    double y_velocity = drand48() + VELOCITY;

    // keep playing until game over
    waitForClick();
       
    
    while(lives > 0 && bricks > 0)
    {
    // paddle follows mouse
        GEvent follow_x = getNextEvent(MOUSE_EVENT);
        if (follow_x != NULL)
        {
            if (getEventType(follow_x) == MOUSE_MOVED)
            {
                double x = getX(follow_x) - getWidth(paddle) / 2;
                double y = HEIGHT - 150;
                setLocation(paddle, x, y);
            }   
        }
        //move ball
        move(ball, x_velocity, y_velocity);
        //move ball left to right
        if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
           x_velocity = -x_velocity;
        }
         else if (getX(ball) <= 0)
        {
            x_velocity = -x_velocity;
        }
        //move ball up and down
        if (getY(ball) + getHeight(ball) >= getHeight(window))
        {
            int x = (WIDTH / 2);
            int y = (HEIGHT / 2);
            // y_velocity = -y_velocity;
            lives += -1;
            setLocation(ball, x, y);
            waitForClick();
            
        }
        else if (getY(ball) <= 0)
        {
            y_velocity = -y_velocity;
        }
        pause(10);
        
       // ball collides with object
        GObject object = detectCollision(window, ball);
        if (object != NULL)
        {
            if(strcmp(getType(object), "GLabel") == 0)
            {
                
            }else if (object == paddle)
            {
                y_velocity = -y_velocity;           
            }
            else if (strcmp(getType(object), "GRect") == 0 && object != paddle)
            {
                bricks += -1;
                removeGWindow(window, object); 
                y_velocity = -y_velocity;
                points++;
                updateScoreboard(window, label, points);
                       
            }
        }
       
   }   
              
     

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #28
0
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;
    
    // initial velocity
    double xvelocity = drand48();
    double yvelocity = 2.0;
    
    updateScoreboard(window, label, points);

    // wait for mouse clicked
    waitForClick();

    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // bounce off top edge of window
        if (getY(ball) <= 0)
        {
            yvelocity = -yvelocity;
        }

        // bounce off right edge of window
        if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
            xvelocity = -xvelocity;
        }

        // bounce off left edge of window
        else if (getX(ball) <= 0)
        {
            xvelocity = -xvelocity;
        }

        // detect collision
        if (detectCollision(window,ball) != NULL)
        {           
            GObject collision = detectCollision(window, ball);
            
            if (strcmp(getType(collision), "GRect") == 0)
            {
                // if ball collided with any bricks remove
                if (collision != paddle)
                {
                    removeGWindow(window, collision);
                    points++;
                    bricks--;
                    updateScoreboard(window, label, points);
                    yvelocity = -yvelocity;
                }
                
                // if ball collided with paddle bounce 
                else if (collision == paddle)
                {
                    yvelocity = -yvelocity;
                }           
            }
        } 

        // restart ball if hit bottom edge of window
        if (getY(ball) + getHeight(ball) >= getHeight(window))
        {
            --lives;
            if (lives <= 0)
            {
                continue;
            }
            waitForClick();
            removeGWindow(window, ball);
            ball = initBall(window);
        }

        // start moving the ball
        move(ball, xvelocity, yvelocity);
        pause(10);

        // wait for mouse event
        GEvent start_paddle = getNextEvent(MOUSE_EVENT);
            
        if (start_paddle != NULL)
        {
            // move paddle with the mouse
            if (getEventType(start_paddle) == MOUSE_MOVED)
            {            
                // ensure paddle follows top cursor
                double x = getX(start_paddle) - getWidth(paddle) / 2;
                if (x >= 0 && x <= WIDTH - 60)
                {
                    setLocation(paddle, x, HEIGHT - 60);
                }
            }
        }        
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #29
0
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 
    // TODO
    while (lives > 0 && bricks > 0)
    {
        move(ball, x_velocity, y_velocity);
        if(getX(ball) + getWidth(ball) >= getWidth(window))
        {
            x_velocity = -x_velocity;
        }
        else if(getX(ball) <= 0)
        {
            x_velocity = -x_velocity;
        }
        else if(getY(ball) <= 0)
        {
            y_velocity = -y_velocity;
        }
        else if(getY(ball) + getHeight(ball) >= getHeight(window))
        {
            lives--;
            setLocation(ball, 190, 290);
            setLocation(paddle, 180, 560);
            waitForClick();
        }
        pause(10);
         
        GObject object = detectCollision(window, ball);
        if (object != NULL && strcmp(getType(object), "GLabel") != 0)
        if (strcmp(getType(object), "GRect") == 0)
        {
            if (object == paddle)
            {
                y_velocity = -y_velocity;
                x_velocity = -x_velocity;
            }
            else
            {
                removeGWindow(window, object);
                bricks--;
                points++;
                y_velocity = -y_velocity;
                x_velocity = -x_velocity;
            }
        }
         
        //Update scoreboard
        updateScoreboard(window, label, points);
         
        GEvent event = getNextEvent(MOUSE_EVENT);
        if (event != NULL)
        {
            //if the event was mouse movement
            if(getEventType(event) == MOUSE_MOVED)
            {
                double x = getX(event) - getWidth(paddle)/2;
                double y = 560;
                setLocation(paddle, x, y);
            }
        }
    }   
    // You win! or...lose...
    if(bricks > 0)
    {
        GLabel verdict = newGLabel("That all you got?");
        setFont(verdict, "SansSerif-50");
        setColor(verdict, "C71585");
        add(window, verdict);
        setLocation(verdict, 45, 150);
    }
    else
    {
        GLabel verdict = newGLabel("You Win!!");
        setFont(verdict, "SansSerif-50");
        setColor(verdict, "C71585");
        add(window, verdict);
        setLocation(verdict, 185, 150);
    }
 
    // wait for click before exiting
    waitForClick();
 
    // game over
    closeGWindow(window);
    return 0;
}
Example #30
0
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();
    
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        double velocityX = drand48() * 5.0;
        double velocityY = 4.0;
                
        while (true)
        {
            GEvent mouse = getNextEvent(MOUSE_EVENT);
            
            if (mouse != NULL)
            {
                if (getEventType(mouse) == MOUSE_MOVED)
                {
                    double x = getX(mouse) - WPAD / 2;
                    setLocation(paddle, x, PADDLE_Y);
                }
            }
            
            
            move (ball, velocityX, velocityY);
            
            if (getX(ball) + RADIUS * 2 >= WIDTH)
            {
                velocityX = -velocityX;
            }
            
            else if (getX(ball) <= 0)
            {
                velocityX = -velocityX;
            }
            
            else if (getY(ball) + RADIUS * 2 >= HEIGHT)
            {
                lives--;
                
                if (lives <= 0)
                {
                    updateScoreboard(window, label, lives);
                    removeGWindow(window, ball);
                    removeGWindow(window, paddle);
                }
                else
                {
                    setLocation(ball, (WIDTH / 2 - RADIUS), HEIGHT / 2 + RADIUS);
                    setLocation(paddle, (WIDTH - WPAD) / 2, PADDLE_Y);
                    waitForClick();
                    velocityX = drand48() * 5.0;
                }
            }
            
            else if (getY(ball) <= 0)
            {
                velocityY = -velocityY;
            }
            
            GObject object = detectCollision(window, ball);
            
            if (detectCollision(window, ball))
            {
                if (object == paddle)
                {
                    velocityY = -velocityY;
                }
                
                else if (strcmp(getType(object), "GRect") == 0)
                {
                    if (strcmp(getType(object), "GLabel") != 0)
                    {
                        velocityY = -velocityY;
                        removeGWindow(window, object);
                        bricks--;
                        points++;
                        updateScoreboard(window, label, points);
                        
                        if(bricks <= 0)
                        {
                            removeGWindow(window, ball);
                            removeGWindow(window, paddle);
                            return false;
                        }
                        
                    }
                }
            }
            
            pause(10);         
        }
    }
    
    
    
    // wait for click before exiting
    waitForClick();

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