如何根据y分配一个值

发布于 2025-01-19 17:46:07 字数 482 浏览 1 评论 0原文

我的这段代码写起来非常烦人,但可以完成工作,我想知道是否有某种函数或类可以帮助我。

这是我的功能:

func add_health():
    if wave in range(10,20):
        enemy_health = 2
    elif wave in range(20,30):
        enemy_health = 3.5
    elif wave in range(30,40):
        enemy_health = 5
    elif wave in range(40,50):
        enemy_health = 6.5
    elif wave in range(50,59):
        enemy_health = 8
    elif wave in range(60,69):
        enemy_health = 9.5
    else:
        enemy_health = 1

I have this code that is very annoying to write but does the job, I was wondering if there was a function or class of some sort to help me.

Here's my function:

func add_health():
    if wave in range(10,20):
        enemy_health = 2
    elif wave in range(20,30):
        enemy_health = 3.5
    elif wave in range(30,40):
        enemy_health = 5
    elif wave in range(40,50):
        enemy_health = 6.5
    elif wave in range(50,59):
        enemy_health = 8
    elif wave in range(60,69):
        enemy_health = 9.5
    else:
        enemy_health = 1

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

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

发布评论

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

评论(1

总以为 2025-01-26 17:46:07

如果您的 wave 变量是整数,则很简单1。如果不是,您应该能够将其转换为 int 以获得相同的效果。将其作为函数的输入使这变得简单(因为您可以强制转换)。

看起来你每 10 波都会将 enemy_health 值增加 1.5(至少到 70 波时会恢复为 1)。因此,只需将 wave 除以 10,然后乘以 1.5,并将其添加到偏移量即可。您可能需要对基本情况进行一些特殊处理(wave <10 或 >=70)。

如果不考虑这些边缘情况,您的函数将变为

func add_health(current_wave: int):
    # Parentheses probably unnecessary, but useful for clarity
    enemy_health = ((current_wave/10) * 1.5) + 0.5

1 将两个整数相除得到一个整数,并丢弃任何余数。所以 53/10 == 5

If your wave variable is an integer, this is simple1. If it is not, you should be able to cast it to an int for the same effect. Making it an input to the function makes this simple (since you can force the conversion).

It looks like you increase the enemy_health value 1.5 per 10 wave (at least until 70, where you revert to 1). So simply divide wave by 10 and then multiple by 1.5, and add it to an offset. You may need some special handling for base cases (wave <10 or >=70).

Without considering those edge cases, your function becomes

func add_health(current_wave: int):
    # Parentheses probably unnecessary, but useful for clarity
    enemy_health = ((current_wave/10) * 1.5) + 0.5

1 Dividing two integers results in an integer, discarding any remainder. So 53/10 == 5

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