コンテンツにスキップ

How to make a sprite follow a specific color in Scratch

このコンテンツはまだ日本語訳がありません。

💡 Having trouble with Scratch block assembly? Don’t know how to implement code logic? 🚀 Get Help Now

CS

ColorTracker_Sam

Posted on July 24, 2025 • Advanced

🎨 Need sprite to follow specific colors

Hey everyone! I’m working on an animation project where I need a sprite to track and follow a specific color on the screen, rather than following another sprite. This would be useful for:

  • Creating interactive art installations
  • Building computer vision demos
  • Making responsive animations
  • Educational projects about color detection

Is there an efficient way to implement color tracking in Scratch? I’m looking for both accuracy and performance! 😊

CV

ComputerVision_Expert

Replied 3 hours later • ⭐ Best Answer

Fantastic question @ColorTracker_Sam! Color tracking is a fascinating computer vision technique. Let me show you several approaches from basic to advanced:

🔍 Color Detection Algorithm Flow

Here’s how color following works:

flowchart TD A[🎯 Start Color Search] --> B[Create Detection Probe] B --> C[Scan in Pattern] C --> D{Color Found?} D -->|Yes| E[Calculate Direction] D -->|No| F[Continue Scanning] E --> G[Move Toward Color] G --> H[Update Position] H --> I{Close Enough?} I -->|Yes| J[✅ Following Color] I -->|No| K[Keep Moving] F --> L{Scan Complete?} L -->|No| C L -->|Yes| M[❌ Color Not Found] J --> N[Monitor Color Position] K --> C M --> O[Search Wider Area] N --> P{Color Moved?} P -->|Yes| C P -->|No| N style A fill:#e1f5fe style J fill:#e8f5e8 style M fill:#ffebee

🎨 Method 1: Basic Radial Scanning

This approach scans in a circle around the sprite:

    // Create custom block: find color direction
define find color direction (target color) (scan radius)
set [best direction v] to [-1]
set [closest distance v] to [999]
set [scan angle v] to [0]

// Scan in 360 degrees
repeat (36) // Check every 10 degrees
// Create temporary detection point
set [test x v] to ((x position) + ((scan radius) * ([cos v] of (scan angle))))
set [test y v] to ((y position) + ((scan radius) * ([sin v] of (scan angle))))

// Check if color exists at this point
go to x: (test x) y: (test y)
if <touching color (target color)?> then
set [best direction v] to (scan angle)
set [closest distance v] to (scan radius)
end

change [scan angle v] by (10)
end

// Return to original position
go to x: (original x) y: (original y)
  

🚀 Method 2: Optimized Grid Search

More efficient approach using a grid pattern:

    // Custom block: smart color search
define smart color search (target color)
set [found color v] to [false]
set [search size v] to [20] // Start with small search area

repeat until <(found color) = [true]>
set [grid x v] to (0 - (search size))
repeat ((search size) * (2))
set [grid y v] to (0 - (search size))
repeat ((search size) * (2))
// Test this grid position
go to x: ((x position) + (grid x)) y: ((y position) + (grid y))

if <touching color (target color)?> then
set [target x v] to (x position)
set [target y v] to (y position)
set [found color v] to [true]
stop [this script v]
end

change [grid y v] by (5) // Grid step size
end
change [grid x v] by (5)
end

// Expand search area if not found
change [search size v] by (20)
if <(search size) > [200]> then
stop [this script v] // Give up if too far
end
end
  

🎯 Method 3: Intelligent Following System

Complete system with smooth movement:

    when flag clicked
