📄 gameengine.cpp
字号:
if (SUCCEEDED(DirectSoundCreate(NULL, &lpDS, NULL)))
{
if (SUCCEEDED(lpDS->SetCooperativeLevel(hWndMain,
DSSCL_NORMAL)))
{
hsoStop = SndObjCreate(lpDS, "Stop", 1);
hsoShoot = SndObjCreate(lpDS, "Shoot",1);
hsoBoom = SndObjCreate(lpDS, "Boom", 8);
hsoUfoShoot = SndObjCreate(lpDS, "UfoShoot", 8);
hsoEnter = SndObjCreate(lpDS, "Enter", 1);
hsoSelect = SndObjCreate(lpDS, "Select", 1);
hsoGetExtra = SndObjCreate(lpDS, "GetExtra", 1);
hsoGameOver = SndObjCreate(lpDS, "GameOver", 1);
bSoundEnabled = TRUE;
}
else
{
lpDS->Release();
lpDS = NULL;
}
}
}
void DestroySound( void )
{
bSoundEnabled = FALSE;
if (lpDS != NULL)
{
SndObjDestroy(hsoEnter);
hsoEnter = NULL;
SndObjDestroy(hsoStop);
hsoStop = NULL;
SndObjDestroy(hsoShoot);
hsoShoot = NULL;
SndObjDestroy(hsoUfoShoot);
hsoUfoShoot = NULL;
SndObjDestroy(hsoBoom);
hsoBoom = NULL;
SndObjDestroy(hsoSelect);
hsoSelect = NULL;
SndObjDestroy(hsoGetExtra);
hsoGetExtra = NULL;
SndObjDestroy(hsoGameOver);
hsoGameOver = NULL;
lpDS->Release();
lpDS = NULL;
}
}
//-----------------------------------------------------------------
//
// Name: KeyDown
// Instruction: member fuctions on level
//
//-----------------------------------------------------------------
void KeyDown(HWND hWnd,WPARAM wParam)
{
switch (wParam)
{
case VK_RETURN:
if (iAppState == APP_MAINMENU)
{
// if the user hit <enter> at the menu
// change the app setting depending on the user selection
switch(iOption)
{
case 0:
lastTickCount = 0;
sndPlaySound("res\\Blub.wav",SND_ASYNC);
//SndObjPlay(hsoEnter, FALSE);
// Set the app state to start the game
iAppState = APP_LEVELSCREEN;
lastTickCount = 0;
// Reset the score counter, level, ship state and ammo
score = 0;
iLevel = 1;
iTankNum=0;
iShield = 50;
iMaxWeapon = 0;
Lastscore = 99;
// Initialize Level
InitLevel(TRUE);
break;
case 1:
// User selected the second option, credits
iAppState = APP_CREDITS;
break;
case 2:
// User selected the help screen
iAppState = APP_HELPSCREEN;
lastTickCount = 0;
break;
case 3:
// User want磗 to quit
// Cleanup up everything
sndPlaySound("res\\GameOver.wav",SND_ASYNC);
DestroyGame();
PostQuitMessage(0);
break;
}
}
bSelect=FALSE;
break;
case VK_CONTROL:
// Control key is used to change the weapon
// If not in game screen, get out
if (iAppState != APP_GAMESCREEN)
break;
// Increment weapon type
iWeapon++;
// If we reached the Maximum weapon avaible for the ship
// reset to the first weapon
if (iWeapon > iMaxWeapon)
iWeapon = 0;
break;
case VK_LEFT:
// If not in game screen, get out
if( iAppState==APP_GAMESCREEN)
{
bKeyState=TRUE;
Tank[0][0].SetMoveDirection(TANK_MOVE_LEFT);
Tank[0][0].SetTankState(TANK_STATE_MOVE);
// if(SearchMovePath(TANK_MOVE_UP,Tank[0][0]))
// Tank[0][0].Move();
break;
}
if (iAppState != APP_MAINMENU)
break;
break;
case VK_UP:
// Up key is just used in the main menu screen
// If not in main menu, get out
if(iAppState==APP_GAMESCREEN)
{
bKeyState=TRUE;
Tank[0][0].SetMoveDirection(TANK_MOVE_UP);
Tank[0][0].SetTankState(TANK_STATE_MOVE);
break;
}
if (iAppState != APP_MAINMENU)
break;
if(bSelect)
{
// User hit up key, raise up 1 menu option
iOption--;
// Play the select option sound
sndPlaySound("res\\Tap.wav",SND_ASYNC);
//SndObjStop(hsoSelect);
//SndObjPlay(hsoSelect,NULL);
// If we reach the first option, go the last one
if (iOption < 0)
iOption = 3;
}
break;
case VK_RIGHT:
// Right key is just used during the game
// If we aren磘 in the game screen, get out
if(iAppState==APP_GAMESCREEN)
{
bKeyState=TRUE;
Tank[0][0].SetMoveDirection(TANK_MOVE_RIGHT);
Tank[0][0].SetTankState(TANK_STATE_MOVE);
break;
}
if (iAppState != APP_GAMESCREEN)
break;
// Set the movement rate to +4 pixels
break;
case VK_DOWN:
if(iAppState==APP_GAMESCREEN)
{
bKeyState=TRUE;
Tank[0][0].SetMoveDirection(TANK_MOVE_DOWN);
Tank[0][0].SetTankState(TANK_STATE_MOVE);
break;
}
if (iAppState != APP_MAINMENU)
break;
if(bSelect)
{
// User hit up key, raise up 1 menu option
iOption++;
// Play the select option sound
sndPlaySound("res\\Tap.wav",SND_ASYNC);
//SndObjStop(hsoSelect);
//SndObjPlay(hsoSelect,NULL);
// If we reach the first option, go the last one
if (iOption > 3)
iOption = 0;
}
break;
case VK_SPACE:
// SPACE BAR is used to shoot during the game screen
// Not at the game screen
if (iAppState != APP_GAMESCREEN)
break;
// Check if we can shoot or we are waiting for our weapon
// recharge
if(Tank[0][0].IsAttackable()&&Tank[0][0].IsLivable())
{
InstallBullet(0,0);
Tank[0][0].Attack();
Tank[0][0].SetTankState(TANK_STATE_STOP);
sndPlaySound("res\\TANKSHOOT.wav",SND_ASYNC);
}
else break;
//SndObjStop(hsoShoot);
//SndObjPlay(hsoShoot,0);
if (iWeapon == 0)
SetTimer(hWnd,1,100,NULL);
else
SetTimer(hWnd,1,50,NULL);
break;
case VK_F12:
case VK_ESCAPE:
// ESCAPE KEY AND F12 IS THE SAME AS QUIT
// Quit if main menu, gameover if gamescreen
if (iAppState == APP_MAINMENU)
{
DestroyGame();
PostQuitMessage(0);
break;
}
else
{
lastTickCount = 0;
iAppState = 0;
}
break;
}
}
void KeyUp(HWND hWnd,WPARAM wParam)
{
switch(wParam)
{
case VK_UP:
bKeyState=FALSE;
Tank[0][0].SetTankState(TANK_STATE_STOP);
break;
case VK_DOWN:
bKeyState=FALSE;
Tank[0][0].SetTankState(TANK_STATE_STOP);
break;
case VK_LEFT:
bKeyState=FALSE;
Tank[0][0].SetTankState(TANK_STATE_STOP);
break;
case VK_RIGHT:
bKeyState=FALSE;
Tank[0][0].SetTankState(TANK_STATE_STOP);
break;
}
}
//-----------------------------------------------------------------
//
// Name: MainWndproc
// Instruction: member fuctions on level
//
//-----------------------------------------------------------------
long FAR PASCAL MainWndproc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
switch( message )
{
case WM_TIMER:
switch(wParam)
{
case 1:
bShootEnable = TRUE;
KillTimer(hWnd,1);
break;
}
break;
case WM_ACTIVATEAPP:
if(iAppState==APP_MAINMENU)
{
iAppState = APP_MAINMENU;
bIsActive = (BOOL) wParam;
}
else
{
iAppState=iAppState;
bIsActive = (BOOL) wParam;
}
break;
case WM_KEYDOWN:
if (iAppState == APP_HELPSCREEN)
{
lastTickCount = 0;
iAppState = APP_MAINMENU;
return 0L;
}
KeyDown(hWnd,wParam );
break;
case WM_KEYUP:
KeyUp(hWnd,wParam );
break;
case WM_CREATE:
//SetTimer(hWnd,1,50,NULL);
break;
case WM_DESTROY:
DestroyGame();
PostQuitMessage(0);
break;
case WM_PAINT:
break;
default:
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
} /* MainWndproc */
//-----------------------------------------------------------------
//
// Name: Draw
// Instruction: member fuctions on draw
//
//-----------------------------------------------------------------
void DrawBackGround(LPDIRECTDRAWSURFACE lpDest, int x, int y, int wx, int wy)
{
// Blit the background to the destination surface
RECT rcRect;
HRESULT hRet;
rcRect.left = x;
rcRect.top = y;
rcRect.right = wx;
rcRect.bottom = wy;
while (1)
{
hRet = lpDest->BltFast(x, y, lpBkGround, &rcRect, FALSE);
if (hRet == DD_OK)
{
break;
}
if (hRet == DDERR_SURFACELOST)
{
hRet = RestoreSurfaces();
if (hRet != DD_OK)
break;
}
if (hRet != DDERR_WASSTILLDRAWING)
break;
}
}
//-----------------------------------------------------------------------------
//
// Name: DrawInvalidBackGround
// Instruction: the member function of the class of the Bullet
//
//-----------------------------------------------------------------------------
void DrawInvalidBackGround()
{
RECT rcRect;
HRESULT hRet;
static Sx = 0;
static Sy = 0;
const int iFimx = 1024;
const int iFimy = 768;
if (lastTickCount = 0)
{
Sx =0;
Sy =0;
}
Sx =(GAME_SCREEN_WIDTH+screenmovex)%(iFimx+2);
Sy =(GAME_SCREEN_HEIGHT+screenmovey)%(iFimy+2);
if (Sx>iFimx)
Sx =0;
if (Sy>iFimy)
Sy =0;
rcRect.left = 0;
rcRect.top = 0;
rcRect.right = iFimx-Sx;
rcRect.bottom = iFimy-Sy;
while (1)
{
hRet = lpBackBuffer->BltFast(Sx, Sy, lpBkGround, &rcRect, DDBLTFAST_NOCOLORKEY);
if (hRet == DD_OK)
{
break;
}
if (hRet == DDERR_SURFACELOST)
{
hRet = RestoreSurfaces();
if (hRet != DD_OK)
break;
}
if (hRet != DDERR_WASSTILLDRAWING)
break;
}
rcRect.left = 0;
rcRect.top = iFimy-Sy;
rcRect.right = iFimx-Sx;
rcRect.bottom = iFimy;
while (1)
{
hRet = lpBackBuffer->BltFast(Sx, 0, lpBkGround, &rcRect, DDBLTFAST_NOCOLORKEY);
if (hRet == DD_OK)
{
break;
}
if (hRet == DDERR_SURFACELOST)
{
hRet = RestoreSurfaces();
if (hRet != DD_OK)
break;
}
if (hRet != DDERR_WASSTILLDRAWING)
break;
}
rcRect.left = iFimx-Sx;;
rcRect.top = 0;
rcRect.right = iFimx;
rcRect.bottom = iFimy-Sy;
while (1)
{
hRet = lpBackBuffer->BltFast(0, Sy, lpBkGround, &rcRect, DDBLTFAST_NOCOLORKEY);
if (hRet == DD_OK)
{
break;
}
if (hRet == DDERR_SURFACELOST)
{
hRet = RestoreSurfaces();
if (hRet != DD_OK)
break;
}
if (hRet != DDERR_WASSTILLDRAWING)
break;
}
rcRect.left = iFimx-Sx;
rcRect.top = iFimy-Sy;
rcRect.right = iFimx;
rcRect.bottom = iFimy;
while (1)
{
hRet = lpBackBuffer->BltFast(0, 0, lpBkGround, &rcRect, DDBLTFAST_NOCOLORKEY);
if (hRet == DD_OK)
{
break;
}
if (hRet == DDERR_SURFACELOST)
{
hRet = RestoreSurfaces();
if (hRet != DD_OK)
break;
}
if (hRet != DDERR_WASSTILLDRAWING)
break;
} }
//-----------------------------------------------------------------------------
//
// Name: Bullet
// Instruction: the member function of the class of the Bullet
//
//-----------------------------------------------------------------------------
void InstallBullet(int m,int n)
{
// if(!Bullet[m][n].IsFlying())
{
Bullet[m][n].SetLifeState(TRUE);
Bullet[m][n].SetBullet( Tank[m][n].GetTankType(),
Tank[m][n].GetTankBelong(),
Tank[m][n].GetMDirection());
iTankMovX=Tank[m][n].GetNowPos().x;
iTankMovY=Tank[m][n].GetNowPos().y;
MovDirection=Tank[m][n].GetMDirection();
switch(MovDirection)
{
case 0:
iBulletMovX=iTankMovX+TANK_WIDTH/2-9;
iBulletMovY=iTankMovY;
Bullet[m][n].SetNowPos(iBulletMovX,iBulletMovY);
break;
case 1:
iBulletMovX=iTankMovX+TANK_WIDTH/2-9;
iBulletMovY=iTankMovY+TANK_HEIGHT;
Bullet[m][n].SetNowPos(iBulletMovX,iBulletMovY);
break;
case 2:
iBulletMovX=iTankMovX;
iBulletMovY=iTankMovY+TANK_WIDTH/2-9;
Bullet[m][n].SetNowPos(iBulletMovX,iBulletMovY);
break;
case 3:
iBulletMovX=iTankMovX+TANK_HEIGHT;
iBulletMovY=iTankMovY+TANK_WIDTH/2-9;
Bullet[m][n].SetNowPos(iBulletMovX,iBulletMovY);
break;
}
}
}
void InitBullet()
{
for(i=0;i<TANK_WAR_SIDE;i++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -