Take Your Python Skills to the Next Level with These Advanced Turtle Examples


 1. Making Painting:

import turtle


my_turtle = turtle.Turtle()

# Draw sky
my_turtle.penup()
my_turtle.goto(-300, 200)
my_turtle.pendown()
my_turtle.begin_fill()
my_turtle.color("#B0E0E6")
my_turtle.goto(300, 200)
my_turtle.goto(300, -100)
my_turtle.goto(-300, -100)
my_turtle.goto(-300, 200)
my_turtle.end_fill()

# Draw sun
my_turtle.penup()
my_turtle.goto(-200, 100)
my_turtle.pendown()
my_turtle.begin_fill()
my_turtle.color("#FFD700")
my_turtle.circle(50)
my_turtle.end_fill()

# Draw mountains
my_turtle.penup()
my_turtle.goto(-300, -100)
my_turtle.pendown()
my_turtle.begin_fill()
my_turtle.color("#808080")
my_turtle.goto(-150, 100)
my_turtle.goto(0, -50)
my_turtle.goto(150, 100)
my_turtle.goto(300, -100)
my_turtle.goto(-300, -100)
my_turtle.end_fill()

# Draw grass
my_turtle.penup()
my_turtle.goto(-300, -100)
my_turtle.pendown()
my_turtle.begin_fill()
my_turtle.color("#228B22")
my_turtle.goto(300, -100)
my_turtle.goto(300, -200)
my_turtle.goto(-300, -200)
my_turtle.goto(-300, -100)
my_turtle.end_fill()

turtle.done()



2. Making Home Painting:

import turtle

my_turtle = turtle.Turtle()

# Set initial position and color
my_turtle.penup()
my_turtle.goto(-150, 150)
my_turtle.pendown()
my_turtle.pensize(3)
my_turtle.speed(0)

# Draw sky
my_turtle.color("#87CEEB")
my_turtle.begin_fill()
my_turtle.forward(600)
my_turtle.right(90)
my_turtle.forward(300)
my_turtle.right(90)
my_turtle.forward(600)
my_turtle.right(90)
my_turtle.forward(300)
my_turtle.end_fill()

# Draw sun
my_turtle.penup()
my_turtle.goto(250, 200)
my_turtle.pendown()
my_turtle.color("#FFD700")
my_turtle.begin_fill()
my_turtle.circle(50)
my_turtle.end_fill()

# Draw clouds
def draw_cloud(x, y):
my_turtle.penup()
my_turtle.goto(x, y)
my_turtle.pendown()
my_turtle.color("#FFFFFF")
my_turtle.begin_fill()
my_turtle.circle(30)
my_turtle.circle(20, 180)
my_turtle.circle(30, 180)
my_turtle.end_fill()

draw_cloud(-150, 75)
draw_cloud(-100, 100)
draw_cloud(0, 75)
draw_cloud(50, 100)

# Draw mountains
my_turtle.penup()
my_turtle.goto(-150, 0)
my_turtle.pendown()
my_turtle.color("#8B4513")
my_turtle.begin_fill()
my_turtle.goto(0, 150)
my_turtle.goto(150, 0)
my_turtle.goto(-150, 0)
my_turtle.end_fill()

# Draw trees
def draw_tree(x, y):
my_turtle.penup()
my_turtle.goto(x, y)
my_turtle.pendown()
my_turtle.color("#8B4513")
my_turtle.begin_fill()
my_turtle.forward(20)
my_turtle.right(90)
my_turtle.forward(40)
my_turtle.right(90)
my_turtle.forward(20)
my_turtle.right(90)
my_turtle.forward(40)
my_turtle.end_fill()
my_turtle.color("#32CD32")
my_turtle.begin_fill()
my_turtle.circle(20)
my_turtle.end_fill()

draw_tree(-120, -100)
draw_tree(50, -100)

turtle.done()   


3. Draw a colorful spiral pattern 

import turtle

# Set up the turtle window
window = turtle.Screen()
window.bgcolor("black")

# Create the turtle
t = turtle.Turtle()
t.speed(0)
t.pensize(2)

# Set up the colors
colors = ["red", "orange", "yellow", "green", "blue", "purple"]

# Draw the spiral pattern
for i in range(360):
t.pencolor(colors[i%6])
t.forward(i * 2)
t.left(59)

# Close the turtle window when finished
turtle.done()
  
 Output : 

4. Drawing a fractal tree:



import
turtle

def fractal_tree(t, branch_len):
if branch_len > 5:
t.forward(branch_len)
t.right(20)
fractal_tree(t, branch_len - 15)
t.left(40)
fractal_tree(t, branch_len - 15)
t.right(20)
t.backward(branch_len)

# Set up the turtle
t = turtle.Turtle()
t.speed(0)
t.left(90)
t.up()
t.backward(200)
t.down()

# Draw the fractal tree
fractal_tree(t, 100)

# Close the turtle window when finished
turtle.done()

5. Creating a Spirograph pattern:
import turtle
import math

def spirograph(t, R, r, l):
k = r / R
for i in range(0, 360 * l + 1, 5):
a = math.radians(i)
x = R * ((1 - k) * math.cos(a) + l * k * math.cos((1 - k) * a / k))
y = R * ((1 - k) * math.sin(a) - l * k * math.sin((1 - k) * a / k))
t.goto(x, y)

# Set up the turtle
t = turtle.Turtle()
t.speed(0)
t.up()
t.goto(-200, 0)
t.down()

# Draw the Spirograph pattern
spirograph(t, 150, 50, 5)

# Close the turtle window when finished
turtle.done()


6. Creating a Sierpinski triangle:
import turtle

def sierpinski(t, order, size):
if order == 0:
for i in range(3):
t.forward(size)
t.left(120)
else:
sierpinski(t, order - 1, size / 2)
t.forward(size / 2)
sierpinski(t, order - 1, size / 2)
t.backward(size / 2)
t.left(60)
t.forward(size / 2)
t.right(60)
sierpinski(t, order - 1, size / 2)
t.left(60)
t.backward(size / 2)
t.right(60)

# Set up the turtle
t = turtle.Turtle()
t.speed(0)
t.up()
t.goto(-150, -150)
t.down()

# Draw the Sierpinski triangle
sierpinski(t, 4, 300)

# Close the turtle window when finished
turtle.done()


7. Drawing a Spirograph - like with circles:


import
turtle

def draw_circle(t, size, angle):
for i in range(36):
t.circle(size)
t.right(angle)

# Set up the turtle
t = turtle.Turtle()
t.speed(0)
t.up()
t.goto(-150, 0)
t.down()

# Draw the circles
for i in range(0, 360, 15):
t.pencolor("blue")
draw_circle(t, 50, 5)
t.left(i)

# Close the turtle window when finished
turtle.done()


Post a Comment

© T and P Knowledge. All rights reserved. Developed by Jago Desain