📄 control.cpp
字号:
// control.cpp : the implement of the control
#include <iostream>
#include <string>
using namespace std;
#include <assert.h>
#include "Control.h"
//----------------------------------------------------------------------------------------------------------------
// Constructors
//----------------------------------------------------------------------------------------------------------------
GameController::GameController( const string &Title /* = " " */, const Position &WinPosition /* = Position(1.0 */,
float WinLength, float WinHeight ) : Level(Slow), Status( SettingUp ), KingOfBug(2),
m_vbDirectBmp(DirectBmp)
{
// creat main game window and open it
GameWindow = new SimpleWindow("游戏窗口", WinLength,WinHeight,WinPosition);
GetGameWindow() ->Open();
// whether the window is open
assert( GetGameWindow() );
// creat control window and open it
ControlWindow = new SimpleWindow("Control Window", WinLength / 2.4, WinHeight,
WinPosition + Position(WinLength, 0.0 ) );
GetControlWindow() ->Open();
assert( GetControlWindow() );
BitMap Back(ControlWindow);
Back.Load("bmp//back.bmp"); //load the control window background
Back.Draw();
PlaySound("wav//begin.wav", NULL, SND_SYNC |SND_FILENAME); // play the begin music
InitBitMap();
m_Hunter = new Hunter( *GetGameWindow() );
assert( m_Hunter);
KingOfBug[Slow] = new SlowBug( *GetGameWindow() );
assert(KingOfBug[Slow]);
KingOfBug[Fast] = new FastBug( *GetGameWindow() );
assert(KingOfBug[Fast]);
}
//--------------------------------------------------------------------------
// Destructors
//--------------------------------------------------------------------------
GameController::~GameController()
{
// get rid of the bugs and the window
delete KingOfBug[Slow];
delete KingOfBug[Fast];
delete GameWindow;
}
//----------------------------------------------------------------------------
SimpleWindow* GameController::GetGameWindow()
{
return GameWindow;
}
SimpleWindow* GameController::GetControlWindow()
{
return ControlWindow;
}
void GameController::Reset()
{
Status = SettingUp;
Level = Slow;
CurrentBug() ->Create();
}
void GameController::Play(const GameLevel l)
{
Level=l;
Status=Playing;
GetGameWindow()->StartTimer(GameSpeed);
}
int GameController::TimerTick()
{
CurrentBug() ->Move();
m_Hunter ->Move();
const Position p = m_Hunter ->GetPosition();
if( CurrentBug() ->IsHit( p ) )
{
BugHit();
if( Status == GameWon )
{
GetGameWindow() ->StopTimer();
// load the win bmp
BitMap Win(GameWindow);
Win.Load("bmp//win.bmp");
Win.Draw();
// play the win music
PlaySound("wav//win.wav", NULL, SND_SYNC |SND_FILENAME);
int message = MessageBox(NULL, "你获胜了啊!\n想再玩一次吗?", "提示", MB_YESNO );
if( message == 1 )
{
Win.Erase();
Reset();
Play(Slow);
}
else
exit(1);
Win.Erase();
Reset();
Play(Slow);
}
}
return 1;
}
int GameController::MouseClick(const Position& MousePosition)
{
//the direction for the pet to move in different conditions
if ( m_vbDirectBmp[Hunter_Up].IsInside( MousePosition ) )
{
m_Hunter->SetDirection( Hunter_Up );
}
else if ( m_vbDirectBmp[Hunter_Down].IsInside( MousePosition ) )
{
m_Hunter->SetDirection( Hunter_Down );
}
else if ( m_vbDirectBmp[Hunter_Left].IsInside( MousePosition ) )
{
m_Hunter->SetDirection( Hunter_Left );
}
else if ( m_vbDirectBmp[Hunter_Right].IsInside( MousePosition ) )
{
m_Hunter->SetDirection( Hunter_Right );
}
return 1;
}
void GameController::BugHit()
{
CurrentBug() ->Kill();
Level = ( GameLevel(Level+1) );
if( Level == Done )
Status = GameWon;
else
CurrentBug() ->Create();
}
GameLevel GameController::BugLevel() const
{
return Level;
}
Bug* GameController::CurrentBug() const
{
GameLevel L = BugLevel();
return KingOfBug[L];
}
void GameController::InitBitMap()
{
vector<string> vtBmpFiles(DirectBmp);
vtBmpFiles[0] = "bmp\\Direct-U.bmp";
vtBmpFiles[1] = "bmp\\Direct-D.bmp";
vtBmpFiles[2] = "bmp\\Direct-L.bmp";
vtBmpFiles[3] = "bmp\\Direct-R.bmp";
for( HunterDirection i = Hunter_Up; i < Hunter_DirectNumber - 1; i = (HunterDirection)(i+1) )
{
m_vbDirectBmp[i].SetWindow( *GetControlWindow() );
m_vbDirectBmp[i].Load( vtBmpFiles[i] );
assert( m_vbDirectBmp[i].GetStatus() == BitMapOkay );
}
const float VTSideLength = m_vbDirectBmp[0].GetWidth();
Position InitPosn( 3.5 - VTSideLength / 1.0, 3.0 );
m_vbDirectBmp[0].SetPosition( InitPosn + Position(-0.5, -VTSideLength+7.0 ) );
m_vbDirectBmp[1].SetPosition( InitPosn + Position(-0.5, VTSideLength+7.0 ) );
m_vbDirectBmp[2].SetPosition( InitPosn + Position(-0.5 - VTSideLength, 7.0 ) );
m_vbDirectBmp[3].SetPosition( InitPosn + Position(-0.5 + VTSideLength, 7.0 ) );
BitMap Center(ControlWindow);
Center.Load("bmp//center.bmp");
Center.SetPosition( InitPosn + Position(-0.5, VTSideLength + 6.0 ) );
Center.Draw();
m_vbDirectBmp[0].Draw();
m_vbDirectBmp[1].Draw();
m_vbDirectBmp[2].Draw();
m_vbDirectBmp[3].Draw();
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -