How to Create a Health Bar Animation in Scratch: Healthy vs. Junk Food Game Tutorial

Published By Kodex Academy — Learn. Build. Innovate.
How to Make a Health Bar in Scratch Healthy vs Junk Food Game Scratch Animation Kodex Academy

Introduction to Health Bar Animation in Scratch

Creating fun and engaging games in Scratch not only helps kids learn coding, but also encourages creativity and logical thinking. In this detailed Scratch game tutorial, you will learn how to make a Healthy vs Junk Food Game with a live health bar animation—just like real video games!

What You Will Learn:

This comprehensive guide teaches you how to:

  • ✔ Create a health bar animation in Scratch
  • ✔ Build a healthy vs junk food game
  • ✔ Program movement controls (up/down/left/right)
  • ✔ Detect collision between the player and food
  • ✔ Increase/decrease health based on food choices
  • ✔ Add custom sprites, variables, pen extension, and broadcast messages
  • ✔ Add enhancement features like game over screens, sound effects, speed boosters, and bonus fruits

This project is perfect for beginners and intermediate Scratch users who want to learn Scratch programming, Scratch animation, and how to make games in Scratch.

👉 Watch the full YouTube tutorial here:
How to Make a Health Bar in Scratch | Healthy vs Junk Food Game | Scratch Animation | Kodex Academy

Best Tablets to Make Scratch Even More Fun

Coding this health bar game is awesome on any device, but a tablet + stylus makes drawing custom food, backgrounds, and testing the Pen extension feel like real magic! These are the tablets parents recommend to buy for Scratch:

Disclaimer: This post contains Amazon affiliate links. We may earn a small commission at no extra cost to you.

Project Overview: What You’ll Build

In this game:

  • The player controls a cat sprite that moves around the screen.
  • There are two types of food:
    🥗 Healthy Food (Fruit Salad) → Increases Health
    🍟 Junk Food (Cheesy Puffs) → Decreases Health
  • A green health bar fills or shrinks based on the player’s eating choices.
  • When health becomes 0 or 100, the game ends.

This is an excellent starter project for learning live effects, animations, variables, and Scratch game logic.

Step-by-Step Guide to Coding – Health Bar Animation in Scratch

Step-by-Step Video Tutorial Link

1. Set Up Your Scratch Project

Before writing any code, we prepare our scene:

  • Open Scratch at scratch.mit.edu
  • Choose a bedroom, kitchen, or any background that fits your game theme
  • Keep the Cat sprite or replace it with any character you prefer

This step ensures your game starts with a clean, organized layout before you begin programming.

2. Program Player Movement with Arrow Keys

Add arrow key controls:

when green flag clicked
go to x: 0 y: -100

forever
    if <key [up arrow] pressed?> then
        change y by 10
    end
    if <key [down arrow] pressed?> then
        change y by -10
    end
    if <key [right arrow] pressed?> then
        change x by 10
        point in direction 90
    end
    if <key [left arrow] pressed?> then
        change x by -10
        point in direction -90
    end
end

Explanation:

  • when green flag clicked
    This starts the movement script whenever the game begins.
  • go to x: 0 y: -100
    Places the player sprite at the bottom center of the screen.
  • forever loop
    Continuously checks for key presses so movement feels smooth.
  • if key up arrow pressed → change y by 10
    Moves the player upward.
  • if key down arrow pressed → change y by -10
    Moves downward.
  • if key right arrow pressed → change x by 10
    Moves right AND the sprite faces right using point in direction 90.
  • if key left arrow pressed → change x by -10
    Moves left AND faces left with point in direction -90.

This creates smooth 4-direction movement like typical game controls.

Fix upside-down flipping:

set rotation style [left-right v]

By default, Scratch spins sprites in full rotation.
This command forces the sprite to rotate only left and right, preventing upside-down movement.

Add walking animation:

forever
    next costume
    wait 0.1 seconds
