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

📄 demo12_5_16b.cpp

📁 《Windows游戏编程大师技巧(第二版)》源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
Load_Bitmap_File(&bitmap16bit, "BOTS24.BMP");

// create bat bob
Create_BOB(&bot,320,200,116,60,16,BOB_ATTR_MULTI_ANIM | BOB_ATTR_VISIBLE, DDSCAPS_SYSTEMMEMORY,0,16);
Set_Anim_Speed_BOB(&bot, 2);

// load the bot in 
for (index=0; index < 16; index++)
    Load_Frame_BOB16(&bot, &bitmap16bit, index, index%2, index/2, BITMAP_EXTRACT_MODE_CELL);

// unload bot
Unload_Bitmap_File(&bitmap16bit);

// create animations for bot
for (index=0; index<8; index++)
    {
    // generate the animation on the fly
    bot_anim[0] = index*2;
    bot_anim[1] = bot_anim[0]+1;

    // load the generated animation
    Load_Animation_BOB(&bot, index, 2, bot_anim);
    } // end for index

// set the animation to right direction
Set_Animation_BOB(&bot,0);

// initialize directinput
DInput_Init();

// acquire the keyboard only
DInput_Init_Keyboard();

// initilize DirectSound
DSound_Init();

// load background sounds
engines_id = DSound_Load_WAV("ENGINES.WAV");

// start the sounds
DSound_Play(engines_id, DSBPLAY_LOOPING);

// hide the mouse
if (!WINDOWED_APP)
   ShowCursor(FALSE);

// seed random number generate
srand(Start_Clock());

// return success
return(1);

} // end Game_Init

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

int Game_Shutdown(void *parms)
{
// this function is where you shutdown your game and
// release all resources that you allocated

// shut everything down

// kill all the bobs
Destroy_BOB(&bot);

// shutdown directdraw last
DDraw_Shutdown();

// now directsound
DSound_Stop_All_Sounds();
DSound_Shutdown();

// shut down directinput
DInput_Shutdown();

// return success
return(1);
} // end Game_Shutdown

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

void Bot_AI(void)
{
// this function controls the ai of the bot and the pattern
// processing

static int opcode,  // current pattern opcode
           operand; // current operand 

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

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

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

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

            } break;

        case OPC_NE:
            {
            // set direction to northeast
            Set_Vel_BOB(&bot,6,-6);
  
            // set animation to northeast
            Set_Animation_BOB(&bot,opcode);
            
            // set counter to instuction operand
            bot_counter = operand;

            } break;

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

            } break;            

        case OPC_NW:
            {
            // set direction to northwest
            Set_Vel_BOB(&bot,-6,-6);
  
            // set animation to northwest
            Set_Animation_BOB(&bot,opcode);
            
            // set counter to instuction operand
            bot_counter = operand;

            } break;            

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

        case OPC_SW:
            {
            // set direction to southwest
            Set_Vel_BOB(&bot,-6,6);
  
            // set animation to southwest
            Set_Animation_BOB(&bot,opcode);
            
            // set counter to instuction operand
            bot_counter = operand;

            } break;            

        case OPC_S: 
            {
            // set direction to south
            Set_Vel_BOB(&bot,0,6);
  
            // set animation to south
            Set_Animation_BOB(&bot,opcode);
            
            // set counter to instuction operand
            bot_counter = operand;

            } break;            

        case OPC_SE:
            {
            // set direction to southeast
            Set_Vel_BOB(&bot,6,6);
  
            // set animation to southeast
            Set_Animation_BOB(&bot,opcode);
            
            // set counter to instuction operand
            bot_counter = operand;

            } break;            

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

            } break;

        case OPC_RAND:
            {
            // set direction randomly
            Set_Vel_BOB(&bot,-4+rand()%9,-4+rand()%9);
  
            // set animation to south
            Set_Animation_BOB(&bot,OPC_S);
            
            // set counter to instuction operand
            bot_counter = operand;

            } break;

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

            // reset counter
            bot_counter = 0;

            } break;
        
        default: break;

        } // end switch

    } // end if


// draw stats
sprintf(buffer,"Pattern #%d",bot_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 ", bot_ip);
Draw_Text_GDI(buffer,10, 432,RGB(0,255,0), lpddsback);

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

} // end Bot_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_Bitmap16(&background_bmp, back_buffer, back_lpitch,0);

// unlock back surface
DDraw_Unlock_Back_Surface();

// read keyboard
DInput_Read_Keyboard();

// do the ai on bot
Bot_AI();

// the animate the bot
Animate_BOB(&bot);

// move bot
Move_BOB(&bot);

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

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

// draw the bot
Draw_BOB16(&bot, lpddsback);

// draw title
Draw_Text_GDI("(16-Bit Version) I-ROBOT 3D - Pattern Demo",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();

    } // end if

// return success
return(1);

} // end Game_Main

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

⌨️ 快捷键说明

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