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

How to Make a Health Bar in Scratch for Your Game

Your player can take damage, but nobody can tell how close they are to losing until the game suddenly ends.

A laptop screen showing a colorful block coding editor next to a small game stage with a green bar in the top corner

Your player can take damage, but nobody can tell how close they are to losing until the game suddenly ends. A health bar in Scratch fixes that, and it is one of the first pieces of interface that makes a project feel like a real game instead of a demo.

This tutorial builds a working bar in about 30 minutes, using two approaches: a costume based bar that anyone can follow, and a pen drawn bar that resizes smoothly. You only need a game with a player sprite and something that hurts it. If you are not there yet, start with your first Scratch game, from idea to playable and come back.

Set up the health variable first

Every health bar is just a picture of a number. Get the number right and the picture is easy.

  1. Open the Variables category and click Make a Variable.
  2. Name it health and choose For all sprites, so the bar sprite can read it.
  3. Make a second variable called max health, also for all sprites.
  4. Uncheck the boxes beside both variables so the default readouts disappear from the stage.

In the stage's code area, or your player sprite, add:

  • when green flag clicked
  • set max health to 100
  • set health to (max health)

Using a separate max health variable feels like extra work now, but it means you can later add upgrades or difficulty levels by changing one number.

Make damage change the number

Inside your player sprite, find where it touches an enemy. A typical script:

  • forever
  • if touching (Enemy) then
  • change health by -10
  • wait 1 seconds

That one second wait is your invincibility window. Without it, the enemy drains 10 health every frame, which at 30 frames a second empties a 100 point bar in a third of a second.

Method one: a costume health bar in Scratch

This is the method I teach first because you can see every step.

  1. Create a new sprite called HealthBar and open the Costumes tab.
  2. Switch the editor to Bitmap or stay in Vector; both work. Draw a green rectangle about 100 pixels wide and 12 pixels tall.
  3. Duplicate the costume 10 times. In each copy, shorten the green rectangle by 10 pixels, so costume 1 is full and costume 11 is empty. Change the last three to orange, then red.
  4. Name them clearly, such as hp10, hp9 down to hp0. Clear names save you headaches later.

Now the code for the HealthBar sprite:

  • when green flag clicked
  • go to x: -170 y: 160
  • go to front layer
  • forever
  • switch costume to ( (11) - (round ((health) / (max health) * 10)) )

The math turns any health value into a costume number from 1 to 11. At full health it picks costume 1, at 50 it picks costume 6, at zero it picks costume 11. Because it uses max health, the same bar works if you later raise max health to 150.

The downside is visible steps. The bar jumps in chunks of 10 percent, which looks fine for retro games and a little rough for anything else.

Method two: a smooth bar with the Pen extension

For a bar that shrinks one pixel at a time, draw it with pen. Click the extension button at the bottom left and add Pen.

Make a new, empty sprite called PenBar. Then create a custom block from My Blocks, named draw bar, and tick Run without screen refresh. Inside it:

  1. erase all
  2. set pen size to 12
  3. set pen color to dark grey, then go to x: -220 y: 160, pen down, change x by 100, pen up. That is the empty background track.
  4. set pen color to green, then go to x: -220 y: 160, pen down, change x by ( (health) / (max health) * 100 ), pen up.

Then the main script: when green flag clicked, hide, forever, draw bar.

To change color as health drops, add an check using the less than block: if health is under 30 then set pen color to red before the second pen down. The pen line has rounded ends, so a bar at very low health looks like a dot rather than a sliver. That is usually fine. If it bothers you, stop drawing when health is under 2.

Clamp the value so the bar never breaks

Healing pickups can push health above max, and fast damage can push it below zero. Add this inside the player's forever loop:

  • if health is greater than max health then set health to (max health), built with the greater than operator block
  • if health is less than 1 then broadcast game over

Without the clamp, a pen bar at 130 health draws past its own frame, and a costume bar asks for costume number negative two.

Why you should not always show the number

A lot of beginner tutorials tell you to put the raw health number next to the bar, "so the player knows exactly where they stand." For most small Scratch games I would leave it off. Players read a bar at a glance during action; they read a number only when they stop. A number also invites them to do math about whether to risk the next jump, which slows a fast game down.

Show the number when it changes decisions: a role playing game with healing potions worth exactly 25, or a boss fight where players plan around it. For a platformer or a dodge game, a bar plus a short red flash on hit tells the player everything. Add the flash with change color effect by 50, wait 0.1 seconds, clear graphic effects in the player sprite whenever health drops.

Testing and polish

Run through this checklist before you share:

  • Does the bar reset to full when you press the green flag a second time?
  • Does it stay on top of every sprite, including ones created as clones?
  • Does taking damage at exactly 10 health trigger game over, not leave a sliver?
  • Does it look right in full screen mode?

Hand the game to a friend for two minutes and watch where their eyes go. If they never look at the bar, it is too small or in the wrong corner. Our guide to designing your first game level covers where players look first, and fixing bugs without losing your mind helps when the bar behaves strangely.

Frequently asked questions

How do you make a health bar go down in Scratch?

Store health in a variable, lower it with "change health by" when the player is hit, and have the bar sprite update from that variable inside a forever loop. With costumes, switch costume based on the health percentage. With pen, redraw the bar each frame at a length based on health.

How do you make a boss health bar in Scratch?

Use the same pen method with a separate variable such as boss health and a wider bar, often 300 pixels across the top or bottom of the stage. Show it only during the fight by broadcasting a message when the boss appears. Give the custom block an input so one block can draw both bars.

Why does my Scratch health bar flicker?

Flicker usually means the pen bar is being erased and redrawn across several screen refreshes. Tick "Run without screen refresh" on the custom block that draws it. For costume bars, check that two scripts are not switching costumes at the same time.

MK
Mira Kovac

Mira teaches coding to beginners and writes the step-by-step guides, always testing each one on someone who has never coded before.

More posts by Mira

More in Tutorials

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