Example #1
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 #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 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 #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
    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 #4
0
int main(void)
{
    // seed pseudorandom number generator
    srand48(time(NULL));

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

    //instantiate timer
    GLabel timer = initScoreboard(window);

    // 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();
    
    // establish velocity
    double vx = 2.0 + drand48();
    double vy = 2.0 + drand48();
      
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        updateScoreboard(window, label, points);       
        GObject object = detectCollision(window, ball);
        GEvent event = getNextEvent(MOUSE_EVENT);
        move(ball, vx, vy);
        pause(10);
           
        if (event != NULL)
        {
            // if the event was movement
            if (getEventType(event) == MOUSE_MOVED)
            {
                // ensure circle follows top cursor
                double x = getX(event);
                setLocation(paddle, x, 500);
            }
        }
        
        if (object != NULL)
        {
            if (object == paddle)
            {
                vy = -vy;
            }
            else if (strcmp(getType(object), "GRect") == 0)
            {
                vy = -vy;
                removeGWindow(window, object);
                points++;
            }
        }

     
        if (getY(ball) + getWidth(ball) >= getHeight(window))
        {
            vy = -vy;
        }
            
        if (getY(ball) + getWidth(ball) <= 0)
        {
            vy = -vy;
        }

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

        // bounce off left edge of window
        if (getX(ball) + getWidth(ball) <= 0)
        {
            vx = -vx;
        }

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

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

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

    // keep playing until game over
     double velocity= 2.0 + drand48() ;
     double vely = 2.0;
     updateScoreboard(window, label, points);
     waitForClick();
    while (lives > 0 && bricks > 0)
    {
        
        updateScoreboard(window, label, points);
        GEvent event = getNextEvent(MOUSE_EVENT);
        if (event != NULL)
            {
            
                
                if (getEventType(event) == MOUSE_MOVED)
                {
                    double x = getX(event) - getWidth(paddle) / 2;
                    setLocation(paddle, x, getY(paddle));                                              
                }
            }
                 
        move(ball, velocity, vely);
        
        if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
            velocity = -velocity;
        }
        else if (getX(ball) <= 0)
        {
            velocity = -velocity;
          
        }
        if( getY(ball) <= 0)
        {
            vely = -vely;
        }
        else if (getY(ball)+getWidth(ball) >= HEIGHT)
        {      
           
            lives--;
            setLocation(ball, (WIDTH/2)-10, HEIGHT/2);
            waitForClick();
           
            
        }   
        
        GObject obj = detectCollision(window, ball);
        if(obj != NULL)
        {
            if(strcmp(getType(obj), "GRect")==0)
            {
                vely = -vely;
                if(obj != paddle)
                {
                    removeGWindow(window, obj);
                    bricks--;
                    points++;
                    
                    
                }
            }
        }
        pause(10);   
    }
         
    

    // 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;
    // velocity of ball
    double vX = drand48();
    double vY = 2;
    // keep playing until game over
    while (lives > 0 && points<50)
    {
        move(ball,vX,vY);
        GEvent mouse = getNextEvent(MOUSE_EVENT);
        if(mouse!=NULL) {
            if(getEventType(mouse) == MOUSE_MOVED) {
            int y = 500;
            double x = getX(mouse) - getWidth(paddle)/2;
            setLocation(paddle,x,y);
            }
        }
      if(getX(ball) + getWidth(ball) >= getWidth(window)) {
        vX = -vX;
    } else if(getY(ball) <=0) {
        vY = -vY;
    } else if(getX(ball)<=0) {
        vX = -vX;
    } else if(getY(ball) + getHeight(ball) >= getHeight(window)) {
    lives--;
    waitForClick();
    setLocation(ball, 190, 290);
    setLocation(paddle, 150, 480);
    }
    
    GObject obj = detectCollision(window,ball);
    if(obj!=NULL) {
        if(obj == paddle) {
            vY = -vY;
    }   else if (strcmp(getType(obj),"GRect")==0) {
            removeGWindow(window,obj);
            points++;
            vY = -vY;
            updateScoreboard(window, label, points);
    
    }  

    }
    pause(10);
}
    // wait for click before exiting
    waitForClick();
    // game over
    closeGWindow(window);
    return 0;
}
Example #10
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
    double velocityy = 4.0;
    double velocityx = drand48()* 3.3;
    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 top cursor
                double x = getX(event) - getWidth(paddle) / 2;
      
                setLocation(paddle, x, 550);
            }
        }
    
		// ball bounce
       // initial velocity
  
    
    // bounce forever
        // move circle along y-axis
        move(ball, velocityx , velocityy);

        // bounce off right edge of window
        if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
            velocityx = -velocityx;
        }
        // bounce off top
        if (getY(ball) <= 0)
        {
        	velocityy = -velocityy;
        }

        // bounce off left edge of window
        if (getX(ball) <= 0)
        {
            velocityx = -velocityx;
        }
        // linger before moving again
        pause(10);
        // collide with paddle
       GObject object = detectCollision(window, ball);       
       if (object != NULL) 
       {  
       if (object == paddle)        
       {
            if (velocityy > 0)
            {         
            velocityy = -velocityy;
            }           
       }              
        // update scorboard & remove bricks hit
       else if (strcmp(getType(object), "GRect") == 0 )
     //  else 
        {
        	points++;
            removeGWindow(window, object);
        	velocityy = -velocityy;
        	updateScoreboard(window, label, points);
         }
       }
        
        // restart game after losing life

        if (getY(ball) + getHeight(ball) > getHeight(window))
        {
            lives--;
            if (lives >= 2 || lives == 0)
            {
                char s[12];
                sprintf(s, "%i lives left", lives);
                setLabel(label, s);
                setLocation(label, 180, 13);
            }
            else if (lives == 1)
            {
                char s[12];
                sprintf(s, "%i life left", lives);
                setLabel(label, s);
                setLocation(label, 180, 13);
            }
            waitForClick();
            setLocation(ball, 180, 100);
        }
        if (points == 50)
        {
            char s[12];
            sprintf(s, "You won!!");
            setLabel(label, s);
            setLocation(label, 175, 300);
        }
       
        
    }
    // 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;
}
Example #12
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 xvelocity = drand48() * .05;
    double yvelocity = .05;
    
    waitForClick();

    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        updateScoreboard(window, label, points);
        
        move(ball, xvelocity, yvelocity);
        
        //Moves paddle with the mouse
        GEvent event = getNextEvent(MOUSE_EVENT);
        
        if (event != NULL){
            if (getEventType(event) == MOUSE_MOVED) {
                double x = getX(event) - getWidth(paddle) / 2;
                if (x < 0) {
                    x = 0;
                }
                if (x > 330) {
                    x = 330;
                }
                setLocation(paddle, x, 560);
            }
        }
        //Detects collision
        GObject collision = detectCollision(window, ball);
        
        if (collision != NULL && strcmp(getType(collision), "GRect")== 0) {
        
            yvelocity *= -1;
            
            //Detects collision on a brick
            if (collision != paddle)
            {
                removeGWindow(window, collision);
                bricks--;
                points++;
                updateScoreboard(window, label, points);   
            }
        }
        
        //Moves ball back if it is at the new of the window
        if (getX(ball) + getWidth(ball) >= getWidth(window) || getX(ball) <= 0) {
            xvelocity *= -1;
        }   
        
        if (getY(ball) <= 0) {
            yvelocity *= -1;
        }
        
        //If the ball goes to the bottom of the screen it subtracts points and restarts the game
        if (getY(ball) + getHeight(ball) >= getHeight(window)) {
            lives--; 
            setLocation(ball,190,290);
            setLocation(paddle,165,560); 
            waitForClick();    
        }             
    }
    
    // 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);
    add(window, ball);

    // 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 on x axis
    double xvel = drand48() + 1;
    
    //velocity on y axis
    double yvel = drand48() + 1;
    
    waitForClick();
    
    // keep playing until game over
    while (lives > 0 && points < 50)
    {
        GEvent event = getNextEvent(MOUSE_EVENT);
           
        if (event != NULL)
        {
            if (getEventType(event) == MOUSE_MOVED)
            {
                double x = getX(event) - getWidth(paddle) / 2;
                setLocation(paddle, x, HEIGHT - 10);
            }
        }
        
        move(ball, xvel, yvel);
        
        if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
        xvel = -xvel;
        }
        
        else if (getX(ball) <= 0)
        {
        xvel = -xvel;
        }
        
        else if (getY(ball) >= getHeight(window))
        {
        waitForClick();
        printf("lol");
        lives--;
        setLocation(ball, (WIDTH / 2) - RADIUS, (HEIGHT / 2) - RADIUS);
        }
        
        
        else if (getY(ball) <= 0)
        {
        yvel = -yvel;
        }

        pause(10);
        
        GObject object = detectCollision(window, ball);
                       
            if (object != NULL)
            {
            if (object == paddle)
            {
                yvel = -yvel;
            }
        
            else if (strcmp(getType(object), "GRect") == 0)
            {
                yvel = -yvel;
                removeGWindow(window, object);
                points++;
            }
        }
     updateScoreboard(window, label, points);   
    }

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

    // 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 + 1;
    double velocity_y = drand48() * 2 + 1;
    
    bool ballMove = false;

    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // check for mouse event
        GEvent event = getNextEvent(MOUSE_EVENT);
        GEvent event2 = getNextEvent(MOUSE_EVENT);
        
        // if we heard one
        if (event != NULL)
        {   
            // if the event was moved
            if (getEventType(event) == MOUSE_CLICKED)
            {
                ballMove = true;
            }
        }
        // if we heard one
        if (event != NULL)
        {
            // if the event was moved
            if (getEventType(event) == MOUSE_MOVED && ballMove == true)
            {
                
                double y = getY(paddle);
                // ensure paddle doesn't go out of window
                if (getX(event) <= 0)
                {
                    setLocation(paddle, 0, y);    
                }
                else if (getX(event) + PADDLEWIDTH >= WIDTH)
                {
                    setLocation(paddle, WIDTH - PADDLEWIDTH, y);
                }
                else
                {
                    // ensure paddle x position follows cursor
                    double x = getX(event);
                    setLocation(paddle, x, y);
                }
            }
        }
        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);
        }
        GObject object = detectCollision(window, ball);
        if (object != NULL)
        {
            if (object == paddle)
            {
                velocity_y *= -1;
            }
            else if (strcmp(getType(object), "GRect") == 0)
            {
                velocity_y *= -1;
                removeGWindow(window, object);
                points += 1;
                updateScoreboard(window, label, points);
            }
        }
        // when no more live or all bricks are broken
        if (lives == 0 || points == ROWS * COLS)
        {
            break;
        }
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Example #15
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;
}
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 = drand48()*5;
    double velocityX = drand48()*5;
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
   
       
       move(ball, velocityX, velocity);
       pause(10); 
      // 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 follows cursor
                double x = getX(event);
                if(x <= 350&&velocityX!=0)
                setLocation(paddle, x, 550);
                
            }
        
        }
     

        GObject object = detectCollision(window, ball);
       if(object != NULL)
       {
       if (object == paddle)
       {
         velocity = -velocity;
       updateScoreboard(window,label, points);
       }
       else if (strcmp(getType(object), "GRect") == 0)
        {
	    velocity = -velocity;
        points++;
        updateScoreboard(window,label, points);
        bricks--;
        removeGWindow(window, object);
   
   
        }
        } 
      
        
    
         
       

        // bounce off bottom edge of window
       if (getY(ball) + getHeight(ball) >= getHeight(window))
        {
          velocity = 0;
          velocityX = 0;
          lives--;
        
        }

        // bounce off up edge of window
        else if (getY(ball) <= 0)
        {
            velocity = -velocity;
          
        }
        
        if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
          velocityX = -velocityX;
        }

        // bounce off bottom edge of window
        else if (getX(ball) <= 0)
        {
            velocityX = -velocityX;
          
        
        }
        // linger before moving again
        pause(10);
       
    }
    if(points==50)
    {
    GLabel label = newGLabel("You bricked out the breakout!");
    setFont(label, "SansSerif-36");
    add(window, label);
    setLocation(label, 100, 200);
    pause(10);
    }
    else if(points<50)
    {
    GLabel label = newGLabel("Try again!");
    setFont(label, "SansSerif-36");
    add(window, label);
    setLocation(label, 100, 200);
    pause(10);
    }
    
    // wait for click before exiting
    if(bricks==0)
    { 
    waitForClick();
    // game over
    closeGWindow(window);
    }
    if(lives==0)
    { 
    waitForClick();
    // game over
    closeGWindow(window);
    }
    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;

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

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

    // number of points initially
    int points = 0;

    // default ball direction is down
    Velocity *velocity = (Velocity *) malloc(sizeof(Velocity));
    velocity->horizontalVelocity = 0.0;
    velocity->verticalVelocity = 2.0;

    char *paddleDirection = malloc(sizeof(char) * 10);
  
    Paddle *bat = (Paddle *) malloc(sizeof(Paddle));
    bat->paddle = paddle;
    bat->direction = paddleDirection;
    bat->x = getX(paddle);
    bat->y = getY(paddle);

    // show the scoreboard
    updateScoreboard(window, label, points);

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

        GObject object = detectCollision(window, ball);
        if (object != NULL)
        {
            if (object == bat->paddle)
            {
                ballHitsPaddle(bat, velocity, ball);
            }
            else if (strcmp(getType(object), "GRect") == 0)
            {
                ballHitsBrick(velocity, &window, &object, &label, &points, &bricks);
            }
        }
        else
        {
            if (hasBallHitTheSides(&ball, &window))
            {
                invertHorizontalDirection(velocity);
            }
            else if (hasBallHitTheTop(&ball, &window))
            {
                invertVerticalDirection(velocity);
            }
            else if (hasBallHitTheBottom(&ball, &window))
            {
                resetGameplay(velocity, &ball, bat, &lives);
            }
        }

        move(ball, velocity->horizontalVelocity, velocity->verticalVelocity);
        pause(10);
    }

    showGameOverMessage(window, label);

    // 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 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 #20
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;
}
Example #21
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 #22
0
int main(void)
{   
    // seed pseudorandom number generator
    srand48(time(NULL));
    double vy = drand48();
    double vx = drand48();

    // 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);
    GLabel llabel = initlives(window);
    setLocation(llabel, 10, 590);
    // number of bricks initially
    int bricks = COLS * ROWS;

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;
    pause(1000);

    // keep playing until game over
    updatellabel(window, llabel, 3);
    while (lives > 0 && bricks > 0)
    {
        while(bricks > 0)
        {
            GEvent mouse = getNextEvent(MOUSE_EVENT);
            if (mouse != NULL)
            {
                if (getEventType(mouse) == MOUSE_MOVED)
                {
                    double x = getX(mouse) - getWidth(paddle) / 2;
                    double y = getY(paddle);
                    setLocation(paddle, x,y);
                }
            }
            move(ball,vx,vy);
            if (getX(ball) + CD >= WIDTH)
            {
                vx = -vx;
            } 
            else if (getX(ball) <= 0)
            {
                vx = -vx;
            }
            else if (getY(ball) <= 0)
            {
                vy = -vy;
            }
            pause(1);
            GObject object = detectCollision(window, ball);
            if (object != NULL)
            {
                if (object == paddle)
                {
                    vy = -vy;
                }
                else if (strcmp(getType(object), "GRect") == 0)
                {
                    bricks--;
                    points++;
                    updateScoreboard(window, label, points);
                    vy = -vy;
                    removeGWindow(window, object);
                }
            }
            if (getY(ball) + CD >= HEIGHT)
            {
                lives = lives - 1;
                updatellabel(window, llabel, lives);
                setLocation(ball,(WIDTH/2) - (CD/2), (HEIGHT/2) - (CD/2));
                pause (1000);
                if (bricks == 0)
                {
                GLabel win = initWin(window);
                pause(5000);
                break;
                }
                else
                {
                break;
                }
            } 
        }
        
    }

    // wait for click before exiting
    removeGWindow(window, label);
    GLabel messege = initEnd(window);
    sleep(5000);
    waitForClick();

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