Wednesday, 7 October 2026

✨ Python Turtle The 3D Ribbon Bloom

 

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

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 (1378) Python Coding Challenge (1269) Python Library (20) Python Mathematics (19) Python Mistakes (51) Python Pattern Challenge (17) Python Quiz (652) 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)