Code:
import turtle
import math
import time
screen = turtle.Screen()
screen.setup(800, 800)
screen.bgcolor("#02030a")
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
t.width(2)
colors = [
"#ff006e",
"#ff7b00",
"#ffe600",
"#00ff9d",
"#00e5ff",
"#4169ff",
"#9b30ff"
]
# -----------------------------
# Butterfly Curve
# -----------------------------
def butterfly(scale, color, phase):
t.color(color)
points = 260
for i in range(points):
theta = i * 2 * math.pi / points
# Butterfly curve
r = math.exp(math.sin(theta)) - 2 * math.cos(4 * theta)
x = scale * r * math.sin(theta + phase)
y = scale * r * math.cos(theta + phase)
if i == 0:
t.penup()
t.goto(x, y)
t.pendown()
else:
t.goto(x, y)
screen.update()
time.sleep(0.004)
# -----------------------------
# Outer butterfly
# -----------------------------
for i in range(7):
butterfly(
85 + i * 12,
colors[i],
i * 0.035
)
time.sleep(0.08)
# -----------------------------
# Inner butterfly
# -----------------------------
for i in range(5):
butterfly(
35 + i * 8,
colors[(i + 2) % len(colors)],
-i * 0.04
)
time.sleep(0.08)
# -----------------------------
# Body
# -----------------------------
t.color("#ffffff")
t.width(5)
t.penup()
t.goto(0, -115)
t.pendown()
t.goto(0, 115)
screen.update()
time.sleep(0.3)
# -----------------------------
# Antennae
# -----------------------------
t.width(2)
for side in [-1, 1]:
t.penup()
t.goto(0, 110)
t.setheading(90 + side * 35)
t.pendown()
for _ in range(35):
t.forward(3)
t.left(side * 2)
screen.update()
time.sleep(0.01)
# -----------------------------
# Glowing body
# -----------------------------
for r in range(18, 2, -3):
t.penup()
t.goto(0, -r)
t.dot(
r,
colors[r % len(colors)]
)
screen.update()
time.sleep(0.05)
# -----------------------------
# Star particles
# -----------------------------
for i in range(45):
angle = i * 137.5
radius = 180 + (i % 5) * 22
x = radius * math.cos(math.radians(angle))
y = radius * math.sin(math.radians(angle))
t.penup()
t.goto(x, y)
t.dot(
2 + i % 3,
colors[i % len(colors)]
)
screen.update()
time.sleep(0.025)
turtle.done()
Explanation:
1. Import Libraries
import turtle
import math
import time
turtle → Drawing.
math → Mathematical calculations.
time → Animation delays.
2. Create the Screen
screen = turtle.Screen()
screen.setup(800, 800)
screen.bgcolor("#02030a")
Creates the window.
Sets size to 800 × 800.
Adds a dark background.
3. Create the Turtle
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
t.width(2)
Creates the drawing turtle.
Hides the cursor.
Sets maximum speed.
Sets line width to 2.
4. Define Colors
colors = [...]
Stores bright neon colors.
Colors are reused for the butterfly.
5. Define Butterfly Function
def butterfly(scale, color, phase):
Creates a reusable butterfly-drawing function.
scale → Size.
color → Line color.
phase → Rotation/offset.
6. Set Drawing Properties
t.color(color)
points = 260
Sets the selected color.
Uses 260 points for a smooth curve.
7. Calculate the Angle
theta = i * 2 * math.pi / points
Generates an angle for each point.
Covers a complete circular cycle.
8. Calculate Butterfly Radius
r = math.exp(math.sin(theta)) - 2 * math.cos(4 * theta)
Uses the butterfly-curve formula.
Produces the wing-like shape.
9. Calculate Coordinates
x = scale * r * math.sin(theta + phase)
y = scale * r * math.cos(theta + phase)
Calculates the X and Y positions.
scale controls the size.
phase slightly rotates the curve.
10. Draw the Curve
if i == 0:
t.penup()
t.goto(x, y)
t.pendown()
else:
t.goto(x, y)
Moves to the first point without drawing.
Connects all remaining points.
Creates the butterfly outline.
11. Animate the Curve
screen.update()
time.sleep(0.004)
Updates the screen.
Adds a tiny delay for animation.
12. Draw Outer Butterflies
for i in range(7):
Creates 7 outer butterfly layers.
butterfly(85 + i * 12, colors[i], i * 0.035)
Gradually increases the size.
Changes colors.
Adds a small phase shift.
13. Draw Inner Butterflies
for i in range(5):
Creates 5 smaller inner layers.
butterfly(
35 + i * 8,
colors[(i + 2) % len(colors)],
-i * 0.04
)
Creates smaller curves.
Cycles through colors.
Applies reverse phase rotation.
14. Draw Butterfly Body
t.color("#ffffff")
t.width(5)
Changes the body to white.
Makes it thicker.
t.penup()
t.goto(0, -115)
t.pendown()
t.goto(0, 115)
Starts at the bottom.
Draws a vertical body through the center.
15. Draw Antennae
t.width(2)
for side in [-1, 1]:
Makes thinner lines.
Draws both antennae.
t.goto(0, 110)
t.setheading(90 + side * 35)
Moves to the top of the body.
Sets the antenna direction.
for _ in range(35):
Creates each antenna using 35 small segments.
t.forward(3)
t.left(side * 2)
Moves forward.
Slightly bends the antenna.
16. Create Glowing Body
for r in range(18, 2, -3):
Creates multiple shrinking circles.
t.dot(r, colors[r % len(colors)])
Draws colorful dots.
Creates a glowing effect.
17. Create Star Particles
for i in range(45):
Creates 45 particles around the butterfly.
angle = i * 137.5
radius = 180 + (i % 5) * 22
Generates different particle angles and distances.
Creates a scattered pattern.
18. Calculate Particle Position
x = radius * math.cos(math.radians(angle))
y = radius * math.sin(math.radians(angle))
Converts polar coordinates into X/Y positions.
19. Draw Particles
t.goto(x, y)
t.dot(
2 + i % 3,
colors[i % len(colors)]
)
Moves to each particle position.
Draws small colorful dots with varying sizes.
20. Finish
turtle.done()
Keeps the Turtle window open.
Ends the animation.

0 Comments:
Post a Comment