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