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

📄 fjmain.cpp

📁 经典飞机游戏的源代码 dx实现
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	if(load==FALSE)return MessageBox(GetActiveWindow(), "DDLoadBitmap FAILED","FAILED",MB_OK);
	return TRUE;

 } 

//*****************************************
// Directx Input init
BOOL InitDInput(HINSTANCE hInstance)
{ 
    HRESULT hr;
 
	hr = DirectInputCreate(hInstance, DIRECTINPUT_VERSION, 
		&g_lpDI, NULL); 
	  if ( FAILED(hr) )         return hr;
	hr = g_lpDI->CreateDevice(GUID_SysKeyboard, 
		&g_lpKey, NULL); 
	  if ( FAILED(hr) )         return hr;
	hr = g_lpKey->SetDataFormat(&c_dfDIKeyboard); 
	  if ( FAILED(hr) )         return hr;
	hr = g_lpKey->SetCooperativeLevel(hWnd, 
                   DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); 
	  if ( FAILED(hr) )         return hr;
	  
     return TRUE;
}

//*****************************************************
//释放所有的DirectDraw对象。
void FreeDDraw( void )
{
	  if (g_pDD != NULL)
    {
        if (g_pDDSPrimary != NULL)
        {
            g_pDDSPrimary->Release();
            g_pDDSPrimary = NULL;
        }
        if (Map != NULL)
        {
            Map->Release();
            Map = NULL;
        }
        g_pDD->Release();
        g_pDD = NULL;
    }
	  midi.Stop();
}

//************************************
//释放DINPUT对象

		void FreeDInput()
{
		    if (NULL != g_lpKey) 
    {
        // Unacquire the device one last time just in case 
        // the app tried to exit while the device is still acquired.
        g_lpKey->Unacquire();
        g_lpKey->Release();
        g_lpKey = NULL;
    }

    // Release any DirectInput objects.
    if (NULL != g_lpDI) 
    {
        g_lpDI->Release();
        g_lpDI = NULL;
    }

 }

		//****画底背景**********
void DrawMap(void )
{
	count++;
	if(count>480)count=1;
    movebkg(count);
}

void movebkg(int count)
{
     RECT rect;
	GetRect(&rect,0,481-count,640,count-1);
	g_pDDSBack->BltFast(0,0,m_back,&rect,DDBLTFAST_NOCOLORKEY|DDBLTFAST_WAIT);
		GetRect(&rect,0,0,640,481-count);
    g_pDDSBack->BltFast(0,count-1,m_back,&rect,DDBLTFAST_NOCOLORKEY|DDBLTFAST_WAIT);
}


HRESULT  RestoreAll(void)
{
    HRESULT                     hRet;

    hRet = g_pDDSPrimary->Restore();
    if (hRet == DD_OK)
    {
        hRet = Map->Restore();
        if (hRet == DD_OK)
        {
            DDReLoadBitmap(Map, szBitmap);
        }
    }
    return hRet;
}

int process_input()
{
 HRESULT   hr;
#define KEYDOWN(key) (KEY_INPUT[key] & 0x80) 
 static int newtime,oldtime=0;
hr = g_lpKey->GetDeviceState(sizeof(KEY_INPUT),(LPVOID)&KEY_INPUT); 

if (KEYDOWN(DIK_NUMPADMINUS)){speed--;}
    if(speed<0)speed=0;
if (KEYDOWN(DIK_NUMPADPLUS))speed++;
    
if (KEYDOWN(DIK_UP)&&!play_dead)play.y-=fspeed;

if (KEYDOWN(DIK_DOWN)&&!play_dead)play.y+=fspeed;

if (KEYDOWN(DIK_LEFT)&&!play_dead){play.x-=fspeed;move_flag=LEFT;};

if (KEYDOWN(DIK_RIGHT)&&!play_dead){play.x+=fspeed;move_flag=RIGHT;};

if(!KEYDOWN(DIK_RIGHT)&&!KEYDOWN(DIK_LEFT))move_flag=STOP;

if(KEYDOWN(DIK_LCONTROL)&&!play_dead){ 
	//static int newtime,oldtime=GetTickCount();
		newtime=GetTickCount();
		if (newtime>oldtime+slug_delay)
		{
			oldtime=newtime;
			slug[slug_sum].x=play.x+13;
			slug[slug_sum].y=play.y;
			slug_sum++;
			if (slug_sum==MAXSLUG) slug_sum=0;
			if(missile_num>0)
			{
			if(m_l[m_num].die)m_l[m_num].init(play.x+8,play.y+40);
            if(m_r[m_num].die)m_r[m_num].init(play.x+30,play.y+40);
            m_num++;
			if(m_num==MAXSLUG||m_num==missile_num)m_num=0;
			}
			
			wav_obj.PlaySound(FALSE, 1);
		}

};

return TRUE;
}

