Oh, you are kidding. You already know what energy is. But, just to clarify, energy is a concept that is consumable and recoverable in social games. It limits how far players can advance in each session.
Players use energy to perform actions such as farming, housing, or social interactions. Then consumed energy will be recovered after certain amount of time designed by the developer. Recovery is the essence of energy system. It will make players to come back to the game periodically.
Popular social games such as FarmVille , Zoo Invasion or The Sims Social are benefited from the system in high retention rate.
Install via PyPI first:
$ easy_install energy
Or check out developement version:
$ git clone git://github.com/sublee/energy.git
What you need to implement energy system is only Energy object. Maximum energy and recovery interval have to be set in seconds before use:
from energy import Energy
energy = Energy(max=10, recovery_interval=300)
The example Energy object has 10 of maximum and will recover in every 5 minutes. When a player performs a action that requires energy just call Energy.use() method:
>>> print energy
<Energy 10/10>
>>> energy.use()
>>> print energy
<Energy 9/10 recover in 05:00>
If the player has not enough energy, it throws ValueError:
>>> print energy
<Energy 9/10 recover in 04:12>
>>> energy.use(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "energy.py", line 104, in use
raise ValueError('Not enough energy')
ValueError: Not enough energy
You may want to save Energy object within a specific player’s data in database.
An Energy object is serializable by Pickle. If you have a key-value storage, you can save an Energy object with a player easily. Or, you should prepare some columns for Energy.used and Energy.used_at in your database to save them. Here’s an example of save/load an Energy object:
>>> MAX_ENERGY, ENERGY_RECOVERY_INTERVAL = 10, 300
>>> energy = Energy(MAX_ENERGY, ENERGY_RECOVERY_INTERVAL)
>>> saved_used, saved_used_at = energy.used, energy.used_at
>>> loaded_energy = Energy(MAX_ENERGY, ENERGY_RECOVERY_INTERVAL,
... used=saved_used, used_at=saved_used_at)
>>> loaded_energy == energy
True
A consumable and recoverable stuff in social gamers. Think over reasonable energy parameters for your own game. Energy may decide return period of your players.
Parameters: |
|
---|
Updates max or recovery_interval.
Parameters: |
|
---|
Calculates the current energy. This equivalents to casting to int but can work with specified time.
>>> energy = Energy(10, 300)
>>> energy.use()
>>> energy.current()
9
>>> int(energy)
9
Parameters: | time – the time when checking the energy. Defaults to the present time in UTC. |
---|
The maximum energy.
Calculates the seconds passed from using the energy first.
Parameters: | time – the time when checking the energy. Defaults to the present time in UTC. |
---|
Calculates seconds to the next energy recovery. If the energy is full, this returns None.
Parameters: | time – the time when checking the energy. Defaults to the present time in UTC. |
---|
Calculates the recovered energy from the player used energy first.
Parameters: | time – the time when checking the energy. Defaults to the present time in UTC. |
---|
The interval in seconds to recover energy.
The quantity of once energy recovery.
Makes the energy to be full. Most social games reset energy when the player reaches higher level.
Parameters: | time – the time when setting the energy. Defaults to the present time in UTC. |
---|
Sets the energy to the fixed quantity.
>>> energy = Energy(10, 300)
>>> print energy
<Energy 10/10>
>>> energy.set(3)
>>> print energy
<Energy 3/10 recover in 05:00>
You can also set over the maximum when give bonus energy.
>>> energy.set(15)
>>> print energy
<Energy 15/10>
Parameters: |
|
---|
Consumes the energy.
Parameters: |
|
---|
Quantity of used energy.
A time when using the energy first.
This project licensed with BSD. See LICENSE for the details.
I’m Heungsub Lee, a game developer. Any regarding questions or patches will be welcomed.