射击游戏中如何设置精灵的概率?
我的游戏是cocos2d中的一款小型射击游戏。敌人每隔一段时间就会产生子弹射击玩家。我创建了一个随机 y ,以便子弹以随机高度接触相对的边缘。如果子弹接触到玩家,敌人就会获胜。
但是,我需要设置敌人准确度的概率。如果敌人的概率为 80% 准确度呢?我如何在我的程序中设置?对于 10 次射击,其中 8 次应该直接朝向玩家。
我怎样才能决定哪些应该在 10 中直出,哪些应该错过。同时玩家也射击敌人。
谢谢。
My game is a small shooting game in cocos2d. The enemy generates the bullets to shoot player at intervals of time. I have created a random y , so that bullets touch the opposite edge at random heights. If the bullet touches the player the enemy wins.
But, I need to set probability for the enemy accuracy. If the probability of enemy is given as 80% accuracy? How can I set in my program? For 10 shots 8 should be straight towards player.
How can I decide which ones should go straight in 10 which one should miss. In mean time player also shoots the enemy.
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会这样做。
假设你的球员的位置是 y。要获得 80% 的准确度,请从区间
[y - height*0.2, y + height*0.2]
中选择一个随机数,其中height
是屏幕的高度。一般来说,为了获得准确度 p,请从[y - height*(1-p), y + height*(1-p)]
中选择一个数字。当 p = 1.0 (100%) 时,子弹将准确瞄准玩家的位置。这并不意味着准确度为 80% 的敌人会准确地向玩家发射 10 次射击中的 8 次,而是子弹距离越近,越准确。
这当然忽略了子弹传播的时间,但无需做太多工作就可以将其包括在内。
I would do it like this.
Assume your player has position y. To get 80% accuracy choose a random number from the interval
[y - height*0.2, y + height*0.2]
, whereheight
is the height of the screen. In general to get accuracy p choose a number from[y - height*(1-p), y + height*(1-p)]
. When p = 1.0 (100%) then the bullet will be aimed at exactly the position of the player.This doesn't mean that an enemy with 80% accuracy will fire 8 out of 10 shots exactly at the player, but the more accurate the closer the bullets are.
This of course neglects the time bullet travels, but that can be included without that much work.