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;
}
Beispiel #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);
  
  // deal out the powerUps
  //initPowerUps(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;

  // how long to wait
  int wait = 9;

  // Velocity for ball
  double velocity = 3.0;
  double xvelocity = 0;

  add(window, label);
  add(window, paddle);
  add(window, ball);
  // keep playing until game over
  waitForClick();
  while (lives > 0 && bricks > 0)
  {
    GEvent event = getNextEvent(MOUSE_EVENT);
    if (event != NULL)
    {
      // if the event was movement
      if (getEventType(event) == MOUSE_MOVED)
      {
        // ensure ball follows top cursor
        double x = getX(event) - getWidth(paddle) / 2;
        setLocation(paddle, x, getY(paddle));
      }
    }
    // move ball along x-axis
    xvelocity = xvelocity <= 0? -drand48() : drand48();
    move(ball, xvelocity, velocity);

    // bounce off right edge of window
    if (getX(ball) + RADIUS*2 >= getWidth(window))
    {
      setLocation(ball, getWidth(window) - RADIUS*2, getY(ball));
      xvelocity = -xvelocity;
    }
    // bounce off left edge of window
    else if (getX(ball) <= 0)
    {
      setLocation(ball, 1, getY(ball));
      xvelocity = -xvelocity;
    }

    // die off bottom of window
    if (getY(ball) + RADIUS*2 >= getHeight(window))
    {
      lives--;
      setLocation(ball, WIDTH/2, HEIGHT/2);
      waitForClick();
      velocity = -velocity;
      xvelocity = -xvelocity;
    }
    // bounce off top of window
    else if (getY(ball) <= 0)
    {
      velocity = -velocity;
    }
    GObject collision = detectCollision(window, ball);
    if (collision != NULL && collision != label)
    {
      /*if (strcmp(getType(collision), "GOval") == 0)
      {
        setVisible(collision, false);
        if (strcmp(getColorGObject(collision), "#FFCC00") == 0)
        {
          points+=2;
        } else if (strcmp(getColorGObject(collision), "#000000") == 0)
        {
          setSize(paddle, getWidth(paddle)+20, getHeight(paddle));
          points++;
          if (getWidth(paddle) >= 120)
          {
            setColor(paddle, "#000000");
            setColor(ball, "#000000");
            wait = 3;
          }
        }
      }*/
      /*else*/ if (strcmp(getType(collision), "GRect") == 0 && collision != paddle)
      {
        setVisible(collision, false);
        bricks--;
        velocity = -velocity;
        if (strcmp(getColorGObject(collision), "#FF0000") == 0)
        {
          if (!(wait < 5))
          {
            wait = 5;
            setColor(ball, "#FF0000");
            setColor(paddle, "#FF0000");
          }
          else
          {
            // leave wait alone!
          }
          
          points += 5;
        }
        else if (strcmp(getColorGObject(collision), "#FF9933") == 0)
        {
          if (!(wait < 7))
          {
            wait = 7;
            setColor(ball, "#FF9933");
            setColor(paddle, "#FF9933");
          }
          else
          {
            // leave wait alone...
          }
          points += 3;
        }
        else if (strcmp(getColorGObject(collision), "#FFCC00") == 0)
        {
          points += 2;
        }
        else
        {
          points++;
        }
      } else {
        velocity = -velocity;
        if (getX(ball) < getWidth(paddle)/2)
        {
          xvelocity = -xvelocity;
        }
        else if (getX(ball) > getWidth(paddle)/2)
        {
          // leave xvelocity alone.
        }
      }
    }
    // linger before moving again
    pause(wait);
    updateScoreboard(window, label, points);
  }

  if (lives > 0)
  {
    char s[12];
    sprintf(s, "WIN: %i!", points);
    setLabel(label, s);
  } else {
    setLabel(label, "LOOSE!");
  }
  // wait for click before exiting
  waitForClick();

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