Coding Python
Home›Python Coding Games›Castle Builder

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

  1. Use build(kind) and train() to spend gold; gold() tells you what is left.
  2. On raid levels, scout() reports what is coming and next_day() moves time on.
  3. Watch the kingdom grow as the program runs — the castle itself gets bigger as you upgrade it.
  4. Meet the goal without running out of gold.

All 6 levels — what each one teaches

#LevelPython ideaGoal
1A New KingdomCalling functionsBuild a house and a tower, and train 1 knight.
2Village Rowfor loopsBuild 4 houses.
3Raise an ArmyVariables and //Spend the treasury on as many knights as it can pay for.
4The Master PlanFunctions with two inputsBuild 3 houses, 2 farms and 2 towers with build_many(kind, n).
5Goblin Seasonif statementsSurvive 6 days: a knight on raid days, a farm on calm days.
6The Grand Castlewhile loopsUpgrade 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.

Prefer to code on your phone? Keep practising on Android: the free Coding Python app is a full Python 3 editor and interpreter with 30 examples and 12 challenges.
Get the free app

More Python adventure games

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
Coding Python app icon