Thursday, 1 October 2026

๐ŸŒ€ Python Turtle The Spiral Illusion

 



Code:

import turtle import math import time screen = turtle.Screen() screen.setup(700, 700) screen.bgcolor("#020205") t = turtle.Turtle() t.hideturtle() t.speed(0) t.width(1) colors = ["#00ffff", "#ffb6d9", "#ffffff"] for i in range(90): r = 10 + i * 2.8 rotation = i * 4 points = [] # Rotating square for j in range(4): angle = math.radians(rotation + j * 90) x = r * math.cos(angle) y = r * math.sin(angle) points.append((x, y)) t.color(colors[i % 3]) t.penup() t.goto(points[0]) t.pendown() # Draw square slowly for p in points[1:]: t.goto(p) screen.update() time.sleep(0.10) # Close square t.goto(points[0]) screen.update() time.sleep(0.12) # Pause before next layer time.sleep(0.08) turtle.done()





















Explanation:

1. Import Libraries

import turtle

import math

import time

turtle → Used for drawing.

math → Calculates angles and coordinates.

time → Controls animation speed.


2. Create the Screen

screen = turtle.Screen()

screen.setup(700, 700)

screen.bgcolor("#020205")

Creates a 700 × 700 window.

Sets a dark background.


3. Configure the Turtle

t = turtle.Turtle()

t.hideturtle()

t.speed(0)

t.width(1)

Creates the turtle.

Hides the cursor.

Sets maximum drawing speed.

Uses a thin line.


4. Define Neon Colors

colors = ["#00ffff", "#ffb6d9", "#ffffff"]

Stores cyan, pink, and white colors.

These colors repeat throughout the pattern.


5. Create 90 Layers

for i in range(90):

Draws 90 rotating squares.

Each square creates another layer of the spiral.


6. Calculate Size and Rotation

r = 10 + i * 2.8

rotation = i * 4

r gradually increases the square size.

rotation rotates each square by 4°.


7. Create an Empty Point List

points = []

Creates a list to store the four square corners.


8. Calculate Four Corners

for j in range(4):

Runs four times because a square has four corners.

angle = math.radians(rotation + j * 90)

Calculates each corner's angle.

90° separates the four corners.


9. Calculate X and Y Coordinates

x = r * math.cos(angle)

y = r * math.sin(angle)

Calculates the position of each corner using trigonometry.


10. Store Each Point

points.append((x, y))

Saves the calculated (x, y) coordinate.



11. Select the Square Color

t.color(colors[i % 3])

Cycles through the three neon colors.


12. Move to the First Corner

t.penup()

t.goto(points[0])

t.pendown()

Moves to the first corner without drawing.

Starts drawing from that point.


13. Draw the Three Sides

for p in points[1:]:

Loops through the remaining three corners.

t.goto(p)

Connects each corner with a line.

screen.update()

time.sleep(0.10)

Updates the screen.

Creates a slow drawing animation.


14. Close the Square

t.goto(points[0])

Returns to the first corner.

Completes the fourth side.


15. Pause Between Layers

screen.update()

time.sleep(0.12)

time.sleep(0.08)

Updates the screen.

Adds pauses so each rotating layer is visible.


16. Finish the Drawing

turtle.done()

Keeps the Turtle window open.

Completes the neon spiral 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 (15) 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) 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 (1377) Python Coding Challenge (1263) Python Library (15) Python Mathematics (19) Python Mistakes (51) Python Pattern Challenge (15) Python Quiz (647) 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)