Friday, 9 October 2026

✨ Python Turtle The Neon Lotus Firework

 


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

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (347) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (359) Bootcamp (16) C (78) C# (12) C++ (83) cloud (1) Course (93) Coursera (305) Cybersecurity (36) data (10) Data Analysis (47) Data Analytics (31) data management (16) Data Science (435) Data Strucures (19) Deep Learning (222) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (9) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Gadgets (1) Generative AI (78) Git (13) Google (55) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (407) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1380) Python Coding Challenge (1272) Python Library (22) Python Mathematics (20) Python Mistakes (51) Python Pattern Challenge (19) Python Quiz (655) Python Tips (112) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (22) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)