Skip to content

How to implement a fill tool in Scratch drawing games

💡 Struggling with advanced drawing tools? Need help with complex algorithms? 🚀 Get Expert Help

DM

DrawingMaster_Alex

Posted on January 22, 2024 • Advanced

🎨 Need help implementing a fill tool in my drawing game

Hey everyone! I’m working on an advanced drawing game in Scratch and I want to add a professional fill tool that can fill enclosed areas with color, similar to the paint bucket tool in image editors.

The challenge is that I need it to:

  • Detect enclosed boundaries accurately
  • Fill only the selected area without going outside lines
  • Work with different shapes and sizes
  • Handle complex polygons and curved shapes

I’ve tried simple approaches but they either create circles that go outside boundaries or leave gaps. Looking for a robust solution! 🎯

AP

AlgorithmPro_Sarah

Replied 3 hours later • ⭐ Best Answer

Excellent question @DrawingMaster_Alex! Implementing a proper fill tool requires a flood fill algorithm. Here’s a comprehensive solution:

🧠 Flood Fill Algorithm Architecture

Here’s how a professional flood fill system works:

flowchart TD A[🖱️ User Clicks Fill Tool] --> B[📍 Get Click Position] B --> C[🎨 Get Target Color] C --> D[🔍 Check if Already Filled] D --> E{Same Color?} E -->|Yes| F[❌ Exit - Already Filled] E -->|No| G[📚 Initialize Stack] G --> H[📌 Add Start Point to Stack] H --> I[🔄 Main Fill Loop] I --> J{Stack Empty?} J -->|Yes| K[✅ Fill Complete] J -->|No| L[📤 Pop Point from Stack] L --> M[🎨 Fill Current Point] M --> N[🔍 Check 4 Neighbors] N --> O[⬆️ Check Up] O --> P{Valid & Same Color?} P -->|Yes| Q[📌 Add to Stack] P -->|No| R[➡️ Check Right] Q --> R R --> S{Valid & Same Color?} S -->|Yes| T[📌 Add to Stack] S -->|No| U[⬇️ Check Down] T --> U U --> V{Valid & Same Color?} V -->|Yes| W[📌 Add to Stack] V -->|No| X[⬅️ Check Left] W --> X X --> Y{Valid & Same Color?} Y -->|Yes| Z[📌 Add to Stack] Y -->|No| I Z --> I K --> AA[🎉 Show Fill Effect] F --> BB[🔊 Play Error Sound] subgraph "Fill Variables" CC["🎯 Target Color"] DD["🎨 Fill Color"] EE["📚 Point Stack"] FF["📍 Current Position"] GG["🔢 Stack Size"] end subgraph "Boundary Detection" HH["🖤 Black Lines"] II["🎨 Different Colors"] JJ["🚫 Canvas Edges"] KK["🔍 Color Tolerance"] end style A fill:#e1f5fe style M fill:#c8e6c9 style K fill:#fff3e0 style AA fill:#f3e5f5 style BB fill:#ffebee

🔧 Step 1: Set Up Fill Variables

First, create the necessary variables for the flood fill algorithm:

    when flag clicked
