Saturday, 26 September 2026

✨ Python Turtle The Neon Spiral Cube


 



Code :

import turtle import math import time screen = turtle.Screen() screen.setup(700, 700) screen.bgcolor("#02030a") t = turtle.Turtle() t.hideturtle() t.speed(0) t.width(2) colors = [ "#00e5ff", "#2979ff", "#7c4dff", "#d500f9", "#ff2d75", "#00ff9d" ] # Neon spiral squares for i in range(45): size = 260 - i * 5 angle = i * 7 t.color(colors[i % len(colors)]) t.penup() for j in range(4): a = math.radians(angle + j * 90) x = size * math.cos(a) y = size * math.sin(a) if j == 0: t.goto(x, y) t.pendown() else: t.goto(x, y) screen.update() time.sleep(0.06) # slow drawing of each side # Close square t.goto( size * math.cos(math.radians(angle)), size * math.sin(math.radians(angle)) ) screen.update() time.sleep(0.12) # pause between squares # Glowing center for r in range(25, 2, -3): t.penup() t.goto(0, -r) t.dot(r, colors[r % len(colors)]) screen.update() time.sleep(0.10) turtle.done()








































Explanation:


1. Import Libraries
import turtle
import math
import time
turtle → Drawing.
math → Angle and coordinate calculations.
time → Controls animation speed.

2. Create the Screen
screen = turtle.Screen()
screen.setup(700, 700)
screen.bgcolor("#02030a")
Creates a 700 × 700 window.
Sets a dark background.

3. Configure the Turtle
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
t.width(2)
Creates the turtle.
Hides the cursor.
Uses maximum drawing speed.
Sets line width to 2.

4. Define Neon Colors
colors = [...]
Stores multiple neon colors.
Colors are rotated through the squares.

5. Create Spiral Squares
for i in range(45):
Creates 45 squares.
Each square becomes smaller.

6. Set Size and Rotation
size = 260 - i * 5
angle = i * 7
Decreases the square size.
Rotates each new square by 7°.

7. Select the Color
t.color(colors[i % len(colors)])
Cycles through the neon colors.

8. Draw Four Corners
for j in range(4):
A square has four corners.
Each corner is calculated separately.

9. Calculate Corner Position
a = math.radians(angle + j * 90)

x = size * math.cos(a)
y = size * math.sin(a)
Adds 90° for each corner.
Calculates the X and Y coordinates.

10. Connect the Corners
if j == 0:
    t.goto(x, y)
    t.pendown()
else:
    t.goto(x, y)
Moves to the first corner without drawing.
Connects the remaining corners with lines.

11. Animate Each Side
screen.update()
time.sleep(0.06)
Updates the screen.
Adds a small delay for a visible drawing effect.

12. Close the Square
t.goto(
    size * math.cos(math.radians(angle)),
    size * math.sin(math.radians(angle))
)
Returns to the first corner.
Completes the square.

13. Pause Between Squares
time.sleep(0.12)
Adds a longer pause.
Makes the spiral formation easier to see.

14. Create the Glowing Center
for r in range(25, 2, -3):
Creates several shrinking circles.
t.penup()
t.goto(0, -r)
t.dot(r, colors[r % len(colors)])
Places colorful dots near the center.
Creates a glowing-core effect.

15. Finish
turtle.done()
Keeps the Turtle window open.
Ends the animation.




0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (346) 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 (434) 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 (407) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1377) Python Coding Challenge (1253) Python Library (10) Python Mathematics (19) Python Mistakes (51) Python Pattern Challenge (13) Python Quiz (642) 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)