Python Strategy Game: Castle Builder
Gold, houses, farms, knights and goblin raids — a small kingdom-builder you run entirely from a Python script.
About this python strategy game
Strategy games are really resource puzzles, which makes them a good fit for code. In Castle Builder your program is the ruler: it spends gold on buildings, trains knights, checks the scouts' report each day and decides what to do. The levels teach budgeting with //, a reusable build_many(kind, n) function, day-by-day decisions with if, and a final while loop that upgrades the castle until it reaches level five.
How to play Castle Builder
- Use
build(kind)andtrain()to spend gold;gold()tells you what is left. - On raid levels,
scout()reports what is coming andnext_day()moves time on. - Watch the kingdom grow as the program runs — the castle itself gets bigger as you upgrade it.
- Meet the goal without running out of gold.
All 6 levels — what each one teaches
| # | Level | Python idea | Goal |
|---|---|---|---|
| 1 | A New Kingdom | Calling functions | Build a house and a tower, and train 1 knight. |
| 2 | Village Row | for loops | Build 4 houses. |
| 3 | Raise an Army | Variables and // | Spend the treasury on as many knights as it can pay for. |
| 4 | The Master Plan | Functions with two inputs | Build 3 houses, 2 farms and 2 towers with build_many(kind, n). |
| 5 | Goblin Season | if statements | Survive 6 days: a knight on raid days, a farm on calm days. |
| 6 | The Grand Castle | while loops | Upgrade the castle all the way to level 5. |
New to these ideas? Each level pairs with a free lesson: loops, variables, functions and conditions. When you finish, try Word Locks next.
More Python adventure games
- Python adventure game — Python Quest
- Python space shooter game — Code Defender
- Python turtle online — Python Painter
- Karel python — Robot Factory
- Python farming game — Python Farmer
- Python treasure hunt game — Treasure Hunter
- Python math game — Math Island
- Python rocket game — Space Explorer
- Python string game — Word Locks
Or browse all the Python coding games.
Frequently asked questions
Is Castle Builder a real-time strategy game?
No — it is turn-based. Your whole program runs, and the game plays it back day by day, so you can think as long as you like.
Can I write a strategy game in Python myself?
Yes. A turn-based kingdom game needs only variables, a loop and a few functions — exactly what Castle Builder practises.
Is it free, and do I need to sign up?
It is free with no account. The game runs in your browser and saves your progress in this browser only.
Does it run real Python?
Yes. Your code runs on CPython compiled to WebAssembly (Pyodide) inside the page. Errors are real Python errors, and an endless loop is stopped after a few seconds instead of freezing the tab.
Does it work on a phone?
Yes. On narrow screens the editor sits at the bottom of the game. For longer coding sessions on Android, the free Coding Python app has a full Python editor.
Take Python with you
Coding Python packs a real Python 3 compiler, 30 example programs, 112 quiz questions and 12 self-checking challenges into one free Android app.
Get it on Google Play