Thursday, 17 September 2026

Python Turtle Graphics - RGB & HSV Colors

 



Code:

import turtle import math t = turtle.Turtle() screen = turtle.Screen() screen.bgcolor("black") t.speed(0) t.hideturtle() t.width(2) for i in range(80): y = -240 + i * 6 x1 = 100 * math.sin(i * 0.25) x2 = -x1 t.color("cyan") t.penup() t.goto(x1, y) t.pendown() t.dot(7) t.color("magenta") t.penup() t.goto(x2, y) t.pendown() t.dot(7) t.color("white") t.penup() t.goto(x1, y) t.pendown() t.goto(x2, y) screen.mainloop()


Explanation:

1. Import Libraries
import turtle
import math
turtle → Used to draw graphics and shapes.
math → Provides mathematical functions such as sin().

2. Create Turtle and Screen
t = turtle.Turtle()
Creates a Turtle object named t.
This turtle acts as the drawing pen.
screen = turtle.Screen()
Creates the drawing window.
screen.bgcolor("black")
Sets the background color to black.

3. Configure the Turtle
t.speed(0)
Sets the turtle's drawing speed to maximum.
t.hideturtle()
Hides the turtle arrow/cursor while drawing.
t.width(2)
Sets the pen width to 2 pixels.

4. Create the Wave Using a Loop
for i in range(80):
Runs the loop 80 times.
i takes values from 0 to 79.
Each iteration creates one pair of dots and one connecting line.

5. Calculate the Vertical Position
y = -240 + i * 6
Calculates the y coordinate for each row.
Starting position is -240.
Every iteration moves upward by 6.

For example:

i = 0   → y = -240
i = 1   → y = -234
i = 2   → y = -228
...

This creates 80 horizontal levels.

6. Calculate the First Wave Position
x1 = 100 * math.sin(i * 0.25)
math.sin() generates a smooth wave-like movement.
i * 0.25 controls how quickly the wave changes.
100 controls the horizontal size/amplitude of the wave.

So x1 continuously moves left and right.

7. Create the Opposite Wave
x2 = -x1
Makes x2 the exact opposite of x1.
This creates a mirror effect.

For example:

x1 = 80
x2 = -80

So the two dots appear symmetrically around the center.

๐Ÿ”ต 8. Draw the Cyan Dot
t.color("cyan")
Changes the turtle drawing color to cyan.
t.penup()
Lifts the pen so movement doesn't create a line.
t.goto(x1, y)
Moves the turtle to the calculated (x1, y) position.
t.pendown()
Places the pen down again.
t.dot(7)
Draws a circular dot with size 7.

So this creates the left cyan wave.

๐ŸŸฃ 9. Draw the Magenta Dot
t.color("magenta")
Changes the drawing color to magenta.
t.penup()
Stops drawing while moving.
t.goto(x2, y)
Moves to the mirrored position (x2, y).
t.pendown()
Enables drawing again.
t.dot(7)
Creates the magenta dot.

This creates the right-side mirrored wave.

⚪ 10. Connect Both Dots
t.color("white")
Changes the line color to white.
t.penup()
Prevents a line from being drawn while moving to the first dot.
t.goto(x1, y)
Moves back to the cyan dot's position.
t.pendown()
Starts drawing.
t.goto(x2, y)
Draws a white horizontal line from the cyan dot to the magenta dot.

๐Ÿ” 11. Repeat the Pattern

The loop repeats everything:

for i in range(80):

Each repetition creates:

๐Ÿ”ต────────────๐ŸŸฃ
 ๐Ÿ”ต──────────๐ŸŸฃ
  ๐Ÿ”ต────────๐ŸŸฃ
    ๐Ÿ”ต────๐ŸŸฃ
      ๐Ÿ”ต๐ŸŸฃ

Because sin() changes the x positions, the endpoints form two mirrored waves.

๐Ÿ–ฅ️ 12. Keep the Window Open
screen.mainloop()
Keeps the Turtle window open.
Without this, the window may close immediately after the program finishes.

๐ŸŽฏ Main Concept

The most important line is:

x1 = 100 * math.sin(i * 0.25)

It uses the sine function to create the wave, while:

x2 = -x1

creates its mirror image. The result is a symmetrical neon-style wave pattern made from dots and connecting lines.













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 (46) Data Analytics (31) data management (16) Data Science (432) Data Strucures (18) Deep Learning (220) 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 (403) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1374) Python Coding Challenge (1241) Python Library (2) Python Mathematics (17) Python Mistakes (51) Python Pattern Challenge (5) Python Quiz (634) 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)