Beispiel #1
0
void Game::init(int* argcp, char** argvp) {
    glutInit(argcp, argvp);
    glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowSize (480, 480);
    glutInitContextVersion( 3, 2 );
    glutInitContextProfile( GLUT_CORE_PROFILE );
    glutCreateWindow ("Ablockalypse");
    glutSetWindowTitle("Ablockalypse");
    glutSetIconTitle("Ablockalypse");
    
    glewExperimental = GL_TRUE;
    glewInit ();

    // do shader stuff here...
    GLuint program = InitShader( "vshader.glsl", "fshader.glsl" );
    glUseProgram( program );

    vPosition = glGetAttribLocation( program, "vPosition" );
    vColor = glGetAttribLocation( program, "vColor" );
    xlate = glGetUniformLocation( program, "xlate" );

    initGeometry (paddle);
    initGeometry (ball);
    initBricks ();

    glEnable( GL_DEPTH_TEST );
    glClearColor(0, 0, 0, 0); 
}
Beispiel #2
0
void Game::initGame() {
    if (callbackInstance == NULL){
        callbackInstance = this;
    }
	// game init code here...
    score = 0;
    numBricks = 48;
    lives = 3;
    paused = false;
    initBricks ();
}
Beispiel #3
0
/**
 * Initializes all the modules of the system
 */
