Code:
import turtle
import math
import time
screen = turtle.Screen()
screen.setup(650, 700)
screen.bgcolor("#fff8fa")
screen.tracer(0)
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
t.width(3)
# ๐ฟ Stem
t.color("#168039")
t.penup()
t.goto(0, -280)
t.setheading(90)
t.pendown()
t.forward(230)
# ๐ Leaves
for side in [-1, 1]:
t.penup()
t.goto(0, -170)
t.setheading(140 if side == -1 else 40)
t.color("#22a447")
t.fillcolor("#22a447")
t.begin_fill()
for _ in range(2):
t.circle(70, 60)
t.left(120)
t.end_fill()
# ๐น Rose petals
colors = ["#b3003b", "#e60045", "#ff1744", "#ff4d6d"]
for layer in range(8):
radius = 35 + layer * 18
t.color(colors[layer % len(colors)])
for petal in range(10):
angle = petal * 36 + layer * 4
t.penup()
for i in range(31):
a = math.radians(i * 6)
r = radius * math.sin(a)
x = r * math.cos(a)
y = r * math.sin(a) * 0.65
rot = math.radians(angle)
X = x * math.cos(rot) - y * math.sin(rot)
Y = x * math.sin(rot) + y * math.cos(rot)
t.goto(X, Y)
if i == 0:
t.pendown()
screen.update()
time.sleep(0.012)
time.sleep(0.08)
# ๐ผ Rose center
t.penup()
t.goto(0, -10)
t.dot(22, "#8f0030")
screen.update()
time.sleep(1)
turtle.done()
Explanation:
1. Import Libraries
import turtleimport mathimport time
- turtle draws the rose.
- math calculates curves and angles.
- time controls animation speed.
2. Create the Screen
screen = turtle.Screen()screen.setup(650, 700)screen.bgcolor("#fff8fa")screen.tracer(0)
- Creates a 650 × 700 canvas.
- Sets a light pink background.
- Disables automatic screen updates.
Note: Remove five for this from your code because it will cause a syntax error.
3. Configure the Turtle
t = turtle.Turtle()t.hideturtle()t.speed(0)t.width(3)
- Hides the turtle cursor.
- Sets maximum drawing speed and line thickness.
4. Draw the Stem
t.color("#168039")t.penup()t.goto(0, -280)t.setheading(90)t.pendown()t.forward(230)
- Moves to the bottom of the canvas.
- Points upward and draws a green stem.
5. Draw the Leaves
for side in [-1, 1]:
- Creates two leaves on opposite sides.
t.goto(0, -170)t.setheading(140 if side == -1 else 40)
- Positions the turtle and sets each leaf's direction.
t.begin_fill()for _ in range(2): t.circle(70, 60) t.left(120)t.end_fill()
- Draws curved leaf shapes.
- Fills them with green color.
6. Define Rose Colors
colors = ["#b3003b", "#e60045", "#ff1744", "#ff4d6d"]
- Defines four shades of red and pink for the petals.
7. Create Rose Layers
for layer in range(8): radius = 35 + layer * 18
- Creates eight layers.
- Increases the radius to make the rose expand outward.
8. Create Petals
for petal in range(10): angle = petal * 36 + layer * 4
- Creates ten petal curves per layer.
- Rotates each petal to form a circular arrangement.
9. Calculate Petal Curves
for i in range(31): a = math.radians(i * 6) r = radius * math.sin(a)
- Generates 31 points for each curve.
- Uses the sine function to create a curved petal shape.
10. Calculate Coordinates
x = r * math.cos(a)y = r * math.sin(a) * 0.65
- Calculates horizontal and vertical positions.
- The 0.65 factor makes the petal flatter.
11. Rotate the Petals
rot = math.radians(angle)X = x * math.cos(rot) - y * math.sin(rot)Y = x * math.sin(rot) + y * math.cos(rot)
- Converts the angle into radians.
- Rotates the coordinates to position petals around the rose.
12. Draw and Animate Petals
t.goto(X, Y)if i == 0: t.pendown()screen.update()time.sleep(0.012)
- Moves through the calculated points.
- Draws the petal curves.
- Updates the screen with a short delay.
time.sleep(0.08)
- Pauses briefly after each petal.
13. Add the Rose Center
t.penup()t.goto(0, -10)t.dot(22, "#8f0030")
- Draws a dark red circle at the center of the rose.
14. Finish the Drawing
screen.update()time.sleep(1)turtle.done()
- Displays the final rose.
- Waits one second and keeps the window open.


0 Comments:
Post a Comment