Project 16 of 35  ·  Scratch Programming  ·  Level 3
Level 3

Swarms Schools Flocks

Simulate flocking behaviour with cloned sprites that follow simple emergent rules.

🍓  Open on Raspberry Pi Projects  ↗ Opens the official Raspberry Pi Foundation project — includes starter files and full step-by-step guide

What You Will Build

Simulate flocking behaviour with cloned sprites that follow simple emergent rules. Follow the guide below to build the project, then open the official Raspberry Pi page for starter files, worked examples and extra help.

🍓 This project is on the Raspberry Pi Foundation website. Click the orange button above to access the full guide, starter Scratch project and extension activities on projects.raspberrypi.org.

What You Will Learn

  • Use create clone of myself to generate many identical sprites
  • Apply three flocking rules: separation, alignment, cohesion
  • Use sensing to detect nearby clones
  • Control flocking strength with slider variables
  • Observe how complex group behaviour emerges from simple rules

Step-by-Step Guide

  • 1Create a blue sky or ocean backdrop.
  • 2Design a simple fish or bird sprite.
  • 3On start: repeat 20create clone of [myself].
  • 4Each clone starts: go to random position, point in direction (pick random 1 to 360).
  • 5In a forever loop on each clone: move 2 steps. If touching edge: turn 180 degrees.
  • 6Separation: if touching [clone]: turn 15 degrees away.
  • 7Alignment: gradually turn toward the average direction of nearby clones (approximate with +5 or -5).
  • 8Cohesion: slowly move toward the centre of the stage (adjust x and y by small amounts toward 0,0).
  • 9Add three slider variables for separation, alignment and cohesion strength.
  • 10Observe: initially random, gradually they form a flowing flock.

Extension Challenges

Finished the main project? Push further:

  • Add a predator sprite that all clones avoid when within 50 pixels.
  • Add a food source that attracts the flock and disappears when reached.
  • Try with 50 clones — does the behaviour change?