Code:
import turtle
import math
import time
screen = turtle.Screen()
screen.setup(700, 700)
screen.bgcolor("#03000a")
screen.tracer(0)
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
colors = ["#ff1744", "#ff4081", "#d500f9", "#7c4dff", "#00e5ff"]
# ❤️ Neon Heart
for i in range(360):
a = math.radians(i)
x = 16 * math.sin(a) ** 3
y = (
13 * math.cos(a)
- 5 * math.cos(2*a)
- 2 * math.cos(3*a)
- math.cos(4*a)
)
scale = 15
t.penup()
t.goto(0, 0)
t.pendown()
t.color(colors[i % len(colors)])
t.goto(x * scale, y * scale)
t.dot(3 + i % 3)
screen.update()
time.sleep(0.02)
# ✨ Glowing Center
for r in range(25, 2, -3):
t.penup()
t.goto(0, -r)
t.dot(r, colors[r % len(colors)])
screen.update()
time.sleep(0.06)
# ⭐ Small Sparkles
for i in range(25):
angle = i * 137.5
radius = 230 + (i % 4) * 15
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.03)
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(700, 700)
screen.bgcolor("#03000a")
screen.tracer(0)
Creates a 700 × 700 window.
Sets a dark background.
tracer(0) gives manual screen updates.
3. Configure the Turtle
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
Creates the turtle.
Hides the cursor.
Sets maximum drawing speed.
4. Define Neon Colors
colors = [...]
Stores the colors used for the heart, glow, and sparkles.
5. Generate the Heart
for i in range(360):
Creates 360 points around the heart.
a = math.radians(i)
Converts the angle from degrees to radians.
6. Calculate Heart Coordinates
x = 16 * math.sin(a) ** 3
Calculates the X-coordinate using the heart equation.
y = (
13 * math.cos(a)
- 5 * math.cos(2*a)
- 2 * math.cos(3*a)
- math.cos(4*a)
)
Calculates the Y-coordinate.
Together, these equations create the heart shape.
7. Scale the Heart
scale = 15
Enlarges the mathematical heart.
8. Move to Each Point
t.penup()
t.goto(0, 0)
t.pendown()
Moves to the center without drawing.
Starts drawing from the center.
9. Draw the Neon Heart
t.color(colors[i % len(colors)])
t.goto(x * scale, y * scale)
t.dot(3 + i % 3)
Cycles through neon colors.
Draws each heart point.
Adds small glowing dots.
10. Animate the Heart
screen.update()
time.sleep(0.02)
Updates the screen.
Adds a small delay for the drawing animation.
11. Create the Center Glow
for r in range(25, 2, -3):
Creates several shrinking circles.
t.goto(0, -r)
t.dot(r, colors[r % len(colors)])
Places colorful dots near the center.
Creates a glowing effect.
12. Add Sparkles
for i in range(25):
Creates 25 sparkles.
angle = i * 137.5
radius = 230 + (i % 4) * 15
Generates different angles and distances.
Spreads the sparkles around the heart.
13. Calculate Sparkle Positions
x = radius * math.cos(math.radians(angle))
y = radius * math.sin(math.radians(angle))
Converts the angle and radius into X/Y coordinates.
14. Draw the Sparkles
t.goto(x, y)
t.dot(2 + i % 3, colors[i % len(colors)])
Moves to each position.
Draws colorful dots of different sizes.
15. Finish
turtle.done()
Keeps the Turtle window open and finishes the animation.

0 Comments:
Post a Comment