다음을 통해 공유


Bizzy Bees XNA to DirectX/DirectXTK – Part 6

This is part of a blog series… if you came here directly you might want to read the introduction first.

 

The bees will be placed in the empty area at the bottom of the screen right under the columns.

For this we need the individual bees (a class called Bee) and a class that handles generating and replacing bees (BeePicker).  Similarly to the flowers, the bees are only drawn as sections of a sprite map which we have already loaded in the project.

1. Let’s start with adding two new classes (unchecking the Windows Store checkbox) called Bee and BeePicker

 class Bee
{
public:
    Bee(int color);
    ~Bee();
    int Color;
};

class BeePicker
{
public:
    BeePicker();
    ~BeePicker();
    void ResetBeePicker();
    void Draw(shared_ptr<SpriteBatch> spriteBatch, ComPtr<ID3D11ShaderResourceView> texture);

private:
    static const int beeDeltaX = 96;
    static const int beeStartX = 5;
    static const int beeStartY = 700;
    static const int numBeeColors = 5;

    vector<shared_ptr<Bee>> bees;

    void AddRandomBee();
};

The Bee class is extremely simple, it just knows the color of the bee and that’s it.  The bee picker will take care of the location.

The BeePicker (for now) has the following elements

  • A method ResetBeePicker which will generate the bees. We call this ResetBeePicker because we will call this later on when we want to restart the game (in the last article)
  • A draw method which draws all the bees
  • Some constants to space out the bees evenly
  • A list of the bees shown in the bee picker
  • A method AddRandomBee which will add a random bee to the bee picker

2. Implement the Bee constructor so that it just initializes the Color property

 Bee::Bee(int color) : Color(color){}

3. In the BeePicker::ResetBeePicker method, clear out any existing bees and add 5 random bees

 void BeePicker::ResetBeePicker(){
    bees.clear();
    for (int i = 0; i < 5; i++)
        AddRandomBee();
}

4. BeePicker::AddRandomBee is extremely simple, just adding a bee with a random color to the list

 void BeePicker::AddRandomBee(){
    bees.push_back(make_shared<Bee>(rand() % (numBeeColors + 1)));
}

5. In the BeePicker::Draw method, draw the bees beeDeltaX pixels apart, starting the first one at beeStartX. Just like when we draw the flowers we use a RECT to specify which portion of the bees to draw.  The bees here are 91px wide and 91px high.

 void BeePicker::Draw(shared_ptr<SpriteBatch> spriteBatch, ComPtr<ID3D11ShaderResourceView> texture){
    for (int i = 0; i < 5; i++){
        RECT sourceRect = { bees[i]->Color * 91, 0, (bees[i]->Color + 1) * 91, 91 };
        Vector2 position = Vector2(beeStartX + i*beeDeltaX, beeStartY);
        spriteBatch->Draw(texture.Get(), position, &sourceRect, Colors::White);
    }
}

Now we are all set for generating and drawing Bees, we just need to initiate it from the BizzyBeesGame class

1. Add a new private member variable beePicker to the BizzyBeeGame class

     shared_ptr<BeePicker> beePicker;

2. In BizzyBeeGame::CreateWindowSizeDependentResources right before the call to ResetGame create a new BeePicker

     //TODO: Initialize the game
    beePicker = make_shared<BeePicker>();
    ResetGame();

3. At the bottom of the BizzyBeeGame::ResetGame method, reset the bee picker

 void BizzyBeeGame::ResetGame(){
    GameOver = false;

    //RANDOMIZE
    srand((unsigned)time(NULL));

    //INITIALIZE COLUMNS
    columns.clear();
    for (int i = 0; i < 5; i++)
        columns.push_back(make_shared<Column>(i * 92 + 22));

    //INTIALIZE BEE PICKER
    beePicker->ResetBeePicker();
}

4. Finally to draw the bee picker, call the BeePicker::Draw method in BizzyBeeGame::DrawBees

 void BizzyBeeGame::DrawBees(){
    beePicker->Draw(spriteBatch, beeMapTexture);
}

5. Build and Run

image

Now we have all the flowers and the bees in place, but still the game is far from interesting since we don’t have any user interaction:) you guessed right, that’s what we’ll get to next:).