我正在继续一个嵌入式微控制器(Arduino)的游戏,我有一个关于课堂互动的问题——这个问题延续了我之前的问题
here
我的代码基于sheddenizen的建议(请参阅“此处”中对给定链接的响应):
我有三个从基类继承的类-
(i)
类精灵
-(低音类)在LCD上具有位图形状和(x,y)位置
(ii)
类导弹:公共精灵
-有一个特定的形状(x,y),也有一个obj
(iii)
类外星人:公共精灵
-具有特定形状,并且(x,y)
(iv)
职业选手:公共精灵
- ""
它们都有不同的(虚拟)移动方法,并显示在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();