static inline void
init_modules()
{
	TRACE_FUNC_START();

	TRACE_DEBUG_LOG("Initializing the engines module \n");
	pktengine_init();
	interface_init();
	initBricks();

	TRACE_FUNC_END();
}
Beispiel #4
0
int main(void)
{
    // seed pseudorandom number generator
    srand48(time(NULL));

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

    // instantiate bricks
    initBricks(window);

    // instantiate ball, centered in middle of window
    GOval ball = initBall(window);

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

    // instantiate scoreboard, centered in middle of window, just above ball
    GLabel label = initScoreboard(window);

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

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;

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

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Beispiel #5
0
int main(int argc, string argv[])
{
    bool god = false;
    if (argc == 2 && (strcmp(argv[1], "GOD") == 0))
    {
        god = true;
    } 
    // 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 x_velocity = 2 * (drand48() + 1);
    double y_velocity = 2 * (drand48() + 1);
    
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // regular non god mode play
        if (!god)
        {
            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 the x of the mouse
                    double x = getX(event) - getWidth(paddle) / 2;
                    double y = getY(paddle);
                    setLocation(paddle, x, y);
                }
            }
        }
        // god mode, set as X of ball
        else if (god)
        {
            double x = getX(ball);
            double y = getY(paddle);
            setLocation(paddle, x, y);
                
        }
        
        
        // Ball moves
        // move circle along x-axis
        move(ball, x_velocity, y_velocity);

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

        // bounce off left edge of window
        else if (getX(ball) <= 0)
        {
            x_velocity *= -1;
        }
        
        
        if (getY(ball) + getHeight(ball) >= getHeight(window))
        {
            
            lives -= 1;
            points -= 1;
            removeGWindow(window, ball);
            ball = initBall(window);
            waitForClick();
        }

        // bounce off top edge of window
        
        
        else if (getY(ball) < 0)
        {
            y_velocity *= -1;
            
        }
        // goes off bottom edge of window
        
        
        
        GObject object = detectCollision(window, ball);
            // sees if any collisions
        if (object != NULL)
        {
            // avoids segmentation errors
            const char *daType = getType(object);
            int daMatch = strcmp(daType, "GRect");
            // if hit paddle, bounce
            if (object == paddle)
            {
                y_velocity *= -1;
            
            }
            else if (daMatch == 0)
            {
                // ran into the bricks
                for (int i = 0; i < ROWS; i++)
                {
                    for (int j = 0; j < COLS; j++)
                    {
                        if (object == brickz[i][j])
                        {
                            /** leftover code
                            int daWidth = getWidth(brickz[i][j]);
                            int daX = getX(brickz[i][j]);
                            int daHeight = getHeight(brickz[i][j]);
                            int daY = getY(brickz[i][j]);
                            int collisionX = getX(ball) + RADIUS / 2;
                        //if (collisionX >= daX && collisionX <= daX + daWidth)
                        //{
                            //if (getY(ball) == daY + daHeight)
                            //{
                            */
                            y_velocity *= -1;
                            bricks -= 1;
                            points += 50 / (i + 1) + 30 / (bricks + 1);
                            removeGWindow(window,  brickz[i][j]);
                            // changes direction, subtracts bricks by 1
                            // increases points depending on row of brick
                            // and depending on how many bricks left
                            // removes away the bricks
                          // }
                        // }
                        }
                    }
                }
                // y_velocity *= -1;
            
            }
            /*
            else if (strcmp(getType(object), "GLabel") == 0)
            {
                // do nothing
            }
            */
        }    
        /*
        char s[8];
        sprintf(s, "%i", points);
        setLabel(label, s);
        */
        updateScoreboard(window, label, points);
        // updates scoreboard
        // linger before moving again
        pause(10);
    }
    /** useless leftover code
    string overmessage = "Game Over. Click to Close";
    char g[100];
    sprintf(g, "%s", overmessage);
    setLabel(label, g);
    */
    // wait for click before exiting
    waitForClick();
    // game over
    closeGWindow(window);
    return 0;
}
Beispiel #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);
    add(window, ball);

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

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

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

    // number of lives initially
    int lives = LIVES;

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

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

    //initialize ball velocity
    double v_x = drand48() * LIMIT;
    double v_y = 2.0;
        
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {   
         // check for mouse event
         GEvent event = getNextEvent(MOUSE_EVENT);

         // if we heard one
         if (event != NULL)
         {
            // if the event was movement, ensure paddle follows cursor and doesn't go beyond window
            if (getEventType(event) == MOUSE_MOVED)
                {
                    double x = getX(event) - P_WIDTH / 2;
                    double y = HEIGHT - (2*P_HEIGHT);
               
                    if (x < 0 )
                        setLocation(paddle, 0, y);
                    else if (x > WIDTH - P_WIDTH)
                        setLocation(paddle, WIDTH - P_WIDTH, y);
                    else
                        setLocation(paddle, x, y);        
                }
         }
           
         // move ball
         move(ball, v_x, v_y);

         // bounce off top edge of window
         if (getY(ball) < 0)
         {
            v_y = -v_y;
         }
         
         // bounce off left and right edge of window
         if (getX(ball) < 0 || getX(ball) > WIDTH - (2*RADIUS) )
         {
            v_x = -v_x;
         }
                
         // bounce off collision object    
         if (detectCollision(window, ball)!=NULL)
         {
            GObject collide = detectCollision(window, ball);
            
            //bounce off paddle       
            if(collide == paddle)
            {
                v_y = -v_y;
            }
            //bounce off brick, removing brick from window and updating score
            else if (strcmp(getType(collide), "GRect")==0)
            {
                v_y = -v_y;
                removeGWindow(window, collide);
                bricks--;
                points++;
                updateScoreboard(window, label, points);
            }
         } 

         // linger before moving again
         pause(5);
        
         //pause game and decrease lives if ball goes past bottom of window        
         if (getY(ball) > HEIGHT)
         { 
            lives--;
            
            waitForClick();
            
            //reinitalializes ball at centre of window when game resumes
            if(lives > 0)
            {
                setLocation(ball, WIDTH/2, HEIGHT/2);
                pause(1000); 
            } 
         }
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Beispiel #8
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;
}
Beispiel #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;
    
    // need to get velocity of paddle
    double t_paddle = 0.0;
    

    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
      // Win condition
      if (points == bricks) {
        setLabel(scoreboard, "You WIN!");
        break;
      }
      
      // Lose life condition
      if (getHeight(ball) == HEIGHT) {
        lives--;
        break;
      }
      
      // Dictate Paddle movement
      // Test to see if you can move off screen
      // if left and right cursors move at an accelrated pace, time based
      GKeyEvent e = waitForEvent(KEY_EVENT);
      
      
      
      if (getEventType(e) == LEFT_ARROW_KEY) {
          //KEY_PRESSED
         // KEY_RELEASED
        t_paddle = e.getEventTime();
        move(paddle, -1, 0); 
      }
      else if (getEventType(e) == LEFT_ARROW_KEY) {
        t_paddle = e.getEventTime();
        move(paddle, 1, 0); 
      }
      printf("time of event: %d", t_paddle);
        
      // If ball hits brick
      // Test if top or side of ball hits a brick, does it still detect
      GObject hitBrick = detectCollision(window, ball);
      if(hitBrick != NULL) {
        remove(window, hitBrick);
        points++;
        updateScoreboard(window, scoreboard, points);
        //call ball bounce logic;
      }

      // Ball bouncing logic
      int ballX = getX(ball);
      int ballY = getY(ball);
      int paddleX = getX(paddle);
      
      
      if (ballX == 0 || ballX + getWidth(ball) == WIDTH) {
        setLocation(ball, -ballX, y);
      }
      else if (ballY == 0) {
        setLocation(ball, x, -ballY); 
      }
      
      // Hit paddle and within the width of paddle, do complicated bouncing
      else if (ballY + getHieght(ball) == getY(paddle) &&
        ballX >= paddleX &&
        ballX + getWidth(ball) <= paddleX + getWidth(paddle)) {
        //bounce logic
      }
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Beispiel #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);
    
    // randomize x velocity of ball
    double xvelocity = drand48()/50 + 0.05;
    double yvelocity = 0.1;

    // 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;
    
    while (lives > 0 && bricks > 0)
    {
        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 mouse
                double x = getX(event) - WIDTHPADDLE/2;
        
                // makes sure paddle stays within screen
                if (x >= 0 || x <= WIDTH)
                {
                    setLocation(paddle, x, 9*HEIGHT/10);
                }
                if (x < 0)
                {
                    setLocation(paddle, 0, 9*HEIGHT/10);
                }
                if (x > WIDTH - WIDTHPADDLE) 
                {
                    setLocation(paddle, WIDTH - WIDTHPADDLE, 9*HEIGHT/10);
                }
             }
         }
        
        // compels ball to move
        move(ball, xvelocity, yvelocity);
                
        // bounce off edge of window
        if (getX(ball) + getWidth(ball) >= WIDTH || getX(ball) <= 0)
        {
            xvelocity = -xvelocity;
        }
        
        // bouce off top edge of window
        if (getY(ball) <= 0)
        {
            yvelocity = -yvelocity;
        }
        
        // detect collision
        GObject object = detectCollision(window, ball);
        
       
        // bounce off paddle
        if (object == paddle)
        {
            yvelocity = -yvelocity;
        }

        // bounce off brick
        else if (object != NULL)
        {
            if (strcmp(getType(object), "GRect") == 0)
            {
                yvelocity = -yvelocity;
                bricks--;
                removeGWindow(window, object);
                points++;
            }
        }

        // update scoreboard
        updateScoreboard(window, label, points);
        
        // ball at bottom edge of window
        if (getY(ball) + 2*RADIUS >= HEIGHT)
        {
            lives--;
            if (lives >= 1)
            {
                waitForClick();
            }    
            setLocation(ball, WIDTH/2 - RADIUS, HEIGHT/2 - RADIUS);
        }
    }

    // wait for click before exiting
    waitForClick();

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

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

    // instantiate bricks
    initBricks(window);

    // instantiate ball, centered in middle of window
    GOval ball = initBall(window);

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

    // instantiate scoreboard, centered in middle of window, just above ball
    GLabel label = initScoreboard(window);

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

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;
    
    
    // keep playing until game over
    double velocity = drand48()*2;
    double angle = drand48()*3;
    while (lives > 0 && bricks > 0)
    {    
        // check for mouse event
        GEvent event = getNextEvent(MOUSE_EVENT);

        // if we heard one
        if (event != NULL)
        {
            // if the event was movement
            if (getEventType(event) == MOUSE_MOVED)
            {   
                double x = getX(event)-PADDLEW/2;
                double y = HEIGHT - PADDLEH;
                
                setLocation(paddle, x, y);
            }
        }

        move(ball, velocity, angle);
        GObject object = detectCollision(window, ball);
        // bounce off right edge of window
        if (getX(ball) + getWidth(ball) > 400)
        {
            
            velocity = -velocity;
        }

        // bounce off left edge of window
        else if (getX(ball) <= 0)
        {
            velocity = -velocity;
        }
        // Dtects if the ball hit the paddle then reverses angle
        else if(object == paddle)
        {
            angle = -angle;
        }
        //Detects if it hits the bottom of the screen
        else if(getY(ball) + getHeight(ball) > HEIGHT)
        {
            removeGWindow(window, ball);
            waitForClick();
            ball = initBall(window);
            lives -= 1;   
        }
        else if (getY(ball) <= 0)
        {
            angle = -angle;
        }
        else if (strcmp(getType(object), "GRect") == 0)
        {
            angle = -angle;
            removeGWindow(window, object);
            initScoreboard(window);
            updateScoreboard(window, label, points);
            
            points += 1;
            if(points == 50)
            {
                break;
            }
            
        }
        
        // linger before moving again
        pause(5);
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Beispiel #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;
    
    // initial y-axis velocity
    double y_velocity = 2.0;
    
    // initial x-axis velocity
    double x_velocity = drand48() + LIMIT;
    
    //mouse click to start
    waitForClick();
    
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        
        GEvent event = getNextEvent(MOUSE_EVENT);
        if (event != NULL)
        {
            // paddle movement
            if(getEventType(event) == MOUSE_MOVED)
            {
                double x = getX(event) - P_WIDTH / 2;
                double y = 550;
                setLocation(paddle, x, y);
            
                // prevent paddle from leaving screen
                if (getX(paddle) + getWidth(paddle) >= getWidth(window))
                {
                    x = getWidth(window) - getWidth(paddle);
                    setLocation(paddle, x, y);
                }
                else if (getX(paddle) <= 0)
                {
                    x = 0;
                    setLocation(paddle, x, y);
                }
            }
        }
               
        // bouncing ball
        move(ball, x_velocity, y_velocity);
        
        /* not sure my x and y axis are coded correctly, may be backwards*/
        //bounce off bottom of window
        if (getY(ball) + getWidth(ball) >= getHeight(window))
        {
            lives--;
            setLocation(ball, 190, 350);
            // game pause until mouse click
            waitForClick();
        }
        // bounce off top of window
        else if (getY(ball) <= 0)
        {
            y_velocity = - y_velocity;
        }
        // bounce off right edge of window
        else if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
            x_velocity = -x_velocity;
        }
        // bounce off left edge of window
        else if (getX(ball) <= 1)
        {
            x_velocity = -x_velocity;
        }
        // make sure ball doesn't move too fast
        pause (20);
        
        
        //detect collisions between the ball and objects in window
        GObject object = detectCollision(window, ball);
        
        if (object != NULL)
        {
            // bounce off paddle
            if (object == paddle)
            {
                y_velocity = - (drand48() + LIMIT);
            }
            
            // bounce off bricks
            if ((strcmp(getType(object), "GRect") == 0) && (object != paddle))
            {
                //x_velocity = -x_velocity;
                y_velocity = - y_velocity;
                
                // remove brick when hit
                removeGWindow(window, object);
                points++;
                updateScoreboard(window, label, points);
            }
        }
    }

    // wait for click before exiting
    waitForClick();

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

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

    // instantiate bricks
    initBricks(window);

    // instantiate ball, centered in middle of window
    GOval ball = initBall(window);

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

    // instantiate scoreboard, centered in middle of window, just above ball
    GLabel label = initScoreboard(window);

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

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;
	
	//  make the ball’s initial velocity along its (horizontal) x-axis random.
	srand48((long int) time(NULL));
	double velocityX = -BALL_VELOCITY + 2 * BALL_VELOCITY * drand48();
	double velocityY = BALL_VELOCITY;
	
	while (true)
	{ 
	    GEvent event = getNextEvent(MOUSE_EVENT);
		if (event != NULL)
		{
		    if (getEventType(event) == MOUSE_CLICKED)
			{
			    break;
			}
						
		}
	}

    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // TODO		
		move(ball, velocityX, velocityY);
		
		GEvent event = getNextEvent(MOUSE_EVENT);
		if (event != NULL)
		{			
			if (getEventType(event) == MOUSE_MOVED)
			{
				double x = 0;
				if (getX(event) - PADDLE_WIDTH / 2 <= 0)
				{
				    x = 0;	
				} 
				else if (getX(event) + PADDLE_WIDTH / 2 >= WIDTH)
				{
					x = WIDTH - PADDLE_WIDTH;
				}
				else
			    {
					x = getX(event) - PADDLE_WIDTH / 2;
				}			
				
				double y = getY(paddle);
				setLocation(paddle, x, y);
			}
		}		
		
		if (getX(ball) + 2 * RADIUS >= WIDTH)
		{
			velocityX = -velocityX;
		}
		else if (getX(ball) <= 0)
		{
			velocityX = -velocityX;
		}
		/*// the bottom edge condition
		else if (getY(ball) + 2 * RADIUS >= HEIGHT)
		{
			velocityY = -velocityY;
		}
		*/		
		else if (getY(ball) > getY(paddle) + PADDLE_WIDTH / 2)
		{
			lives--;
			if (lives > 0)
		    {				
			    while (true)
				{ 
			        GEvent event = getNextEvent(MOUSE_EVENT);
				    if (event != NULL)
					{
						if (getEventType(event) == MOUSE_CLICKED)
						{
						    break;
						}
						
					}
			    }
                				
			    setLocation(ball, (getWidth(window) - 2 * RADIUS) / 2,
                                   (getHeight(window) - 2 * RADIUS) / 2);
			    move(ball, velocityX, velocityY);
		    }			
		}		
		else if (getY(ball) <= 0)
		{
			velocityY = -velocityY;
		}
		
		GObject object = detectCollision(window, ball);
		
		if (object != NULL)
        {
            if (object == paddle)
            {
                // TODO
		        velocityY = -velocityY;   
            }		
		    /*// More generally with the same functionality
		    if (strcmp(getType(object), "GRect") == 0)
            {
                // TODO
            }
		    */
		    if (strcmp(getType(object), "GLabel") == 0)
            {
                // TODO
				sendToBack(object);
            }
		
		    if (strcmp(getType(object), "GRect") == 0 && object != paddle)
            {
                // TODO
				bricks--;				
			    removeGWindow(window, object);				
				updateScoreboard(window, label, ++points);
				velocityY = -velocityY;
            }
        }
		
		pause(8);
    }

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Beispiel #14
0
int main(void)
{
    // seed pseudorandom number generator
    srand48(time(NULL));
 
    // instantiate window
    GWindow window = newGWindow(WIDTH, HEIGHT);
 
    // instantiate bricks
    initBricks(window);
 
    // instantiate ball, centered in middle of window
    GOval ball = initBall(window);
 
    // instantiate paddle, centered at bottom of window
    GRect paddle = initPaddle(window);
 
    // instantiate scoreboard, centered in middle of window, just above ball
    GLabel label = initScoreboard(window);
 
    // number of bricks initially
    int bricks = COLS * ROWS;
 
    // number of lives initially
    int lives = LIVES;
 
    // number of points initially
    int points = 0;
 
    // keep playing until game over 
    // TODO
    while (lives > 0 && bricks > 0)
    {
        move(ball, x_velocity, y_velocity);
        if(getX(ball) + getWidth(ball) >= getWidth(window))
        {
            x_velocity = -x_velocity;
        }
        else if(getX(ball) <= 0)
        {
            x_velocity = -x_velocity;
        }
        else if(getY(ball) <= 0)
        {
            y_velocity = -y_velocity;
        }
        else if(getY(ball) + getHeight(ball) >= getHeight(window))
        {
            lives--;
            setLocation(ball, 190, 290);
            setLocation(paddle, 180, 560);
            waitForClick();
        }
        pause(10);
         
        GObject object = detectCollision(window, ball);
        if (object != NULL && strcmp(getType(object), "GLabel") != 0)
        if (strcmp(getType(object), "GRect") == 0)
        {
            if (object == paddle)
            {
                y_velocity = -y_velocity;
                x_velocity = -x_velocity;
            }
            else
            {
                removeGWindow(window, object);
                bricks--;
                points++;
                y_velocity = -y_velocity;
                x_velocity = -x_velocity;
            }
        }
         
        //Update scoreboard
        updateScoreboard(window, label, points);
         
        GEvent event = getNextEvent(MOUSE_EVENT);
        if (event != NULL)
        {
            //if the event was mouse movement
            if(getEventType(event) == MOUSE_MOVED)
            {
                double x = getX(event) - getWidth(paddle)/2;
                double y = 560;
                setLocation(paddle, x, y);
            }
        }
    }   
    // You win! or...lose...
    if(bricks > 0)
    {
        GLabel verdict = newGLabel("That all you got?");
        setFont(verdict, "SansSerif-50");
        setColor(verdict, "C71585");
        add(window, verdict);
        setLocation(verdict, 45, 150);
    }
    else
    {
        GLabel verdict = newGLabel("You Win!!");
        setFont(verdict, "SansSerif-50");
        setColor(verdict, "C71585");
        add(window, verdict);
        setLocation(verdict, 185, 150);
    }
 
    // wait for click before exiting
    waitForClick();
 
    // game over
    closeGWindow(window);
    return 0;
}
int main(void)
{
    // seed pseudorandom number generator
    srand48(time(NULL));

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

    // instantiate bricks
    initBricks(window);

    // instantiate ball, centered in middle of window
    GOval ball = initBall(window);

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

    // instantiate scoreboard, centered in middle of window, just above ball
    GLabel label = initScoreboard(window);

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

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;

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

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

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

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

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

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

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

    // wait for click before exiting
    waitForClick();

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

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

    // instantiate bricks
    initBricks(window);

    // instantiate ball, centered in middle of window
    GOval ball = initBall(window);

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

    // instantiate scoreboard, centered in middle of window, just above ball
    GLabel label = initScoreboard(window);

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

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;

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

    // wait for click before exiting
    waitForClick();

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

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

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;

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

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

    waitForClick();

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

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

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

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

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

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

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

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

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

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

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

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

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

            updateScoreboard(window, scoreboard, points);
        }

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

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Beispiel #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 bricks initially
    int bricks = COLS * ROWS;

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;
    
    // set velocity
    double x_velocity = drand48() + VELOCITY;
    double y_velocity = drand48() + VELOCITY;

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

    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
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;
    
    // wait for click before starting
    waitForClick();
    
    // set velocity
    double velocity = drand48() * 4.0;
    double velocity_y = 5.0;
    
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {

        // TODO
        // Scoreboard
        updateScoreboard(window, label, points);
        
        // move ball
        move(ball, velocity, velocity_y);
        
        // linger before ball moving again
        pause(10);
        
        // check for mouse event
        GEvent mouseMove = getNextEvent(MOUSE_EVENT);

        // move paddle with cursor
        if (mouseMove != NULL)
        {
            // if the event was movement
            if (getEventType(mouseMove) == MOUSE_MOVED)
            {
                // ensure paddle follows top cursor
                double x = getX(mouseMove) - getWidth(paddle) / 2;
                double y = 550;
                setLocation(paddle, x, y);
            }
        }

        GObject object = detectCollision(window, ball);
        
        if (object != NULL)
        {
            // If hits the paddle.
            if (object == paddle)
            {
                velocity_y = -velocity_y;
            }
            
            // If hits a brickck. Remove brick, add point.
            else if (strcmp(getType(object), "GRect") == 0)
            {
                removeGWindow(window, object);
                velocity_y = -velocity_y;
                points++;
                bricks--;                
            }
        }
        
        // If hits right wall.
        if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
            velocity = -velocity;
        }
        
        // If hits left wall.
        if (getX(ball) <= 0)
        {
            velocity = -velocity;
        }
        
        // If hits top wall.
        if (getY(ball) <= 0)
        {
            velocity_y = -velocity_y;
        }
        
        // If hits bottom, subtract life and start over.
        if (getY(ball) + getHeight(ball) >= getHeight(window))
        {
            int x = (getWidth(window) - 25) / 2;
            int y = (getHeight(window) - 25) / 2;
            lives--;
            // move ball to start
            setLocation(ball, x, y);
            // move paddle to start
            setLocation(paddle, (getWidth(window) - 100) / 2, 550);
            waitForClick();
        }

    }
    
    // You Lose Label Message for kicks.
    if (bricks > 0)
    {
        GLabel game_over = newGLabel("YOU LOSE!");
        setFont(game_over, "SansSerif-70");
        setColor(game_over, "RED");
        add(window, game_over);
        setLocation(game_over, 15, 300);
    }
    
    // wait for click before exiting
    waitForClick();

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

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

    // instantiate bricks
    initBricks(window);

    // instantiate ball, centered in middle of window
    GOval ball = initBall(window);

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

    // instantiate scoreboard, centered in middle of window, just above ball
    GLabel label = initScoreboard(window);

    // number of bricks initially
    int bricks = COLS * ROWS;
    updateScoreboard(window, label, 0);

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;

    // define ball's velocity
    double xvelocity = 2.5 * drand48();
    double yvelocity = 2.5;

    waitForClick();

    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // TODO
        //set paddle to follow mouse
        GEvent event = getNextEvent(MOUSE_EVENT);
        
        if (event != NULL)
        {
            if (getEventType(event) == MOUSE_MOVED)
            {
                double x = getX(event) - 25;
                setLocation(paddle, x, 500);
            }
        }
        
        // move ball
        move(ball, xvelocity, yvelocity);
        
        // bounce from side walls
        if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
            xvelocity = -xvelocity;
        }
        else if (getX(ball) <= 0)
        {
            xvelocity = -xvelocity;
        }
        
        // bounce from ceiling or floor
        if (getY(ball) + getHeight(ball) >= getHeight(window))
        {
            lives--;
            removeGWindow(window, ball);
            ball = initBall(window);
            waitForClick();
            continue;
        }
        else if (getY(ball) <= 0)
        {
            yvelocity = -yvelocity;
        }
        
        // detect collision between objects
        GObject object = detectCollision(window, ball);
        
        if (object != NULL)
        {
            // bounce off paddle
            if (object == paddle)
            {
                if (yvelocity > 0.0)
                {
                    yvelocity = -yvelocity;
                }
            }
            
            // bounce off and remove block
            else if (strcmp(getType(object), "GRect") == 0)
            {
                yvelocity = -yvelocity;
                bricks--;
                updateScoreboard(window, label, 50 - bricks);
                removeGWindow(window, object);
            }
        }
        
        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);

    // 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 points initially
    int points = 0;
    
    // number of bricks initially
    int bricks = COLS * ROWS - points;
    
    // initial velocity
    srand48(time(NULL));
    double xvelocity = 2.0;
    double yvelocity = (1 + drand48()) * 2;
    
        
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // move ball along x & y
        move(ball, xvelocity, yvelocity);
        
        // bounce off right edge of window
        if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
            xvelocity = -xvelocity;
        }        
        
        // bounce off left edge of window
        if (getX(ball) <= 0)
        {
            xvelocity = -xvelocity;
        }
        
        // ball out of bounds
        if (getY(ball) + getHeight(ball) >= getHeight(window))
        {
            removeGWindow(window, ball);
            lives--;
            ball = newGOval(WIDTH / 2 - RADIUS, HEIGHT / 2 - RADIUS,
             RADIUS * 2, RADIUS * 2);
            setColor(ball, "RED");
            setFilled(ball, true);
            add(window, ball);
            pause(1800);
            setColor(ball, "BLACK");
            setFilled(ball, true);
        }
        
        // bounce off top
        if (getY(ball) <= 0)
        {
            yvelocity = -yvelocity;
        }
        
        
        // linger before moving again
        pause (10);
        // check for mouse event
        GEvent event = getNextEvent(MOUSE_EVENT);
        
        // if we heard event
        if (event != NULL)
        {
            // if the event was a movement
            if (getEventType(event) == MOUSE_MOVED)
            {
                // ensure the paddle follows mouse
                double x = getX(event) - getWidth(paddle) / 2;
                setLocation(paddle, x, HEIGHT * .92);
            }
        }
        
        // ball paddle collision
        GObject object = detectCollision(window, ball);
        
        if (object == paddle)
        {
            yvelocity = -yvelocity;
        }
        
        else if (object == label)
        {
            ;
        }
        else if (object != NULL)
        {
            // xvelocity = -xvelocity;
            yvelocity = -yvelocity;
            removeGWindow(window, object);
            points++;
            updateScoreboard(window, label, points);
        }
    }
    
    // wait for click before exiting
    waitForClick();

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

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

    // instantiate bricks
    initBricks(window);

    // instantiate ball, centered in middle of window
    GOval ball = initBall(window);

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

    // instantiate scoreboard, centered in middle of window, just above ball
    GLabel label = initScoreboard(window);

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

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;
    
    // initial velocity
    double standardReduction = 0.07;
    double velocity = drand48() - standardReduction;
    double velocityY = 2;

    waitForClick();
    
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // Get the next mouse event
        GEvent event = getNextEvent(MOUSE_EVENT);
        
        // If some move
        if (event != NULL)
        {
            if (getEventType(event) == MOUSE_MOVED)
            {
                double x = getX(event) - (PADDLE_WIDTH / 2);
                double y = 500;
                setLocation(paddle, x, y);
            }
        }
                
        move(ball, velocity, velocityY);
        
        // bounce off right edge of window
        if (getX(ball) + RADIUS >= WIDTH)
        {
            velocity = -velocity;
        }
        // bounce off left edge of window
        else if (getX(ball) <= 0)
        {
            velocity = -velocity;
        }
        // bounce off top edge
        else if (getY(ball) <= 0)
        {
            velocityY = -velocityY;
        }
        // bottom edge
        else if (getY(ball) + RADIUS >= HEIGHT)
        {
            velocityY = 0;
            velocity = 0;
            lives--;
            removeGWindow(window, ball);
            removeGWindow(window, paddle);
            ball = initBall(window);
            paddle = initPaddle(window);
            velocity = drand48() - standardReduction;
            velocityY = 2;
            waitForClick();
        }

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

    if (bricks == 0)
    {
        updateScoreboard(window, label, 51);
    }
    else 
    {
        updateScoreboard(window, label, 0);
    }
    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    return 0;
}
Beispiel #24
0
int main(int argc, string argv[])
{
    bool god_mode = false;
    
    if (argc > 1)
    {
        if (strlen(argv[1]) == 3 && strncmp(argv[1], "GOD", 3) == 0)
            // set god mode flag
            god_mode = true;
        else
        {
            printf("It seems you don't know the magic word...\n");
            return 1;
        }
    }
        
    // seed pseudorandom number generator
    srand48(time(NULL));

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

    // instantiate bricks and return array with brick-Y coordinates for scoring
    int *brick_offsets = initBricks(window);

    // instantiate ball, centered in middle of window
    GOval ball = initBall(window);
    add(window, ball);

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

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

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

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;
    
    // let scoreboard show up at the beginning of the game
    updateScoreboard(window, label, points);

    // declare once useful variables for game loop
    double mouse_x, paddle_x;
    double ball_x, ball_y;
    // paddle width is gonna change through the game
    double paddle_width = PADDLE_WIDTH;
    
    // start the ball with random x (scaled down by const)
    double ball_x_velocity = fmod(drand48() * BALL_VELOCITY_SCALE, BALL_VELOCITY_SCALE);
    double ball_y_velocity = 0.1;
    
    // wait for click before entering the game loop
    waitForClick(); 
  
    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {
        // move the ball  
        move(ball, ball_x_velocity, ball_y_velocity);
        
        // god mode
        if (god_mode)
        {
            if (getX(ball) > 0 && getX(ball) + paddle_width < WIDTH)
                setLocation(paddle, getX(ball), PADDLE_BOTTOM_OFFSET);      
        }
        // check user input (mouse movements)
        else
        {
            GEvent event = getNextEvent(MOUSE_EVENT);
            if (event != NULL && getEventType(event) == MOUSE_MOVED)
            {
                // set paddle x to be the paddle center aligned to the x mouse event 
                // keep paddle y constant (don't move through the y axis)
                mouse_x = getX(event);
                paddle_x = mouse_x - paddle_width / 2;
            // check mouse event x to keep paddle in the canvas
            if (mouse_x <= paddle_width / 2)
                paddle_x = 0;
            else if (mouse_x >= WIDTH - paddle_width / 2)
                paddle_x = WIDTH - paddle_width;
            // update paddle position
            setLocation(paddle, paddle_x, PADDLE_BOTTOM_OFFSET);    
            }  
        } 
              
        // store x and y for ball and prevent further functions calls
        ball_x = getX(ball);
        ball_y = getY(ball);
        // bouncing checks
        // top window bouncing
        if (ball_y <= 0) 
            ball_y_velocity = -ball_y_velocity;
        // bottom window bouncing
        else if (ball_y + BALL_DIMENSION >= HEIGHT)
        {
            // spawn ball from the center
            setLocation(ball, WIDTH / 2 - BALL_RADIUS, HEIGHT / 2 - BALL_RADIUS);
            // center the paddle
            setLocation(paddle, WIDTH / 2 - PADDLE_WIDTH / 2, 
                        PADDLE_BOTTOM_OFFSET);
            // pause 1 sec, update lives and starting ball velocity to random 
            pause(1000);
            ball_x_velocity = fmod(drand48() * BALL_VELOCITY_SCALE, BALL_VELOCITY_SCALE);
            lives -= 1;
        }    
        // left and right bouncing
        if (ball_x + BALL_DIMENSION >= WIDTH || ball_x <= 0)
            ball_x_velocity = -ball_x_velocity;
            
        // check for collisions
        GObject collided_obj = detectCollision(window, ball);
        if (collided_obj != NULL)
        {
            // check for rectangular object type
            if (strcmp(getType(collided_obj), "GRect") == 0)
            {   
                // ball hit the paddle
                if (collided_obj == paddle)
                {  
                    ball_y_velocity = -ball_y_velocity;
                    // prevent paddle-ball overlapping
                    setLocation(ball, ball_x, 
                                PADDLE_BOTTOM_OFFSET - BALL_RADIUS * 2);
                }
                // ball hit a brick
                else if (collided_obj != paddle)
                {
                    ball_y_velocity = -ball_y_velocity;
                    ball_y_velocity += ball_y_velocity * BALL_VELOCITY_FACTOR;
                    // reduce paddle width on every destroyed brick
                    paddle_width -= PADDLE_SHRINKING;
                    setSize(paddle, paddle_width, PADDLE_HEIGHT);
                    // remove brick and update game status variables
                    // points get updated properly for bricks on different levels
                    points += getPoints(brick_offsets, getY(collided_obj)); 
                    removeGWindow(window, collided_obj);
                    bricks -= 1;
                    updateScoreboard(window, label, points); 
                }
            }
        }   
    }
    
    // wait for click before exiting
    waitForClick();

    // game over
    closeGWindow(window);
    
    // fremem
    free(brick_offsets);
    
    return 0;
}
Beispiel #25
0
int main(void)
{
    // seed pseudorandom number generator
    srand48(time(NULL));

    // instantiate window
    GWindow window = newGWindow(WIDTH, HEIGHT);
    
    // instantiate bricks
    initBricks(window);
    
    // instantiate life counter
    GOval lifeball1 = newGOval(WIDTH - 30, 20, DIAMETER, DIAMETER);
    setFilled(lifeball1, true);
    setColor(lifeball1, "#000000");
    add(window, lifeball1);

    GOval lifeball2 = newGOval(WIDTH - 50, 20, DIAMETER, DIAMETER);
    setFilled(lifeball2, true);
    setColor(lifeball2, "#000000");
    add(window, lifeball2);

    // instantiate ball, centered in middle of window
    GOval ball = initBall(window);

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

    // instantiate scoreboard, centered in middle of window, just above ball
    GLabel scoreboard = initScoreboard(window);

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

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;

    //initialize ball velocities
    double xvelocity = drand48();
    double yvelocity = 2.0;

    // keep playing until game over
    
    if (lives > 0)
    {
        waitForClick();
    }
    while (lives > 0 && bricks > 0)
    {

        // compel paddle to follow mouse along x-axis by listening for mouse_event
        GEvent paddlemove = getNextEvent(MOUSE_EVENT);
        // if there was an event
        if (paddlemove != NULL)
            {
                // if the event was movement
                if (getEventType(paddlemove) == MOUSE_MOVED)
                {
                    // make paddle follow mouse pointer's motion along the x axis only
                    double paddlex = getX(paddlemove) - getWidth(paddle) / 2;
                    double paddley = 565;
                    setLocation(paddle, paddlex, paddley);
                }
            }
        

        // move ball along both x and y
        move(ball, xvelocity, yvelocity);
        
        GObject collided = detectCollision(window, ball);
        
        // Correct code to make ball bounce - this does not work, despite working exactly as so in bounce.c
        //perhaps a switch is in order using detect collision for bouncing
        if (getX(ball) + DIAMETER >= WIDTH)
        {
            xvelocity = -xvelocity;
        }

        // bounce off left edge of window
        else if (getX(ball) <= 0)
        {
            xvelocity = -xvelocity;
        }
        
        else if (getY(ball) <= 0)
        {
            yvelocity = -yvelocity;
        }
        
        else if (collided == paddle)
        {
            yvelocity = -yvelocity;
            xvelocity = xvelocity + (drand48() - drand48());
        }
        else if ((((collided != NULL) && (collided != paddle))
        && (collided != scoreboard)) && (strcmp(getType(collided), "GOval") != 0))
        {
            yvelocity = -yvelocity;
            xvelocity = xvelocity + (drand48() - drand48());
            removeGWindow(window, collided);
            points++;
            bricks = bricks - 1;
            updateScoreboard(window, scoreboard, points);
        }
        else if (getY(ball) + DIAMETER >= HEIGHT)
        {
            yvelocity = 2.0;
            xvelocity = drand48();
            lives = lives - 1;
            if (lives == 2)
            {
                removeGWindow(window, lifeball2);
            }
            else if (lives == 1)
            {
                removeGWindow(window, lifeball1);
            }
            
            setLocation(ball, (WIDTH / 2) - (DIAMETER / 2), (HEIGHT / 2) - (DIAMETER / 2));
            setLocation(paddle, (WIDTH / 2) - (PADDLEWIDTH / 2), HEIGHT - 35);
            if (lives > 0)
            {
                waitForClick();
            }
        }

        // linger before moving again
        pause(10);
    }
    // game over
    if (lives > 0 && bricks == 0)
    {
        GLabel winner = newGLabel("YOU WON!");
        setFont(winner, "SansSerif-22");
        double x = (WIDTH / 2) - (getWidth(winner) / 2);
        double y = HEIGHT - 250;
        setLocation(winner, x, y);
        setColor(winner, "#0033FF");
        add(window, winner);
    }
    else
    {
        GLabel gameover = newGLabel("GAME OVER");
        setFont(gameover, "SansSerif-22");
        double x = (WIDTH / 2) - (getWidth(gameover) / 2);
        double y = HEIGHT - 250;
        setLocation(gameover, x, y);
        setColor(gameover, "#990000");
        add(window, gameover);
    }
    waitForClick();
    return 0;
}
Beispiel #26
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_x = drand48() * 3;
    double velocity_y = 2.5;
     
    double x = getX(ball);
    double y = getY(ball);
    waitForClick();
    
    while (lives > 0 && bricks > 0)
    {
        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);
        }
             
             
        move(ball, velocity_x, velocity_y);
        if (getY(ball) + getHeight(ball) > getHeight(window))
        {
            velocity_y = -velocity_y;
        }
        else if (getY(ball) <= 0)
        {
            velocity_y = -velocity_y;
        }
        else if (getX(ball) + getWidth(ball) > getWidth(window))
        {
            velocity_x = -velocity_x;
        }
        else if (getX(ball) <= 0)
        {
            velocity_x = -velocity_x;
        }
        
        GObject object = detectCollision(window, ball);
        if (object == paddle)
        {
          velocity_y = -velocity_y;  
        }
        
        if ((strcmp(getType(object), "GRect") == 0) && (object != paddle))    
        {
            points = points + 1;
            removeGWindow(window, object);
            velocity_y = -velocity_y;
            char s[12];
            sprintf(s, "Score: %i", points);
            setLabel(label, s);
        }
            if (points == 50)
            {
                char s[12];
                sprintf(s, "Well Done!");               
                setLabel(label, s);
                pause(10);    
                break;
            }
        
        if (getY(ball) + 95 >= getHeight(window))
        {
            lives = lives -1;
            removeGWindow(window, ball);
            waitForClick(); 
            ball = initBall(window);
            char t[12];
            sprintf(t, "Lives: %i", lives);
            setLabel(label, t); 
        }
            if (lives == 0)
            {
                char s[12];
                sprintf(s, "Score: %i", points);
                sprintf(s,"Nice try!");
                setLabel(label, s);
                pause(20);
                break;
            }   
        
        pause(10);
        
    
    }
 
           
        waitForClick();
        pause(10);
    // game over
       
   
    closeGWindow(window);
    return 0;
    }
