游戏中的攻击频率如何实现?

发布于 2024-08-16 19:45:55 字数 158 浏览 4 评论 0原文

在一款RPG游戏中,假设有角色A和B,

A每秒会进行x次攻击,

B每秒会进行y次攻击,

假设A发起攻击,最终的攻击可能是:

AABAB...

如何计算攻击顺序?

In a RPG game,suppose there are role A and B.

A will conduct x attacks per second

B will conduct y attacks per second

If we suppose A initiates the attack and the final attacks may be :

A A B A B ...

How to calculate the sequence of attacks?

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

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

发布评论

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

评论(2

梦冥 2024-08-23 19:45:55

以下是在 Python 3.0 中使用生成器和分数执行此操作的一种方法:

def get_attack_sequence(a, b):
    from fractions import Fraction
    count_a = count_b = 0
    rate_a = Fraction(1, a)
    rate_b = Fraction(1, b)
    while 1:
        new_count_a = count_a + rate_a
        new_count_b = count_b + rate_b

        if new_count_a < new_count_b:
           yield "A"
           count_a = new_count_a
        elif new_count_a > new_count_b:
           yield "B"
           count_b = new_count_b
        else:
           yield "A|B"
           count_a = new_count_a
           count_b = new_count_b

attack_sequence = get_attack_sequence(3, 2)
print(' '.join(next(attack_sequence) for _ in range(10)))

输出:

A B A A|B A B A A|B A B

需要检查攻击频率 0。为了简单起见,我没有在上面的代码中这样做,但它很容易修复,并且可能最好在此函数之外处理(无论如何,一个玩家无法攻击的战斗不会很有趣)。

这个想法的一个优点是它可以很容易地扩展到两个以上的同时玩家。

另一个优点是它还可以在不做任何修改的情况下处理每秒小于一次攻击的攻击率(例如B攻击每两秒一次,即攻击频率=0.5)。

Here's one way to do it in Python 3.0 using a generator and fractions:

def get_attack_sequence(a, b):
    from fractions import Fraction
    count_a = count_b = 0
    rate_a = Fraction(1, a)
    rate_b = Fraction(1, b)
    while 1:
        new_count_a = count_a + rate_a
        new_count_b = count_b + rate_b

        if new_count_a < new_count_b:
           yield "A"
           count_a = new_count_a
        elif new_count_a > new_count_b:
           yield "B"
           count_b = new_count_b
        else:
           yield "A|B"
           count_a = new_count_a
           count_b = new_count_b

attack_sequence = get_attack_sequence(3, 2)
print(' '.join(next(attack_sequence) for _ in range(10)))

Output:

A B A A|B A B A A|B A B

An attack frequency of 0 needs to be checked for. I haven't done this in the above code for simplicity, but it's easy to fix and probably best handled outside this function (a battle where one player can't attack wouldn't be very interesting anyway).

An advantage of this idea is that it could be easily extended to more than 2 simultaneous players.

Another advantage is that it can also handle attack rates of less than one attack per second without any modification (e.g. B attacks only once every two seconds, i.e. attack frequency = 0.5).

终止放荡 2024-08-23 19:45:55

统计谁的攻击次数多。叫他更多。除以 MORE/LESS 并占据发言权,结果 = N。然后,对于 MORE 的每 N 次攻击,添加 LESS 之一,并在完成后用 MORE 的攻击填充。这就是每一秒。

例子:

MORE = 5
LESS = 2
MORE/LESS floor = 2

Then:
MORE MORE LESS MORE MORE LESS MORE

另一个例子:

MORE = 3
LESS = 2
MORE/LESS floor = 1

Then:
MORE LESS MORE LESS MORE

Count who has more attacks. Call him MORE. Divide MORE/LESS and take floor, the result is = N. Then for every N attacks of MORE add one of LESS and pad with attacks of MORE when finished. That's each second.

Example:

MORE = 5
LESS = 2
MORE/LESS floor = 2

Then:
MORE MORE LESS MORE MORE LESS MORE

Another example:

MORE = 3
LESS = 2
MORE/LESS floor = 1

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