文字游戏角色碰撞检测问题

发布于 2024-11-08 16:55:26 字数 1197 浏览 0 评论 0原文

我正在制作一款游戏,当你向下移动屏幕时,你要避开由“X”组成的墙壁,如果你击中了“X”,你就会失去一个盾牌,并继续穿过“X”而不会失去更多的盾牌,直到你弹出到一个角色空间中。 但是,当运行下面的代码

以及其他一些不必要的代码时,如果我在 Visual Studio 中放置了停止符,那么它会完美地工作,但是当我在代码运行时开始碰到“X”墙时,墙壁移动得非常快,它不起作用,你最终会进行犁耕,但你会在这个过程中失去所有护盾,并且当你“犁”过它们时,X不会消失。

这是代码:

char coll;  // the buffer for the collided char

if (theScreen.check_collision(player, xMove, yMove, coll) != true) {
    // no collision
    // get rid of old player placing
    theScreen.Insert(' ', player.get_location()[0], player.get_location()[1]);
    // put in new charater placing
    player.move(xMove, yMove);
    theScreen.Insert(player);
    numb_coll = 0;
} else {
    // collision
    if (coll == 'X' && player.get_shield() > 0) {
        for (int I = 0; I < numb_coll && numb_coll < player.get_location()[1]; I++) {
            theScreen.Insert(' ', player.get_location()[0],
                     player.get_location()[1] - (I + 1));
        }
        if (numb_coll == 0)
            player.set_shield(player.get_shield() - 1);
        numb_coll++;
        theScreen.Insert(player);
    }
    if (coll == 'X' && player.get_shield() <= 0)
        dead = true;
};

I'm making a game where you avoid walls made of 'X's as you move down the screen, and if you hit an X you lose a shield and keep plowing through the X's without losing any more shields until you pop out into a character space where there isn't an X.

However, when running the code below along with some other unneccesary code it woks perfectly if I have stops placed in Visual Studio, but when I start to hit an 'X' wall while the code is running and walls moving quite fast it doesn't work right and you end up doing the plowing, but you lose all shields in the proccess and the X's don't disappear as you "plow" through them.

Here's the code:

char coll;  // the buffer for the collided char

if (theScreen.check_collision(player, xMove, yMove, coll) != true) {
    // no collision
    // get rid of old player placing
    theScreen.Insert(' ', player.get_location()[0], player.get_location()[1]);
    // put in new charater placing
    player.move(xMove, yMove);
    theScreen.Insert(player);
    numb_coll = 0;
} else {
    // collision
    if (coll == 'X' && player.get_shield() > 0) {
        for (int I = 0; I < numb_coll && numb_coll < player.get_location()[1]; I++) {
            theScreen.Insert(' ', player.get_location()[0],
                     player.get_location()[1] - (I + 1));
        }
        if (numb_coll == 0)
            player.set_shield(player.get_shield() - 1);
        numb_coll++;
        theScreen.Insert(player);
    }
    if (coll == 'X' && player.get_shield() <= 0)
        dead = true;
};

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

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

发布评论

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

评论(1

疾风者 2024-11-15 16:55:26

我建议对代码进行彻底的重新组织。此例程似乎涉及在屏幕上绘制玩家更改玩家的护盾等级。我相信,如果您将播放器的显示与播放器的修改分开,您的代码将更易于阅读和维护。

考虑一下:

theScreen.check_collision(player, xMove, yMove, coll) {
    /* your current check_collision code, which I hope is correct */
    if (collision)
        player.destroy_a_shield();
    return collision;
}

在您的 player 对象中:

player.destroy_a_shield() {
    self.shield--;
    if (self.shield == 0)
        player.dead = true;
}

然后这段代码可能看起来更像是:

if (theScreen.check_collision(player, xMove, yMove, coll) {
    if (player.dead) 
        theScreen.insert("Ooops. Game over. Better luck next life.");
    else {
        for (int I = 0; I < numb_coll && numb_coll < player.get_location()[1]; I++) {
            theScreen.Insert(' ', player.get_location()[0],
                    player.get_location()[1] - (I + 1));
            }
        numb_coll++;
        theScreen.Insert(player);
    }
} else {
    theScreen.Insert(' ', player.get_location()[0], player.get_location()[1]);
    player.move(xMove, yMove);
    theScreen.Insert(player);
}

这些修改的想法(以伪代码呈现,因为我很懒,而且我不知道到底是什么其余代码看起来像)是将屏幕显示与游戏机制分开。 (您以前可能听说过“模型-视图-控制器”这个术语——这种模式准确地描述了我的建议。将业务规则移出您的视图。: )

I suggest a drastic re-organization of the code. This routine appears to be concerned with both drawing the player on the screen and changing the player's shield rating. I believe if you separate the display of the player from the modification of the player, your code will be easier to read and maintain.

Consider this:

theScreen.check_collision(player, xMove, yMove, coll) {
    /* your current check_collision code, which I hope is correct */
    if (collision)
        player.destroy_a_shield();
    return collision;
}

In your player object:

player.destroy_a_shield() {
    self.shield--;
    if (self.shield == 0)
        player.dead = true;
}

And then this code could look more like:

if (theScreen.check_collision(player, xMove, yMove, coll) {
    if (player.dead) 
        theScreen.insert("Ooops. Game over. Better luck next life.");
    else {
        for (int I = 0; I < numb_coll && numb_coll < player.get_location()[1]; I++) {
            theScreen.Insert(' ', player.get_location()[0],
                    player.get_location()[1] - (I + 1));
            }
        numb_coll++;
        theScreen.Insert(player);
    }
} else {
    theScreen.Insert(' ', player.get_location()[0], player.get_location()[1]);
    player.move(xMove, yMove);
    theScreen.Insert(player);
}

The idea of these modifications (rendered in pseudo-code, because I'm lazy and because I don't know exactly what the rest of your code looks like) is to separate the screen display from the game mechanics. (You've probably heard the term Model-View-Controller before -- this pattern accurately describes what I'm suggesting. Move the business rules out of your views. :)

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