Chase the Cycler Simulation (with Source Code)

In a previous project called massive chasing game, you simulated a game where you created 100 turtles and each turtle chase the next one and the last turtle chase the first one.

In this project, instead of making the last turtle chase the first one, make it move on a circle forever. It will create an interesting animation shown above.

Source Code:

import turtle
import random
import colorsys

screen = turtle.Screen()
screen.tracer(0,0)
screen.setup(1000,1000)
screen.title('Chase the Cycler - PythonTurtle.Academy')
turtle.hideturtle()

n=200
chasers = []
for i in range(n):
    chasers.append(turtle.Turtle())

h = 0
for i in range(n):
    c = colorsys.hsv_to_rgb(h,1,0.8)
    h += 1/n
    chasers[i].color(c)
    chasers[i].up()
    chasers[i].goto(random.uniform(-500,500), random.uniform(-500,500))
chasers[n-1].goto(0,-300)
chasers[n-1].shape('circle')
chasers[n-1].shapesize(1)

def chase():
    for i in range(n-1):
        angle = chasers[i].towards(chasers[i+1])
        chasers[i].seth(angle)
    chasers[n-1].left(2)
    chasers[n-1].fd(10)
    for i in range(n-1):
        chasers[i].fd(10)
    screen.update()
    screen.ontimer(chase,1000//20)
    
chase()

Swarm

What will happen to a large number of randomly moving objects if you let them follow only one rule: try to move in the same direction as your neighbors do. Soon, you will find these objects forms into groups and moving together, which is a type of swarm behavior.

Create a large number of turtles with random colors and moving in random directions. At each tick, update each turtles direction slightly toward the average direction of its neighbors. Also change the color of the turtle slightly toward the average color of its neighbors. When a turtle hits the boundary of the screen, let is bounce off to a random opposite direction.

Check out video animation of the swarm behavior.

Video of Swarm Behavior
Swarm Behavior with Python Turtle

Related Projects: