我正在继续为嵌入式微控制器(Arduino)开发游戏,并且我有一个关于类交互的问题——这个问题从我之前的问题继续,我的代码基于 sheddenizen 的建议(参见对给定的响应'这里'中的链接):
我有三个从基类继承的类-
(i) 类 Sprite -(低音类)在 LCD 上具有位图形状和 (x,y) 位置
(ii) 类 Missile : public Sprite - 有一个特定的形状,(x,y) 并且需要一个 obj
(iii)类 Alien : public Sprite - 具有特定的形状和 (x,y)
(iv) 类 Player : public Sprite - ""
它们都有不同的(虚拟)移动方法,并显示在 LCD 上:
我的简化代码如下 - 具体来说,我只希望导弹在某些条件下发射:创建导弹时,它需要一个对象 (x,y) 值,我如何在继承的类中访问传递的对象值?
// Bass class - has a form/shape, x and y position
// also has a method of moving, though its not defined what this is
class Sprite
{
public:
Sprite(unsigned char * const spacePtrIn, unsigned int xInit, unsigned int yInit);
virtual void Move() = 0;
void Render() { display.drawBitmap(x,y, spacePtr, 5, 6, BLACK); }
unsigned int X() const { return x; }
unsigned int Y() const { return y; }
protected:
unsigned char *spacePtr;
unsigned int x, y;
};
// Sprite constructor
Sprite::Sprite(unsigned char * const spacePtrIn, unsigned int xInit, unsigned int yInit)
{
x = xInit;
y = yInit;
spacePtr = spacePtrIn;
}
/*****************************************************************************************/
// Derived class "Missile", also a sprite and has a specific form/shape, and specific (x,y) derived from input sprite
// also has a simple way of moving
class Missile : public Sprite
{
public:
Missile(Sprite const &launchPoint): Sprite(&spaceMissile[0], launchPoint.X(), launchPoint.Y()) {}
virtual void Move();
};
void Missile::Move()
{
// Here - how to access launchPoint.X() and launchPoint.Y() to check for
// "fire conditions"
y++;
Render();
}
// create objects
Player HERO;
Alien MONSTER;
Missile FIRE(MONSTER);
// moving objects
HERO.Move();
MONSTER.Move();
FIRE.Move();