Pong:球拍如何知道球会击中何处?

发布于 2024-10-09 16:17:52 字数 341 浏览 0 评论 0原文

在实现 Pacman 和 Snake 之后,我正在实现下一个非常非常经典的游戏:Pong。

实现非常简单,但我还剩下一个小问题。当其中一个桨(我不确定它是否称为桨)由计算机控制时,我很难将其定位在正确的位置。

球有当前位置、速度(目前是恒定的)和方向角。这样我就可以计算出它撞击计算机控制桨侧面的位置。这样我就可以将桨定位在那里。但在真实的比赛中,电脑的球拍有可能会漏球。我该如何实现这个概率?

如果我只用计算机的球拍击球的概率为 0.5,问题就解决了,但我认为事情没那么简单。

从最初的游戏来看,我认为概率取决于当前球拍位置和球击中边界的位置之间的距离。

有人知道这是如何精确计算的吗?

After implementing Pacman and Snake I'm implementing the next very very classic game: Pong.

The implementation is really simple, but I just have one little problem remaining. When one of the paddle (I'm not sure if it is called paddle) is controlled by the computer, I have trouble to position it at the correct position.

The ball has a current position, a speed (which for now is constant) and a direction angle. So I could calculate the position where it will hit the side of the computer controlled paddle. And so Icould position the paddle right there. But however in the real game, there is a probability that the computer's paddle will miss the ball. How can I implement this probability?

If I only use a probability of lets say 0.5 that the computer's paddle will hit the ball, the problem is solved, but I think it isn't that simple.

From the original game I think the probability depends on the distance between the current paddle position and the position the ball will hit the border.

Does anybody have any hints how exactly this is calculated?

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

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

发布评论

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

评论(7

盛夏已如深秋| 2024-10-16 16:17:52

引用非常有趣的书“Racing the Beam”(Google 图书:http://books.google.co.uk/books?id=DqePfdz_x6gC&lpg=PP1&dq=racing%20the%20beam& pg=PA40#v=onepage&q&f=false)原始技术是:

为了帮助模拟精确桨定位中固有的人为错误,AI 桨每八帧就会跳过一次调整。由此产生的行为在视觉上是不易察觉的,但它使计算机玩家的目标发生足够的漂移,以至于偶尔会错过球。它在技术上实现起来也很简单,只需要一个掩码和二进制 AND 运算,对此存在相应的 6502 指令。程序员可以使用另一个操作码测试结果是否为零,如果需要则分支以跳过移动桨的指令。

即使是这种行为也必须稍微修改才能让游戏正常运行。如果人工智能玩家只是每八帧停止跟踪球,那么它就会在几秒钟内完全失去同步。为了防止这种情况发生,人工智能在比赛场地的顶部和底部附近采用了辅助球跟踪方案。如果球与这些墙壁之一碰撞,而球拍也与之对齐,球拍会重新调整,从球上次撞击墙壁以来积累的任何漂移中恢复。结果是计算机桨和球的随机错位和重新对准。

Quoting from the very enjoyable book "Racing the Beam" (Google Books: http://books.google.co.uk/books?id=DqePfdz_x6gC&lpg=PP1&dq=racing%20the%20beam&pg=PA40#v=onepage&q&f=false) the original technique was:

To help simulate the human error inherent in precise paddle positioning, The AI paddle skips its adjustment every eight frames. The resulting behaviour is visibly unnoticeable, but it allows the computer player's aim to drift enough that it occasionally misses the ball. It is also technically trivial to implement, requiring only a single mask and the binary AND operation, for which there exists a corresponding 6502 instruction. The programmer can test if the result is zero with another single opcode, branching if needed to skip the instructions that move the paddle.

Even this behaviour must be modified slightly for the game to work at all. If the AI player simply stopped tracking the ball every eight frames, it would be hopelessly out of sync within a few seconds. To prevent this, the AI follows a secondary ball-tracking scheme near the top and bottom of the playfield. If the ball collides with one of these walls while the paddle is also aligned with it, the paddle readjusts, recovering from any drift that had accumulated since the ball last struck the wall. The result is a stochastic misalignment and realignment of computer paddle and ball.

琴流音 2024-10-16 16:17:52

我们为高中 CS 课制作了一个(伪)3D 乒乓球游戏。我们所做的是,我们让计算机总是将球拍移向球,但以最大速度——这样,如果球太远,它可能会错过球,但它仍然很聪明。这有帮助吗?

We made a (pseudo-)3D ping-pong game for our high school CS class. What we did was, we made the computer always move the paddle toward the ball, but with a maximum speed -- that way, it could miss the ball if it's too far, but it's still smart. Does this help?

樱&纷飞 2024-10-16 16:17:52

我认为你应该让球拍始终从当前位置移动到特定点,这就是球预计与球拍垂直对齐的位置。然后,您可能有 50% 的机会球拍将移动到该精确点并使球偏转,有 25% 的机会球会过冲(可能超出一些 X 像素),还有 25% 的机会会下冲。更好的方法可能是让它移动到钟形曲线上的那个位置,这样每次都会错过不同的量。

I would think you should have the paddle always move from its current position to a specific point, which would be where the ball is expected to align vertically with the paddle. You could then have a 50% chance that the paddle will move to this exact point and deflect the ball, a 25% chance that it will overshoot, perhaps by some X pixels, and 25% chance that it will undershoot. An even better way to do it might be to have it move to that position on a bell curve so that it misses by different amounts each time.

不羁少年 2024-10-16 16:17:52

其他答案讨论了如何在不同情况下决定正确的前进方向。您还可以在计算机玩家开始朝这个方向移动“做出反应”之前添加滞后时间,这反映了人类玩家的典型反应。

Other answers discuss how to decide the correct direction to move in in different circumstances. You can also add a lag time before the computer player "reacts" by starting to move in this direction -- that mirrors the typical response of human players.

千紇 2024-10-16 16:17:52

我不确定执行此操作的“官方”方法是什么,但我总是使用(伪代码)

if (ball is above paddle) {
    move paddle up
}
if (ball is below paddle) {
    move paddle down
}

然后我给桨一个稍微不同的速度,该速度足够慢,以至于它无法始终跟上球。有点粗糙,但确实有效。

该线程还有一些您可能想要查看的有趣想法: http:// /www.gamedev.net/community/forums/topic.asp?topic_id=439576

I'm not sure what the 'official' way to do this is, but I've always just used (pseudocode)

if (ball is above paddle) {
    move paddle up
}
if (ball is below paddle) {
    move paddle down
}

Then I gave the paddle a slightly varying speed that was slow enough that it couldn't always keep up with the ball. Kind of crude, but it works.

This thread also has some interesting ideas you might want to look at: http://www.gamedev.net/community/forums/topic.asp?topic_id=439576

苦行僧 2024-10-16 16:17:52

碰巧我前几天写了一个 pong 克隆只是为了好玩。

您可以在此处播放它并查看源代码此处

AI 获取球的当前速度并乘以距墙的 x 距离。然后它以上限速度向计算出的位置移动。它没有考虑垂直弹跳,但这是有意为之的(为了让人工智能变得可打败)。

这是相关的片段:

/* Update enemy based on simple AI */
enemy.update = function (delta) {
    var impactDistance, impactTime, targetY, speed;
    speed = .25; // a little slower than the human player

    if (ball.vx < 0) {
        // Ball is moving away, AI takes a nap ..
        return;
    }

    // Figure out linear trajectory ..
    impactDistance = width - ball.width - ball.x;
    impactTime = impactDistance / (ball.vx * .25 * 1000);
    targetY = ball.y + (ball.vy * .25 * 1000) * impactTime;

    if (Math.abs(targetY - (this.y + this.height/2)) < 10) {
        // AI doesn't need to move
        return;
    }

    if (targetY < this.y + (this.height / 2)) {
        // Move up if ball is going above paddle
        speed = -speed;
    }

    this.y += speed * delta;
    this.keepInBounds();
};

It just so happens I wrote a pong clone the other day just for fun.

You can play it here and view the source code here.

The AI takes the ball's current speed and multiplies the x-distance away from the wall. It then moves towards that calculated position at a capped speed. It doesn't account for vertical bounces, but that's sort of intended (to make the AI beatable).

Here is the relevant snippet:

/* Update enemy based on simple AI */
enemy.update = function (delta) {
    var impactDistance, impactTime, targetY, speed;
    speed = .25; // a little slower than the human player

    if (ball.vx < 0) {
        // Ball is moving away, AI takes a nap ..
        return;
    }

    // Figure out linear trajectory ..
    impactDistance = width - ball.width - ball.x;
    impactTime = impactDistance / (ball.vx * .25 * 1000);
    targetY = ball.y + (ball.vy * .25 * 1000) * impactTime;

    if (Math.abs(targetY - (this.y + this.height/2)) < 10) {
        // AI doesn't need to move
        return;
    }

    if (targetY < this.y + (this.height / 2)) {
        // Move up if ball is going above paddle
        speed = -speed;
    }

    this.y += speed * delta;
    this.keepInBounds();
};
红玫瑰 2024-10-16 16:17:52

经过几次迭代后,如果您的桨(即您的桨或 AI 的桨,具体取决于您的视角)距离屏幕顶部或底部太近,则根本不可能覆盖所需的距离。球拍可以轻松地跟随球的 y 值。如果你太远你就会错过。

或者,您可以在每次击球后将人工智能重置为中心,或者只要球远离(即朝向对手)就向中心移动

或更简单地说:
- 当球远离时向中心移动
- 模拟球接近时的 y 坐标。

从这里开始,您可以通过使 y 坐标模仿机制比球慢(更一致,可能就像最初的实现一样;因为困难被抽象为简单的速度系数),或者向每个动作添加随机误差或否则。

after several iterations, if your paddle (that is yours or the AI's depending on your perspective) is too close to the top or bottom of the screen it is simply impossible to cover the distance required. The paddle can easily just follow the y-value of the ball. If you are too far you'll miss.

Alternatively, you can have the AI reset to center after each hit, or travel toward the center as long as the ball is moving away (i.e. toward the opponent)

Or more briefly:
- move toward the center while the ball is moving away
- imitate y-coordinate of ball while it is approaching.

from here you can change the difficulty by making the y-coord imitation mechanism slower than the ball (more consistent, and probably what the original implementation was like; as difficulties are abstracted into simple speed coefficients), or adding random error to each movement or otherwise.

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