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

📄 demo13_4_16b.cpp

📁 《Windows游戏编程大师技巧(第二版)》源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
cannon_ids[0] = DSound_Load_WAV("CANNON.WAV");

// clone sounds
for (index=1; index < 8; index++)
    cannon_ids[index] = DSound_Replicate_Sound(cannon_ids[0]);

// define points of cannon
VERTEX2DF cannon_vertices[4] =  { 0,-2, 30,0, 30,2, 0,2, };

// initialize ship
cannon.state       = 1;   // turn it on
cannon.num_verts   = 4;  
cannon.x0          = CANNON_X0; // position it
cannon.y0          = CANNON_Y0;
cannon.xv          = 0;
cannon.yv          = 0;
cannon.color       = RGB16Bit(0,255,0); // green
cannon.vlist       = new VERTEX2DF [cannon.num_verts];
 
for (index = 0; index < cannon.num_verts; index++)
    cannon.vlist[index] = cannon_vertices[index];

// build the 360 degree look ups
Build_Sin_Cos_Tables();

// initialize the missiles
Init_Projectiles();

// set clipping region
min_clip_x = 0;
max_clip_x = screen_width - 1;
min_clip_y = 0;
max_clip_y = screen_height - 1;

// 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

// 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 Cannon_Sound(void)
{        
// this functions hunts for an open handle to play a cannon sound

// start a hit sound
for (int sound_index=0; sound_index < 8; sound_index++)
    {
    // test if this sound is playing
    if (DSound_Status_Sound(cannon_ids[sound_index])==0)
       {
       DSound_Play(cannon_ids[sound_index]);
       break;
       } // end if

     } // end for 

} // end Cannon_Sound

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

void Init_Projectiles(void)
{
// this function initializes the projectiles
memset(missiles, 0, sizeof(PROJECTILE)*NUM_PROJECTILES);

} // end Init_Projectiles

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

void Move_Projectiles(void)
{
// this function moves all the projectiles and does the physics model
for (int index=0; index<NUM_PROJECTILES; index++)
    {
    if (missiles[index].state==1)
       {
        // translate
        missiles[index].x+=missiles[index].xv;
        missiles[index].y+=missiles[index].yv;

        // apply forces
        missiles[index].xv+=wind_force;
        missiles[index].yv+=gravity_force;

        // test for off screen
        if (missiles[index].x >= screen_width || 
            missiles[index].y >=screen_height || 
            missiles[index].y < 0) 
           {
           // kill the missile
           missiles[index].state = 0;
           } // end if

        } // end if on
 
    } // end for index

} // end Move_Projectiles

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

void Draw_Projectiles(void)
{
// this function draws all the projectiles 
for (int index=0; index < NUM_PROJECTILES; index++)
    {
    // is this one alive?
    if (missiles[index].state==1)
       {
       Draw_Rectangle(missiles[index].x-1,missiles[index].y-1, 
                      missiles[index].x+1,missiles[index].y+1,
                      RGB16Bit(0,255,0), lpddsback);
       } // end if

    } // end for index

} // end Draw_Projectiles

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

void Fire_Projectile(int angle, float vel)
{
// this function starts a projectile with the given angle and velocity 
// at the tip of the cannon
for (int index=0; index < NUM_PROJECTILES; index++)
    {
    // find an open projectile
    if (missiles[index].state==0)
       {
       // set this missile in motion at the head of cannon with the proper angle
       missiles[index].x = cannon.vlist[1].x+cannon.x0;
       missiles[index].y = cannon.vlist[1].y+cannon.y0;

       // compute velocity vector based on angle
       missiles[index].xv = vel*cos_look[angle];
       missiles[index].yv = -vel*sin_look[angle];

       // mark as active
       missiles[index].state = 1;

       // make sound
       Cannon_Sound();

       // bail
       break;

       } // end if

    } // end for index

} // end Fire_Projectile

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

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

static int   curr_angle = 0; // current angle of elevation from horizon
static float curr_vel   = 10; // current velocity of projectile

// 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);

// do the graphics
Draw_Polygon2D16(&cannon, back_buffer, back_lpitch);

// unlock back surface
DDraw_Unlock_Back_Surface();

// read keyboard
DInput_Read_Keyboard();

// test for rotate
if ((curr_angle < 90) && keyboard_state[DIK_UP]) // rotate left
   {
   Rotate_Polygon2D_Mat(&cannon, -5);
   curr_angle+=5;
   } // end if
else
if ((curr_angle > 0) &&keyboard_state[DIK_DOWN]) // rotate right
   {
   Rotate_Polygon2D_Mat(&cannon, 5);
   curr_angle-=5;
   } // end if

// test for projectile velocity
if (keyboard_state[DIK_RIGHT])  
   { 
   if (curr_vel < 30) curr_vel+=0.1;
   } // end if
else
if (keyboard_state[DIK_LEFT]) 
   { 
   if (curr_vel > 0) curr_vel-=0.1;
   } // end if

// test for wind force
if (keyboard_state[DIK_W])  
   { 
   if (wind_force < 2) wind_force+=0.01;
   } // end if
else
if (keyboard_state[DIK_E]) 
   { 
   if (wind_force > -2) wind_force-=0.01;
   } // end if

// test for gravity force
if (keyboard_state[DIK_G])  
   { 
   if (gravity_force < 15) gravity_force+=0.1;
   } // end if
else
if (keyboard_state[DIK_B]) 
   { 
   if (gravity_force > -15) gravity_force-=0.1;
   } // end if

// test for fire!
if (keyboard_state[DIK_LCONTROL]) 
   { 
   Fire_Projectile(curr_angle, curr_vel);

   } // end fire

// move all the projectiles
Move_Projectiles();

// draw the projectiles
Draw_Projectiles();

// draw the title
Draw_Text_GDI("(16-Bit Version) Trajectory DEMO, Press <ESC> to Exit.",10, 10,RGB(255,255,255), lpddsback);
Draw_Text_GDI("<RIGHT>, <LEFT> to adjust velocity, <UP>, <DOWN> to adjust angle",10, 25, RGB(255,255,255), lpddsback);
Draw_Text_GDI("<G>, <B> to adjust gravity, <W>, <E> to adjust wind, <CTRL> to fire.",10, 40, RGB(255,255,255), lpddsback);

sprintf(buffer, "Ang=%d, Vel=%f", curr_angle, curr_vel);
Draw_Text_GDI(buffer,10, 60, RGB(255,255,255), lpddsback);

sprintf(buffer, "Wind force=%f, Gravity Force=%f", wind_force, gravity_force);
Draw_Text_GDI(buffer,10, 75, RGB(255,255,255), lpddsback);

// flip the surfaces
DDraw_Flip();

// sync to 30 fps = 1/30sec = 33 ms
Wait_Clock(33);

// 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 + -