Exemplo n.º 1
0
/**
 * Changes the balls direction when it hits a brick.
 */
void ballHitsBrick(Velocity *velocity, 
        GWindow *window, 
        GObject *brick, 
        GLabel *label, 
        int *score, 
        int *bricksCount)
{
    invertHorizontalDirection(velocity);
    invertVerticalDirection(velocity);
    removeGWindow(*window, *brick);
    freeGObject(*brick);
    (*score)++;
    (*bricksCount)--;

    updateScoreboard(*window, *label, *score);
}
Exemplo n.º 2
0
int main(int argv, string args[] )
{
    int isGod= -1;
    if (argv ==2)
    {
        if (strcmp(args[1] ,"GOD")==0)
        {
            isGod = 1;; //printf("args %s \n",args[1]);
        }
    }
    // 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;

    // Variable declation for whiel
    
    double velocityX = drand48()+5;
    double velocityY = drand48()+1;
    
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
       // ball beyond paddle
        if( (getY(ball) + getWidth(ball)) > getHeight(window) )
        {
            lives--;
            removeGWindow(window, ball);
            freeGObject(ball);
            
            if(lives <= 0)
            {
                break;
            }
            
            initBall(window);
        }
        waitForClick();
        // paddle moment
        
        if (isGod == -1) 
        {
            GEvent event = getNextEvent(MOUSE_EVENT);
        
            if(event !=NULL)
            {
                //printf("before MOUSE_MOVED event\n");
                if(getEventType(event) == MOUSE_MOVED)
                {
                    double x = getX(event);
                
                    // repositioning paddle from going beyond the window's width
                    if(x + getWidth(paddle)  > getWidth(window))
                    {
                        x = getWidth(window) - getWidth(paddle);
                    
                    }
                    double y = getHeight(window)-SPACE - getHeight(paddle);
                
                    setLocation(paddle, x , y );
                }
                //printf("After  MOUSE_MOVED event\n");
        
            }
            
         }
         else if(isGod == 1)//Its GOD mode 
         {
            double ballX  = getX(ball);
            double ballY =  getX(ball);
            double x = ballX;
            
            if(x + getWidth(paddle)  > getWidth(window))
            {
                x = getWidth(window) - getWidth(paddle);                    
            }
            double y = getHeight(window)-SPACE - getHeight(paddle);
            setLocation(paddle, x , y );
            
         }
        // Accelerate x cordinates of the ball i.e. left and right
      
       
        if( getX(ball) + getWidth(ball) > getWidth(window) )
        {
            velocityX = - velocityX ;
        }
        else if( getX(ball) <= 0)
        {
            velocityX = - velocityX;
        }
        
        
        // 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);
            }
            
            // Ignore collision 
            if (strcmp(getType(object), "GLabel") == 0)
            {
                //do nothing
            }
            
            // Acceleration, ScoreBoard,  brick control if ball collided with brick
            if( (strcmp(getType(object), "GRect") == 0) && getY(ball) < HEIGHT / 2)
            {
                velocityY = - velocityY;
                //update points
                string colorB = getColorGObject(object);
                
                 // return type is "#rrggbb"
                if(strcmp(colorB,"RED")==0 ) 
                {
                    points = points+5;
                }
                else if(strcmp(colorB,"ORANGE")==0) 
                {
                    points = points+4;
                }
                else if(strcmp(colorB,"YELLOW")==0 )  
                {
                    points = points+3;
                }
                else if(strcmp(colorB,"GREEN")==0) 
                {
                    points = points+2;
                }
                else if(strcmp(colorB,"CYAN")==0) 
                {
                    points++;
                }
                
                // shrink bat with points increased
                if( points % 10 == 0 )
                {
                    double x = getX(paddle);
                    double y = getY(paddle);
                    
                    paddleL -= 5;
                    removeGWindow(window, paddle);
                    paddle = newGRect(x, y, paddleL, paddleH);
                    setFilled(paddle, true);
                    setColor(paddle,"BLACK");
                    
                    add(window,paddle);
                    
                    //paddle = initPaddle(window)
                    
                }
                
                //update Scoreboard
                updateScoreboard(window, label, points);
                
                //brick
                removeGWindow(window, object);
                bricks--;
                if(bricks < 0)
                {
                    break;
                }
            }    
        
        }
        
        // move ball 
        
        
        move(ball, velocityX, velocityY);
        pause(10);
        
        
    }

    // 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);
    sendForward(ball);  //send ball forword.
    
    // 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);
    sendBackward(label);    //send label backwords

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

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;

    //velocity constants for x and y directions for velocity of ball
    double velocityX = 0.029;
    double velocityY = 0.043;
    //wait for click before starting
    waitForClick();

    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {   
       //moving paddle with mouse movement in x direction only.
        // check for mouse event
        GEvent Mevent = getNextEvent(MOUSE_EVENT);
        // if we heard one
        if (Mevent != NULL)
        {
            // if the Mevent was a mouse movement
            if (getEventType(Mevent) == MOUSE_MOVED)
            {
                // moves paddle only in x direction with movement of mouse.
                double x = getX(Mevent);
                if(x > PADDLEW/2 && x < WIDTH-(PADDLEW/2))
                {
                    setLocation(paddle, x-(PADDLEW/2), PADDLEY );
                }
            }
        }

        //move ball with given velocity constants.
        move(ball, velocityX, velocityY );
            // bounce off right edge of window
            if (getX(ball) + getWidth(ball) >= getWidth(window))
            {
                velocityX = -velocityX;
            }

            // bounce off left edge of window
            else if (getX(ball) <= 0)
            {
                velocityX = -velocityX;
            }
            //bounce off the top edge of window
            else if (getY(ball) <= 0)
            {
                velocityY = -velocityY;
            }
            //if ball touch bottom edge decrese a life and reset the ball at center of window and lunnch again after click.
            else if(getY(ball) + getHeight(ball) >= getHeight(window)) 
            {
                lives = lives-1;
                waitForClick();
                setLocation(ball, WIDTH/2, HEIGHT/2);
                    //close window if live finishes.
                    if(lives == 0){
                    closeGWindow(window);
                    }
            }
        //ball movement, edge bounce , and lives calculation part end here.
        //detect collsion of ball with other objects.
            //paddlecollde is the object with ball collided.
            GObject collide = detectCollision(window ,ball);
                if(collide == paddle){
                    velocityY = -velocityY;
                    //funkey fun with color
                    char* colorpaddle = malloc(20*sizeof(char));
                    colorpaddle = getColorGObject(ball);
                    setColor(paddle,colorpaddle);
                    freeGObject(colorpaddle);
                }
                if(collide == label ){
                    //funkey fun with color
                    char* colorlabel = malloc(20*sizeof(char));
                    colorlabel = getColorGObject(ball);
                    setColor(label,colorlabel);               
                    freeGObject(colorlabel);
                }
             //collidedObj is the object with ball collides.
             GObject collidedObj = detectCollision(window, ball);   
             if(collidedObj){   
             if(collidedObj != paddle && collidedObj != label){
                    //funkey fun with color
                    char* colorball = malloc(20*sizeof(char));
                    colorball = getColorGObject(collidedObj);
                    setColor(ball,colorball);
                    freeGObject(colorball);
                    //if collided object is brick then remove bricks and increse and update score and decrese no of bricks and bounce ball.
                    removeGWindow(window, collidedObj);
                    points++;
                    bricks--;
                    updateScoreboard(window, label, points);
                    velocityY = -velocityY;
                    freeGObject(colorball);
                    freeGObject(collidedObj);
             }
          }
    }
    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    freeGObject(paddle);
    freeGObject(ball);
    freeGObject(label);
    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;
}