📄 bug.cpp
字号:
// bug.cpp: Implemention of class bug, SlowBug and FastBug
#include <iostream>
#include <string>
#include <assert.h>
#include "Bug.h"
using namespace std;
//------------------------------------------------
// Constructor
//------------------------------------------------
Bug::Bug( SimpleWindow &W, int M) :
Window(W), ChanceOfDirectionChange(M),
PercentageNumber(1, 100),Bmp(NUMBER)
{
// no code needed!
}
//-------------------------------------------------------
// Inspector
//-------------------------------------------------------
const BitMap& Bug::GetBmp(const Direction &D ) const
{
return Bmp[D];
}
BitMap& Bug::GetBmp( const Direction &D )
{
return Bmp[D];
}
//---------------------------------------------------------
void Bug::Create()
{
Draw(); // create a bug
return;
}
void Bug::Kill()
{
Erase(); // clear the bug
return;
}
//----------------------------------------------------------
// Judge whether the bug is hit
//----------------------------------------------------------
bool Bug::IsHit(const Position& HitPosition)
{
if( GetBmp(GetDirection() ) . IsInside(HitPosition) )
{
return true;
}
else
return false;
}
//---------------------------------------------------------------------------
// Judge whether the bug is at the Edge
//---------------------------------------------------------------------------
bool Bug::AtUpEdge() const
{
const float fBugYPosn = GetPosition().GetYDistance();
return fBugYPosn - GetYMovement() <= 0.0;
}
bool Bug::AtDownEdge() const
{
const float fBugYPosn = GetPosition().GetYDistance();
const float fBugYSize = GetBmp( GetDirection() ).GetHeight();
const float fWndYSize = GetWindow().GetHeight();
return fBugYPosn + fBugYSize + GetYMovement() >= fWndYSize;
}
bool Bug::AtLeftEdge() const
{
const float fBugXPosn = GetPosition().GetXDistance();
return fBugXPosn - GetXMovement() <= 0.0;
}
bool Bug::AtRightEdge() const
{
const float fBugXPosn = GetPosition().GetXDistance();
const float fBugXSize = GetBmp( GetDirection() ).GetHeight();
const float fWndXSize = GetWindow().GetWidth();
return fBugXPosn + fBugXSize + GetXMovement() >= fWndXSize;
}
//-----------------------------------------------------------------------------------------
SimpleWindow& Bug::GetWindow() const
{
return Window;
}
Position Bug::GetPosition() const
{
return BugPosition;
}
Direction Bug::GetDirection() const
{
return BugDirection;
}
float Bug::GetXMovement() const
{
return XMovement;
}
float Bug::GetYMovement() const
{
return YMovement;
}
int Bug::GetChanceOfDirectionChange() const
{
return ChanceOfDirectionChange;
}
void Bug::SetPosition(const Position& P)
{
for(Direction D = East; D < NUMBER; D = (Direction)(D+1) )
{
Bmp[D].SetPosition(P);
}
BugPosition = P;
return;
}
void Bug::SetDirection(const Direction& D )
{
BugDirection = D;
return;
}
void Bug::SetXMovement( float X)
{
XMovement = X;
return;
}
void Bug::SetYMovement( float Y)
{
YMovement = Y;
return;
}
void Bug::ChangeDirection()
{
RandomInt M( East, NUMBER - 1 );
SetDirection( (Direction) M.Draw() );
return;
}
void Bug::Draw()
{
GetBmp( GetDirection() ).Draw();
return;
}
void Bug::Erase()
{
GetBmp(GetDirection() ).Erase();
return;
}
//--------------------------------------------------------------
// the four position of bug
//--------------------------------------------------------------
Position Bug::NewPosition() const
{
const Position p = GetPosition() ;
if( GetDirection() == North )
return p + Position( 0.0, -GetYMovement() );
else if( GetDirection() == South )
return p + Position( 0.0, GetYMovement() );
else if( GetDirection() == East )
return p + Position( GetXMovement(), 0.0 );
else
return p + Position( -GetXMovement(), 0.0 );
}
//------------------------------------------------------------------------------
// SlowBug
//------------------------------------------------------------------------------
SlowBug::SlowBug(SimpleWindow &W, int p):Bug(W,p)
{
vector<string> BitMapSlow( NUMBER );
BitMapSlow[0] = "bmp\\sbug-r.bmp";
BitMapSlow[1] = "bmp\\sbug-l.bmp";
BitMapSlow[2] = "bmp\\sbug-u.bmp";
BitMapSlow[3] = "bmp\\sbug-d.bmp";
//---------------------------------------------------------
// load the bitmap in each direction
//----------------------------------------------------------
for(Direction D = East; D <= North; D = (Direction)(D+1) )
{
GetBmp(D).SetWindow( GetWindow() );
GetBmp(D).Load( BitMapSlow[D] );
assert( GetBmp(D).GetStatus() == BitMapOkay );
}
// ----------------------------------------------------------
SetXMovement(GetBmp(West).GetWidth() / 8.0 );
SetYMovement(GetBmp(North).GetHeight() / 8.0 );
//------------------------------------------------------------
// Initially make it go right
//------------------------------------------------------------
SetDirection(West);
SetPosition(Position(6.0,7.0) );
}
void SlowBug::Move()
{
Erase();
if( PercentageNumber.Draw() < GetChanceOfDirectionChange() )
{
ChangeDirection();
}
const Direction D = GetDirection();
if( D == East && AtRightEdge() )
SetDirection( West );
else if ( D == West && AtLeftEdge() )
SetDirection( East );
else if ( D == South && AtDownEdge() )
SetDirection( North );
else if( D == North && AtUpEdge() )
SetDirection( South );
SetPosition( NewPosition() );
Draw();
}
//--------------------------------------------------------------------
// FastClass
//--------------------------------------------------------------------
FastBug::FastBug( SimpleWindow &W, int p ) : Bug( W, p )
{
vector<string> BitMapFast(NUMBER);
BitMapFast[0] = "bmp\\fbug-r.bmp";
BitMapFast[1] = "bmp\\fbug-l.bmp";
BitMapFast[2] = "bmp\\fbug-d.bmp";
BitMapFast[3] = "bmp\\fbug-u.bmp";
for( Direction D = East; D < NUMBER; D = (Direction)(D+1))
{
GetBmp(D).SetWindow( GetWindow() );
GetBmp(D).Load( BitMapFast[D] );
assert( GetBmp(D).GetStatus() == BitMapOkay );
}
SetYMovement( GetBmp(North).GetWidth() / 4.0 );
SetXMovement( GetBmp(North).GetHeight() / 4.0 );
SetDirection(North);
SetPosition( Position(5.0,5.0) );
}
void FastBug::Move()
{
Erase();
if( PercentageNumber.Draw() < GetChanceOfDirectionChange() )
{
ChangeDirection();
}
SetPosition(NewPosition());
Draw();
Direction D = GetDirection();
if( D == East && AtRightEdge() )
{
Erase();
SetPosition( Position(0.0, GetPosition().GetYDistance() ) );
Draw();
}
else if( D == South && AtDownEdge() )
{
Erase();
SetPosition( Position( GetPosition().GetXDistance(), 0.0 ) );
Draw();
}
else if ( D == West && AtLeftEdge() )
{
Erase();
SetPosition( Position( GetWindow().GetWidth() - GetBmp( GetDirection() ).GetWidth(),
GetPosition().GetYDistance()));
Draw();
}
else if ( D == North && AtUpEdge() )
{
Erase();
SetPosition( Position( GetPosition().GetXDistance(),
GetWindow().GetHeight() - GetBmp(GetDirection()).GetHeight()));
Draw();
}
}
// end
//---------------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -