Saving game state

You can use Solana blockchain to save the state of your game in program accounts. These are accounts that are owned by your program and they are derived from the program ID and some seeds. These can be thought of as database entries. We can for example create a PlayerData account and use the players public key as a seed. This means every player can have one player account per wallet. These accounts can be up to 10Kb by default. If you need a bigger account look into how to handle big accounts This can be done in a program like this:

pub fn init_player(ctx: Context<InitPlayer>) -> Result<()> { = MAX_ENERGY; = MAX_HEALTH;
    ctx.accounts.player.last_login = Clock::get()?.unix_timestamp;
pub struct InitPlayer <'info> {
        payer = signer,
        space = 1000,
        seeds = [b"player".as_ref(), signer.key().as_ref()],
    pub player: Account<'info, PlayerData>,
    pub signer: Signer<'info>,
    pub system_program: Program<'info, System>,
pub struct PlayerData {
    pub name: String,
    pub level: u8,
    pub xp: u64,
    pub health: u64,
    pub log: u64,
    pub energy: u64,
    pub last_login: i64

You can then interact with this player data via transaction instructions. Lets say we want the player to get experience for killing a monster for example:

    pub fn kill_enemy(mut ctx: Context<KillEnemy>, enemyId: u8) -> Result<()> {
        let account = &mut ctx.accounts;
        ... handle energy
        if == 0 {
            return err!(ErrorCode::NotEnoughEnergy);
        ... get enemy values by id and calculate battle
        ctx.accounts.player.xp = ctx.accounts.player.xp + 1; = - 1;
        ... handle level up
        msg!("You killed enemy and got 1 xp. You have {} xp and {} energy left.", ctx.accounts.player.xp,;

This is how this would look like from a JavaScript client:

const wallet = useAnchorWallet();
const provider = new AnchorProvider(connection, wallet, {});
const program = new Program(IDL, PROGRAM_ID, provider);
const [pda] = PublicKey.findProgramAddressSync(
  [Buffer.from("player", "utf8"),
  new PublicKey(PROGRAM_ID)
try {
  const transaction = program.methods
      player: pda,
      signer: publicKey,
      systemProgram: SystemProgram.programId,
  const tx = await transaction;
  const txSig = await sendTransaction(tx, connection);
  await connection.confirmTransaction(txSig, "confirmed");

How to actually build this energy system you can learn here: Building an Energy system