
Teaching a reinforcement-learning agent to play the real Temple Run — on a physical phone

I wanted to learn reinforcement learning by teaching an agent to play Temple Run. The hard part was getting enough practice at the speeds where it kept failing. I reverse-engineered the game, built a fast simulator, and trained a PPO policy there before testing it on my Android phone.
The median and best come from different evaluation sessions. The 14-run benchmark had a 4,000-frame limit; one run was still alive beyond 15 km when it was cut off.
My first approach was to build an endless runner with Claude Code, so I could train without depending on a phone. I spent too much time on graphics and still didn't have the mechanics I needed. Matching how the real game behaves turned out to be much harder than making something that looked like it.





The early Panda3D prototype: useful experiments, but too much work before training could begin.
I switched to reverse-engineering the Android APK. A Frida gadget reads the game's state from memory and calls its native movement methods. ADB forwards the connection to my computer. The agent receives compact observations about speed, position, obstacles and upcoming turns, rather than screenshots.
That let me control the game, but one phone still runs at real-time speed. The ARM64 game also made instrumentation on my x86_64 computer awkward. Scaling PPO training by adding phones wasn't practical.
Instead, I reproduced the relevant game logic in a headless Python simulator, then moved it to C++ for faster training. It models the observations, jump timing, turns, narrow paths and death rules. Each failure on the phone gives me a way to check where that approximation is wrong.
Temple Run accelerates as you survive: speed rises from 100 to 210 over roughly 4 km. Starting every episode at zero means a weak policy spends most of its time practising the slow opening, then dies before it can learn the fast sections.
Half of the episodes start at a random distance between 0 and 5,000 m, with the corresponding speed and late-game obstacles. That gives the agent practice at high speed without first having to survive several kilometres.
The 256 environments also mix ordinary tracks with targeted drills in every batch. Roughly half are normal play; the others make difficult situations appear much more often. Training one skill in isolation helped initially, but could make the policy forget another—especially turns.
| Share | Track | Practice |
|---|---|---|
| 45.5% | Normal game | All skills together |
| 12.7% | Obstacle-dense | 90% obstacle probability; repeated reactions |
| 10.9% | Crevasses | Level changes every 9 cells |
| 10.9% | Wide gaps | 90% wide-gap probability; long-jump timing |
| 5.5% | Stacked obstacles | Sequences such as narrow → jump → narrow |
| 5.5% | Narrow paths | Narrow strips available from the start |
| 9.1% | Hardcore mix | Dense obstacles, combos, gaps and level changes |
The released policy is the result of a long sequence of refinements. I started each stage from the previous best checkpoint, trained for 10 million steps, and tested it on the phone. The next stage targeted the weakness I could actually see.
Some fixes changed training: more chained jumps, a penalty for dangerous landing spots, or practice turning before a corner. Others changed the interface: buffering turns through the native swipe queue and delivering fresher observations. Timing the action correctly mattered as much as choosing it.
Stages use a small decreasing learning rate, fading exploration and the best checkpoint. The released Stable-Baselines3 PPO policy has two 512-unit hidden layers and a 67-value observation. Recent actions provide context for avoiding repeated commands.
Model and evaluation notesThe base reward measures forward distance and penalises death. Extra terms encourage staying on narrow strips, reacting to obstacles and turning early. They also discourage unnecessary actions and jumps that clear one obstacle only to land on the next.
At each decision step (10 per second), d is distance in metres and each indicator is 1 when its condition holds, otherwise 0. The action term is ρ.
p is progress through the current cell, v is game speed, and ℓ is lateral position. A jump spans σ cells; k identifies its landing cell (0–3). ℓ★ marks the side of the safe strip. Strip closeness c is set to 1 on its last cell. Turn aim α peaks when the predicted position after turning is centred in the cell.
Three stages of learning, running on the real phone.
The code and pretrained policy are available on GitHub. Run the baseline on your phone, train your own agent, and compare its best distance and median across device runs.
You'll need a non-rooted ARM64 Android phone, USB debugging, ADB and your own Temple Run APK. The repository includes the instrumentation tools; the game itself isn't included.