Sub topic 4
Experience Points and Levels
Prerequisites and Working Model
This chapter assumes you understand basic game analysis, can read short Python programs, and know elementary algebra, including exponents and sums.
Experience points (XP) are a numeric record of progress. A player earns XP from actions such as completing quests, defeating enemies, or discovering locations. A level is a named milestone reached after accumulating enough XP. The level can unlock abilities, increase attributes, or provide access to later content.
Keep two values separate:
- XP earned is the total progress recorded for the player.
- XP required is the threshold for reaching a particular level.
For example, a game might use these thresholds:
| Level | Total XP needed |
|---|---|
| 1 | 0 |
| 2 | 100 |
| 3 | 215 |
| 4 | 347 |
The increase is not constant. Each new level requires 15% more XP than the previous level. This makes early progress quick while extending the time needed for later levels.
Building an Exponential Curve
Let base be the XP needed to move from level 1 to level 2. Let growth be the multiplier applied at each level. The XP needed for the transition from level L to L + 1 is:
base × growth^(L - 1)
The total XP for a target level is the sum of all earlier transitions. A player reaches level 4 only after paying the costs for transitions 1→2, 2→3, and 3→4.
def xp_to_next(level, base=100, growth=1.15):
"""XP needed to go from level to level + 1."""
return round(base * growth ** (level - 1))
def total_xp(level, base=100, growth=1.15):
"""Cumulative XP needed to reach level from level 1."""
return sum(xp_to_next(l, base, growth) for l in range(1, level))
def level_from_xp(xp, base=100, growth=1.15):
level = 1
while xp >= xp_to_next(level, base, growth):
xp -= xp_to_next(level, base, growth)
level += 1
return level
print(xp_to_next(1))
print(xp_to_next(10))
print(total_xp(10))
print(level_from_xp(500))This example treats xp in level_from_xp as unspent XP toward future levels. If your game stores lifetime XP instead, compare it with cumulative thresholds instead. Mixing these two representations is a common source of incorrect level displays.
Choosing Curve Behavior
A curve controls pacing, not just arithmetic. A linear curve gives predictable progress and makes future goals easy to estimate. An exponential curve creates increasingly distant milestones and can support long-running games. A piecewise curve uses different rules for different ranges, such as a gentle tutorial curve followed by a slower endgame curve.
Use a curve that matches the amount of content available. If a player can reach level 20 but the game has meaningful rewards only through level 12, the remaining levels feel empty. If levels arrive too quickly, rewards lose significance.
Level-Up Flow
The runtime flow is simple: add XP, check thresholds, then grant every level crossed. Granting levels in a loop matters when one large reward advances the player several levels at once.
Exercise
Exercise
Why should a game process multiple level-ups in a loop after awarding XP?
Show answer
One quest or reward can cross several thresholds, and the player should receive every level and its corresponding reward.