如何根据y分配一个值
我的这段代码写起来非常烦人,但可以完成工作,我想知道是否有某种函数或类可以帮助我。
这是我的功能:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的
wave
变量是整数,则很简单1。如果不是,您应该能够将其转换为 int 以获得相同的效果。将其作为函数的输入使这变得简单(因为您可以强制转换)。看起来你每 10 波都会将
enemy_health
值增加 1.5(至少到 70 波时会恢复为 1)。因此,只需将wave
除以 10,然后乘以 1.5,并将其添加到偏移量即可。您可能需要对基本情况进行一些特殊处理(wave
<10 或 >=70)。如果不考虑这些边缘情况,您的函数将变为
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 dividewave
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
1 Dividing two integers results in an integer, discarding any remainder. So 53/10 == 5