C++控制台坦克大战
最近接了一个企业的C++定制培训班,在语法阶段结束后要求带学生做一个项目。思来想去就做一个控制台的坦克大战吧,在这里分享给大家。
工具类
- 坐标点类
class Point
{
public:
Point();
Point(int x, int y);
inline int getX(){
return x;}
inline int getY(){
return y;}
inline void setX(int x){
this->x = x;}
inline void setY(int y){
this->y = y;}
bool operator==(const Point& other);
Point operator+(Point other);
private:
int x;
int y;
};
2.尺寸类
class Size
{
public:
Size();
Size(int width, int height);
inline int getWidth(){
return width;}
inline int getHeight(){
return height;}
inline void setWidth(int width){
this->width = width;}
inline void setHeight(int height){
this->height = height;}
bool operator==(const Size& other);
private:
int width;
int height;
};
3.矩形类
class Rect
{
public:
Rect();
Rect(Point pos, Size size);
inline Point getPos(){
return pos;}
inline Size getSize(){
return size;}
inline void setPos(Point pos){
this->pos = pos;}
inline void setSize(Size size){
this->size = size;}
bool contains(Point point);//包含点
bool collide(Rect other);//矩形碰撞 AABB碰撞算法
int minX();
int maxX();
int minY();
int maxY();
private:
Point pos;
Size size;
};
类的继承关系
- 继承关系图
- Sprite类
class Sprite
{
public:
Sprite();
public:
virtual void draw() = 0;//绘制
virtual void clearDraw() = 0;//清理绘制
virtual void update() = 0;//周期逻辑
inline Rect getRect(){
return Rect(pos, size);}//获得矩形对象
inlin