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

📄 demo12_6.cpp

📁 windows游戏编程大师源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:


// these are local defines to help compute the direction to move the
// skelaton if it needs to hunt down bat, note they are all in power
// of 2 order, hence, they are mutually exclusive

#define WEST_BIT     1
#define EAST_BIT     2
#define NORTH_BIT    4 
#define SOUTH_BIT    8 

// test if it's time to process a new instruction
if (curr_pattern==NULL)
   {
   // select a random pattern in pattern bank
   skelaton_pattern_index = rand()%NUM_PATTERNS;
   curr_pattern = patterns[skelaton_pattern_index];
   
   // now reset instuction pointer
   skelaton_ip = 0;

   // reset counter
   skelaton_counter = 0;
 
   } // end if

// process next instruction if it's time
if (--skelaton_counter <= 0)
    {
    // get next instruction
    opcode  = curr_pattern[skelaton_ip++];
    operand = curr_pattern[skelaton_ip++];

    // test what the opcode is
    switch(opcode)
        {
        case OPC_E:
            {
            // set direction to east
            Set_Vel_BOB(&skelaton,3,0);
  
            // set animation to east
            Set_Animation_BOB(&skelaton,opcode);
            
            // set counter to instuction operand
            skelaton_counter = operand;

            } break;

        case OPC_NE:
            {
            // set direction to northeast
            Set_Vel_BOB(&skelaton,3,-3);
  
            // set animation to northeast
            Set_Animation_BOB(&skelaton,opcode);
            
            // set counter to instuction operand
            skelaton_counter = operand;

            } break;

        case OPC_N: 
            {
            // set direction to north
            Set_Vel_BOB(&skelaton,0,-3);
  
            // set animation to north
            Set_Animation_BOB(&skelaton,opcode);
            
            // set counter to instuction operand
            skelaton_counter = operand;

            } break;            

        case OPC_NW:
            {
            // set direction to northwest
            Set_Vel_BOB(&skelaton,-3,-3);
  
            // set animation to northwest
            Set_Animation_BOB(&skelaton,opcode);
            
            // set counter to instuction operand
            skelaton_counter = operand;

            } break;            

        case OPC_W: 
            {
            // set direction to west
            Set_Vel_BOB(&skelaton,-3,0);
  
            // set animation to west
            Set_Animation_BOB(&skelaton,opcode);
            
            // set counter to instuction operand
            skelaton_counter = operand;
			 
            } break;            

        case OPC_SW:
            {
            // set direction to southwest
            Set_Vel_BOB(&skelaton,-3,3);
  
            // set animation to southwest
            Set_Animation_BOB(&skelaton,opcode);
            
            // set counter to instuction operand
            skelaton_counter = operand;

            } break;            

        case OPC_S: 
            {
            // set direction to south
            Set_Vel_BOB(&skelaton,0,3);
  
            // set animation to south
            Set_Animation_BOB(&skelaton,opcode);
            
            // set counter to instuction operand
            skelaton_counter = operand;

            } break;            

        case OPC_SE:
            {
            // set direction to southeast
            Set_Vel_BOB(&skelaton,3,3);
  
            // set animation to southeast
            Set_Animation_BOB(&skelaton,opcode);
            
            // set counter to instuction operand
            skelaton_counter = operand;

            } break;            

        case OPC_STOP: 
            {
            // stop motion
            Set_Vel_BOB(&skelaton,0,0);
            
            // set counter to instuction operand
            skelaton_counter = operand;

            } break;

        case OPC_RAND:
            {
            // set counter to instuction operand
            skelaton_counter = 0;

            } break;

        case OPC_TEST_DIST:
             {
             // test distance between bat and skelaton
             // if bat is too far, then move toward bat

             int dx = (bat.x - skelaton.x);
             int dy = (bat.y - skelaton.y);

             // test distance against minimum lonelyness
             if (sqrt((dx*dx) + (dy*dy)) > MIN_LONELYNESS)
                {
                // the skelaton needs to be pointed toward the bat (player)
                // this is a bit hard because we need to point the skelaton
                // in 1 of 8 directions, instead of just giving him a velocity
                // to solve the problem well break it up into a dx and a dy and then
                // use a look up table to set everything up right
   
                DSound_Play(laugh_sound_id);

                int direction = 0; // the bit encoded direction

                // first east-west
                if (bat.x > skelaton.x)
                   direction|=EAST_BIT;
                else
                if (bat.x < skelaton.x)
                   direction|=WEST_BIT;
                
                // now north-south
                if (bat.y > skelaton.y)
                   direction|=SOUTH_BIT;
                else
                if (bat.y < skelaton.y)
                   direction|=NORTH_BIT;
          
                // test final direction, note this could be compressed into
                // another look up table, but this is simpler 
                switch(direction)
                        {
                        case WEST_BIT:
                            {
                            // set motion
                            Set_Vel_BOB(&skelaton,-3,0);
  
                            // set animation 
                            Set_Animation_BOB(&skelaton,OPC_W);
            
                            // set counter to instuction operand
                            skelaton_counter = operand;

                            } break;

                        case EAST_BIT:     
                             {
                            // set motion
                            Set_Vel_BOB(&skelaton,3,0);
  
                            // set animation 
                            Set_Animation_BOB(&skelaton,OPC_E);
            
                            // set counter to instuction operand
                            skelaton_counter = operand;

                             } break;

                        case NORTH_BIT:     
                            {
                            // set motion
                            Set_Vel_BOB(&skelaton,0,-3);
  
                            // set animation 
                            Set_Animation_BOB(&skelaton,OPC_N);
            
                            // set counter to instuction operand
                            skelaton_counter = operand;

                            } break;

                        case SOUTH_BIT:     
                            {
                            // set motion
                            Set_Vel_BOB(&skelaton,0,3);
  
                            // set animation 
                            Set_Animation_BOB(&skelaton,OPC_S);
            
                            // set counter to instuction operand
                            skelaton_counter = operand;
                            } break;

                        case (NORTH_BIT | WEST_BIT):
                            {
                            // set motion
                            Set_Vel_BOB(&skelaton,-3,-3);
  
                            // set animation 
                            Set_Animation_BOB(&skelaton,OPC_NW);
            
                            // set counter to instuction operand
                            skelaton_counter = operand;
                            } break;

                        case (NORTH_BIT | EAST_BIT):
                            {
                            // set motion
                            Set_Vel_BOB(&skelaton,3,-3);
  
                            // set animation 
                            Set_Animation_BOB(&skelaton,OPC_NE);
            
                            // set counter to instuction operand
                            skelaton_counter = operand;
                            } break;

                        case (SOUTH_BIT | WEST_BIT):
                            {
                            // set motion
                            Set_Vel_BOB(&skelaton,-3,3);
  
                            // set animation 
                            Set_Animation_BOB(&skelaton,OPC_SW);
            
                            // set counter to instuction operand
                            skelaton_counter = operand;
                            } break;

                        case (SOUTH_BIT | EAST_BIT):
                            {
                            // set motion
                            Set_Vel_BOB(&skelaton,3,3);
  
                            // set animation 
                            Set_Animation_BOB(&skelaton,OPC_SE);
            
                            // set counter to instuction operand
                            skelaton_counter = operand;
                            } break;

                        default: break;

                        } // end switch 
               
                } // end if

             } break;

        case OPC_END: 
            {
            // stop motion
            Set_Vel_BOB(&skelaton,0,0);
            
            // select a random pattern in pattern bank
            skelaton_pattern_index = rand()%NUM_PATTERNS;
            curr_pattern = patterns[skelaton_pattern_index];
   
            // now reset instuction pointer
            skelaton_ip = 0;

            // reset counter
            skelaton_counter = 0;

            } break;
        
        default: break;

        } // end switch

    } // end if