void gameinit()
{
 play.x=344;
 play.y=364;
 move_flag=STOP;
 slug_sum=0;
 m_num=0;
 slug_delay=500;
 missile_num=0;
 boss_hp=BOSS_MAX_HP;
	int pos[10][4]={
		{50,0,p_speed,p_type},
		{120,-20,p_speed,p_type},
		{200,-40,p_speed,p_type},
		{260,-60,p_speed,p_type},
		{320,-80,p_speed,p_type},
		{380,-100,p_speed,p_type},
		{440,-120,p_speed,p_type},
		{500,-140,p_speed,p_type},
		{560,-180,p_speed,p_type},
		{620,-200,p_speed,p_type},
	};

 for(int i=0;i<10;i++ )
 {  
    ep[i].init(pos[i][0],pos[i][1],pos[i][2],pos[i][3]);

 }
   boss.init();
   //dxsound init
   wav_obj.Initialize(hWnd);
   //midi init
   midi.Play(hWnd,"impact.mid");

   for(int e=0;e<3;e++)
  { 
      b_l_slug[e].y=530;
      b_m_slug[e].y=530; 
	  b_r_slug[e].y=530; 
  }

   if(!wav_obj.SetSoundTrack("gun.wav",1))wav_error=TRUE;
   if(!wav_obj.SetSoundTrack("dead.wav",2))wav_error=TRUE;
   if(!wav_obj.SetSoundTrack("start.wav",3))wav_error=TRUE;
   if(!wav_obj.SetSoundTrack("bhit.wav",4))wav_error=TRUE;
}

void game_loop()
{
 //将BACK用黑色涂满,并有COLORKEY起透明作用
	RECT rc1;

	int pos[10][4]={
		{50,0,p_speed,p_type},
		{120,-20,p_speed,p_type},
		{200,-40,p_speed,p_type},
		{260,-60,p_speed,p_type},
		{320,-80,p_speed,p_type},
		{380,-100,p_speed,p_type},
		{440,-120,p_speed,p_type},
		{500,-140,p_speed,p_type},
		{560,-180,p_speed,p_type},
		{620,-200,p_speed,p_type},
	};

	//将背景画面进行黑色填充
    DDBLTFX dblt;
	dblt.dwSize=sizeof(dblt);
	dblt.dwFillColor=0;
    back->Blt(NULL,NULL,NULL,DDBLT_COLORFILL|DDBLT_WAIT,&dblt);
    DrawMap();
    process_input();

//play 位置限定
if(play.x<50)play.x=50;
if(play.x>640)play.x=640;
if(play.y<50)play.y=50;
if(play.y>480)play.y=480;
//**************************

                        if(!final) 
						{
//敌机的出现及各项判定
for(int i=0;i<MAXENEMY;i++ )
 { 
	 if(ep[i].die==TRUE&&!enemy_over)
	 {
	 	 ep[i].init(pos[i][0],pos[i][1],pos[i][2],pos[i][3]);
	 }
	 if(ep[0].die&&ep[1].die&&ep[2].die&&ep[3].die&&ep[4].die&&ep[5].die&&ep[6].die&&
		 ep[7].die&&ep[8].die&&ep[9].die)
	 {
	 final=true;
	 }
	 if(d_time(500))p_type++;
	 if(p_type>3)p_type=0;
	 switch(shot_down)
	 {
	 case 50:
		 p_speed=3;
		 break;
	 case 100:
         p_speed=4;
		 break;
	 case 150:
		 p_speed=5;
		 break;
	 case 200:
		 p_speed=6;
		 break;
	 }

 ep[i].move();
 ep[i].draw_plane();
 d_fire[i].draw_fire();


 //敌机发射子弹

 if(ep[i].y>70&&!e_slug[i].flag&&!ep[i].die)
 { 
	 if(rand()%2==1)continue;
   switch(ep[i].type)
   { 
   case 0://A10
	   e_slug[i].init(ep[i].x+25,ep[i].y+60,play.x,play.y);
	   break;
   case 1://small plane
	   e_slug[i].init(ep[i].x+17,ep[i].y+35,play.x,play.y);
	   break;
   case 2://big
	   e_slug[i].init(ep[i].x+25,ep[i].y+70,play.x,play.y);
	   break;
   case 3://big
	   e_slug[i].init(ep[i].x+25,ep[i].y+70,play.x,play.y);
	   break;
   }
 }
 e_slug[i].draw();
}

//敌机的出现及各项判定结束

//碰撞检测1: 自机的子弹和敌机碰撞检测

 for(int e=0;e<MAXENEMY;e++)
 {
	 if(ep[e].die==TRUE)continue; 
	 
      for(int i=0;i<MAXSLUG;i++)
	 {
		 switch(ep[e].type)
		 {
		 case 0://A10
       if(slug[i].x>ep[e].x&&abs(slug[i].x-ep[e].x)<30&&abs(slug[i].y-ep[e].y)<50)
		  
	   { 
		   ep[e].die=TRUE;
		   d_fire[e].init(ep[e].x,ep[e].y);
		   slug[i].y=-1000;
		   shot_down++;if(shot_down==pm){power=true;missile=true;pm+=pm2;}
		   wav_obj.PlaySound(FALSE, 2);
       }
        if(!m_l[i].die&&abs(m_l[i].x-ep[e].x-20)<30&&abs(m_l[i].y-ep[e].y-30)<25)
		{
		   ep[e].die=TRUE;
		   m_l[i].die=true;
		   d_fire[e].init(ep[e].x,ep[e].y);
		   shot_down++;if(shot_down==pm){power=true;missile=true;pm+=pm2;}
		   wav_obj.PlaySound(FALSE, 2);
		}
		if(!m_r[i].die&&abs(m_r[i].x-ep[e].x-20)<30&&abs(m_r[i].y-ep[e].y-30)<25)
		{
		   ep[e].die=TRUE;
		   m_r[i].die=true;
		   d_fire[e].init(ep[e].x,ep[e].y);
		   shot_down++;if(shot_down==pm){power=true;missile=true;pm+=pm2;}
		   wav_obj.PlaySound(FALSE, 2);
		}
	   break;
	 case 1://small plane
       if(slug[i].x>ep[e].x&&abs(slug[i].x-ep[e].x)<30&&abs(slug[i].y-ep[e].y)<20)
	   { 
		   ep[e].die=TRUE;
		   d_fire[e].init(ep[e].x-5,ep[e].y-5);
		   slug[i].y=-1000;
		   shot_down++;if(shot_down==pm){power=true;missile=true;pm+=pm2;}
		   wav_obj.PlaySound(FALSE, 2);
       }
	   if(!m_l[i].die&&abs(m_l[i].x-ep[e].x-15)<15&&abs(m_l[i].y-ep[e].y-15)<15)
		{
		   ep[e].die=TRUE;
		   m_l[i].die=true;
		   d_fire[e].init(ep[e].x,ep[e].y);
		   shot_down++;if(shot_down==pm){power=true;missile=true;pm+=pm2;}
		   wav_obj.PlaySound(FALSE, 2);
		}
		if(!m_r[i].die&&abs(m_r[i].x-ep[e].x-15)<15&&abs(m_r[i].y-ep[e].y-15)<15)
		{
		   ep[e].die=TRUE;
		   m_r[i].die=true;
		   d_fire[e].init(ep[e].x,ep[e].y);
		   shot_down++;if(shot_down==pm){power=true;missile=true;pm+=pm2;}
		   wav_obj.PlaySound(FALSE, 2);
		}
	   break;
	   	 case 2://red big
       if(slug[i].x>ep[e].x&&abs(slug[i].x-ep[e].x)<30&&abs(slug[i].y-ep[e].y)<40)
	   { 
		   ep[e].die=TRUE;
		   d_fire[e].init(ep[e].x+5,ep[e].y+5);
		   slug[i].y=-1000;
		   shot_down++;if(shot_down==pm){power=true;missile=true;pm+=pm2;}
		   wav_obj.PlaySound(FALSE, 2);
       }
	   if(!m_l[i].die&&abs(m_l[i].x-ep[e].x-28)<30&&abs(m_l[i].y-ep[e].y-35)<30)
		{
		   ep[e].die=TRUE;
		   m_l[i].die=true;
		   d_fire[e].init(ep[e].x,ep[e].y);
		   shot_down++;if(shot_down==pm){power=true;missile=true;pm+=pm2;}
		   wav_obj.PlaySound(FALSE, 2);
		}
		if(!m_r[i].die&&abs(m_r[i].x-ep[e].x-28)<30&&abs(m_r[i].y-ep[e].y-35)<30)
		{
		   ep[e].die=TRUE;
		   m_r[i].die=true;
		   d_fire[e].init(ep[e].x,ep[e].y);
		   shot_down++;if(shot_down==pm){power=true;missile=true;pm+=pm2;}
		   wav_obj.PlaySound(FALSE, 2);
		}
	   break;
	   	 case 3://blue big
       if(slug[i].x>ep[e].x&&abs(slug[i].x-ep[e].x)<30&&abs(slug[i].y-ep[e].y)<40)
	   { 
		   ep[e].die=TRUE;
		   d_fire[e].init(ep[e].x+5,ep[e].y+5);

⌨️ 快捷键说明

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