Code:
import turtle
import math
import time
screen = turtle.Screen()
screen.setup(700, 700)
screen.bgcolor("#020208")
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
t.width(3)
colors = [
"#ff1744", "#ff2d75",
"#d500f9", "#7c4dff",
"#00e5ff", "#00ff9d"
]
for layer in range(14):
t.color(colors[layer % len(colors)])
for i in range(180):
a = math.radians(i * 2)
# Flower ribbon curve
r = 90 + 85 * math.sin(5 * a + layer * 0.25)
x = r * math.cos(a)
y = r * math.sin(a)
# Slight 3D rotation
rot = math.radians(layer * 4)
X = x * math.cos(rot) - y * math.sin(rot)
Y = x * math.sin(rot) + y * math.cos(rot)
if i == 0:
t.penup()
t.goto(X, Y)
t.pendown()
else:
t.goto(X, Y)
screen.update()
time.sleep(0.002)
time.sleep(0.08)
# ✨ Bold glowing center
for r in range(30, 2, -3):
t.penup()
t.goto(0, -r)
t.dot(r, colors[r % len(colors)])
screen.update()
time.sleep(0.06)
t.penup()
t.goto(0, -5)
t.dot(9, "white")
turtle.done()
Explanation:
1. Import Libraries
import turtleimport mathimport time
- turtle → draws the flower.
- math → handles angles and trigonometry.
- time → adds animation delays.
2. Create the Screen
screen = turtle.Screen()screen.setup(700, 700)screen.bgcolor("#020208")
- Creates a 700×700 window.
- Sets a very dark background.
3. Configure the Turtle
t = turtle.Turtle()t.hideturtle()t.speed(0)t.width(3)
- Creates the drawing turtle.
- Hides the turtle cursor.
- speed(0) gives maximum drawing speed.
- width(3) makes the lines bold.
4. Define Neon Colors
colors = [ "#ff1744", "#ff2d75", "#d500f9", "#7c4dff", "#00e5ff", "#00ff9d"]
- Creates a palette of six neon colors.
- These colors are used for different flower layers.
5. Create Flower Layers
for layer in range(14):
- Creates 14 overlapping flower ribbons.
- Each layer has a slightly different rotation and phase.
6. Select Layer Color
t.color(colors[layer % len(colors)])
- Selects a color from the list.
- % makes the colors repeat after reaching the last color.
7. Generate Curve Points
for i in range(180):
- Creates 180 points for each flower ribbon.
- More points make the curve smoother.
8. Calculate the Angle
a = math.radians(i * 2)
- Converts the angle from degrees to radians.
- The angle increases as the loop progresses.
9. Create the Flower Ribbon Curve
r = 90 + 85 * math.sin(5 * a + layer * 0.25)
- Calculates the changing radius.
- sin() creates the petal-like wave.
- 5 * a produces five major flower lobes.
- layer * 0.25 shifts each layer slightly.
10. Calculate X Coordinate
x = r * math.cos(a)
- Converts the radius and angle into the X position.
11. Calculate Y Coordinate
y = r * math.sin(a)
- Calculates the Y position.
- Together, x and y create the flower curve.
12. Calculate 3D Rotation
rot = math.radians(layer * 4)
- Gives every layer a slightly different rotation.
- Creates a 3D/orbital appearance.
13. Rotate X and Y Coordinates
X = x * math.cos(rot) - y * math.sin(rot)Y = x * math.sin(rot) + y * math.cos(rot)
- Applies a mathematical rotation transformation.
- Produces the twisted ribbon effect.
14. Start Drawing the Layer
if i == 0: t.penup() t.goto(X, Y) t.pendown()
- For the first point, the turtle moves without drawing.
- Then it puts the pen down to start the curve.
15. Connect the Points
else:
t.goto(X, Y)
- Connects every calculated point.
- This forms the complete flower ribbon.
16. Animate the Drawing
screen.update()time.sleep(0.002)
- Updates the screen after each point.
- Adds a tiny delay for the drawing animation.
17. Pause Between Layers
time.sleep(0.08)
- Adds a short pause after completing each ribbon.
- Makes the layer-by-layer effect visible.
18. Create the Glowing Center
for r in range(30, 2, -3):
- Creates multiple circles.
- Radius decreases from 30 to 3.
t.penup()t.goto(0, -r)t.dot(r, colors[r % len(colors)])
- Moves to the center area.
- Draws colorful dots of different sizes.
- Together they create a glowing center.
19. Animate the Center Glow
screen.update()time.sleep(0.06)
- Updates the screen.
- Slows down the center-glow animation.
20. Add White Core
t.penup()t.goto(0, -5)t.dot(9, "white")
- Adds a small white dot.
- Creates a bright core/highlight in the center.
21. Finish the Drawing
turtle.done()
- Keeps the Turtle window open.

0 Comments:
Post a Comment