Loading design...

A beautiful rainbow wave spine created with Python Turtle. Read the code below, copy it, and try it in your own Python environment.
import turtle
import math
import colorsys
screen = turtle.Screen()
screen.setup(1000, 600)
screen.bgcolor("#111120")
screen.tracer(0)
pen = turtle.Turtle()
pen.speed(0)
pen.hideturtle()
pen.pensize(2)
NUM_SEGMENTS = 30
SEGMENT_SIZE = 12
LEG_LENGTH = 20
SPEED = 15
WAVE_AMPLITUDE = 40
body_segments = []
for i in range(NUM_SEGMENTS):
body_segments.append((0, 0))
frame_count = 0
def update_mouse(x, y):
global mouse_x, mouse_y
mouse_x = x
mouse_y = y
mouse_x, mouse_y = 0, 0
screen.getcanvas().bind('<Motion>',
lambda e: update_mouse(
e.x - screen.window_width()//2,
-e.y + screen.window_height()//2
)
)
def update_insect():
global frame_count
pen.clear()
target_x = mouse_x
target_y = mouse_y
head_x, head_y = body_segments[0]
dx = target_x - head_x
dy = target_y - head_y
distance = math.sqrt(dx**2 + dy**2)
if distance > 2:
angle = math.atan2(dy, dx)
move_x = dx * 0.2
move_y = dy * 0.2
move_distance = math.sqrt(move_x**2 + move_y**2)
if move_distance > SPEED:
scale = SPEED / move_distance
move_x *= scale
move_y *= scale
new_head_x = head_x + move_x
new_head_y = head_y + move_y
body_segments.insert(0, (new_head_x, new_head_y))
body_segments.pop()
for i, (x, y) in enumerate(body_segments):
hue = (i / NUM_SEGMENTS + frame_count * 0.001) % 1.0
rgb = colorsys.hsv_to_rgb(hue, 0.9, 1.0)
pen.pencolor(rgb)
pen.penup()
pen.goto(x, y)
pen.pendown()
pen.dot(SEGMENT_SIZE - i * 0.2, rgb)
leg_phase = frame_count * 0.15 - i * 0.8
leg_offset = math.sin(leg_phase) * 8
pen.penup()
pen.goto(x, y)
pen.pendown()
pen.goto(x - LEG_LENGTH, y - 10 + leg_offset)
pen.penup()
pen.goto(x, y)
pen.pendown()
pen.goto(x + LEG_LENGTH, y - 10 - leg_offset)
frame_count += 1
screen.update()
screen.ontimer(update_insect, 20)
update_insect()
turtle.done()