set [fill color v] to [#ff0000]
set [stack size v] to [0]
delete all of [fill stack x v]
delete all of [fill stack y v]
  

🖱️ Step 2: Detect Fill Tool Click

Add this code to detect when the user wants to fill an area:

    when this sprite clicked
if <(current tool) = [fill]> then
set [target x v] to (mouse x)
set [target y v] to (mouse y)
set [target color v] to (color at x: (target x) y: (target y))
if <not <(target color) = (fill color)>> then
flood fill (target x) (target y)
end
end
  

🌊 Step 3: Implement Flood Fill Algorithm

Create this custom block for the main flood fill logic:

    define flood fill (start x) (start y)
delete all of [fill stack x v]
delete all of [fill stack y v]
set [stack size v] to [1]
add (start x) to [fill stack x v]
add (start y) to [fill stack y v]

repeat until <(stack size) = [0]>
set [current x v] to (item (stack size) of [fill stack x v])
set [current y v] to (item (stack size) of [fill stack y v])
delete (stack size) of [fill stack x v]
delete (stack size) of [fill stack y v]
change [stack size v] by [-1]

if <(color at x: (current x) y: (current y)) = (target color)> then
pen up
go to x: (current x) y: (current y)
set pen color to (fill color)
set pen size to [1]
pen down
pen up

// Check 4 neighbors
check and add (current x) ((current y) + [1])
check and add ((current x) + [1]) (current y)
check and add (current x) ((current y) - [1])
check and add ((current x) - [1]) (current y)
end
end
  

🔍 Step 4: Neighbor Checking Function

Create this helper block to check and add valid neighbors:

    define check and add (x) (y)
if <<(x) > [-240]> and <<(x) < [240]> and <<(y) > [-180]> and <(y) < [180]>>>> then
if <(color at x: (x) y: (y)) = (target color)> then
change [stack size v] by [1]
add (x) to [fill stack x v]
add (y) to [fill stack y v]
end
end
  

🎨 Step 5: Color Detection Function

Create a custom block to detect colors at specific positions:

    define color at x: (x) y: (y)
pen up
go to x: (x) y: (y)
set [detected color v] to (pen color)
report (detected color)
  

⚡ Step 6: Optimized Line Fill

For better performance, implement scanline fill:

    define scanline fill (start x) (start y)
set [left bound v] to (start x)
set [right bound v] to (start x)

// Find left boundary
repeat until <not <(color at x: (left bound) y: (start y)) = (target color)>>
change [left bound v] by [-1]
end
change [left bound v] by [1]

// Find right boundary
repeat until <not <(color at x: (right bound) y: (start y)) = (target color)>>
change [right bound v] by [1]
end
change [right bound v] by [-1]

// Fill the line
pen up
go to x: (left bound) y: (start y)
set pen color to (fill color)
set pen size to [1]
pen down
go to x: (right bound) y: (start y)
pen up
  

🚀 Step 7: Advanced Features

Add these enhancements for a professional fill tool:

Anti-aliasing Support:

    define fill with tolerance (tolerance)
if <(color difference (detected color) (target color)) < (tolerance)> then
// Fill this pixel
pen up
go to x: (current x) y: (current y)
set pen color to (fill color)
pen down
pen up
end
  

Undo System:

    when [u v] key pressed
if <(undo stack size) > [0]> then
restore canvas state (item (undo stack size) of [undo stack v])
delete (undo stack size) of [undo stack v]
change [undo stack size v] by [-1]
end
  

This implementation gives you a professional-grade fill tool that handles complex shapes, boundaries, and edge cases! 🎨✨

DM

DrawingMaster_Alex

Replied 45 minutes later

@AlgorithmPro_Sarah This is absolutely incredible! 🤩 The flood fill algorithm works perfectly!

I implemented it and it handles all the complex shapes in my drawing game. The scanline optimization made it super fast too. Thank you so much for the detailed explanation!

One question - how can I add different fill patterns instead of just solid colors?

PT

PatternTech_Mike

Replied 1 hour later

@DrawingMaster_Alex Great question! Here’s how to add pattern fills:

    define pattern fill (pattern type) (x) (y)
if <(pattern type) = [dots]> then
if <((x) mod [5]) = [0]> then
if <((y) mod [5]) = [0]> then
set pen color to (fill color)
else
set pen color to (background color)
end
end
else
if <(pattern type) = [stripes]> then
if <((x) + (y)) mod [8] < [4]> then
set pen color to (fill color)
else
set pen color to (background color)
end
end
end
  

This creates dots and stripe patterns! You can expand it with more pattern types. 🎨

VB

Vibelf_Community

Pinned Message • Moderator

🚀 Master Advanced Drawing Algorithms

Fantastic discussion on flood fill implementation! For those wanting to create even more sophisticated drawing tools, our experts can help you build:

  • 🎨 Advanced brush engines
  • 🔧 Vector drawing tools
  • 🌈 Gradient fill systems
  • 📐 Geometric shape tools
  • 🖼️ Layer management systems

📚 Related Topics

Ready to build professional-grade drawing applications? Get personalized guidance from our algorithm experts!