⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 g24-01.cpp

📁 游戏开发数据结构-Data.Structures.for.Game.Programmers
💻 CPP
📖 第 1 页 / 共 2 页
字号:

    g_dungeontiles[10] = SDL_LoadBMP( "dirtotl.bmp" );
    g_dungeontiles[11] = SDL_LoadBMP( "dirtotr.bmp" );
    g_dungeontiles[12] = SDL_LoadBMP( "dirtobl.bmp" );
    g_dungeontiles[13] = SDL_LoadBMP( "dirtobr.bmp" );

    
    // players
    g_people[0][0][0] = SDL_LoadBMP( "hero1u1.bmp" );
    g_people[0][0][1] = SDL_LoadBMP( "hero1u2.bmp" );
    g_people[0][0][2] = SDL_LoadBMP( "hero1u3.bmp" );
    g_people[0][0][3] = SDL_LoadBMP( "hero1u4.bmp" );
    g_people[0][1][0] = SDL_LoadBMP( "hero1r1.bmp" );
    g_people[0][1][1] = SDL_LoadBMP( "hero1r2.bmp" );
    g_people[0][1][2] = SDL_LoadBMP( "hero1r3.bmp" );
    g_people[0][1][3] = SDL_LoadBMP( "hero1r4.bmp" );
    g_people[0][2][0] = SDL_LoadBMP( "hero1d1.bmp" );
    g_people[0][2][1] = SDL_LoadBMP( "hero1d2.bmp" );
    g_people[0][2][2] = SDL_LoadBMP( "hero1d3.bmp" );
    g_people[0][2][3] = SDL_LoadBMP( "hero1d4.bmp" );
    g_people[0][3][0] = SDL_LoadBMP( "hero1l1.bmp" );
    g_people[0][3][1] = SDL_LoadBMP( "hero1l2.bmp" );
    g_people[0][3][2] = SDL_LoadBMP( "hero1l3.bmp" );
    g_people[0][3][3] = SDL_LoadBMP( "hero1l4.bmp" );

    g_people[1][0][0] = SDL_LoadBMP( "hero2u.bmp" );
    g_people[1][0][1] = g_people[1][0][0];
    g_people[1][0][2] = g_people[1][0][0];
    g_people[1][0][3] = g_people[1][0][0];
    g_people[1][1][0] = SDL_LoadBMP( "hero2r.bmp" );
    g_people[1][1][1] = g_people[1][1][0];
    g_people[1][1][2] = g_people[1][1][0];
    g_people[1][1][3] = g_people[1][1][0];
    g_people[1][2][0] = SDL_LoadBMP( "hero2d.bmp" );
    g_people[1][2][1] = g_people[1][2][0];
    g_people[1][2][2] = g_people[1][2][0];
    g_people[1][2][3] = g_people[1][2][0];
    g_people[1][3][0] = SDL_LoadBMP( "hero2l.bmp" );
    g_people[1][3][1] = g_people[1][3][0];
    g_people[1][3][2] = g_people[1][3][0];
    g_people[1][3][3] = g_people[1][3][0];

    g_people[2][0][0] = SDL_LoadBMP( "hero3u.bmp" );
    g_people[2][0][1] = g_people[2][0][0];
    g_people[2][0][2] = g_people[2][0][0];
    g_people[2][0][3] = g_people[2][0][0];
    g_people[2][1][0] = SDL_LoadBMP( "hero3r.bmp" );
    g_people[2][1][1] = g_people[2][1][0];
    g_people[2][1][2] = g_people[2][1][0];
    g_people[2][1][3] = g_people[2][1][0];
    g_people[2][2][0] = SDL_LoadBMP( "hero3d.bmp" );
    g_people[2][2][1] = g_people[2][2][0];
    g_people[2][2][2] = g_people[2][2][0];
    g_people[2][2][3] = g_people[2][2][0];
    g_people[2][3][0] = SDL_LoadBMP( "hero3l.bmp" );
    g_people[2][3][1] = g_people[2][3][0];
    g_people[2][3][2] = g_people[2][3][0];
    g_people[2][3][3] = g_people[2][3][0];


    // items
    g_items[0] = SDL_LoadBMP( "wallh.bmp" );
    g_items[1] = SDL_LoadBMP( "wallv.bmp" );
    g_items[2] = SDL_LoadBMP( "walltl.bmp" );
    g_items[3] = SDL_LoadBMP( "walltr.bmp" );
    g_items[4] = SDL_LoadBMP( "wallbl.bmp" );
    g_items[5] = SDL_LoadBMP( "wallbr.bmp" );

    g_items[6] = SDL_LoadBMP( "wepicon-axe.bmp" );
    g_items[7] = SDL_LoadBMP( "wepicon-bow.bmp" );
    g_items[8] = SDL_LoadBMP( "wepicon-knife.bmp" );
    g_items[9] = SDL_LoadBMP( "wepicon-mace.bmp" );
    g_items[10] = SDL_LoadBMP( "wepicon-spear.bmp" );
    g_items[11] = SDL_LoadBMP( "wepicon-sword.bmp" );
    g_items[12] = SDL_LoadBMP( "wepicon-helm.bmp" );
    g_items[13] = SDL_LoadBMP( "wepicon-shield.bmp" );

    g_items[14] = SDL_LoadBMP( "rvortex.bmp" );
    g_items[15] = SDL_LoadBMP( "gvortex.bmp" );
    g_items[16] = SDL_LoadBMP( "bvortex.bmp" );

    g_statusbar = SDL_LoadBMP( "bar.bmp" );
    g_verticalbar = SDL_LoadBMP( "vbar.bmp" );
    g_youlose = SDL_LoadBMP( "lose.bmp" );


    // make it so that purple pixels are transparent.
    for( x = 0; x < OUTDOORTILES; x++ )
    {
        SDL_SetColorKey( g_outdoortiles[x], SDL_SRCCOLORKEY, 
                         SDL_MapRGB( g_outdoortiles[x]->format, 255, 0, 255 ) );
    }
    for( x = 0; x < DUNGEONTILES; x++ )
    {
        SDL_SetColorKey( g_dungeontiles[x], SDL_SRCCOLORKEY, 
                         SDL_MapRGB( g_dungeontiles[x]->format, 255, 0, 255 ) );
    }
    for( x = 0; x < ITEMS; x++ )
    {
        SDL_SetColorKey( g_items[x], SDL_SRCCOLORKEY, 
                         SDL_MapRGB( g_items[x]->format, 255, 0, 255 ) );
    }

    
    for( x = 0; x < PEOPLE; x++ )
    {
        for( d = 0; d < DIRECTIONS; d++ )
        {
            for( f = 0; f < FRAMES; f++ )
            {
                SDL_SetColorKey( g_people[x][d][f], 
                                 SDL_SRCCOLORKEY, 
                                 SDL_MapRGB( g_people[x][d][f]->format, 
                                             255, 0, 255 ) );
            }
        }
    }


    // set up the item templates
    for( x = 0; x < ITEMS; x++ )
    {
        g_itemtemplates[x].SetType( x );
        g_itemtemplates[x].SetGraphic( g_items[x] );
    }

    // make the walls block
    for( x = 0; x < 6; x++ )
    {
        g_itemtemplates[x].SetBlock( true );
    }

    // axe
    g_itemtemplates[6].SetSpeed( 1500 );
    g_itemtemplates[6].SetStrength( 15 );

    // bow
    g_itemtemplates[7].SetSpeed( 750 );
    g_itemtemplates[7].SetStrength( 5 );

    // knife
    g_itemtemplates[8].SetSpeed( 500 );
    g_itemtemplates[8].SetStrength( 4 );

    // mace
    g_itemtemplates[9].SetSpeed( 1250 );
    g_itemtemplates[9].SetStrength( 12 );

    // spear
    g_itemtemplates[10].SetSpeed( 1250 );
    g_itemtemplates[10].SetStrength( 13 );

    // sword
    g_itemtemplates[11].SetSpeed( 1500 );
    g_itemtemplates[11].SetStrength( 20 );

    // helm
    g_itemtemplates[12].SetStrength( 30 );
    g_itemtemplates[12].SetArmor( true );

    // shield
    g_itemtemplates[13].SetStrength( 40 );
    g_itemtemplates[13].SetArmor( true );

    // vortexes
    g_itemtemplates[14].SetGet( false );
    g_itemtemplates[14].SetExit( true );
    g_itemtemplates[14].SetExitNumber( 0 );

    g_itemtemplates[15].SetGet( false );
    g_itemtemplates[15].SetExit( true );
    g_itemtemplates[15].SetExitNumber( 1 );

    g_itemtemplates[16].SetGet( false );
    g_itemtemplates[16].SetExit( true );
    g_itemtemplates[16].SetExitNumber( 2 );
    
    
    for( x = 0; x < PEOPLE; x++ )
    {
        // set the person type
        g_persontemplates[x].SetPersonType( x );

        for( d = 0; d < DIRECTIONS; d++ )
        {
            for( f = 0; f < FRAMES; f++ )
            {
                // set the person graphic
                g_persontemplates[x].SetGraphic( g_people[x][d][f], d, f );
            }
        }
    }

    g_persontemplates[1].SetArmor( 10 );
    g_persontemplates[1].SetHealth( 20 );
    g_persontemplates[1].SetAttackModifier( -500 );

    g_persontemplates[2].SetArmor( 15 );
    g_persontemplates[2].SetHealth( 30 );
    g_persontemplates[2].SetAttackModifier( -300 );

    // load the map
    SetNewMap( "level1.map" );
}


