#ifdef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include<windows.h>
#include<windowsx.h>
#include<ddraw.h>
#include<stdio.h>
#include<stdlib.h>
#include<mmsystem.h>
#include<math.h>
#include<direct.h>
#include<wingdi.h>
#include<dsound.h>
#include<winbase.h>
#include<dinput.h>
#include"resource.h"
#include"ddutil.h"
#include"dsutil.h"
#define GameLogo 1//进入游戏
#define GameInit 2//
#define GameReady 3
#define GameStart 4//游戏开始
#define GamePass 5//游戏过关
#define GameWin 6//游戏胜利
#define GameOver 7//游戏结束
#define GameSpeed 1//游戏速度
#define DINPUT_BUFFERSIZE 16
#define Aup 0
#define Aright 1
#define Adown 2
#define Aleft 3
#define cenemy 10//敌机数量
#define ctree 40//树的数量
#define cmap 10//游戏地图数
#define leftblank 100//地图离左边的象素距离
#define super 0//
#define control 1//用于无敌时的情况
#define Fplane 0//飞机发射的子弹
#define Fenemy 1//地极发射的子弹
#define TimeValue 20
#define name "fight"
#define title "fight"
class PLANE
{
public:
int planestatus;//飞机状态
int lifenum;//飞机的生命数
int x,y;//飞机坐标
int height,width;//飞机页面的尺寸
int speed;//飞机速度
int direct;//飞机方向
bool bfire;//是否可以发射子弹
bool bdead;//飞机是否死亡
};
class FIRE
{
public:
int x,y;
int height,width;
int speed;
int direct;
int Efire;
bool bdead;//子弹是否还需要显示
bool type;//是飞机发射的还是敌机发射的
};
class ENEMY
{
public:
int x,y;
int height,width;
int speed;
int direct;
bool bfire;
bool bdead;
};
class TREE
{
public:
int x,y;
int height,width;
int type;//是指定树的种类
};
class FLAME
{
public:
int x,y;
int height,width;
int ftps;//所显示的是爆炸过程的第ftps屏
bool bdead;
};
class MAP
{
public:
int x,y;
int height,width;
};
LPDIRECTDRAW lpdd=NULL;
LPDIRECTDRAWSURFACE lpddsprimary=NULL;
LPDIRECTDRAWSURFACE lpddsback=NULL;
LPDIRECTDRAWSURFACE lpddsbackground=NULL;
LPDIRECTDRAWSURFACE lpddsmap=NULL;
LPDIRECTDRAWSURFACE lpddsflame=NULL;
LPDIRECTDRAWSURFACE lpddstree[3];
LPDIRECTDRAWSURFACE lpddsplane[4];
LPDIRECTDRAWSURFACE lpddsenemy[4];
LPDIRECTDRAWSURFACE lpddsfire[4];
LPDIRECTSOUND lpds=NULL;
LPDIRECTSOUNDBUFFER lpdsbprimary=NULL;
LPDIRECTSOUNDBUFFER lpdsbshot=NULL;
LPDIRECTINPUT lpdi=NULL;
LPDIRECTINPUTDEVICE lpKeybord=NULL;
unsigned int active;//窗口是否激活
int gamestatus=GameLogo;//游戏的进度
int gamerank=0;//游戏等级
int supertime=0;//飞机无敌的时间数;
HWND hWnd=NULL;
PLANE A11;
ENEMY Aenemy[cenemy];
FLAME Aflame[cenemy];
FIRE Afire[cenemy];
TREE Atree[ctree];
MAP Amap;
DWORD thistickcount;
DWORD lasttickcount;
char apppath[50]; //应用程序路径
char openmidi[50]; //打开midi命令字符串
char closemidi[50]; //关闭midi命令字符串
char stopmidi[50];
int nsorce;
HRESULT CALLBACK WinProc(HWND hwnd,UINT msg,WPARAM wParam,
LPARAM lParam);
HWND InitWin(HINSTANCE hInstance,int nCmdShow);
void GetRect(RECT *rect,long x,long y,long height,long width);
void FreeObject(void);
void InitData(void);//初始化数据
bool InitDraw(void);//初始化directdraw接口
bool InitSound(void);
bool InitInput(HWND);
HRESULT UpdataInputData(void);
void InitPass(void);//处理过关后的数据变换
bool DrawMap(void);
bool DrawPlane(void);
bool DrawEnemy(void);
bool DrawFire(void);
bool DrawFlame(void);
bool DrawInfoText(void);
void DrawText(void);
void GetInput(void);
void EnemyAI(void);//处理控制敌机发弹、飞行、躲避的动作
bool Hit(RECT drect,RECT srect);
void Checkhit(void);
void DataPro(void);//处理飞机、子弹发弹、飞行、躲避动作后数据处理
void PlaySound(void);
void UpFlame(void);
void Flip(void);
void ClrScr(void);//清屏;
void FreeObject(void)
{
if (lpdi!=NULL)
{
if(lpKeybord)
{
lpKeybord->Unacquire();
lpKeybord->Release();
lpKeybord = NULL;
}
lpdi->Release();
lpdi = NULL;
}
if(lpds!=NULL)
{
if(lpdsbprimary!=NULL)
{
lpdsbprimary->Release();
lpdsbprimary=NULL;
}
if(lpdsbshot!=NULL)
{
lpdsbshot->Release();
lpdsbshot=NULL;
}
}
if(lpdd!=NULL)
{
if(lpddsprimary!=NULL)
{
lpddsprimary->Release();
lpddsprimary=NULL;
}
if(lpddsback!=NULL)
{
lpddsback->Release();
lpddsback=NULL;
}
if(lpddsbackground!=NULL)
{
lpddsbackground->Release();
lpddsbackground=NULL;
}
if(lpddsmap!=NULL)
{
lpddsmap->Release();
lpddsmap=NULL;
}
if(lpddsflame!=NULL)
{
lpddsflame->Release();
lpddsflame=NULL;
}
for(int i=0;i<3;i++)
if(lpddstree[i]!=NULL)
{
lpddstree[i]->Release();
lpddstree[i]=NULL;
}
for( i=0;i<4;i++)
if(lpddsplane[i]!=NULL)
{
lpddsplane[i]->Release();
lpddsplane[i]=NULL;
}
for( i=0;i<4;i++)
if(lpddsenemy[i]!=NULL)
{
lpddsenemy[i]->Release();
lpddsenemy[i]=NULL;
}
for( i=0;i<4;i++)
if(lpddsfire[i]!=NULL)
{
lpddsfire[i]->Release();
lpddsfire[i]=NULL;
}
lpdd->Release();
lpdd=NULL;
}
}
//***********************************************
//初始化窗口类和创建窗口并返回hwnd,付给hWnd全局变量。
//***********************************************
HWND InitWin(HINSTANCE hInstance,int nCmdShow)
{
HWND hwnd;
WNDCLASS wc;
HDC hdc;
wc.style=CS_HREDRAW | CS_VREDRAW ;
wc.lpfnWndProc=WinProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,"icon");
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName=0;
wc.lpszClassName=name;
if(FAILED(RegisterClass(&wc)))
{
MessageBox(NULL,"failed in register",NULL,NULL);
return 0;
}
hwnd=CreateWindow(name,title,WS_OVERLAPPEDWINDOW,0,0,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,NULL,hInstance,NULL);
if(!hwnd)
{
MessageBox(hwnd,"failed in hwnd","HWND",NULL);
return FALSE;
}
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
SetFocus(hwnd);
hdc=GetDC(hwnd);
SetTextColor(hdc, RGB(250,250,250));
SetBkMode(hdc, TRANSPARENT);
TextOut(hdc,250,200,"Loading...",10);
return hwnd;
}
//------------------------------------------------
//初始化directsound。
//-------------------------------------------------
bool InitSound(void)
{
HRESULT hr;
DSBUFFERDESC dsbdesc;
WAVEFORMATEX wfm;
if(FAILED(DirectSoundCreate(NULL,&lpds,NULL)))
{
OutputDebugString("failed in create directsound");
return FALSE;
}
if(FAILED(lpds->SetCooperativeLevel(hWnd,DSSCL_PRIORITY)))
{
OutputDebugString("failed in setcoolevel");
}
memset(&dsbdesc,0,sizeof(DSBUFFERDESC));
dsbdesc.dwSize=sizeof(DSBUFFERDESC);
dsbdesc.dwFlags=DSBCAPS_PRIMARYBUFFER;
dsbdesc.dwBufferBytes=0;
dsbdesc.lpwfxFormat=NULL;
ZeroMemory( &wfm, sizeof(WAVEFORMATEX) );
wfm.wFormatTag = WAVE_FORMAT_PCM;
wfm.nChannels = 2;
wfm.nSamplesPerSec = 22050;
wfm.wBitsPerSample = 16;
wfm.nBlockAlign = wfm.wBitsPerSample / 8 * wfm.nChannels;
wfm.nAvgBytesPerSec = wfm.nSamplesPerSec * wfm.nBlockAlign;
hr=lpds->CreateSoundBuffer(&dsbdesc,&lpdsbprimary,NULL);
if(SUCCEEDED(hr))
{
hr=lpdsbprimary->SetFormat(&wfm);
}
else
{
OutputDebugString("failed in create primary bufefer\n");
}
lpdsbshot=DSLoadSoundBuffer(lpds,"SHOT");
if(lpdsbshot==NULL)
{
OutputDebugString("failed in create shot buffer\n");
return FALSE;
}
return TRUE;
}
//*****************************************************