🎮 My First C++ Game â€ĸ Episode 6 of 7 â€ĸ See All Episodes
🎮 Episode 6 â€ĸ Big Project â€ĸ Tower Defence Part 1 of 2

Hold the Line!
Tower Defence Part 1

The two-part finale: a path-following enemy system, grid-snapped tower placement, gold economy, and range visualisation. Tower defence is systems programming, exactly what C++ is for.

🎮 Ages 12+ 🎮 ~2 Hours 🎮 C++ & SFML ✓ Free
🎮? Waypoint paths 🎮 Vector maths ? Tower placement 🎮 Gold economy 🎮 Range circles ? Mouse building
⭐
0 XP
Level 1
🎮0
Your Progress 0 / 6 steps
đŸ› ī¸
Build it in our C++ Studio too: lay out a level on a grid, tick the SFML features and download a real main.cpp plus a build file, right in the browser with no install. Open the C++ Mod Studio →
đŸ“Ļ
Reference project: download the finished code files used in this workshop: âŦ‡ cpp-tower-part1-workshop-reference.zip. Every code block also has a Copy button.
đŸ’ģ
How to run: Save, compile and run: g++ game.cpp -o game -lsfml-graphics -lsfml-window -lsfml-system && ./game
🎮
Heads up: SFML 3 renamed a few things. The install step gives you SFML 3 today, but the short snippets in the steps below are written in the classic SFML 2 style. The differences are small and mechanical: sf::VideoMode({800, 500})sf::VideoMode({800, 500}) (curly braces), the new event loop while (const std::optional e = window.pollEvent())while (const std::optional e = window.pollEvent()) with e->is<sf::Event::Closed>()e->is<sf::Event::Closed>(), sf::Keyboard::Key::Leftsf::Keyboard::Key::Left (add ::Key::Key), and setPosition({x, y})setPosition({x, y}) / move({dx, dy})move({dx, dy}) / setOrigin({x, y})setOrigin({x, y}) (curly braces). The ? complete finished code at the bottom of this page is full SFML 3 and compiles on a fresh install, use it as your reference.
🎮
Need the game setup? Each episode builds a new game on the same C++/SFML skeleton: the sf::RenderWindowsf::RenderWindow, the game loop and event polling. If you're starting here or need that boilerplate, grab it in one click: Episode 1 setup ? â€ĸ C++ Cheatsheet ?
🌐
How it works: write the code in each step, and the canvas on the right shows what your code does. It is a live preview that builds the game as you go. Type to see it update, or press Apply to Canvas to test it straight away.
đŸĻŠ
Lumo says: every step adds one feature to the same game. Type a little, click Apply to Canvas, and test it straight away. There is no wrong answer you can't undo.

Build Tower Defence Part 1 Step by Step

Each step adds one feature. Write the code, watch it come alive in the canvas, then make it your own.

Step 1 / 6
A path, towers and enemies
🎮🎮🎮🎮?
Workshop Complete!
Waypoints, vector maths, an economy and buildable towers, the foundation is rock solid. Part 2 arms the towers: bullets, waves and victory!
0
Total XP
1
Level
0
Best Streak
0%
Accuracy
🎮 Episode 7: Tower Defence Part 2 ?
⭐ View My Progress & Certificates

This workshop was free and took many hours to build. If it helped you learn something new, consider supporting the project.

☕ Support on Ko-fi

🧩 C++ Game Loop Challenge

Drag the SFML game loop steps into the correct order:

window.display()
window.draw(sprite) for each object
window.clear()
Update game state (positions, collisions)
Poll window events in a while loop
Create the RenderWindow