Skip to content

Instantly share code, notes, and snippets.

@SpoopyTim
Created August 12, 2021 22:20
Show Gist options
  • Save SpoopyTim/6f8bb3fe4a90b6e3678d2d4af099e697 to your computer and use it in GitHub Desktop.
Save SpoopyTim/6f8bb3fe4a90b6e3678d2d4af099e697 to your computer and use it in GitHub Desktop.
Levelling system based on arithmetic linear progression
import math
class Levels:
def __init__(self, xp_per_message, multiplier):
self.threshold = round(multiplier/5, 0)
# Multiplier would previously use large numbers
# so multiplying by 10 would make input numbers
# smaller by default, change this if you dont
# like that
self.multiplier = multiplier * 10
self.xp_per_message = xp_per_message
def current_level(self, xp):
"""The current level based on the xp amount"""
# 'math.floor' used to round levels down for obvious reasons
return math.floor((1+math.sqrt(1 + self.multiplier * xp / 50))/2)
def level_xp(self, level):
"""The xp required to pass the specified level"""
return ((level**self.threshold - level) * self.multiplier)/2
def messages_for_level(self, level):
"""The messages required for the specified level"""
l_xp = self.level_xp(level=level)
return l_xp / self.xp_per_message
# Configuration (can merge into init line, just useful for external config options)
xp_per_message = 10
multiplier = 15
# Initialize the class
levels = Levels(
xp_per_message=xp_per_message,
multiplier=multiplier
)
# Print out the first 4 levels along with their respective progression values
for level in range(1,5):
print(f"[Level {level}]")
print(f"- {levels.level_exp(level)} XP")
print(f"- {levels.messages_for_level(level)} messages")
print("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment