Coding Python
Home›Python Coding Games›Code Defender

Python Space Shooter Game: Code Defender

Aliens are heading for the base. You do not get a joystick — you get a Python editor.

About this python space shooter game

Tutorials that build a space shooter in Pygame spend most of their time on sprites and event loops. Code Defender skips the plumbing and keeps the fun part: deciding what the ship does. Your program moves between lanes and fires; the game plays it back turn by turn. The waves get smarter — armoured cruisers, cargo ships you must not hit, and a final wave you can only beat with a while loop that reacts to what it sees.

How to play Code Defender

  1. Each wave lists the commands you can use, such as fire(), move_down() and scan().
  2. Write the program in the editor over the battlefield and press Run — or leave Auto on to run lines as you finish them.
  3. Every command is one turn. Aliens advance each turn, so wasted shots and moves matter.
  4. Clear the wave without losing the base; shorter programs score more stars.

All 6 levels — what each one teaches

#LevelPython ideaGoal
1First ContactCalling functionsDestroy both aliens before they reach the base.
2The Swarmfor loopsDestroy all 6 bugs in the swarm.
3Armored CruiserVariablesBreak through the cruiser's armor.
4Lane PatrolFunctionsSweep all 5 lanes from top to bottom.
5Friend or Foeif statementsDestroy every alien and let every cargo ship dock safely.
6The Final Wavewhile loopsSurvive the final wave of 10 aliens.

New to these ideas? Each level pairs with a free lesson: loops, variables, functions and conditions. When you finish, try Python Painter 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

Do I need Pygame to play?

No. The game runs real Python in the page, and your program only controls the ship — the drawing is done for you. If you want to build a shooter yourself afterwards, Pygame is the usual next step.

Is this like a Python Space Invaders game?

It borrows the idea of waves descending on a base, but you play it by programming: you decide the moves and shots in code before the wave starts, rather than reacting with the keyboard.

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