Pong:球拍如何知道球会击中何处?
在实现 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
引用非常有趣的书“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)原始技术是:
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:
我们为高中 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?
我认为你应该让球拍始终从当前位置移动到特定点,这就是球预计与球拍垂直对齐的位置。然后,您可能有 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.
其他答案讨论了如何在不同情况下决定正确的前进方向。您还可以在计算机玩家开始朝这个方向移动“做出反应”之前添加滞后时间,这反映了人类玩家的典型反应。
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.
我不确定执行此操作的“官方”方法是什么,但我总是使用(伪代码)
然后我给桨一个稍微不同的速度,该速度足够慢,以至于它无法始终跟上球。有点粗糙,但确实有效。
该线程还有一些您可能想要查看的有趣想法: 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)
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
碰巧我前几天写了一个 pong 克隆只是为了好玩。
您可以在此处播放它并查看源代码此处。
AI 获取球的当前速度并乘以距墙的 x 距离。然后它以上限速度向计算出的位置移动。它没有考虑垂直弹跳,但这是有意为之的(为了让人工智能变得可打败)。
这是相关的片段:
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:
经过几次迭代后,如果您的桨(即您的桨或 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.