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

📄 directxframe.cpp

📁 VC+DirectX写的飞机小游戏
💻 CPP
字号:
// DirectXFrame.cpp : implementation file
//

#include "stdafx.h"
#include "DirectXr.h"
#include "DirectXFrame.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// DirectXFrame

IMPLEMENT_DYNCREATE(DirectXFrame, CFrameWnd)

DirectXFrame::DirectXFrame()
{    
	  srand((unsigned)time(NULL)); //产生随机种子
 	  work=0;

	  for(int i=0;i<=7;i++)
	  {
		  shitleft[i]=(rand()%30)*29; 
		  shitspeed[i]=rand()%20+5; 
		  shittop[i]=-60; 
	  }

	  MessageBox("在窗体中单击鼠标左键开始游戏!");

	RECT rect;
	Create(NULL,"陨石撞飞机DirectX版  作者:傅城州");
	CClientDC dc(this);
	int width = dc.GetDeviceCaps(HORZRES);
	int height = dc.GetDeviceCaps(VERTRES);
	GetWindowRect( &rect );
	width = ( width - ( rect.right - rect.left ))/2 ;
	height = (height - (rect.bottom - rect.top ))/2 ;
	MoveWindow( width , height , (rect.right - rect.left ) , (rect.bottom - rect.top ) ,true);

}

DirectXFrame::~DirectXFrame()
{
}


BEGIN_MESSAGE_MAP(DirectXFrame, CFrameWnd)
	//{{AFX_MSG_MAP(DirectXFrame)
	ON_WM_PAINT()
	ON_WM_KEYDOWN()
	ON_WM_TIMER()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// DirectXFrame message handlers
d3dTexture a_Bk;
d3dTexture a_Role;
d3dTexture a_Role1;



LRESULT DirectXFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	switch( message )
	{
	case WM_CREATE :
		if( !d3dCreate( m_hWnd , 800 , 600 , true ))
			return PostMessage( WM_CLOSE );
		a_Bk.Create( "背景.bmp" );
		a_Role.Create( "FlightWar.bmp" );
		a_Role1.Create( "FlightShit.bmp" );


		return 0 ;
	case WM_DESTROY :
		d3dRelease();
		return 0 ;
	}
	return CFrameWnd::WindowProc(message, wParam, lParam);
}

void DirectXFrame::OnPaint() 
{
	// TODO: Add your message handler code here
	Render();
	// Do not call CFrameWnd::OnPaint() for painting messages
	CPaintDC dc(this); // device context for painting
}

void DirectXFrame::Render()
{
	d3dClear();
	d3d_Device->BeginScene();

		d3d_Device->SetRenderState( D3DRS_CULLMODE , D3DCULL_NONE );
		d3d_Device->SetRenderState( D3DRS_ZENABLE , D3DZB_FALSE );
		d3d_Device->SetRenderState( D3DRS_SHADEMODE , D3DSHADE_FLAT );
		d3d_Device->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
		a_Bk.BltFast( 0 , 0 );

		d3d_Device->SetRenderState( D3DRS_SRCBLEND , D3DBLEND_SRCALPHA );
		d3d_Device->SetRenderState( D3DRS_DESTBLEND , D3DBLEND_INVSRCALPHA );
		d3d_Device->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
		a_Role.BltFast( flileft , flitop );

        for(int i=1;i<=6;i++)
		{
			d3d_Device->SetRenderState( D3DRS_SRCBLEND , D3DBLEND_SRCALPHA );
			d3d_Device->SetRenderState( D3DRS_DESTBLEND , D3DBLEND_INVSRCALPHA );
			d3d_Device->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
			a_Role1.BltFast( shitleft[i]  , shittop[i] );
		}

      

	d3d_Device->EndScene();
	d3d_Device->Present( NULL , NULL , NULL , NULL );

}

void DirectXFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
    int	  flispeed=30;
   switch(nChar)                                 //键盘响应
   {
   case VK_LEFT:
	   if(flileft>0) {flileft-=flispeed;}
       break;
   
   case VK_RIGHT:
	   if(flileft<740){flileft+=flispeed;}
	   break;

   case VK_DOWN:
	   if(flitop<490){flitop+=flispeed;}
	   break;

   case VK_UP:
	   if(flitop>0){flitop-=flispeed;}
	   break;
   }
     Render();
	CFrameWnd::OnKeyDown(nChar, nRepCnt, nFlags);
}

void DirectXFrame::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	 work=0;
    int a=4;   //设定加速度
	 
	if(nIDEvent==1)
	 {   
		for(int k=1;k<=6;k++)
		 {	  
		  		     
			 if(shittop[k]<600){shittop[k]+=shitspeed[k];}
				 else  
				 {
						shittop[k]=-20;
			    		shitleft[k]=(rand()%35)*22; 
			    		shitspeed[k]=rand()%10;  //设定初速度
				 }
			 shitspeed[k]+=(rand()%3)*a;
		}
	 }
	 
	else if(nIDEvent==2)
	  for(int i=1;i<=6;i++)
		 if(shittop[i]>=flitop && shittop[i]<=flitop+103 && shitleft[i]>=flileft-17 && shitleft[i]<=flileft+66 )
			 {                                       //判断是否击中陨石     
			  for(int k=1;k<=6;k++)
			  shittop[k]=-60; 
			  KillTimer(1);	
			  work=1;		  			  
			  Invalidate();   
			  break;
			}
	if(work==1){	 
         flileft=380;
         flitop=490;
		 MessageBox("击中要害,鼠标左击窗体继续!");
	}

     Render();
	CFrameWnd::OnTimer(nIDEvent);
}

void DirectXFrame::OnLButtonDown(UINT nFlags, CPoint point) 
{
	    SetTimer(1,180,NULL);
    	SetTimer(2,170,NULL);            //设置两个定时器
 	CFrameWnd::OnLButtonDown(nFlags, point);
}

⌨️ 快捷键说明

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