set [target color v] to [#ff0000] // Red color
set [follow speed v] to [3]
set [detection range v] to [50]

forever
// Store current position
set [original x v] to (x position)
set [original y v] to (y position)

// Find the color
smart color search (target color)

if <(found color) = [true]> then
// Calculate direction to color
set [dx v] to ((target x) - (original x))
set [dy v] to ((target y) - (original y))
set [distance v] to ([sqrt v] of (((dx) * (dx)) + ((dy) * (dy))))

// Move toward color if not too close
if <(distance) > [10]> then
// Normalize direction and apply speed
set [move x v] to (((dx) / (distance)) * (follow speed))
set [move y v] to (((dy) / (distance)) * (follow speed))

// Smooth movement
go to x: ((original x) + (move x)) y: ((original y) + (move y))

// Point toward the color
point in direction ([atan2 v] of (dy) (dx))
end
else
// Color not found - search behavior
turn right (5) degrees
move (2) steps
end

wait (0.05) seconds // Control update rate
end
  

⚡ Method 4: High-Performance Version

Optimized for better performance:

    // Use smaller detection sprite for efficiency
when flag clicked
switch costume to [detector v] // 1x1 pixel costume
set [ghost v] effect to (100) // Make invisible

forever
// Quick spiral search pattern
set [spiral radius v] to [5]
set [spiral angle v] to [0]
set [found v] to [false]

repeat (20) // Limit search iterations
// Spiral outward
set [test x v] to ((x position) + ((spiral radius) * ([cos v] of (spiral angle))))
set [test y v] to ((y position) + ((spiral radius) * ([sin v] of (spiral angle))))

go to x: (test x) y: (test y)

if <touching color [#ff0000]?> then
// Found color - move main sprite
broadcast [move to color v]
set [color x v] to (x position)
set [color y v] to (y position)
set [found v] to [true]
stop [this script v]
end

// Expand spiral
change [spiral angle v] by (30)
change [spiral radius v] by (2)
end

if <(found) = [false]> then
// Random search if spiral fails
go to x: (pick random (-200) to (200)) y: (pick random (-150) to (150))
end
end

// Main sprite responds to color detection
when I receive [move to color v]
glide (0.2) secs to x: (color x) y: (color y)
  

🎨 Advanced Features

Add these enhancements for professional results:

    // Color tolerance for better matching
define color matches (test color) (target color) (tolerance)
set [r diff v] to ([abs v] of (([r v] of (test color)) - ([r v] of (target color))))
set [g diff v] to ([abs v] of (([g v] of (test color)) - ([g v] of (target color))))
set [b diff v] to ([abs v] of (([b v] of (test color)) - ([b v] of (target color))))
set [total diff v] to (((r diff) + (g diff)) + (b diff))

if <(total diff) < (tolerance)> then
set [color match v] to [true]
else
set [color match v] to [false]
end

// Smooth following with easing
define smooth follow (target x) (target y) (speed)
set [current x v] to (x position)
set [current y v] to (y position)
set [lerp factor v] to ((speed) / (100))

set [new x v] to (((target x) * (lerp factor)) + ((current x) * ((1) - (lerp factor))))
set [new y v] to (((target y) * (lerp factor)) + ((current y) * ((1) - (lerp factor))))

go to x: (new x) y: (new y)
  

💡 Pro Tips

  • Use small detection sprites: 1x1 or 2x2 pixel costumes for efficiency
  • Limit search area: Don’t scan the entire screen every frame
  • Add color tolerance: Account for slight color variations
  • Implement prediction: Anticipate where the color might move
  • Use “run without screen refresh”: For detection blocks to improve performance

This creates smooth, responsive color tracking that works great for interactive projects! 🎯

CS

ColorTracker_Sam

Replied 2 hours later

@ComputerVision_Expert This is absolutely amazing! 🤩

I implemented the optimized grid search method and it works perfectly for my animation project. The sprite now smoothly follows the red elements in my artwork as they move around.

The performance tips made a huge difference - using the 1x1 detection sprite and limiting the search area keeps everything running smoothly even with complex backgrounds.

AI

ArtInstallation_Maya

Replied 4 hours later

This is perfect for interactive art! 🎨 Here are some creative applications I’ve used:

🎭 Creative Project Ideas

  • Interactive paintings: Sprites react to colored brushstrokes
  • Music visualization: Characters follow colored sound waves
  • Educational demos: Show how computer vision works
  • Responsive installations: Art that reacts to colored objects
    // Multi-color tracking system
when flag clicked
set [tracking colors v] to [red blue green yellow]
set [current color index v] to [1]

forever
set [current color v] to (item (current color index) of (tracking colors))
smart color search (current color)

if <(found color) = [true]> then
// Create trail effect
create clone of [trail v]
// Switch to next color
change [current color index v] by (1)
if <(current color index) > (length of (tracking colors))> then
set [current color index v] to [1]
end
end
end
  

The possibilities are endless when you combine color tracking with creativity! 🌈

VB

Vibelf_Community

Pinned Message • Moderator

🔬 Explore Computer Vision Concepts

Amazing discussion on color tracking algorithms! Ready to dive deeper into computer vision and advanced programming concepts? Our community can help you master:

  • 🎯 Advanced image processing techniques
  • 🤖 Machine learning and pattern recognition
  • 📊 Data analysis and visualization
  • 🎨 Interactive media and digital art

📚 Related Topics

Ready to explore the fascinating world of computer vision? Get personalized guidance from our programming experts in the Vibelf app!