Interaction with the built-in C ++ class

I continue the game for the built-in microcontroller (Arduino), and I have a question about the interaction of classes - this question continues from my previous question here and I based my code on the sheddenizen sentence (see the answer to the link in "here"):

I have three classes that inherit from the base class -

(i) Sprite class - (bass class) has a raster shape and (x, y) position on the LCD

(ii) class Missile: public Sprite - has a specific form (x, y), and also takes an obj object

(iii) class Alien: public Sprite - has a specific form and (x, y)

(iv) Player class: public Sprite - ""

They all have different (virtual) moving methods and are displayed on the LCD:

enter image description here

- , , : (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();  
0
1

Missile Sprite, Sprite::x Sprite::y, . x ( this->x, ).

launchpoint, , , memfunction Missile::Move .

x y , , launchpoint ( , ), .

+2

All Articles