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