Thursday, 24 September 2026

⚡ Python Turtle: The Cosmic Vortex

 




Code:

import turtle import math import time screen = turtle.Screen() screen.setup(800, 800) screen.bgcolor("#020208") t = turtle.Turtle() t.hideturtle() t.speed(0) colors = [ "#00f5ff", "#2979ff", "#7c4dff", "#d500f9", "#ff2d75", "#ff6d00", "#ffe600" ] def draw_ring(radius, rotation, color): """Draw one rotating polygon ring.""" points = 10 t.color(color) t.width(2) for i in range(points + 1): angle = rotation + i * (360 / points) x = radius * math.cos(math.radians(angle)) y = radius * math.sin(math.radians(angle)) if i == 0: t.penup() t.goto(x, y) t.pendown() else: t.goto(x, y) screen.update() time.sleep(0.004) # -------------------------------- # Outer → Inner tunnel # -------------------------------- for i in range(55): radius = 270 - i * 4.6 rotation = i * 6 draw_ring( radius, rotation, colors[i % len(colors)] ) time.sleep(0.025) # -------------------------------- # Inner spiral # -------------------------------- t.width(3) t.penup() t.goto(0, 0) t.pendown() for i in range(220): radius = i * 0.65 angle = i * 13 x = radius * math.cos(math.radians(angle)) y = radius * math.sin(math.radians(angle)) t.color(colors[i % len(colors)]) t.goto(x, y) screen.update() time.sleep(0.006) # -------------------------------- # 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.04) t.penup() t.goto(0, -5) t.dot(8, "white") screen.update() turtle.done()







































Explanation:

1. Import Libraries
import turtle
import math
import time
turtle → Used for drawing.
math → Used for trigonometric calculations.
time → Used to control animation speed.

2. Create the Screen
screen = turtle.Screen()
screen.setup(800, 800)
screen.bgcolor("#020208")
Creates the Turtle window.
Sets the size to 800 × 800.
Gives the screen a dark background.

3. Create the Turtle
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
Creates the drawing turtle.
Hides the turtle cursor.
Sets the fastest drawing speed.

4. Define Neon Colors
colors = [
    "#00f5ff",
    "#2979ff",
    "#7c4dff",
    "#d500f9",
    "#ff2d75",
    "#ff6d00",
    "#ffe600"
]
Stores multiple neon colors.
These colors are reused throughout the animation.

5. Create the Ring Function
def draw_ring(radius, rotation, color):
Defines a function for drawing one polygon ring.
radius controls size.
rotation controls angle.
color controls the ring color.

6. Set Ring Properties
points = 10
t.color(color)
t.width(2)
Each ring has 10 points.
Sets the drawing color.
Sets line thickness to 2.

7. Loop Through Points
for i in range(points + 1):
Loops through all polygon points.
+1 closes the polygon.

8. Calculate the Angle
angle = rotation + i * (360 / points)
Divides the circle into equal sections.
Adds rotation to make each ring turn.

9. Calculate X Position
x = radius * math.cos(math.radians(angle))
Converts the angle to radians.
Calculates the X coordinate.

10. Calculate Y Position
y = radius * math.sin(math.radians(angle))
Calculates the Y coordinate using sine.

11. Move to the First Point
if i == 0:
    t.penup()
    t.goto(x, y)
    t.pendown()
Moves to the first point without drawing.
Then puts the pen down to start drawing.

12. Connect the Points
else:
    t.goto(x, y)
Moves between points.
Creates the polygon ring.

13. Create Animation
screen.update()
time.sleep(0.004)
Updates the screen.
Adds a tiny delay for smooth animation.

14. Create the Outer Tunnel
for i in range(55):
Creates 55 rings.
radius = 270 - i * 4.6
Gradually decreases the ring size.
Creates the tunnel effect.
rotation = i * 6
Rotates every new ring.
draw_ring(radius, rotation, colors[i % len(colors)])
Draws each ring.
Cycles through the neon colors.

15. Create the Inner Spiral
t.width(3)
Makes the spiral line thicker.
t.penup()
t.goto(0, 0)
t.pendown()
Starts the spiral from the center.
for i in range(220):
Generates 220 spiral points.
radius = i * 0.65
angle = i * 13
Radius gradually increases.
Angle continuously rotates.
x = radius * math.cos(math.radians(angle))
y = radius * math.sin(math.radians(angle))
Calculates each spiral point.
t.color(colors[i % len(colors)])
t.goto(x, y)
Changes the color.
Draws the spiral.


16. Create the Glowing Center
for r in range(30, 2, -3):
Creates several circles.
Each circle becomes smaller.
t.penup()
t.goto(0, -r)
Positions the turtle for the glow.
t.dot(r, colors[r % len(colors)])
Draws colored dots.
Creates the glowing-center effect.

17. Add the White Core
t.penup()
t.goto(0, -5)
t.dot(8, "white")
Adds a small white dot.
Creates the bright center.

18. Finish the Animation
screen.update()
turtle.done()
Updates the final frame.
Keeps the Turtle window open.



0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (345) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (359) Bootcamp (14) 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 (433) 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) Generative AI (77) 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 (405) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1377) Python Coding Challenge (1251) Python Library (8) Python Mathematics (19) Python Mistakes (51) Python Pattern Challenge (11) Python Quiz (640) 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)