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

📄 g11-01.cpp

📁 游戏开发数据结构-Data.Structures.for.Game.Programmers
💻 CPP
字号:
// ============================================================================
// G11-01.cpp
// Plotline demo
// ============================================================================
#include "SDL.h"
#include "SDLHelpers.h"
#include "Tree.h"
#include <stdlib.h>

// ============================================================================
//  Global Constants
// ============================================================================
const char PROGRAM_NAME[]   = "Game Demo 11-01: Plotline Demo";
const int WIDTH             = 640;
const int HEIGHT            = 480;


// ============================================================================
//  Classes
// ============================================================================
class Player
{
public:
    float x;
    float y;
};

// game can be in three states: Playing, selecting a new level, or won the game.
enum GameState
{
    PLAYING,
    SELECTING,
    WIN
};


// ============================================================================
//  Global Variables
// ============================================================================

// this is the main window for the framework
SDL_Surface* g_window = 0;

Player g_player;

SDL_Surface* g_hero;
SDL_Surface* g_text;
SDL_Surface* g_win;
SDL_Surface* g_tiles[9];

GameState g_state = PLAYING;

Tree<int>* g_tree;
TreeIterator<int> g_itr;

int g_dx = 0;
int g_dy = 0;
int g_timer;

// ============================================================================
//  Functions
// ============================================================================

// this draws the map
void DrawMap()
{
    int x;
    int y;
    
    // go through every x,y
    for( y = 0; y < 8; y++ )
    {
        for( x = 0; x < 10; x++ )
        {
            // blit the tile at the current level.
            SDLBlit( g_tiles[g_itr.Item()], g_window, x * 64, y * 64 );
        }
    }
}

// draw the level select screen
void DrawOptions()
{
    // blit the "select next level" text
    SDLBlit( g_text, g_window, 0, 0 );

    int x = 128;

    // loop through every level option available at the current level
    for( g_itr.ChildStart(); g_itr.ChildValid(); g_itr.ChildForth() )
    {
        // draw the tile of that level
        SDLLine( g_window, x-1, 127, x+64, 127, WHITE );
        SDLLine( g_window, x-1, 192, x+64, 192, WHITE );
        SDLLine( g_window, x-1, 127, x-1, 192, WHITE );
        SDLLine( g_window, x+64, 127, x+64, 192, WHITE );
        SDLBlit( g_tiles[g_itr.ChildItem()], g_window, x, 128 );
        x += 128;
    }
}