Map* LoadMap( char* p_filename )
{
    int maptype;

    // open the file
    FILE* f = fopen( p_filename, "rb" );

    TileMap* tmap;
    DirectionMap* dmap;

    // return if it couldn't be opened.
    if( f == 0 )
        return 0;

    // read in the map type
    fread( &maptype, 1, sizeof(int), f );
    fclose( f );


    // check the type, and load the appropriate type.
    if( maptype == 0 )
    {
        tmap = new TileMap( 64, 64, 2, g_outdoortiles );
        tmap->LoadFromFile( p_filename );
        return tmap;
    }
    else if( maptype == 1 )
    {
        dmap = new DirectionMap( g_dungeontiles );
        dmap->LoadFromFile( p_filename );
        return dmap;
    }

    return 0;
}


void SetNewMap( char* p_filename )
{
    int x;
    Map* newmap;
    Person* newperson;
        
    // load the new map
    newmap = LoadMap( p_filename );

    // load the new viewer
    newperson = newmap->GetViewer();

    // reset the people array count. Note that it is safe
    // to write over all of the persons in the array, 
    // since the map is deleted after the next loop,
    // and when the map is deleted, all the items and
    // people on the map are deleted as well.
    g_peoplecount = 0;
    
    // get all the people from the map, and store
    // them in the array.
    for( x = 0; x < newmap->GetNumberOfCells(); x++ )
    {
        if( newmap->GetPerson( x ) != 0 )
        {
            AddPersonToArray( newmap->GetPerson( x ) );
        }
    }

    // copy the inventory of the current player over into the
    // new players inventory.
    if( g_currentplayer != 0 )
    {
        newperson->CopyInventory( g_currentplayer );
    }

    // check to see if a map already exists.
    if( g_currentmap != 0 )
    {
        // delete the map
        delete g_currentmap;
    }

    g_currentmap = newmap;
    g_currentplayer = newmap->GetViewer();
}


