Code:
import turtle
import time
screen = turtle.Screen()
screen.setup(750, 750)
screen.bgcolor("#030308")
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
t.width(2)
colors = [
"#ff2d75", "#ff7a00", "#ffe600",
"#00ffb3", "#00d9ff", "#536dfe",
"#b84dff", "#ff2de2"
]
def petal(angle, size, color):
t.penup()
t.goto(0, 0)
t.setheading(angle)
t.color(color)
t.pendown()
t.begin_fill()
for _ in range(25):
t.forward(size / 25)
t.left(2.8)
screen.update()
time.sleep(0.015)
for _ in range(25):
t.forward(size / 25)
t.right(5.6)
screen.update()
time.sleep(0.015)
for _ in range(25):
t.forward(size / 25)
t.left(2.8)
screen.update()
time.sleep(0.015)
t.end_fill()
for layer, size in enumerate([150, 125, 100]):
for i in range(12):
angle = i * 30 + layer * 15
petal(angle, size, colors[(i + layer * 2) % len(colors)])
time.sleep(0.15)
t.penup()
t.goto(0, -18)
t.color("white")
t.begin_fill()
for _ in range(36):
t.forward(3.14)
t.left(10)
screen.update()
time.sleep(0.02)
t.end_fill()
t.penup()
t.goto(0, -8)
t.color("#00ffff")
t.begin_fill()
for _ in range(36):
t.forward(1.4)
t.left(10)
screen.update()
time.sleep(0.02)
t.end_fill()
turtle.done()
Explanation:
1. Importing Libraries
import turtle
import time
import turtle
Imports Python's built-in Turtle Graphics library, which is used to create drawings and animations.
import time
Imports the time module, which is used here with time.sleep() to control the animation speed.
2. Creating the Drawing Screen
screen = turtle.Screen()
Creates a new Turtle graphics window and stores it in the variable screen.
screen.setup(750, 750)
Sets the size of the window to:
Width → 750 pixels
Height → 750 pixels
screen.bgcolor("#030308")
Sets the background color to a very dark black-blue shade.
3. Creating the Turtle
t = turtle.Turtle()
Creates a Turtle object and stores it in t.
This Turtle will perform all the drawing operations.
t.hideturtle()
Hides the Turtle cursor so that only the artwork is visible.
t.speed(0)
Sets the Turtle's drawing speed to the fastest possible speed.
t.width(2)
Sets the pen width to 2.
4. Defining the Neon Color Palette
colors = [
"#ff2d75", "#ff7a00", "#ffe600",
"#00ffb3", "#00d9ff", "#536dfe",
"#b84dff", "#ff2de2"
]
Creates a list containing 8 neon colors.
These colors will be used to give different petals different appearances.
The palette contains shades of:
Pink
Orange
Yellow
Green
Cyan
Blue
Purple
Magenta
5. Creating the petal() Function
def petal(angle, size, color):
Defines a function named petal().
The function accepts three parameters:
angle
Controls the direction in which the petal is drawn.
size
Controls the length/size of the petal.
color
Determines the petal's color.
So the function receives:
petal(angle, size, color)
↓ ↓ ↓
direction size color
6. Positioning the Turtle
t.penup()
Lifts the pen from the screen so that moving the Turtle does not create a line.
t.goto(0, 0)
Moves the Turtle to the center of the screen.
(0, 0) represents the center point in Turtle Graphics.
t.setheading(angle)
Sets the Turtle's direction according to the given angle.
For example:
0° → Right
90° → Up
180° → Left
270° → Down
This allows every petal to point in a different direction.
7. Setting the Petal Color
t.color(color)
Sets the Turtle's drawing and filling color to the color received by the function.
t.pendown()
Places the pen back down so the Turtle starts drawing.
8. Starting the Petal Fill
t.begin_fill()
Tells Turtle to fill the shape that is about to be drawn with the selected color.
9. Drawing the First Curve
for _ in range(25):
Runs the loop 25 times.
The _ means we don't need to use the loop counter.
t.forward(size / 25)
Moves the Turtle forward by a small portion of the total size.
Since this happens 25 times, the total forward movement is approximately size.
t.left(2.8)
Turns the Turtle 2.8° to the left after each movement.
Repeated small turns create a smooth curved line.
screen.update()
Manually refreshes the screen so the drawing becomes visible during the animation.
time.sleep(0.015)
Pauses the program for 0.015 seconds.
This creates a visible drawing animation instead of drawing everything instantly.
10. Drawing the Second Curve
for _ in range(25):
Runs another 25 iterations.
t.forward(size / 25)
Moves forward by a small distance.
t.right(5.6)
Turns 5.6° to the right.
The direction is now opposite to the previous curve, helping form the other side of the petal.
screen.update()
Refreshes the screen.
time.sleep(0.015)
Adds a small delay to make the animation smoother.
11. Completing the Petal Curve
for _ in range(25):
Starts the third 25-iteration loop.
t.forward(size / 25)
Moves forward in small steps.
t.left(2.8)
Turns left by 2.8°.
This completes the curved structure of the petal.
screen.update()
Updates the screen during the drawing.
time.sleep(0.015)
Adds a short animation delay.
12. Filling the Petal
t.end_fill()
Ends the filling operation.
The completed petal is filled with the selected neon color.
The complete process is:
begin_fill()
↓
Draw curved shape
↓
end_fill()
↓
Colored petal
13. Creating Multiple Petal Layers
for layer, size in enumerate([150, 125, 100]):
Creates 3 layers of petals.
The sizes are:
Layer 0 → 150
Layer 1 → 125
Layer 2 → 100
enumerate() provides both:
The layer number
The corresponding size
14. Creating 12 Petals in Each Layer
for i in range(12):
Creates 12 petals for each layer.
Since there are 3 layers:
3 layers × 12 petals
= 36 petals
So the final flower contains 36 petals.
15. Calculating the Petal Angle
angle = i * 30 + layer * 15
This line determines the direction of every petal.
i * 30
Places the 12 petals around the circle at approximately 30° intervals:
0°
30°
60°
90°
120°
...
Because:
12 × 30° = 360°
the petals form a complete circular arrangement.
layer * 15
Rotates each new layer by an additional 15°.
This prevents all three layers from perfectly overlapping.
The result is a more complex flower pattern.
16. Selecting Different Colors
colors[(i + layer * 2) % len(colors)]
This expression selects a color from the colors list.
len(colors)
Returns the number of colors:
8
%
The modulo operator keeps the calculated index within the valid range of the list.
For example:
0 → Color 1
1 → Color 2
2 → Color 3
...
7 → Color 8
8 → Color 1 again
This allows the neon colors to repeat automatically.
17. Calling the Petal Function
petal(
angle,
size,
colors[(i + layer * 2) % len(colors)]
)
Calls the petal() function.
It provides:
angle → Direction of the petal
size → Size of the petal
color → Selected neon color
Each function call creates one complete petal.
18. Adding a Delay Between Petals
time.sleep(0.15)
Pauses for 0.15 seconds after each petal.
This makes the flower appear to bloom petal by petal.
19. Moving to the Flower Center
t.penup()
Lifts the pen so no unwanted line is drawn.
t.goto(0, -18)
Moves the Turtle slightly below the center.
This position is used to create the white center of the flower.
20. Creating the White Center
t.color("white")
Sets the drawing/fill color to white.
t.begin_fill()
Starts the fill operation.
21. Drawing the White Circle
for _ in range(36):
Runs 36 times.
t.forward(3.14)
Moves the Turtle forward by 3.14 units.
t.left(10)
Turns the Turtle 10° after every movement.
Because:
36 × 10° = 360°
the Turtle completes one full rotation, creating an approximately circular shape.
screen.update()
Refreshes the screen during the animation.
time.sleep(0.02)
Adds a small animation delay.
t.end_fill()
Fills the completed circular shape with white.
22. Creating the Cyan Inner Circle
t.penup()
Lifts the pen before repositioning.
t.goto(0, -8)
Moves the Turtle closer to the center.
t.color("#00ffff")
Sets the color to bright cyan.
t.begin_fill()
Starts filling the next shape.
23. Drawing the Cyan Center
for _ in range(36):
Runs 36 times to create another circular shape.
t.forward(1.4)
Moves forward by only 1.4 units.
Because this distance is smaller than the white circle's 3.14, the cyan circle is smaller.
t.left(10)
Turns 10° after every movement.
Again:
36 × 10° = 360°
so a complete circle is formed.
screen.update()
Updates the screen during the animation.
time.sleep(0.02)
Adds a small delay.
t.end_fill()
Fills the inner circle with cyan.
24. Keeping the Turtle Window Open
turtle.done()
Keeps the Turtle graphics window open after the drawing is complete.
Without this line, the window may close immediately when the program finishes.

0 Comments:
Post a Comment