Visual C 类继承(游戏结构)

visual C++ class inherit (game structure)

本文关键字:游戏 结构 继承 Visual      更新时间:2023-10-16

我正在尝试制作一些游戏引擎(我知道,应该做一个实际的游戏)。可悲的是在项目结构上失败。看起来像这样(我第五次尝试的镜头):

#include <iostream>
#include <vector>
#include <windows.h> 
using namespace std;
class Player;
class Engine {
private:
    CHAR_INFO *chiBuffer;
    int width = 0;
    int height = 0;
    vector <Player> players;
public:
    Engine(int x = 120, int y = 30) {
        chiBuffer = new CHAR_INFO[x*y];
    }
    void addPlayer(Player player) {
        players.push_back(player);
    }
    void render() {
        for (int i = 0; i < players.size(); i++) {
            Player *player = &players[i];
            player->draw();
        }
    }
protected:
    inline CHAR_INFO& getPixel(int x, int y) { return chiBuffer[x + width * y]; }
};
class Drawable : Engine {
protected:
    inline CHAR_INFO& getPixel(int x, int y) { return Engine::getPixel(x, y); }
    virtual void draw() = 0;
};
class Player : Drawable {
protected:
    int x = 0;
    int y = 0;
public:
    void draw() {
        getPixel(x, y).Char.UnicodeChar = L'*';
    }
};
int main() {
    Engine *eng = new Engine(120, 30);
    Player p;
    eng->addPlayer(p);
    return 0;
}

我想让它容易扩展,因此在我的脑海中,我的想法是创建Master Class(Engine)Sub类可绘制的绘制()方法()方法,然后滴答作用,然后将其滴答滴答。。但是我很确定我做错了。您能告诉我更好的想法吗?或使此功能正常工作,因为这给了我太多的错误来在这里写(vs 2017)

首先,为什么要使用?

for (int i = 0; i < players.size(); i++) {
        Player *player = &players[i];
        player->draw();
 }

与:for(...) { players[i].draw() ;}
相同不要在不需要的地方使用变量和指针(例如,在您的主体中,不要使用新关键字:毫无意义。Engine e = Engine(120,30)是可以的。)。

向量上的for循环应使用迭代器:

for (auto it = begin (vector); it != end (vector); ++it) {
    it->draw;
}

在良好实践的举止中,您应该在不同的文件中写下代码。
Engine.h用于发动机声明,Engine.CPP实施。
球员相同。