// ============================================================================
//  Main Loop
// ============================================================================
int main( int argc, char* argv[] )
{
    // declare coordinates.
    int x, y;
    
    // declare event holder
    SDL_Event event;

    // initialize the video system.
    SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER );
    SDL_WM_SetCaption( PROGRAM_NAME, 0); 
    SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, 
                         SDL_DEFAULT_REPEAT_INTERVAL );

    // set our at exit function
    atexit( SDL_Quit );

    // set the video mode.
    g_window = SDL_SetVideoMode( WIDTH, HEIGHT, 0, SDL_ANYFORMAT );


    Init();
    
    while( 1 )
    {
        //look for an event
        if( SDL_PollEvent( &event ) )
        {
            //an event was found
            if( event.type == SDL_QUIT ) 
                break;
            if( event.type == SDL_MOUSEBUTTONDOWN ) 
            {
                // get the mouse state.
                SDL_GetMouseState( &x, &y );
            }
            if( event.type == SDL_MOUSEBUTTONUP ) 
            {
                // get the mouse state.
                SDL_GetMouseState( &x, &y );
            }
            if( event.type == SDL_KEYDOWN )
            {
                // a key was pressed.
                if( event.key.keysym.sym == SDLK_ESCAPE )
                {
                    // if ESC was pressed, quit the program.
                    SDL_Event quit;
                    quit.type = SDL_QUIT;
                    SDL_PushEvent( &quit );
                }


                if( event.key.keysym.sym == SDLK_LEFTBRACKET )
                {
                    // move to the previous item
                    g_currentplayer->PreviousWeapon();
                }
                if( event.key.keysym.sym == SDLK_RIGHTBRACKET )
                {
                    // move to the next item
                    g_currentplayer->NextWeapon();
                }

                if( event.key.keysym.sym == SDLK_RETURN )
                {
                    // pick up the current item
                    PickUp( g_currentplayer );
                }


                if( event.key.keysym.sym == SDLK_SPACE )
                {
                    // the player is attacking
                    Attack( g_currentplayer );
                }


                // toggle cheat mode
                if( event.key.keysym.sym == SDLK_c )
                {
                    g_cheat = !g_cheat;
                }


                int direction = -1;
                if( event.key.keysym.sym == SDLK_UP )
                    direction = 0;
                if( event.key.keysym.sym == SDLK_DOWN )
                    direction = 2;
                if( event.key.keysym.sym == SDLK_LEFT )
                    direction = 3;
                if( event.key.keysym.sym == SDLK_RIGHT )
                    direction = 1;

                if( direction != -1 )
                {
                    g_currentplayer->SetDirection( direction );
                    g_currentmap->Move( g_currentplayer, direction );
                }
            }

            if( event.type == SDL_KEYUP )
            {
                // a key was released.



            }
        }   // end event loop.


        // clear the screen to black.
        SDL_FillRect( g_window, NULL, SDL_MapRGB( g_window->format, 0, 0, 0 ) );

        if( g_dead == false )
        {
            // perform AI calculations
            PerformAI( SDL_GetTicks() );

            // check for dead people, and remove them
            CheckForDeadPeople();

            // player is cheating!
            if( g_cheat == true )
            {
                g_currentplayer->SetAttackTime( 0 );
            }


            // draw the map
            if( g_currentmap != 0 )
            {
                g_currentmap->Draw( g_window, WIDTH/2, HEIGHT/2 );
            }

            // draw the statusbar        
            DrawStatus();
        }
        else
        {
            SDLBlit( g_youlose, g_window, 0, 0 );
        }


        // update the entire window.
        SDL_UpdateRect( g_window, 0, 0, 0, 0 );
    }
  
    // done
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -