Fox Studios logo Fox StudiosGame projects, devlogs and Scratch builds
Projects

How We Built a Tower Defense Game in Scratch in Ten Days

The first playable version of our tower defense game ran at about nine frames a second once wave four started, and a tester named Priya left a comment on the feedback thread that said, politely, that she thought her computer had frozen.

Top down tower defense map with turrets placed along a winding path

The first playable version of our tower defense game ran at about nine frames a second once wave four started, and a tester named Priya left a comment on the feedback thread that said, politely, that she thought her computer had frozen. She was playing on a school Chromebook. We had tested on a laptop with a decent graphics chip and never saw the problem, which is the oldest mistake in this studio's history and apparently one we needed to make again.

Ten days later it held 29 frames a second on the same Chromebook with 60 enemies on screen. Nothing about that fix was clever. It came from deleting things, from moving work out of forever loops, and from one structural decision about how enemies walk that I would make differently if we started over. Here is the whole build, including the parts that did not work.

The path problem, and why we stopped using motion blocks

A tower defense game needs enemies that follow a track. The obvious approach in Scratch is to give the enemy sprite a sequence of glide blocks, one per corner, and most tutorials do exactly that. We did too, for about a day.

It breaks the moment anything else needs to happen. A glide block owns the sprite until it finishes, so an enemy mid glide cannot be slowed by a frost tower, cannot be knocked back, and cannot die cleanly in the middle of a segment. You end up fighting your own movement code.

What we switched to was waypoints in two lists. The list waypointX holds 9 numbers, waypointY holds the matching 9, and each enemy clone carries a private variable called leg that says which segment it is on. Every frame, the clone points toward waypoint number leg, moves forward by its own speed variable, and checks whether the distance to that waypoint is under 6. If it is, leg goes up by one. When leg passes 9 the clone has reached the exit, the player loses a life, and the clone deletes itself.

That is maybe fourteen blocks total and it solved four problems at once. Speed became a variable we could multiply by 0.45 for a frost effect. Knockback became leg stays the same, move minus 20 steps. And because movement happens one step per frame rather than inside a glide, enemies die exactly where they were hit, which matters for the little coin drop animation.

Clone limits shaped the whole design

Scratch caps you at 300 clones across the project. That sounds generous until you count honestly. Enemies, projectiles, coin pops, explosion puffs and range indicators all want to be clones.

Our first version spawned one bullet clone per shot per tower. Eight towers firing twice a second is sixteen clones a second, and at a travel time of half a second that is a steady eight live bullets, plus more during a swarm. Combined with 60 enemies we hit the cap during wave six and towers simply stopped shooting with no error message at all. The game just quietly stopped working, which is the worst kind of bug for a beginner to debug.

The fix was deciding that bullets do not need to exist. A tower picks a target, draws a line to it with the pen extension for exactly two frames, and applies damage immediately. No travel time, no clone, no collision check. Three testers told us it felt better, and one said it looked like a laser, which we then leaned into for the whole art style. The only tower that still spawns a real projectile is the mortar, because an arcing shell landing late is the entire point of that tower.

What the waves actually look like

Wave design took longer than the code. We rewrote the numbers five times, and the version people stopped complaining about is below. Health values assume a base tower does 10 damage per shot.

WaveEnemiesHealth eachSpeedCoins on clear
18 walkers30245
212 walkers30255
310 walkers, 3 runners30 and 202 and 4.570
518 walkers, 1 tank35 and 2402 and 1.2120
824 mixed, 2 tanks45 and 300varies190
1240 mixed, 4 tanks, 1 boss60 to 900varies400

The number that made the game fun was not in that table. It was the 12 second gap between waves. At 6 seconds nobody had time to place a tower and the game felt unfair. At 20 seconds people got bored and clicked the next wave button early, which told us to add that button on purpose and give a 15 percent coin bonus for calling a wave early. That single addition came out of the feedback thread and is now the thing regular players say they like most.

The advice I would push back on

Beginner guides almost always tell you to build the enemy, then the tower, then the waves, then the economy. Build the thing you can see, then add systems on top. It is sensible and it is how we started, and it cost us four days.

The economy is the game. Tower damage, enemy health, coin drops and tower prices are one single system, and tuning any of them changes all the others. We built a beautiful tower upgrade tree with three tiers before we knew what a coin was worth, and then had to throw away two of the three tiers because upgrading was never the right choice compared to placing a second cheap tower.

What I would do instead: get one enemy type walking the path, one tower shooting it, and then spend a whole session with nothing but numbers. Set a tower at 50 coins, a wave clear at 45, and an enemy at 30 health, then play it and write down the moment you feel rich. If you never feel rich, the prices are wrong, and no amount of art fixes that. This is the same reason we keep saying that your first game should be tiny, since small projects let you find the broken number in an hour instead of a week.

The performance fixes, in order of how much they helped

Four changes took us from nine frames to twenty nine, and they were not equally valuable. Moving the enemy movement logic into one custom block with run without screen refresh turned off, but batching all clones through a single broadcast per frame, gave the biggest single gain. Second was deleting the range indicator clones and drawing those circles with the pen only while a tower is selected. Third was cutting our enemy sprite from six costumes to two, because Scratch reloads costume bitmaps more often than you would expect on low end machines. Fourth, and smallest, was replacing a touching colour check with a distance to comparison, which is both faster and far more predictable.

One thing that did not help at all: turbo mode. We tried it early as a shortcut and it made the game unplayably fast rather than smoother, which is worth knowing before you waste an evening on it. The clone handling patterns we worked out here came straight from the snake build, and the rest of our finished builds live in the projects section.

FAQ

How long does a tower defense game take to build in Scratch?

Ours took ten days of evenings, roughly 22 hours, and about six of those hours were wave tuning rather than code. A stripped version with one tower and one enemy type is a weekend.

Can Scratch handle 60 enemies at once?

Yes, if each enemy runs one short movement routine per frame and nothing else. The trouble comes from extra clones for bullets and effects, and from costume switching, not from the enemy count itself.

How do you store tower placements?

Three parallel lists holding x, y and tower type, with one entry per placed tower. Saving the game then means saving three lists, which is far easier than trying to read positions back off clones.

Do you need the pen extension?

Not strictly, but it removes a lot of clones. Drawing the shot line and the range circle with pen instead of sprites is what kept us under the clone cap.

DF
Diego Fenn

Diego covers finished builds and the community around them, and runs the feedback threads that shape what the studio makes next.

More posts by Diego

More in Projects

01doorganico 084goad 3hot8 4465456 after5internprogram ahandfulofblessings ahmadpc ahomcybersecurity allodialresources annodex antiquesandperiodcollectibles aotc asianworldkashmir beedlectrics bloggertemplatefree bluefeatheragency bodyweightsolution campaign08blog carfixadvisor catstreetcollective cbstvsports cnfanspurchasing committeeof500yearsofdignityandresistance covertopsdjs cs-kaiyuangame cuestacomunicaciontotal cxoutlet dancebetgame deskgearhub destinyvillastay detectmagazine di33 diamondsteelbuildings donmo dyanadropship emarsysfinance emimattl escapechan eskitarih evautoplay everythingweddings-orlando farmzone feedviewerapp gamevui123 gleasongrade greetingsday hacklabasu haskell iw-kaiyun jagoanproperti jilidskripsi joltbusinessfinance juegodepokergratis juegospucca kaeru-festival latestmehndidesigns lestanzediottaviano litemagazines localcommunityhero lustrsazan mariashebets menswheels newwebstechs1 nikkit ootheday oxigenvpn plurfitness prefetial probudgetcarpetcleaning psychicmrsmartin pyaten qvantor rejoignezlogis rexzbot riselayouts ruchin rwxqsgd5 sakaltime shibiaowang shop-bot skrtips spotlesshaven strcoding tetapatrium topselller uneplanteartificielle vipertechcr webtech1223411 wo-kygaming yamaha-mekar yeknava yoybuysheets zenunwa yamaha-mekar jilidskripsi ankertoto ankertoto ankertoto