// draw stats
sprintf(buffer,"Pattern #%d",skelaton_pattern_index);
Draw_Text_GDI(buffer,10, 400,RGB(0,255,0), lpddsback);

sprintf(buffer,"Opcode=%s Operand=%d",opcode_names[opcode],operand);
Draw_Text_GDI(buffer,10, 416,RGB(0,255,0), lpddsback);

sprintf(buffer,"Instruction Ptr=%d ", skelaton_ip);
Draw_Text_GDI(buffer,10, 432,RGB(0,255,0), lpddsback);

sprintf(buffer,"Counter=%d ", skelaton_counter);
Draw_Text_GDI(buffer,10, 448,RGB(0,255,0), lpddsback);

} // end skelaton_AI

//////////////////////////////////////////////////////////

int Game_Main(void *parms)
{
// this is the workhorse of your game it will be called
// continuously in real-time this is like main() in C
// all the calls for you game go here!

int index; // looping var

// start the timing clock
Start_Clock();

// clear the drawing surface
DDraw_Fill_Surface(lpddsback, 0);

// lock back buffer and copy background into it
DDraw_Lock_Back_Surface();

// draw background
Draw_Bitmap(&background_bmp, back_buffer, back_lpitch,0);

// unlock back surface
DDraw_Unlock_Back_Surface();

// read keyboard
DInput_Read_Keyboard();

// do the ai on skelaton
Skelaton_AI();

// animate the bat
Animate_BOB(&bat);

// the animate the skelaton unless its stopped
if (opcode!=OPC_STOP)
   Animate_BOB(&skelaton);

// move skelaton
Move_BOB(&skelaton);

// test if skelaton is off screen, if so wrap around
if (skelaton.x >= SCREEN_WIDTH)
   skelaton.x = -skelaton.width;
else
if (skelaton.x < -skelaton.width)
   skelaton.x = SCREEN_WIDTH;

if (skelaton.y >= SCREEN_HEIGHT)
   skelaton.y = -skelaton.height;
else
if (skelaton.y < -skelaton.height)
   skelaton.y = SCREEN_HEIGHT;

// let player move the bat
// allow player to move
if (keyboard_state[DIK_RIGHT])
   bat.x+=4;
else
if (keyboard_state[DIK_LEFT])
   bat.x-=4;

if (keyboard_state[DIK_UP])
   bat.y-=4;
else
if (keyboard_state[DIK_DOWN])
   bat.y+=4;


// test if bat is off screen, if so wrap around
if (bat.x >= SCREEN_WIDTH)
   bat.x = -bat.width;
else
if (bat.x < -bat.width)
   bat.x = SCREEN_WIDTH;

if (bat.y >= SCREEN_HEIGHT)
   bat.y = -bat.height;
else
if (bat.y < -bat.height)
   bat.y = SCREEN_HEIGHT;

// draw the skelaton
Draw_BOB(&skelaton, lpddsback);

// draw the bat
Draw_BOB(&bat, lpddsback);

// draw title
Draw_Text_GDI("Skelaton Pattern Demo With Tracking",10, 10,RGB(0,255,255), lpddsback);

// flip the surfaces
DDraw_Flip();

// sync to 30ish fps
Wait_Clock(30);

// check of user is trying to exit
if (KEY_DOWN(VK_ESCAPE) || keyboard_state[DIK_ESCAPE])
    {
    PostMessage(main_window_handle, WM_DESTROY,0,0);

    // stop all sounds
    DSound_Stop_All_Sounds();

    // do a screen transition
    Screen_Transitions(SCREEN_DARKNESS,NULL,0);

    } // end if

// return success
return(1);

} // end Game_Main

//////////////////////////////////////////////////////////

⌨️ 快捷键说明

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