int main( int argc, char* argv[] )
{
    // declare coordinates.
    int x, y;
    int dt;

    // declare event holder
    SDL_Event event;

    // the iterator that keeps track of the current level
    TreeIterator<int> itr;

    // a temporary node structure, for creating new nodes
    Tree<int>* node;

    // start the player off at 0,0
    g_player.x = 0.0f;
    g_player.y = 0.0f;


    // create the root node
    g_tree = new Tree<int>;
    g_tree->m_data = 0;
    g_itr = g_tree;

    // create the iterator.
    itr = g_tree;

    // add the '2a' branch
    node = new Tree<int>;
    node->m_data = 1;
    itr.AppendChild( node );

    // add the '2b' branch
    node = new Tree<int>;
    node->m_data = 2;
    itr.AppendChild( node );

    // add the '2d' branch
    node = new Tree<int>;
    node->m_data = 3;
    itr.AppendChild( node );
    
    // add the '3a' branch
    itr.ChildStart();
    itr.Down();
    node = new Tree<int>;
    node->m_data = 4;
    itr.AppendChild( node );

    // add the '3b' branch
    itr.Up();
    itr.ChildStart();
    itr.ChildForth();
    itr.Down();
    node = new Tree<int>;
    node->m_data = 5;
    itr.AppendChild( node );

    // add the '3c' branch
    node = new Tree<int>;
    node->m_data = 6;
    itr.AppendChild( node );

    // add the '3d' branch
    itr.Up();
    itr.ChildStart();
    itr.ChildForth();
    itr.ChildForth();
    itr.Down();
    node = new Tree<int>;
    node->m_data = 7;
    itr.AppendChild( node );

    // add the '3e' branch
    node = new Tree<int>;
    node->m_data = 8;
    itr.AppendChild( node );



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

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

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


    // load the hero
    g_hero = SDL_LoadBMP( "hero1.bmp" );
    SDL_SetColorKey( g_hero, SDL_SRCCOLORKEY, 
                     SDL_MapRGB( g_hero->format, 255, 0, 255 ) );

    // load the text
    g_text = SDL_LoadBMP( "text.bmp" );
    g_win = SDL_LoadBMP( "win.bmp" );

    // load the tiles
    g_tiles[0] = SDL_LoadBMP( "smallcrust.bmp" );
    g_tiles[1] = SDL_LoadBMP( "smallfire.bmp" );
    g_tiles[2] = SDL_LoadBMP( "smallice.bmp" );
    g_tiles[3] = SDL_LoadBMP( "smallmarble.bmp" );
    g_tiles[4] = SDL_LoadBMP( "smallmetal.bmp" );
    g_tiles[5] = SDL_LoadBMP( "smallorangesky.bmp" );
    g_tiles[6] = SDL_LoadBMP( "smallstone.bmp" );
    g_tiles[7] = SDL_LoadBMP( "smallwater.bmp" );
    g_tiles[8] = SDL_LoadBMP( "smallwood.bmp" );



    // set the timer
    g_timer = SDL_GetTicks();


    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( y >= 128 && y <= 192 && g_state == SELECTING )
                {
                    x = (x - 128) / 128;
                    if( x < g_itr.m_node->m_children.Size() )
                    {
                        g_itr.ChildStart();
                        while( x > 0 )
                        {
                            g_itr.ChildForth();
                            x--;
                        }
                        g_itr.Down();
                        g_state = PLAYING;
                    }
                }
            }
            if( event.type == SDL_MOUSEBUTTONUP ) 
            {
                // get the mouse state.
                SDL_GetMouseState( &x, &y );
            }
            if( event.type == SDL_KEYUP )
            {
                if( event.key.keysym.sym == SDLK_LEFT )
                {
                    g_dx += 1;
                }
                if( event.key.keysym.sym == SDLK_RIGHT )
                {
                    g_dx -= 1;
                }
                if( event.key.keysym.sym == SDLK_UP )
                {
                    g_dy += 1;
                }
                if( event.key.keysym.sym == SDLK_DOWN )
                {
                    g_dy -= 1;
                }
            }
            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_LEFT )
                {
                    g_dx -= 1;
                }
                if( event.key.keysym.sym == SDLK_RIGHT )
                {
                    g_dx += 1;
                }
                if( event.key.keysym.sym == SDLK_UP )
                {
                    g_dy -= 1;
                }
                if( event.key.keysym.sym == SDLK_DOWN )
                {
                    g_dy += 1;
                }

            }

        }   // end event loop.



        if( g_state == PLAYING )
        {
            // calculate how much time has passed since the last timer update
            dt = SDL_GetTicks() - g_timer;
            g_timer = SDL_GetTicks();

            // move the player over by 128 pixels per second.
            g_player.x += ((float)(dt * 128) / 1000.0f) * g_dx;
            g_player.y += ((float)(dt * 128) / 1000.0f) * g_dy;

            // make sure the player doesn't go off the top, left, or
            // bottom sides of the screen.
            if( g_player.x < 0.0f )
                g_player.x = 0.0f;
            if( g_player.y < 0.0f )
                g_player.y = 0.0f;
            if( g_player.y > 416.0f )
                g_player.y = 416.0f;



            DrawMap();
            SDLBlit( g_hero, g_window, g_player.x, g_player.y );

            // if the player went past 640, he wins the level!
            if( g_player.x > 640.0f )
            {
                // reset the coordinates of the player
                g_player.x = 0.0f;
                g_player.y = 0.0f;

                // if the current level iterator has no children,
                // then that means the player won the game
                if( g_itr.m_node->m_children.Size() == 0 )
                {
                    g_state = WIN;
                }
                else
                {
                    // if not, then select the next level.
                    g_state = SELECTING;
                }
            }
        }
        else if( g_state == SELECTING )
        {
            // draw the option screen
            SDL_FillRect( g_window, NULL, SDL_MapRGB( g_window->format, 0, 0, 0 ) );
            DrawOptions(); 
        }
        else
        {
            // draw the "You win!" screen
            SDL_FillRect( g_window, NULL, SDL_MapRGB( g_window->format, 0, 0, 0 ) );
            SDLBlit( g_win, 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 + -