在SDL中使物体独立移动(如枪声)

发布于 2024-11-06 20:01:12 字数 204 浏览 1 评论 0原文

我一直在关注 SDL 上的lazyfoos 教程,并且我对他的代码进行了大量修改,以制作一种可以四处移动的船舶游戏。我正在尝试拍摄这艘船,但我完全不知道该怎么做。我在一堂课上讲了船和它的运动以及图像的实际应用,我想知道是否有人有任何技术或某些方法可以有效地使船拍摄,使镜头独立移动,然后在它移动时消失屏幕外。我知道我给出的解释有点模糊,但我不想得到所有的答案,只是一些示例代码和正确方向的一点。

I have been following lazyfoos tutorials on SDL, and I have heavily modified his code to make sort of a ship game, that moves around. I'm trying to make the ship shoot, but i have absolutely no idea how to go about doing this. I have the ship and it's movements and the actual application of the image in a class, and I as wondering if anyone had any techniques or certain ways that are efficient in making the ship shoot, making the shot move independently and then disappearing when it goes off screen. I know that I am giving a vague explanation sort of, but I don't want to be given all of the answers, just a little sample code and a point in the right direction.

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

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

发布评论

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

评论(2

橘味果▽酱 2024-11-13 20:01:13

我只能绝对推荐Aaron 的游戏编程教程,它使用C++ 和SDL。

I can only absolutely recommend Aaron's Game Programming Tutorials, it uses C++ and SDL.

筱果果 2024-11-13 20:01:12

创建一个类来容纳射弹,其中包含您需要的所有信息,例如:

struct Projectile
{
    Vector2 position;
    Vector2 velocity;
    shared_ptr<Image> graphic;
    Time time_until_my_destruction;
    bool dead;

    void update(Time time_delta) {
        if(!dead) {
            position += velocity * time_delta;
            time_until_my_destruction -= time_delta;
            if(time_until_my_destruction < 0.0) dead = true;
        }
    }

    void draw(DrawDest & dest) const {
        graphic->draw(dest, position);
    }

    bool checkCollision(const GameObject & object) const {
        return object.area().contains(position);
    }
};

这个类显然并不完整,您可能想要调整访问级别,并编写一些构造函数和其他东西,但它应该给你基本的想法。

把它们做成一个容器。当船起火时,将一颗放入容器中。每一帧,调用更新、绘制、检查射弹是否死亡并检查与游戏对象的碰撞。如果发生碰撞,请施加损坏或其他什么。如果该物体已死亡,请将其从容器中取出。

Create a class to hold a projectile, with all the information you need in it, such as this:

struct Projectile
{
    Vector2 position;
    Vector2 velocity;
    shared_ptr<Image> graphic;
    Time time_until_my_destruction;
    bool dead;

    void update(Time time_delta) {
        if(!dead) {
            position += velocity * time_delta;
            time_until_my_destruction -= time_delta;
            if(time_until_my_destruction < 0.0) dead = true;
        }
    }

    void draw(DrawDest & dest) const {
        graphic->draw(dest, position);
    }

    bool checkCollision(const GameObject & object) const {
        return object.area().contains(position);
    }
};

This class is not complete obviously, you'll probably want to make adjustments to access levels, and write some constructors and other things, but it should give you the basic idea.

Make a container of those. When the ship fires, put one into the container. Each frame, call update, draw, check if the projectile is dead and check for collisions against the game objects. If a collision occurs, apply damage or whatever. If the object is dead, remove it from the container.

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