Pokemon Mini On-Chain Game

Other Transitional
ecs

Transitional example: This example uses an older Cougr pattern and is preserved for compatibility reference. For the current recommended approach, see snake.

A Pokemon-style mini game implemented as a Soroban smart contract using the cougr-core ECS framework on the Stellar blockchain.

Overview

This example demonstrates how to build deterministic on-chain game logic with:

FeatureDescription
Tile Map8x8 grid with deterministic tile placement
MovementGrid-based with collision detection
EncountersDeterministic triggering on TallGrass tiles
BattleTurn-based 1v1 combat system

Why cougr-core?

AspectBenefit
ComponentTraitStandardized serialization for on-chain storage
Type SafetyUnique Symbol identifiers per component
Storage OptimizationTable vs Sparse storage strategies
Code ReusabilityProven ECS patterns

Prerequisites

ToolInstallation
Rustcurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
WASM Targetrustup target add wasm32v1-none
Stellar CLIbrew install stellar-cli (macOS)

Quick Start

Build

cd examples/pokemon_mini

# Development build
cargo build

# Build WASM contract
stellar contract build

Test

cargo test

Lint

cargo fmt --check
cargo clippy -- -D warnings

Contract Functions

Initialization

FunctionDescription
init_player()Create player at spawn (1,1) with starter creature

Movement

FunctionParametersReturnsDescription
move_playerdirection: u32u32Move player (0=blocked, 1=moved, 2=encounter)
get_tilex, y: i32u32Get tile type at position
get_map_size-(i32, i32)Returns (8, 8)

Direction Values: 0=Up, 1=Down, 2=Left, 3=Right

State Queries

FunctionReturnsDescription
get_player_state(x, y, moves, in_battle, hp)Player position and status
get_creature_stats(species, level, hp, max_hp, atk, def)Creature stats
get_battle_stats(wins, losses, escapes)Battle history

Combat

FunctionParametersReturnsDescription
get_battle_state-(in_battle, player_hp, enemy_hp, turn, result)Current battle
battle_actionaction: u32u32Execute action (0=invalid, 1=continue, 2=ended)

Action Values: 0=Attack, 1=Defend, 2=Run

Result Values: 0=None, 1=Win, 2=Lose, 3=Escaped


Map Layout

8x8 Grid:
+--------+
|WWWWWWWW|  W = Wall
|WS..WTTW|  S = Spawn
|W...WTTW|  T = TallGrass
|W..T.TTW|  . = Grass
|W......W|  ~ = Water
|WTTT.~~W|
|WTTT.~~W|
|WWWWWWWW|
+--------+

Game Mechanics

Encounter System (Deterministic)

Encounters trigger on TallGrass when: (x + y + move_count) % 5 == 0

Battle Damage Formula

damage = max(1, attacker_atk - defender_def)
  • Player always acts first
  • Defending adds +3 to defense for that turn
  • Winning heals creature fully

Deployed Contract (Testnet)

Contract ID: <CONTRACT_ID>

NetworkStatusExplorer
Stellar Testnet✅ LiveView on Stellar Lab

Deployment

Testnet

# Generate keypair
stellar keys generate --global alice --network testnet

# Fund account
# Visit: https://friendbot.stellar.org/?addr=<YOUR_ADDRESS>

# Deploy
stellar contract deploy \
  --wasm target/wasm32v1-none/release/pokemon_mini.wasm \
  --source alice \
  --network testnet

Playing

# Use the deployed contract
CONTRACT_ID="<CONTRACT_ID>"

# Initialize player
stellar contract invoke --id $CONTRACT_ID --source alice --network testnet -- init_player

# Get player state (returns: [x, y, moves, in_battle, hp])
stellar contract invoke --id $CONTRACT_ID --source alice --network testnet -- get_player_state

# Move right (direction: 0=Up, 1=Down, 2=Left, 3=Right)
stellar contract invoke --id $CONTRACT_ID --source alice --network testnet -- move_player --direction 3

# Attack in battle (action: 0=Attack, 1=Defend, 2=Run)
stellar contract invoke --id $CONTRACT_ID --source alice --network testnet -- battle_action --action 0

Project Structure

examples/pokemon_mini/
├── Cargo.toml          # Dependencies (cougr-core, soroban-sdk)
├── README.md           # This documentation
├── .gitignore          # Ignore rules
└── src/
    ├── lib.rs          # Contract entry points + tests
    ├── components.rs   # ECS components using ComponentTrait
    ├── systems.rs      # Game logic (map, movement, battle)
    └── simple_world.rs # Entity-component storage

References

ResourceLink
Soroban Docsdevelopers.stellar.org
Stellar CLICLI Documentation
Cougr Repositorygithub.com/salazarsebas/Cougr

License

Part of the Cougr project. See main repository for license information.