예제 #1
0
int main( int argc, char* args[] )
{

    bool quit = false;
    bool show_menu = true;
    int score = 0;
    int cursor = 0;





    //setup-----------------------------------------------------------------------------------------
    //----------------------------------------------------------------------------------------------
    if( init() == false )               //Starts up SDL and quits if there is a problem.
        return 1;
    //-----------------------------------------------------------------------------
	Player_1.Display = Display;         //Sets Player_1 to be able to blit onscreen
	Player_2.Display = Display;         //Sets Player_2 to be able to blit onscreen
	Ball.Display = Display;             //Sets Ball to be able to blit onscreen
	Menu.Display = Display;             //Sets Menu to be able to blit onscreen
    //------------------------------------------------------------------------------
    //Loads any needed files and quits if there is a problem.
    //----------------------------------------------------------------------------------------------
    if( ( Player_1.load_files() == false ) || ( Player_2.load_files() == false ) || ( Ball.load_files() == false ) || ( Menu.load_files() == false ) )
        return 1;
    //----------------------------------------------------------------------------------------------





    //Splash Screen then menu
    //----------------------------------------------------------------------------------------------
    Menu.logo();
    Menu.fade( "white" );






    //The main loop---------------------------------------------------------------------------------
    //----------------------------------------------------------------------------------------------
    while( quit == false )
    {
        if( ! ( Fps.is_paused() ) )
            Fps.start();

        //While there is an event to handle
        while( SDL_PollEvent( &event ) )
        {

            //Handle inputs for Player 1 anf 2
            //----------------------------------------------
            Player_1.handle_event( event );
            Player_2.handle_event( event );
            //Handles resizing of the window
            Window.handle_event( event );

            quit = Menu.handle_event( event );

            //----------------------------------------------

            //Handles resizing of the bat and ball
            //----------------------------------------------
            if( event.type == SDL_VIDEOEXPOSE || event.type == SDL_VIDEORESIZE )
            {
                Ball.resize();
                Player_1.resize();
                Player_2.resize();
            }
            //----------------------------------------------


            //If the user presses 'p' it will pause the game
            //----------------------------------------------
            if( ( event.type == SDL_KEYDOWN )  )
            {
                if( ( event.key.keysym.sym == SDLK_p ) && ( show_menu == false ) )
                {
                    if( Fps.is_paused() )
                        Fps.unpause();

                    else if( !( Fps.is_paused() ) )
                        Fps.pause();
                }

                if( event.key.keysym.sym == SDLK_ESCAPE )
                {
                    if( show_menu == true )
                        show_menu = false;

                    else if( show_menu == false )
                        show_menu = true;
                }

            }
            //----------------------------------------------
            if( event.type == SDL_QUIT )
            {
                quit = true;
            }


        }


        //Move Player 1 and 2 based on their inputs
        //----------------------------------------------
        if( !( Fps.is_paused() ) && ( show_menu == false ) )
        {
            Player_1.move();
            Player_2.move();

            //Move the ball and check for scores
            score = Ball.move( Player_1.hit_box, Player_2.hit_box );

            if( score == P1_SCORES )
                Player_1.score++;

            if( score == P2_SCORES )
                Player_2.score++;

            if ( score == P1_SCORES || score == P2_SCORES)
                Ball.reset();
        }
        //----------------------------------------------


        //Render-----------------------------------------------------------------------------------
        //-----------------------------------------------------------------------------------------
        SDL_FillRect( Display, &Display->clip_rect, SDL_MapRGB( Display->format, 0x00, 0x00, 0x00 ) );

        if( show_menu == false )
        {
            Player_1.render();
            Player_2.render();
            Ball.render();
        }

        if( show_menu == true )
            Menu.render();


        SDL_Flip( Display );
        Fps.frame_cap();
        //-----------------------------------------------------------------------------------------
    }
    //end of main loop------------------------------------------------------------------------------
    //----------------------------------------------------------------------------------------------

    //Clean up and quit SDL--------
    //-----------------------------
    Player_1.clean_up();
    Player_2.clean_up();
    Ball.clean_up();
    Menu.clean_up();
    clean_up();
    //-----------------------------
    return 0;
}
예제 #2
0
void EnvironmentClass::init(const GLint &width, const GLint &height)
{

    for (int i = 0; i < 1; i++)
    {

        GLPosition robotPos;
        robotPos.posX=400;
        robotPos.posY=400;
        GLint robotRadius=10;
        Ball *pRobot = new(std::nothrow) Ball(robotPos, robotRadius, 0);
        
        if (NULL == pRobot)
            continue;

        //pRobot->type = Object::eRobot;
        pRobot->setSpeed(100);
        pRobot->setOrientation(90);
        //pRobot->direction=120;
        
           
//        pRobot->setTarget(i * 2 + 1010);
        pRobot->render(glColorVec(0.0f, 80.0f, 100.0f));
//         
//        if (i % 2)
//            pRobot->setDirectionColor(glColorVec(100, 0, 0));
//        else
//            pRobot->setDirectionColor(glColorVec(200, 0, 100));
             
        vecRobot.push_back(pRobot);
        //printf("direction is %d\n",pRobot->direction);
        //printf("direction is %d\n",vecRobot[i]->getOrientation());
    }
    /**
     *@endcode
     */
    
    /**
     *@todo must create 2 robots
     */ 
    assert(1 == vecRobot.size());
   
    /** 
     * @code  
     * initialize obstacle objects,
     * rand generater radius for obstacle, it between [16, 48) pixels
     * generate a rand posion for the obstacle and new circular obstacle instance
     * set obstacle id,type, default color, 
     * if this obstacle is appointed to a robot, use the robot's direction line color to draw it
     * add to the obstacle vector
     */ 
    for (int i = 0; i < 2; i++)
    {

        GLPosition obsPos;
        obsPos.posX=300;
        obsPos.posY=790-i*790;
        
        Bat *pCircObs = new(std::nothrow) Bat(obsPos, 200, 10);
        
        if (NULL == pCircObs)
            continue;
            pCircObs->direction=90;
        pCircObs->motionless = false;
        pCircObs->id = 1010 + i;
       
        pCircObs->type = Object::eCircular;
        
        pCircObs->render(glColorVec(0, 100, 0));
        
        vecbat.push_back(pCircObs);
    }
    /**
     *@endcode
     */
    
    /**
     *@todo must create 6 circular obstacle
     */ 
    assert(2 == vecbat.size());
    
    /**
     *@todo remember the gui windows's size
     */ 
    winWidth = width;
    winHeight = height;
}