所以我大胆地尝试将玩家的XP转换为等级编号,我只是想知道是否有更有效/更整洁的方式,因为这看起来很可怕,并且与运行每个elif所花费的时间相比。干杯。
# XP to Level converter
if self.xp >= "0":
self.level = "Level 0"
elif self.xp >= "1000":
self.level = "Level 1"
elif self.xp >= "3000":
self.level = "Level 2"
elif self.xp >= "5000":
self.level = "Level 3"
elif self.xp >= "7500":
self.level = "Level 4"
elif self.xp >= "10000":
self.level = "Level 5"
elif self.xp >= "13000":
self.level = "Level 6"
elif self.xp >= "15000":
self.level = "Level 7"
elif self.xp >= "20000":
self.level = "Level 8"
elif self.xp >= "25000":
self.level = "Level 9"
elif self.xp >= "30000":
self.level = "Level 10"
elif self.xp >= "35000":
self.level = "Level 11"
elif self.xp >= "41000":
self.level = "Level 12"
elif self.xp >= "47500":
self.level = "Level 13"
elif self.xp >= "52500":
self.level = "Level 14"
elif self.xp >= "60000":
self.level = "Level 15"
elif self.xp >= "68000":
self.level = "Level 16"
elif self.xp >= "76500":
self.level = "Level 17"
elif self.xp >= "85500":
self.level = "Level 18"
elif self.xp >= "95000":
self.level = "Level 19"
elif self.xp >= "105000":
self.level = "Level 20"
elif self.xp >= "115000":
self.level = "MAX LEVEL"
else:
self.level = "Level Could Not Be Converted"
print(self.level)
答案 0 :(得分:1)
您可以将数组用于水平点
xp = 7600
limit = [0,1000,3000,5000,7500,10000]
level = -1
for points in limit:
if xp > points:
level = level + 1
else:
break
print(level)
答案 1 :(得分:1)
如果您愿意定期进行级别调整(例如每5000 xp持续执行一次),则可以使用简单快捷的方法:
# Defined at the top, easily modifiable this way
max_level = 21
xp_per_level = 5000
...
level_number = self.xp // xp_per_level
self.level = f"Level {level_number}" if level_number < max_level else "MAX LEVEL"
您可以将上述行为嵌套在类似于已经存在的Elif中,并使用此技术使前10个级别按每级别500 xp缩放,然后可能对下10个级别进行1000 xp的缩放,等等。