stoneskin

learning coding with Scratch , Python and Java

index

02 Drawing with Python Turtle

2.0 Introduction

“Turtle” is a fun and easy-to-use feature in Python that acts like a drawing board. Imagine a turtle that you can control with your commands to create amazing drawings on the screen. By telling the turtle where to move and what to draw, you can explore the exciting world of programming while unleashing your creativity!

2.1 Drawing lines

The first thing you need to do before using Turtle is to import the Turtle module with this line of code:

import turtle

Check more about the python turtle doc

2.1.1 Drawing a line with python Turtle

In your Python editor, create a new file and name it as MyFirstTurtle.py

# Code sample MyFirstTurtle.py
import turtle

# Draw line with length 100
turtle.forward(100)

# After drawing, you can keep the window open by adding the following line. This allows you to view what you have drawn.
turtle.done() 

Notice the turtle.done() function, this is used for terminal waiting for user to close the python running window.

myfirstptyhon

2.1.2 Make turn and draw another line

please add below code before turtle.done() of code in 2.1


# make turn 90 degree and draw another line
turtle.left(90)
turtle.forward(100)

2.1.3 Continue and draw a square

Repeat the code in 2.2 twice, you could get a square. Below example, in stand of using turtle directly, I chose use a turtle.Pen() instance.

# Code sample MyFirstTurtle.py
import turtle
t = turtle.Pen() # make a turtle Pen instance.

# Draw line with length 100
t.forward(100)

# make turn 90 degree and draw another line
t.left(90)
t.forward(100)

# continue turn and drawing line twice
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)

# not close the window
turtle.done()

square

2.1.4 Making a Right Turn

The command to make the turtle turn right is t.right(). Try using this command to draw the stair-step pattern shown below.

Challenge: Can you write the code to create this pattern?

# Hint: You will need to use a combination of forward(), right(), and left()
import turtle

t = turtle.Turtle()

# Your code here to draw the stairs

turtle.done()

stair

2.2 Advanced

2.2.1 Working with Multiple Turtles

You can create more than one turtle to draw on the screen at the same time. Each turtle is an independent object with its own position and orientation.

import turtle

# Create two turtle objects
t1 = turtle.Pen()
t2 = turtle.Pen()

# Move the turtles to different starting positions so they don't draw on top of each other
t1.penup()
t1.goto(-50, 50)
t1.pendown()

t2.penup()
t2.goto(50, -50)
t2.pendown()

# Draw with each turtle
t1.right(90)
t1.forward(100)

t2.left(90)
t2.forward(100)

# Exit when the screen is clicked
turtle.exitonclick()

2.2.2 Customizing the Screen

You can customize the drawing window by setting the background color and title.

import turtle

# Create a screen object
screen = turtle.Screen()

# Set the background color and title
screen.bgcolor("lightblue")
screen.title("My Turtle Drawing")

# Create a turtle object
my_turtle = turtle.Turtle()

# Draw something with the turtle
my_turtle.forward(100)
my_turtle.right(90)

# Finish the drawing
turtle.done()

2.2.3 loop repeat 100 times

Try loop in python, draw 100 squares.

  # Drawing 100 squares
  import turtle
  t= turtle.Pen()

  for x in range(0,100): # repeat 100 times
      # Draw line with length 100
      t.forward(100+x)

      # make turn 90 degree and draw another line
      t.left(90)
      t.forward(100+x)

      # continue turn and drawing line twice
      t.left(90)
      t.forward(100+x)
      t.left(90)
      t.forward(100+x)
      print(x) # x will be from 0 to 99
      
  #when you click the screen, exit the python
  turtle.exitonclick()

Note: Python using Indentation (spaces before line of code) to indicate a block of code.

indentation

2.3 Practice and challenge

Run Turtle on Google Colab

prev charter next charter