📄 puff.cpp
字号:
#include "puff.h"
#include <cassert>
using namespace std ;
// construct Puff
Puff::Puff(SimpleWindow &w):GameWindow(w) , CurrentDirection(Up) , PuffPhoto(4) {
PuffPhoto.reserve(4);
vector<string> BitMapFiles(4) ;
BitMapFiles[0] = "bmp\\Puff-u.BMP" ;
BitMapFiles[1] = "bmp\\Puff-r.BMP" ;
BitMapFiles[2] = "bmp\\Puff-d.BMP" ;
BitMapFiles[3] = "bmp\\Puff-l.BMP" ;
for (Direction d = Up; d <= Left;
d = (Direction) (d + 1)) {
GetBmp(d).SetWindow(GetWindow());
GetBmp(d).Load(BitMapFiles[d]);
assert(GetBmp(d).GetStatus() == BitMapOkay);
}
SetPosition(Position(6.0 ,6.0)) ;
// set the distance to move the enemy in the horizontal
// or vertical directions. The distance is based
// on the size of the bitmap.
SetHorizMovement(GetBmp(GetDirection()).GetWidth() / 10.0) ;
SetVertMovement(GetBmp(GetDirection()).GetHeight() / 10.0) ;
}
//mutators
void Puff::Draw() {
GetBmp(GetDirection()).Draw() ;
}
void Puff::Erase() {
GetBmp(GetDirection()).Erase() ;
}
void Puff::SetPosition(const Position &p) {
CurrentPosition = p ;
for (Direction d = Up; d <= Left;
d = (Direction) (d + 1))
PuffPhoto[d].SetPosition(p);
}
void Puff::SetDirection(const Direction &d) {
CurrentDirection = d ;
}
void Puff::SetHorizMovement(float h) {
HorizMovement = h ;
}
void Puff::SetVertMovement(float v) {
VertMovement = v ;
}
//inspectors
Position Puff::GetPosition() const {
return CurrentPosition ;
}
Direction Puff::GetDirection() const {
return CurrentDirection ;
}
float Puff::GetHorizMovement() const {
return HorizMovement ;
}
float Puff::GetVertMovement() const {
return VertMovement ;
}
BitMap& Puff::GetBmp(const Direction &d) {
return PuffPhoto[d] ;
}
const BitMap& Puff::GetBmp(const Direction &d) const {
return PuffPhoto[d] ;
}
SimpleWindow& Puff::GetWindow() const {
return GameWindow ;
}
// check if enemy is at right edge of window
bool Puff::AtRightEdge() const {
return (GetBmp(GetDirection()).GetPosition().GetXDistance()
+ GetBmp(GetDirection()).GetWidth()
+ HorizMovement >= 15.5) ;
}
// check if enemy is at left edge of window
bool Puff::AtLeftEdge() const {
return (GetBmp(GetDirection()).GetPosition().GetXDistance() - HorizMovement <= 0.6 ) ;
}
// check if enemy is at bottom edge of window
bool Puff::AtBottomEdge() const {
return (GetBmp(GetDirection()).GetPosition().GetYDistance()
+ GetBmp(GetDirection()).GetHeight()
+ VertMovement >= 15.5) ;
}
// check if enemy is at top edge of window
bool Puff::AtTopEdge() const {
return (GetBmp(GetDirection()).GetPosition().GetYDistance() - VertMovement <= 0.6) ;
}
// compute a new position for a Puff
Position Puff::NewPosition() const {
const Position OldPosition = GetPosition();
if (GetDirection() == Left)
return OldPosition
+ Position(-GetHorizMovement(), 0);
else if (GetDirection() == Right)
return OldPosition
+ Position(GetHorizMovement(), 0);
else if (GetDirection() == Up)
return OldPosition
+ Position(0, -GetVertMovement());
else if(GetDirection() == Down)
return OldPosition
+ Position(0, GetVertMovement());
}
// the Puff move to direction
void Puff::Move() {
Direction d = GetDirection() ;
switch(d) {
case Up:
if(AtTopEdge()) {
Draw() ;
break ;
}
else {
Erase();
SetPosition(NewPosition());
Draw();
break ;
}
case Right:
if(AtRightEdge()) {
Draw() ;
break ;
}
else {
Erase() ;
SetPosition(NewPosition());
Draw();
break ;
}
case Down:
if(AtBottomEdge()) {
Draw() ;
break ;
}
else {
Erase() ;
SetPosition(NewPosition());
Draw();
break ;
}
case Left:
if(AtLeftEdge()) {
Draw() ;
break ;
}
else {
Erase() ;
SetPosition(NewPosition());
Draw();
break ;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -