Code:
import turtle
import math
import time
screen = turtle.Screen()
screen.setup(700, 700)
screen.bgcolor("#02020a")
screen.tracer(0)
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
t.width(2)
colors = [
"#ff1744", "#ff4081",
"#d500f9", "#7c4dff",
"#00e5ff", "#00ff9d"
]
# 🌸 Lotus petals
for layer in range(13):
t.color(colors[layer % len(colors)])
size = 55 + layer * 11
for petal in range(12):
rotation = petal * 30 + layer * 2
t.penup()
for i in range(81):
a = math.radians(i * 4.5)
# Smooth pointed petal
r = size * math.sin(a)
r *= 0.75 + 0.25 * math.sin(a * 2)
x = r * math.cos(a)
y = r * math.sin(a)
rot = math.radians(rotation)
X = x * math.cos(rot) - y * math.sin(rot)
Y = x * math.sin(rot) + y * math.cos(rot)
t.goto(X, Y)
if i == 0:
t.pendown()
screen.update()
time.sleep(0.012)
time.sleep(0.08)
# ✨ Sparkling center
for r in range(40, 2, -2):
t.penup()
t.goto(0, -r)
t.dot(r, colors[r % len(colors)])
screen.update()
time.sleep(0.08)
t.penup()
t.goto(0, -6)
t.dot(12, "white")
turtle.done()
Explanation:
1. Import Libraries
import turtleimport mathimport time
- turtle → draws the lotus flower.
- math → performs mathematical and trigonometric calculations.
- time → controls animation speed.
2. Create the Screen
screen = turtle.Screen()screen.setup(700, 700)screen.bgcolor("#02020a")screen.tracer(0)
- Creates a 700 × 700 drawing window.
- Sets a dark background.
- tracer(0) disables automatic screen updates.
3. Configure the Turtle
t = turtle.Turtle()t.hideturtle()t.speed(0)t.width(2)
- Creates the drawing turtle.
- Hides the turtle cursor.
- speed(0) sets maximum drawing speed.
- width(2) sets the line thickness.
4. Define Neon Colors
colors = [ "#ff1744", "#ff4081", "#d500f9", "#7c4dff", "#00e5ff", "#00ff9d"]
- Stores six neon colors.
- The colors repeat across the lotus layers.
5. Create Lotus Layers
for layer in range(13):
- Creates 13 layers of petals.
- Each layer uses a different size and rotation.
6. Select the Layer Color
t.color(colors[layer % len(colors)])
- Selects a color for the current layer.
- The modulo operator % repeats the colors when needed.
7. Calculate Petal Size
size = 55 + layer * 11
- Starts with a size of 55.
- Increases the size by 11 for each new layer.
- Creates an expanding lotus effect.
8. Create 12 Petals
for petal in range(12):
- Generates 12 petal curves in each layer.
9. Calculate Petal Rotation
rotation = petal * 30 + layer * 2
- petal * 30 separates 12 petals evenly around 360°.
- layer * 2 slightly rotates each new layer.
10. Lift the Pen
t.penup()
- Prevents unwanted lines when moving to the starting position.
11. Generate Petal Points
for i in range(81):
- Generates 81 points for each petal curve.
- More points help create a smooth shape.
12. Calculate the Angle
a = math.radians(i * 4.5)
- Converts degrees into radians.
- The angle progresses from 0° to 360°.
13. Create the Petal Radius
r = size * math.sin(a)
- Calculates the distance from the center.
- The sine function helps form the pointed petal shape.
14. Smooth the Petal Shape
r *= 0.75 + 0.25 * math.sin(a * 2)
- Modifies the radius using another sine wave.
- Adds a smooth, curved appearance to the petals.
15. Calculate X Coordinate
x = r * math.cos(a)
- Calculates the horizontal position of the point.
16. Calculate Y Coordinate
y = r * math.sin(a)
- Calculates the vertical position.
- Together, x and y create the petal curve.
17. Convert the Rotation Angle
rot = math.radians(rotation)
- Converts the petal's rotation angle into radians.
18. Rotate the X Coordinate
X = x * math.cos(rot) - y * math.sin(rot)
- Calculates the rotated horizontal position.
19. Rotate the Y Coordinate
Y = x * math.sin(rot) + y * math.cos(rot)
- Calculates the rotated vertical position.
- Together, these formulas rotate each petal around the center.
20. Move to the Calculated Point
t.goto(X, Y)
- Moves the turtle to the new coordinates.
21. Start Drawing the Petal
if i == 0: t.pendown()
- Lowers the pen at the first point.
- The following points are connected to draw the curve.
22. Animate the Petals
screen.update()time.sleep(0.012)
- Refreshes the screen.
- Adds a short delay to show the drawing animation.
23. Pause Between Petals
time.sleep(0.08)
- Adds a small pause after each petal.
- Makes the drawing process easier to watch.
24. Create the Sparkling Center
for r in range(40, 2, -2):
- Creates circles with decreasing sizes.
- The radius decreases from 40 to 4.
t.penup()t.goto(0, -r)t.dot(r, colors[r % len(colors)])
- Moves to the center area.
- Draws colorful dots of different sizes.
- Produces a layered, sparkling center.
25. Animate the Center Glow
screen.update()time.sleep(0.08)
- Updates the screen after each circle.
- Creates a gradual glowing effect.
26. Add the White Core
t.penup()t.goto(0, -6)t.dot(12, "white")
- Adds a white dot in the center.
- Creates a bright highlight.
27. Finish the Drawing
turtle.done()
- Keeps the Turtle graphics window open.

0 Comments:
Post a Comment