“概念”是指面向对象游戏的

发布于 2024-09-30 13:43:13 字数 158 浏览 7 评论 0原文

在大多数或所有面向对象的游戏中,每个类不仅依赖于自己的类,还依赖于父类。这个类连接在C++中是如何实现的呢?您只是为您需要的父类添加一个指针还是有更好的方法?

例如,一场足球比赛,当人类点击它时,询问场景类他是否正在踢任何球,如果他正在踢球,则移动它。我希望这是可以理解的而不是太抽象。

In most or all object oriented games, each class relies on not just its own but parent classes. How is this class connection implemented in C++? Do you just add a pointer for the parent class you need or is there a better way?

For example a football game, when the person class clicks it ask the scene class if he is kicking any balls, and if he is then move it. I hope this is understandable and not too abstract.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

饮湿 2024-10-07 13:43:13

我认为在构造函数中传递父级不是一个好主意。相反,您应该使用一个类来维护所有游戏元素和它们之间的交互设施的列表;例如,下面所示的 Game 类将检查任意两个玩家之间的碰撞,如果检测到碰撞,则告诉每个玩家他们被击中以及被谁击中。

我不确定这是否对您有好处,但我将其输入作为我最初的回复,所以我不妨提交。请注意,如果您谈论的是纯文本游戏,所有这些仍然相关,在这种情况下请忽略对图形的暗示。游戏设计基于连续的游戏循环,可以非常简单地理解为:

while(true)
    for each tick:
        react to user input
        update player, enemies, objects, etc.
end while

其中“tick”是游戏时钟的每次迭代,无论您选择如何实现它——基于 fps、每秒,等等。在您的示例中,用户单击足球,游戏看到单击并告诉足球移动。为了非常简单地做到这一点,维护一个维护状态的类中所有游戏元素的列表。为了说明这一点,这里有一个非常基本的方法可以实现这一点:

class Game {
    vector<GameElement> elements;
Football football;
Player currentPlayer;

Game() {
    this.football = new Football();
}

    void update() {
        for e in elements:
            e.update();

        // Once every element has been updated for the current tick, redraw them on the screen  
        screen.redraw();
    }
    void addElement(GameElement e) {
        elements.add(e);
    }
}

class GameElement {
    int posx, posy;     // screen coordinates
    void paint() {};    // method to draw this element to the screen
    virtual void update();
}

class Football: public GameElement {
    bool kicked;
    int anglex, angley;
    double velocity;

    void update() {
        if(kicked){
            // update position, angle, velocity; slow it down, check for bounce, whatever
            posx = new x position;
            posy = new y position;
        if(velocity == 0)
            kicked = false;     
        }
        paint();    // call the paint method after the new position has been found
    }
}

假设您有另一个继承自 GameElement Player 的类,其方法 kick(football) 将传递的足球设置为运动,即设置 kicked=True。因此,要初始化游戏,您可以使用以下内容进行设置:

Game game = Game();
game.add(new Football());
game.add(new Player());
while(true) {
if(user_input==X)
    game.currentPlayer.kick(football);
    game.update();
    sleep(1);
}

例如,可以更改此设置以维护层而不是整个游戏,然后更高级别的类可以按顺序调用每个层的更新,从而允许事物被涂在彼此和孩子身上,只与兄弟姐妹互动。有很多可能性。

I don't think passing along the parent in a constructor is a good idea. Instead, you should be using a class that maintains a list of all game elements and facilities interactions between them; for instance, the Game class illustrated below would check for collisions between any two players and if it's detected tell each of them that they were hit and by who.

I'm not sure if this will benefit you at all, but I typed it up for my initial response so I might as well submit. Note that all of this is still relevant if you're talking about a text-only game, just ignore the allusions to graphics in that case. Game design is based around a continuous game loop, and can be thought of very simply as:

while(true)
    for each tick:
        react to user input
        update player, enemies, objects, etc.
end while

Where "tick" is every iteration of the game clock, however you choose to implement it--based on the fps, each second, whatever. In your example, the user clicks the football, the game sees the click and tells the football to move. To do this very simply, maintain a list all of all of the game elements within a class that maintains the state. To illustrate, here are is a very basic way you could implement this:

class Game {
    vector<GameElement> elements;
Football football;
Player currentPlayer;

Game() {
    this.football = new Football();
}

    void update() {
        for e in elements:
            e.update();

        // Once every element has been updated for the current tick, redraw them on the screen  
        screen.redraw();
    }
    void addElement(GameElement e) {
        elements.add(e);
    }
}

class GameElement {
    int posx, posy;     // screen coordinates
    void paint() {};    // method to draw this element to the screen
    virtual void update();
}

class Football: public GameElement {
    bool kicked;
    int anglex, angley;
    double velocity;

    void update() {
        if(kicked){
            // update position, angle, velocity; slow it down, check for bounce, whatever
            posx = new x position;
            posy = new y position;
        if(velocity == 0)
            kicked = false;     
        }
        paint();    // call the paint method after the new position has been found
    }
}

Assume you have another class that inherits from GameElement, Player, with a method kick(football) that sets the passed football into motion--i.e., sets kicked=True. So to initialize the game, you'd set it up with something like:

Game game = Game();
game.add(new Football());
game.add(new Player());
while(true) {
if(user_input==X)
    game.currentPlayer.kick(football);
    game.update();
    sleep(1);
}

This could be changed to maintain, for example, layers instead of the entire game, then a higher level class could call update of each layer in order, allowing things to be painted over each other and children to only interact with siblings. There are a lot of possibilities.

十二 2024-10-07 13:43:13

在扑克游戏中遇到类似的问题:这是我的做法:

在您的示例中,将 scene * the_scene 添加到 person 的构造函数中。然后,当 person 初始化时,将其传递给 scene 的指针。既然你说的是父级和子级,如果父级是场景,那么它只会使用“this”,并且会发送父级的地址。

话又说回来,看来你无论如何都会这么做。另一件事,如果您需要 person 与多个不直接位于其内部的类进行交互,您可以创建某种容器类,该容器类将存储指向所有这些类的指针,并将该指针传递给 person 以避免使用构造函数参数太多。

Ran into similar questions working on a poker game: Here's the way I did it:

In your example, add a scene * the_scene to the constructor of person. Then, when person is initialized pass it a pointer to scene. Since you said parent and child, if the parent Is scene then it would just use "this" and it would send the address of the parent.

Then again, it seems like that's how you were going to do it anyway. One more thing, if you need person to interact with more than one class that is not directly inside it you can make some kind of container class that would store the pointers to all of them and just pass person that one to avoid having a constructor with too many parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文