Wednesday, 23 September 2026

Python Turtle: A Heart Made of Code







 Code:

import turtle import math import time screen = turtle.Screen() screen.setup(700, 700) screen.bgcolor("#03000a") screen.tracer(0) t = turtle.Turtle() t.hideturtle() t.speed(0) colors = ["#ff1744", "#ff4081", "#d500f9", "#7c4dff", "#00e5ff"] # ❤️ Neon Heart for i in range(360): a = math.radians(i) x = 16 * math.sin(a) ** 3 y = ( 13 * math.cos(a) - 5 * math.cos(2*a) - 2 * math.cos(3*a) - math.cos(4*a) ) scale = 15 t.penup() t.goto(0, 0) t.pendown() t.color(colors[i % len(colors)]) t.goto(x * scale, y * scale) t.dot(3 + i % 3) screen.update() time.sleep(0.02) # ✨ 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.06) # ⭐ Small Sparkles for i in range(25): angle = i * 137.5 radius = 230 + (i % 4) * 15 x = radius * math.cos(math.radians(angle)) y = radius * math.sin(math.radians(angle)) t.penup() t.goto(x, y) t.dot(2 + i % 3, colors[i % len(colors)]) screen.update() time.sleep(0.03) turtle.done()


























Explanation:

1. Import Libraries
import turtle
import math
import time
turtle → Drawing.
math → Mathematical calculations.
time → Animation delays.

2. Create the Screen
screen = turtle.Screen()
screen.setup(700, 700)
screen.bgcolor("#03000a")
screen.tracer(0)
Creates a 700 × 700 window.
Sets a dark background.
tracer(0) gives manual screen updates.

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

4. Define Neon Colors
colors = [...]
Stores the colors used for the heart, glow, and sparkles.

5. Generate the Heart
for i in range(360):
Creates 360 points around the heart.
a = math.radians(i)
Converts the angle from degrees to radians.

6. Calculate Heart Coordinates
x = 16 * math.sin(a) ** 3
Calculates the X-coordinate using the heart equation.
y = (
    13 * math.cos(a)
    - 5 * math.cos(2*a)
    - 2 * math.cos(3*a)
    - math.cos(4*a)
)
Calculates the Y-coordinate.
Together, these equations create the heart shape.

7. Scale the Heart
scale = 15
Enlarges the mathematical heart.

8. Move to Each Point
t.penup()
t.goto(0, 0)
t.pendown()
Moves to the center without drawing.
Starts drawing from the center.

9. Draw the Neon Heart
t.color(colors[i % len(colors)])
t.goto(x * scale, y * scale)
t.dot(3 + i % 3)
Cycles through neon colors.
Draws each heart point.
Adds small glowing dots.

10. Animate the Heart
screen.update()
time.sleep(0.02)
Updates the screen.
Adds a small delay for the drawing animation.

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

12. Add Sparkles
for i in range(25):
Creates 25 sparkles.
angle = i * 137.5
radius = 230 + (i % 4) * 15
Generates different angles and distances.
Spreads the sparkles around the heart.

13. Calculate Sparkle Positions
x = radius * math.cos(math.radians(angle))
y = radius * math.sin(math.radians(angle))
Converts the angle and radius into X/Y coordinates.

14. Draw the Sparkles
t.goto(x, y)
t.dot(2 + i % 3, colors[i % len(colors)])
Moves to each position.
Draws colorful dots of different sizes.

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










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 (18) Deep Learning (221) 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 (404) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1377) Python Coding Challenge (1250) Python Library (7) Python Mathematics (18) Python Mistakes (51) Python Pattern Challenge (10) Python Quiz (639) 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)