end
  • next costume switches to the next image within the sprite.
  • The wait 0.1 seconds makes the animation smooth.
  • This loop continuously plays a walking animation while the character moves.

3. Add Healthy and Junk Food Sprites

Choose sprites:

  • ✔ Fruit Salad → Healthy Food
  • ✔ Cheesy Puffs → Unhealthy Food

Place them on opposite sides of the screen.

4. Create the Health Score Variable

Go to:

  • Variables → Make a variable → “health score”

Set initial value:

when green flag clicked
set [health score v] to 100

5. Increase Health on Healthy Food Collision

forever
    if <touching [fruit salad]> then
        change [health score v] by 1
        wait 0.5 seconds
    end
end
  • Checks if the Cat sprite touches the healthy food.
  • If yes → increases health by +1
  • The wait 0.5 seconds prevents score from increasing too fast.

This simulates the player eating healthy food and becoming stronger.

6. Decrease Health on Junk Food Collision

forever
    if <touching [cheesy puffs]> then
        change [health score v] by -1
        wait 0.5 seconds
    end
end
  • Detects if the Cat is touching junk food.
  • Reduces health by 1 point.
  • The delay keeps the health bar from instantly dropping to zero.

This creates the game’s challenge mechanic.

7. Initialize Health Score at Game Start

when green flag clicked
set [health score v] to 100
  • At the start of the game, health is set to 100%.
  • Prevents previous game data from carrying over.

8. Build the Visual Health Bar with Pen Extension

You need two custom-painted sprites:

1. Health Bar Outline (Rectangle)

  • Draw a rectangle with only outline.

2. Green Fill Sprite (the actual growing bar)

  • Draw a filled green rectangle.
  • Place the fill sprite inside the outline.

Use the Pen Extension for Live Health Bar Drawing

Add the Pen Extension:

  • Extensions → Pen

9. Define the Update Health Custom Block

  • Go to My Blocks → Make a block → “update health”
  • Check ✔ Run without screen refresh

Code for health bar (fill animation):

define update health
erase all
go to x: -51 y: 140

repeat ((health score / 100) * 169)
    stamp
    change x by 1
end

This is the core engine of the health bar.

Line-by-line breakdown:

  • define update health
    Creates a reusable block that updates the green bar.
  • erase all
    Removes old drawings from previous health updates.
  • go to x: -51 y: 140
    Sets the starting point of the green bar.
  • repeat ((health score / 100) * 169)
    Converts health score into pixel width:
    • 100 health → full bar
    • 50 health → half bar
    • 0 health → empty bar
  • stamp
    Draws one small piece of the bar.
  • change x by 1
    Moves a tiny step to extend the bar horizontally.

This creates a smooth, dynamic health bar that grows or shrinks.

10. Broadcast Health Changes to the Bar

From the player sprite: (Sender Code)

broadcast [health v]

From the bar sprite: (Receiver Code:)

when I receive [health v]
update health
  • The Cat sprite triggers “health update” whenever food is eaten.
  • The Health Bar sprite listens for this broadcast and redraws the bar.
  • This keeps the bar perfectly synced with the health score.

11. Add Game Over Logic

Stop health from going beyond limits:

if <(health score) < 1> then
    erase all
    stop all
end

if <(health score) > 100> then
    erase all
    stop all
end
  • If health drops below 1 → game ends.
  • If health becomes greater than 100 → also end (optional safety rule).
  • stop all freezes the entire game.
  • erase all clears the health bar from the screen.

12. Include Background Music

Choose a loop sound from the Scratch library.

when green flag clicked
forever
    play sound [dance chill out v] until done
end
  • Plays background music continuously.
  • until done ensures the track is not spammed.

Headphones & Speakers for Scratch Game Testing

Nothing beats hearing your sound effects and background music while testing the game! These kid-safe, volume-limited headphones and speakers are the exact ones Scratch parents grab so their coder can play (and debug) without disturbing the whole house.

Optional Enhancements: Level Up Your Game

These features make your Scratch game even more exciting.

1. Add a Speed Booster for Bonus Healthy Food

Add a new sprite: Carrot / Apple / Juice

when green flag clicked
forever
    if <touching [carrot]> then
        change [health score v] by 3
        change [speed v] by 2
        wait 1 second
        set [speed v] to 10
    end
end
  • Adds bonus food that increases both health and movement speed.
  • Speed boost lasts for 1 second before resetting.
  • Great for making gameplay more dynamic.

2. Implement a Game Over Screen

Create a new backdrop with GAME OVER text.

when health score < 1
switch backdrop to [Game Over]
stop all
  • When health reaches 0 → change the background to a Game Over screen.
  • Stops the entire game.

3. Make Food Move and Respawn

when green flag clicked
forever
    change y by 3
    if <touching edge> then
        set y to -150
    end
end
  • Makes food move automatically toward the player.
  • When it hits the top of the screen, it respawns at the bottom.

Adds difficulty and excitement.

4. Add a Timer

when green flag clicked
set [timer v] to 60
forever
    wait 1 second
    change [timer v] by -1
end
  • Counts down from 60 seconds.
  • Perfect for time challenge levels.

5. Add Sound Effects for Eating Food

if <touching [fruit salad]> then
    play sound [pop v]
end
  • Plays a satisfying sound whenever the player eats food.
  • Improves user experience through audio feedback.

Summary of Steps: Healthy vs. Junk Food Game Tutorial

How to Create a Health Bar Animation in Scratch: Healthy vs. Junk Food Game Tutorial

A step-by-step guide to building a simple Scratch game where a player sprite moves with arrow keys, collects healthy food to increase a health score, and junk food to decrease it. The health score drives a live-drawn health bar using the Pen extension, with optional enhancements such as speed boosters, game-over screens, moving food, timers, and sound effects.

1. Start a New Scratch Project

Open Scratch, choose or draw a backdrop, keep or replace the default cat sprite as your player.

2. Code Player Movement & Walking Animation

When green flag clicked → go to x:0 y:-100 → set rotation style [left-right] → forever: move with arrow keys (change x/y by 10), point in direction 90/-90, next costume, wait 0.1 secs.

3. Add Healthy & Junk Food Sprites

Add two sprites (e.g., Fruit Salad = healthy, Cheesy Puffs = junk).
Position them anywhere on the stage.

4. Create and Initialize the Health Score Variable

Make a variable “health score” (for all sprites) → when green flag clicked → set [health score v] to [100].

5. Program Food Collisions (Increase/Decrease Health)

In player sprite → forever:
if touching healthy food → change [health score v] by (1), wait 0.5 secs;
if touching junk food → change [health score v] by (-1), wait 0.5 secs → broadcast [health v] after each change.

6. Draw the Health Bar Outline & Fill Sprites

Create two sprites: one empty rectangle outline and one solid green fill.
Position the fill inside the outline at top-left (e.g., x:-200 y:140). Add the Pen extension.

7. Create the “update health” Custom Block

In the fill sprite → My Blocks → Make a block “update health” (run without screen refresh) → erase all → go to x:-51 y:140 → repeat ((health score / 100) * 169) { stamp → change x by 1 }.

8. Make the Health Bar React to Broadcasts

In the fill sprite → when I receive [health v] → update health. Also call update health once at game start.

Add Game Over Logic

In player or stage → forever: if <(health score) ≤ [0]> or <(health score) > [100]> then erase all → switch backdrop to [Game Over] → stop [all v].

9. Add Background Music & Final Polish

Add a looping sound → when green flag clicked → forever play sound until done. (Optional: moving food, speed boost, custom sounds, etc.)

Conclusion: Your Scratch Game Is Ready!

Great job! You’ve successfully built a complete Healthy vs Junk Food Scratch Game with a fully working health bar system, character movement, scoring logic, sound effects, and interactive gameplay. Along the way, you learned essential Scratch concepts like variables, sensing, loops, custom blocks, and the Pen Extension.

This project not only teaches how to make a fun game but also shows how health bars, life meters, and progress indicators work in real games. With these skills, you can now add more features such as levels, moving food items, power-ups, animations, or even create your own unique games.

Keep experimenting and exploring—your game development journey has just begun!

FAQ: Frequently Asked Questions

QuestionAnswer
Do I need any prior Scratch experience?No! It starts from a blank project and explains every block step-by-step.
Which Scratch version should I use?Scratch 3.0 (online at scratch.mit.edu or the latest desktop app). The Pen extension is built-in.
Why isn’t my health bar updating?Check that the cat broadcasts “health” and the bar sprite has “when I receive [health] → update health”. Make sure “health score” variable is “For all sprites”.
My cat flips upside down when moving!Add set [rotation style v] to [left-right v] in the cat’s “when green flag clicked” script.
Can I use different food sprites?Yes! Replace Fruit Salad or Cheesy Puffs—copy the collision and broadcast code to new sprites.
Health changes too fast when touching foodAdd wait (0.5) secs after each change [health score v] by block for cooldown.
How does the Pen extension draw the health bar?In update health block: pen down, repeat (length) with stamp, pen up. Length = (health score / 100) * 169.
How do I add a Game Over screen?Create “Game Over” backdrop, then switch backdrop to [Game Over v] + stop [all v] when health ≤ 0.
Where can I download the finished project?Use the “Download Project (.sb3)” button at top/bottom—free!
Custom block “update health” not working?Define it in bar sprite (Step 9), ensure it uses correct variable (health score), and broadcast matches exactly.
Food not respawning or moving?In food sprite: when I start as a clonego to random position + forever loop with glide or change x by. Delete clones after eat.
Collision detection not triggering?Use if <touching [Cat v]?> inside forever loop on food sprites. Ensure sprites aren’t too small—resize if needed.
Health goes above 100 or below 0?Add clamps: set [health score v] to (pick random (1) to (100)) or use if <(health score) > [100]?> then set [health score v] to [100].
How to add sound effects for eating?In collision script: play sound [pop v] until done after change [health score v]. Import custom sounds via library.
Pen bar not erasing properly?Before drawing: erase all, pen up, go to [-200 -100], set pen color to [#00FF00], then pen down.

Call to Action

  1. Don’t forget to check out the full video tutorial: How to Make a Health Bar in Scratch | Healthy vs Junk Food Game | Scratch Animation | Kodex Academy
  2. Like, comment & share the video
  3. Visit kodexacademy.com
  4. subscribe to the Kodex Academy YouTube channel for deeper Scratch content.

Happy coding with Kodex Academy! 🚀

Next Steps: Learn More with Kodex Academy

At Kodex Academy, we’re passionate about helping students learn coding in creative ways. This project teaches more than Scratch—it empowers young minds to build tools that work in the real world.

Explore more:

Stay updated with new content, free tutorials, and coding challenges!

Further Reading & Links

Recent Posts

How to Create a Health Bar Animation in Scratch: Healthy vs. Junk Food Game Tutorial

How to Make a Health Bar in Scratch | Healthy vs Junk Food Game | Scratch Animation | Kodex AcademyCreating fun and engaging games in Scratch not only helps kids learn coding, but also encourages...

How to Create a Basketball Game in Scratch: Full Step-by-Step Tutorial (Moving Hoop, Jumping Ball & Scoring)

Are you ready to create an exciting basketball game in Scratch with a moving hoop, jumping player, and real-time scoring? This step-by-step Scratch game tutorial is perfect for beginners who want to...

How to Make 3D Shapes in Scratch – Draw Cubes, Pyramids & Cylinders Using Pen Extension

If you’ve ever wondered how to make 3D shapes in Scratch or create 3D geometry using code, you’re about to dive into a creative world of math, animation, and programming fun. Learn how to make...

How to Make Flappy Bird Game in Scratch | Coin Collection Game in Scratch | Scratch Coding for Beginners

Have you ever wondered how people create fun games like Flappy Bird without writing a single line of code? With Scratch programming, anyone — from complete beginners to young creators — can build...

How to Make Day & Night Animation in Scratch (Step-By-Step Full Tutorial)

If you’ve ever wondered how to make day and night animation in Scratch or wanted to bring your stories and games to life with realistic sky transitions, this tutorial is perfect for you! Scratch is...

How to Make a Shooting Game in Scratch | Jet Shooting Game Tutorial (Step-By-Step Guide)

Introduction - Jet Shooting Game in Scratch Scratch Tutorial Game | Scratch Game Tutorial Easy | Scratch Programming Games | Jet Shooting Game in ScratchWant to build your first arcade-style...

Top 5 Animations in Scratch: Jump, Bounce & Fly (Beginner Tutorial + Code)

In this step-by-step guide, we explore the Top 5 animations in Scratch games that will make your projects smoother, interactive, and fun to play. You’ll learn: ✅ How to make a sprite jump ✅ How to...

How to Make a Tic-Tac-Toe Game in Scratch – Easy Scratch Tutorial for Beginners

We are going to build the all-time favourite logic game in Scratch: Tic‐Tac‐Toe. In this game two players take turns making X and O on a 3×3 grid. The first one to get three in a row — across, down or...

How to Make a Real-Time Wall Clock in Scratch | Step-by-Step Scratch Tutorial

If you’ve ever wondered how to make a real-time wall clock in Scratch, you’re in the right place! In this step-by-step Scratch tutorial, we’ll show you how to build a fully functional analog clock...

How to Make a 3-Level Platformer Game in Scratch | Mario-Style Hen Adventure Game

Have you ever wanted to build your own Mario-style platformer game in Scratch? This step-by-step guide will walk you through how to make a 3-level platformer game in Scratch — featuring a jumping hen...

How to Make a Math Racing Game in Scratch | Game Concepts and Complete Tutorial

In this tutorial, you’ll learn to build a Math Racing Game in Scratch. Players solve math problems to move their character forward; wrong answers benefit the opponent. It’s a race of speed, accuracy...

How to make Memory Skill Game in Scratch | Card Matching Game in Scratch – Part 2 | Step-by-Step Coding

In this tutorial you'll learn how to make memory skill game in Scratch / card matching game in Scratch. This is a great beginner‑to‑intermediate project for scratch tutorial game, scratch programming...

How to make a Card Matching Game in Scratch | Memory Skill Game in Scratch – Part 1 | Step-by-Step Coding

In this Scratch tutorial, we'll walk you through how to make a card matching game in Scratch, also known as a memory game or skill game. This is a popular beginner project that introduces essential...

Create a Quiz Game in Scratch | Spelling Test in Scratch | Picture Identification in Scratch

Want to make learning spelling fun, visual, and interactive? In this Scratch tutorial, you'll learn how to make a spelling quiz game in Scratch using picture identification, text-to-speech, and...

How to make a Double Jump Game in Scratch | Platformer game in Scratch | Step by Step Coding

How to make a Double Jump Game in Scratch. Scratch is a fantastic platform for beginners to learn programming by making games, animations, and interactive stories. Among the many kinds of games...

How to Use Variables in Scratch | Variable Blocks in Scratch | Complete Tutorial

Introduction: Variable Blocks in Scratch Whether you’re just getting started with Scratch programming or looking to take your projects to the next level, understanding variables and lists is...

How to Make Earth Revolve Around the Sun in Scratch: A Complete Tutorial & Enhancements

Animating Earth revolving around the Sun is a classic beginner/intermediate Scratch animation project. It combines trigonometry (sine & cosine), variables, loops, and visual scripting. Kids can learn...

How to Make a Game in Scratch | Snake Game in Scratch | Step-by-Step Game Coding

In this tutorial, we’ll build a Snake Grid style game in Scratch step by step (very similar to the Kodex Academy example). By doing this, you’ll cover many of the core Scratch building blocks. We will...
Scroll to Top