How to Make Your Scratch Code Smarter and Faster

How to Make Your Scratch Code Smarter and Faster

Stop Redrawing Shapes in Scratch

If you’ve ever used a forever block in Scratch to draw a shape or pattern, you might not realize it, but your project is probably doing a lot more work than it needs to.

Many Scratch tutorials, including some of my earlier ones, show how to keep drawing a shape inside a forver loop to keep it visible on the screen. That’s a common approach, and it seems to work fine at first.

But here’s the catch: using forever like this causes Scratch to draw the same exact shape hundreds of times per second, even when nothing has changed.

No one usually notices because it looks correct, but behind the scenes, this repeated drawing can slow down your project and make it less efficient, especially if your shapes or patterns get complex.

Why Is This a Problem?

Think of it like this: if you want to draw a shape on paper, you draw it once. If you want to change something, ike its color, size, or even move it to a different spot, you erase it and redraw the new shape. But if you don’t want to change anything, there’s no need to keep drawing the same shape over and over again.

But using a forever loop in Scratch to draw a shape causes Scratch to draw that same shape hundreds of times per second, essentially redrawing it on top of itself even when nothing has changed. This wastes effort and can slow down your project.

The Goal: Draw Only When Needed

What we really want is for Scratch to redraw the shape only when one of its properties changes, like the number of sides, the length of each side, or the color.

If none of those settings change, there’s no reason to redraw the shape. The program can just leave it as it is and save all the extra work.

How to Fix It in Scratch

The solution is simple and clever:

  1. Combine the important shape settings (number of sides, length, color) into one single text string. We call this the current state.

  2. Remember the last drawn settings in a previous variable.

  3. Before drawing, compare the current and previous states.

    • If they are different, it means something changed, so redraw the shape.

    • If they are the same, it means nothing changed, so skip drawing.

  4. After drawing, update previous to be equal to current.

This way, your project only does the heavy drawing work when it actually needs to, making it more efficient and smoother.

Watch the full video here to learn how to make your Scratch projects smarter and faster.

Why This Matters, Even in Scratch

You might think: “My computer is fast enough to handle all this, so why bother?”

That’s true for many projects, but if you want to become a skilled coder, getting in the habit of writing clean, efficient, and logical code from the start is important.

It’s like learning piano. You can play a song with bad technique, but it’ll slow you down later. If you practice good habits early, you become a better player, and coder.

This mindset helps you not only in Scratch but also in more advanced programming languages and real-world projects.

Advanced Ideas to Try Next

  • Use this change-checking trick when drawing complex pen patterns.
  • Apply the same logic to sprite animations so they update only when necessary.
  • Create a dirty flag, a simple true/false variable that tells your program when something has changed.
  • Extend the combined state string with other properties like rotation, stroke size, or position for more control.

Final Thoughts

This small improvement can make a big difference. It teaches you to think about what your code is actually doing, not just that it “works.”

If you want to see exactly how this works in Scratch, with all the code and explanations, check out the full video tutorial. Seeing the concept in action will help you understand how much smoother your projects can become.

Happy Coding!
TheSTEAMIst
The STEAMistt Avatar
Last updated on