int main(void)
{

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

    // instantiate bricks
    initBricks(window);

    // instantiate ball, centered in middle of window
    GOval ball = initBall(window);

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

    // instantiate scoreboard, centered in middle of window, just above ball
    GLabel label = initScoreboard(window);

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

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;

    // wait for click before starting
    waitForClick();
    
    velocityX = 1.0; 
    velocityY = 2.5;

    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {        
        // Scoreboard
        updateScoreboard(window, label, points);
        
        // move ball
        move(ball, velocityX, velocityY);

        pause(10);
        
        // check for mouse event.
        GEvent event = getNextEvent(MOUSE_EVENT);
        
        // Lock the paddle X to the cursor.
        if (event != NULL)
        {
            // if the event was movement
            if (getEventType(event) == MOUSE_MOVED)
            {
                // ensure paddle follows top cursor
                double x = getX(event) - getWidth(paddle) / 2;
                double y = 500;
                setLocation(paddle, x, y);
            }
        }
        
        
        GObject object = detectCollision(window, ball);
        
        if (object != NULL)
        {
            // If the ball hits the paddle.
            if (object == paddle)
            {
                velocityY = -velocityY;
            }
            
            // If the ball hits a block. Remove block, add a point, decrement count and bounce.
            else if (strcmp(getType(object), "GRect") == 0)
            {
                removeGWindow(window, object);
                velocityY = -velocityY;
                points++;
                bricks--;                
            }
        }
        
        // If the ball hits the right wall.
        if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
            velocityX = -velocityX;
        }
        
        // If the ball hits the left wall.
        if (getX(ball) <= 0)
        {
            velocityX = -velocityX;
        }
        
        // If the ball hits the top wall.
        if (getY(ball) <= 0)
        {
            velocityY = -velocityY;
        }
        
        // Remove a life. Start over.
        if (getY(ball) + getHeight(ball) >= getHeight(window))
        {
            lives--;
            //move ball to start
            setLocation(ball, 190, 200);
            //move paddle to start
            setLocation(paddle, 160, 500);
            waitForClick();
        }
        
    }

    // You Lose Label Message for kicks.
    if (bricks > 0)
    {
        GLabel game_over = newGLabel("YOU LOSE!");
        setFont(game_over, "SansSerif-70");
        setColor(game_over, "RED");
        add(window, game_over);
        setLocation(game_over, 15, 300);
    }
    else
    {
        GLabel game_over = newGLabel("YOU WIN!");
        setFont(game_over, "SansSerif-70");
        setColor(game_over, "GREEN");
        add(window, game_over);
        setLocation(game_over, 15, 300);
    }
    
    // wait for click before exiting
    waitForClick();

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

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

    // instantiate bricks
    initBricks(window);

    // instantiate ball, centered in middle of window
    GOval ball = initBall(window);

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

    // instantiate scoreboard, centered in middle of window, just above ball
    GLabel score = initScoreboard(window);

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

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;

    // creates invisible rect to detect if ball has gone beyond window
    
    
    double x = drand48();
    if (x < 0.5)
    {
        x = x * 2;
    }
    else
    {
        x = x * 4;
    } 
    double y = drand48();
    if (y < 0.5)
    {
        y = y * 3;
    }
    else
    {
        y = y * 6;
    }
    double velocity_x = x;
    double velocity_y = y;
    
    
    // keep playing until game over
    
    while (lives > 0 && bricks > 0)
    {
        move(ball, velocity_x, velocity_y);
        pause(7.5);
            
        if (getX(ball) + getWidth(ball) >= getWidth(window))
        {
            velocity_x = -velocity_x;
        }
        else if (getX(ball) < 0)
        {
            velocity_x = -velocity_x;    
        }
        else if (getY(ball) <= 0 )
        {
            velocity_y = -velocity_y;
        }
        else if (getY(ball) + getHeight(ball) >= getHeight(window))
        {
            lives = lives - 1;
            int velocity_x_end = velocity_x * 0;
            int velocity_y_end = velocity_y * 0;
                
            while (getY(ball) + getHeight(ball) >= getHeight(window))
            {
                move(ball, velocity_x_end, velocity_y_end);
                GEvent mouse_clicked = getNextEvent(MOUSE_EVENT);
                if (mouse_clicked != NULL)
                {
                    if (getEventType(mouse_clicked) == MOUSE_CLICKED)
                    {
                       setLocation(ball,((WIDTH/2)-(30/2)),((HEIGHT/2)+(30/2)));
                       move(ball, velocity_x, velocity_y);
                    } 
                }
            }            
        }
        GObject collide = detectCollision(window, ball);
        if(collide == paddle)
        {               
            if (velocity_y > 0)
            {
                velocity_y = -velocity_y;
            }
        }
        else if (strcmp(getType(collide),"GRect") == 0 && collide != paddle)
        {
            if (velocity_y < 0)
            {
                velocity_y = -velocity_y;
            }
            removeGWindow(window, collide);
            points = points + 1;              
            updateScoreboard(window, score, points);
        }
                        
        GEvent move_paddle = getNextEvent(MOUSE_EVENT);
        if (move_paddle != NULL)
        {
            if (getEventType(move_paddle) == MOUSE_MOVED)
            {
                double x_paddle = getX(move_paddle);
                setLocation(paddle, x_paddle - (P_WIDTH/2), 550);
            } 
        }
          
     if (points == 50)
     {
        break;
     }
     }

    // wait for click before exiting
    waitForClick();

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

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

    // instantiate bricks
    initBricks(window);

    // instantiate ball, centered in middle of window
    GOval ball = initBall(window);

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

    // instantiate scoreboard, centered in middle of window, just above ball
    GLabel label = initScoreboard(window);

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

    // number of lives initially
    int lives = LIVES;

    // number of points initially
    int points = 0;
    
    // initial velocity
    double Xvelocity = drand48();
    double  Yvelocity = 2.0;    

    // keep playing until game over
    while (lives > 0 && bricks > 0)
    {    
        
            // move circle along y-axis
            move(ball, Xvelocity, Yvelocity);

            // bounce off top edge of window
            if (getY(ball) <= 0)
            {
                Yvelocity = -Yvelocity;
            }
            
            //  ball hits bottom edge, lose life
            if (getY(ball) + getHeight(ball) >= getHeight(window))
            {
                lives = lives - 1;
                removeGWindow(window, ball);
                waitForClick();
                ball = initBall(window);
                pause(20);
                Yvelocity = -Yvelocity;
            }
            
            // bounce off right edge of window
            if (getX(ball) + getWidth(ball) >= getWidth(window))
            {
                Xvelocity = -Xvelocity;
            }

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

            // linger before moving again
            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 grect follows top cursor
                double x = getX(event) - pWIDTH / 2;
                double y = 500;
                setLocation(paddle, x, y);
            }
        }
       
       //check for ball collision 
       GObject object = detectCollision(window, ball);
       
       //if there was a collision
       if (object != NULL)
       {
       
            //if it collided with the paddle
            if (object == paddle)
            {
                Yvelocity = -Yvelocity;
            }
            
            //removes brick, updates scores
            else if (strcmp(getType(object), "GRect") == 0)
            {
                Yvelocity = -Yvelocity;
                
                removeGWindow(window, object);
                
                points = points + 1;
                
                updateScoreboard(window, label, points);
            }
            
            //Do not detect scoreboard
            if (strcmp(getType(object), "GLabel") == 0)
            {
                    return 0;
            }
        }
        
    }

    // 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 x = 0.0;
    double vx = 2.5;
    double vy = 2.5;

    waitForClick();

    while (lives > 0 && bricks > 0)  // keep playing until game over //TODO FUN
    {
      GEvent event = getNextEvent(MOUSE_EVENT);
      if (event != NULL)
      {
        if (getEventType(event) == MOUSE_MOVED)
        {
         x = getX(event) -getWidth(paddle)/2;
         setLocation(paddle,x,588);
        }
      }


      move(ball,vx,vy);
      if ((getX(ball) + getWidth(ball) >= WIDTH) || (getX(ball) <= 0))
      {
       vx = -vx;
      }
      else if ((getY(ball) <= 0))
      {
       vy = -vy;
      }
      else if (getY(ball) + getHeight(ball) >= HEIGHT)
      {
          lives--;
          pause(250);
          setLocation(ball, WIDTH/2, HEIGHT/2);
          setLocation(paddle, 165, 588);

      }
      pause(7);


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

    }
    // wait for click before exiting


    // game over
    closeGWindow(window);
    if (points >= 50)
    {
        printf("You won, Congrats!!!!!!!!!!\n");
    }
    else
    {
        printf("You lose!!!\n");
    }
    return 0;
}