📄 gballoon.cpp
字号:
#include "randgen.h"
#include "canvas.h"
#include "utils.h"
#include "gballoon.h"
#include <sstream>
static const int ALT_CHANGE = 10;
static const int JUMP_SIZE = 5;
static const int LEGEND_HEIGHT=15;
static const int RADIUS = 16;
static const int CABLE_HEIGHT = RADIUS/2;
static const int CABLE_WIDTH = 2;
static const int HEIGHT = 3*RADIUS;
static const int BASKET_SIZE = 8;
static const int CANVAS_HEIGHT=300;
static const int CANVAS_WIDTH =400;
AnimatedCanvas Balloon::ourCanvas(CANVAS_WIDTH,CANVAS_HEIGHT,10,10);
int Balloon::ourCount = 0;
Balloon::Balloon()
{
Create(GOLD);
}
Balloon::Balloon(color c)
{
Create(c);
}
void Balloon::Ascend(int height)
{
cout << endl; Message();
cout << " **** rising to " << height << " meters " << endl;
while (myAltitude < height)
{
Message();
Burn();
}
}
void Balloon::Descend(int height)
{
cout << endl; Message();
cout << " **** descending to " << height << " meters " << endl;
while (myAltitude > height)
{
Message();
Vent();
}
}
void Balloon::Message()
{
cout << "balloon #" << myCount << " at (" << mySteps << ", "
<< myAltitude << ") \t";
}
void Balloon::Cruise(int steps)
{
const int START = myAltitude;
const int MARGIN = 5;
cout << endl; Message();
cout << " ***** Cruise at " << START << " m. for ";
cout << steps << " secs." << endl;
int k;
for(k=0; k < steps; k++)
{
Message();
mySteps += 1;
AdjustAltitude();
if (myAltitude > (START+MARGIN))
{
cout << " too high! ";
Vent();
}
else if (myAltitude < (START-MARGIN))
{
cout << " too low! ";
Burn();
}
else
{
Display();
}
}
}
int Balloon::GetLocation() const
{
return mySteps;
}
int Balloon::GetAltitude() const
{
return myAltitude;
}
void Balloon::AdjustAltitude()
{
RandGen rand;
if (rand.RandInt(0,1)==1)
{
int change = rand.RandInt(-JUMP_SIZE,JUMP_SIZE);
myAltitude += change;
if (change != 0)
{
cout << " wind-shear ";
cout.setf(ios::showpos);
cout << change << " ";
cout.unsetf(ios::showpos);
}
if (myAltitude < 0) // keep balloon above ground
{
myAltitude = 0;
}
}
}
void Balloon::Burn()
{
myAltitude += ALT_CHANGE;
cout << " burn ";
Display();
}
void Balloon::Vent()
{
myAltitude -= ALT_CHANGE;
if (myAltitude < 0)
{
myAltitude = 0;
}
cout << " vent ";
Display();
}
void Balloon::Display()
{
myShape->moveTo(Point(RADIUS+mySteps,
CANVAS_HEIGHT - myDisplayHeight - myAltitude));
ostringstream os;
os << "# " << myCount << " at (" << mySteps << ", " << myAltitude << ")";
myLegend->changeText(os.str());
ourCanvas.repaint();
cout << endl;
}
void Balloon::Create(color c)
{
myAltitude = 0;
mySteps = 0;
myCount = ourCount;
ourCount++;
if (myCount == 0)
{
ourCanvas.SetTitle("Balloon Display");
}
CompositeShape *cs = new CompositeShape();
cs->add(new CircleShape(Point(RADIUS,RADIUS),RADIUS,c));
tvector<Point> trapezoid;
trapezoid.push_back(Point(0,RADIUS));
trapezoid.push_back(Point(2*RADIUS,RADIUS));
trapezoid.push_back(Point(3*RADIUS/2,HEIGHT));
trapezoid.push_back(Point(RADIUS/2,HEIGHT));
PolygonShape * ps = new PolygonShape(trapezoid,trapezoid.size(),c);
cs->add(ps);
cs->add(new RectangleShape(Point(RADIUS-BASKET_SIZE/2,HEIGHT),
CABLE_WIDTH,CABLE_HEIGHT,c));
cs->add(new RectangleShape(Point(RADIUS+BASKET_SIZE/2-CABLE_WIDTH,HEIGHT),
CABLE_WIDTH,CABLE_HEIGHT,c));
cs->add(new RectangleShape(Point(RADIUS-BASKET_SIZE/2,HEIGHT+CABLE_HEIGHT),
BASKET_SIZE,BASKET_SIZE,c));
//cs->add(new RectangleShape(RADIUS,RADIUS/2,c,Point(RADIUS/2,HEIGHT)));
//cs->add(new RectangleShape(2,HEIGHT,c,Point(RADIUS/2,RADIUS/2)));
//cs->add(new RectangleShape(2,HEIGHT,c,Point(3*RADIUS/2-2,RADIUS/2)));
// ImageShape *cs = new ImageShape(* new image("c:\\book\\ed2\\code\\balloon2.jpg"),Point(RADIUS,RADIUS));
myShape = new Mover(*cs);
myLegend = new TextShape(Point(0,myCount*LEGEND_HEIGHT)," ",c);
ourCanvas.addShape(myLegend);
ourCanvas.addShape(myShape);
myDisplayHeight = myShape->bbox().height();
Display();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -