Visual C 类继承(游戏结构)
visual C++ class inherit (game structure)
我正在尝试制作一些游戏引擎(我知道,应该做一个实际的游戏)。可悲的是在项目结构上失败。看起来像这样(我第五次尝试的镜头):
#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实施。
球员相同。
相关文章:
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 抛硬币计数游戏C++数据结构!.
- 使用什么数据结构来存储基于游戏对象的二维单元地图?
- Visual C 类继承(游戏结构)
- 游戏引擎:如何从结构:编码实践转换变量
- C++ - 创建游戏实体时,我应该使用"sub classes"还是结构?
- 在C++中为游戏对象设计类层次结构
- 游戏代码体系结构源
- 在矢量中存储结构信息(文本 RPG 游戏)
- 需要推荐c++游戏服务器,关于玩家使用的数据结构,怪物对象,以获得性能优势
- 为什么在文本冒险游戏中使用树数据结构来表示数据
- 设计简单贪吃蛇游戏的数据结构
- c++游戏的列表结构
- 构建一个基于状态的游戏引擎,以及Makefile结构帮助
- 一个经理类游戏的一般结构