Рубрики

drawing

Progression of creating a turtle drawing

Python comes with a module for Turtle. It includes a cardboard sketching screen and a turtle (pen). To create anything on the screen, move the turtle (pen). Other operations, such as forward() and backward(), can move the turtle.


2. Our First Turtle Program¶

Let’s try a couple of lines of Python code to create a new turtle and start drawing a simple figure like a rectangle. We will refer to our first turtle using the variable name alex, but remember that you can choose any name you wish as long as you follow the naming rules from the previous chapter.

The program as shown will only draw the first two sides of the rectangle. After line 4 you will have a straight line going from the center of the drawing canvas towards the right. After line 6, you will have a canvas with a turtle and a half drawn rectangle. Press the run button to try it and see.

Here are a couple of things you’ll need to understand about this program.

The first line tells Python to load a module named turtle . That module brings us two new types that we can use: the Turtle type, and the Screen type. The dot notation turtle.Turtle means “The Turtle type that is defined within the turtle module”. (Remember that Python is case sensitive, so the module name, turtle , with a lowercase t , is different from the type Turtle because of the uppercase T .)

We then create and open what the turtle module calls a screen (we would prefer to call it a window, or in the case of this web version of Python simply a canvas), which we assign to variable wn . Every window contains a canvas, which is the area inside the window on which we can draw.

In line 3 we create a turtle. The variable alex is made to refer to this turtle. These first three lines set us up so that we are ready to do some drawing.

In lines 4-6, we instruct the object alex to move and to turn. We do this by invoking or activating alex’s methods — these are the instructions that all turtles know how to respond to. Here the dot indicates that the methods invoked belong to and refer to the object alex .

Complete the rectangle …

Modify the program by adding the commands necessary to have alex complete the rectangle.

Check your understanding

Which direction does the Turtle face when it is created?

Mixed up programs

The following program uses a turtle to draw a capital L as shown in the picture to the left of this text,

../_images/TurtleL4.png

But the lines are mixed up. The program should do all necessary set-up: import the turtle module, get the window to draw on, and create the turtle. Remember that the turtle starts off facing east when it is created. The turtle should turn to face south and draw a line that is 150 pixels long and then turn to face east and draw a line that is 75 pixels long. We have added a compass to the picture to indicate the directions north, south, west, and east.

Drag the blocks of statements from the left column to the right column and put them in the right order. Then click on Check Me to see if you are right. You will be told if any of the lines are in the wrong order.

import turtle window = turtle.Screen() ella = turtle.Turtle() --- ella.right(90) ella.forward(150) --- ella.left(90) ella.forward(75)

The following program uses a turtle to draw a checkmark as shown to the left:

../_images/TurtleCheckmark4.png

But the lines are mixed up. The program should do all necessary set-up: import the turtle module, get the window to draw on, and create the turtle. The turtle should turn to face southeast, draw a line that is 75 pixels long, then turn to face northeast, and draw a line that is 150 pixels long. We have added a compass to the picture to indicate the directions north, south, west, and east. Northeast is between north and east. Southeast is between south and east.

Drag the blocks of statements from the left column to the right column and put them in the right order. Then click on Check Me to see if you are right. You will be told if any of the lines are in the wrong order.

import turtle --- window = turtle.Screen() --- maria = turtle.Turtle() --- maria.right(45) maria.forward(75) --- maria.left(90) maria.forward(150)

The following program uses a turtle to draw a single line to the west as shown to the left,

../_images/TurtleLineToWest.png

But the program lines are mixed up. The program should do all necessary set-up: import the turtle module, get the window to draw on, and create the turtle. The turtle should then turn to face west and draw a line that is 75 pixels long.

Drag the blocks of statements from the left column to the right column and put them in the right order. Then click on Check Me to see if you are right. You will be told if any of the lines are in the wrong order.

import turtle window = turtle.Screen() jamal = turtle.Turtle() jamal.left(180) jamal.forward(75)

An object can have various methods — things it can do — and it can also have attributes — (sometimes called properties). For example, each turtle has a color attribute. The method invocation alex.color(“red”) will make alex red and the line that it draws will be red too.

The color of the turtle, the width of its pen(tail), the position of the turtle within the window, which way it is facing, and so on are all part of its current state. Similarly, the window object has a background color which is part of its state.

Quite a number of attributes and methods exist that allow us to modify the turtle and window objects. In the example below, we show just show a couple and have only commented those lines that are different from the previous example. Note also that we have decided to call our turtle object tess.

The last line plays a very important role. The wn variable refers to the window shown above. When we invoke its exitonclick method, the program pauses execution and waits for the user to click the mouse somewhere in the window. When this click event occurs, the response is to close the turtle window and exit (stop execution of) the Python program.

Each time we run this program, a new drawing window pops up, and will remain on the screen until we click on it.

Extend this program …

  1. Modify this program so that before it creates the window, it prompts the user to enter the desired background color. It should store the user’s responses in a variable, and modify the color of the window according to the user’s wishes. (Hint: you can find a list of permitted color names at https://www.w3schools.com/colors/colors_names.asp. It includes some quite unusual ones, like “PeachPuff” and “HotPink”.)
  2. Do similar changes to allow the user, at runtime, to set tess’ color.
  3. Do the same for the width of tess’ pen. Hint: your dialog with the user will return a string, but tess’ pensize method expects its argument to be an int . That means you need to convert the string to an int before you pass it to pensize .

Check your understanding

Consider the following code:

import turtle wn = turtle.Screen() alex = turtle.Turtle() alex.forward(150) alex.left(90) alex.forward(75) 

What does the line “import turtle” do?

Why do we type turtle.Turtle() to get a new Turtle object?

True or False: A Turtle object can have any name that follows the naming rules from Chapter 2.

Which of the following code would produce the following image?

long line to north with shorter line to west on top

  • right turn of 90 degrees before drawing, draw a line 150 pixels long, turn left 90, and draw a line 75 pixels long
  • This code would turn the turtle to the south before drawing
  • left turn of 180 degrees before drawing, draw a line 150 pixels long, turn left 90, and draw a line 75 pixels long
  • This code would turn the turtle to the west before drawing
  • left turn of 270 degrees before drawing, draw a line 150 pixels long, turn left 90, and draw a line 75 pixels long
  • This code would turn the turtle to the south before drawing
  • right turn of 270 degrees before drawing, draw a line 150 pixels long, turn right 90, and draw a line 75 pixels long
  • This code is almost correct, but the short end would be facing east instead of west.
  • left turn of 90 degrees before drawing, draw a line 150 pixels long, turn left 90, and draw a line 75 pixels long
  • Yes, the turtle starts facing east, so to turn it north you can turn left 90 or right 270 degrees.
  • Mixed up programs

    The following program uses a turtle to draw a capital L in white on a blue background as shown to the left,

    ../_images/BlueTurtleL.png

    But the lines are mixed up. The program should do all necessary set-up and create the turtle and set the pen size to 10. The turtle should then turn to face south, draw a line that is 150 pixels long, turn to face east, and draw a line that is 75 pixels long. Finally, set the window to close when the user clicks in it.

    Drag the blocks of statements from the left column to the right column and put them in the right order. Then click on Check Me to see if you are right. You will be told if any of the lines are in the wrong order.

    import turtle wn = turtle.Screen() --- wn.bgcolor("blue") jamal = turtle.Turtle() --- jamal.color("white") jamal.pensize(10) --- jamal.right(90) jamal.forward(150) --- jamal.left(90) jamal.forward(75) wn.exitonclick()

    The following program uses a turtle to draw a capital T in white on a green background as shown to the left,

    ../_images/TurtleT.png

    But the lines are mixed up. The program should do all necessary set-up, create the turtle, and set the pen size to 10. After that the turtle should turn to face north, draw a line that is 150 pixels long, turn to face west, and draw a line that is 50 pixels long. Next, the turtle should turn 180 degrees and draw a line that is 100 pixels long. Finally, set the window to close when the user clicks in it.

    Drag the blocks of statements from the left column to the right column and put them in the right order. Then click on Check Me to see if you are right. You will be told if any of the lines are in the wrong order.

    import turtle wn = turtle.Screen() wn.bgcolor("green") jamal = turtle.Turtle() jamal.color("white") jamal.pensize(10) --- jamal.left(90) jamal.forward(150) --- jamal.left(90) jamal.forward(50) --- jamal.right(180) jamal.forward(100) --- wn.exitonclick()

    You have attempted of activities on this page

    © Copyright 2014 Brad Miller, David Ranum, Created using Runestone Interactive. Last updated on Nov 04, 2023.
    Created using Runestone 7.2.4.

    1. History of Turtles¶

    The idea of a virtual turtle, dates back to the 1960’s. Seymour Papert and his group at MIT created the programming language LOGO which could control a robot turtle with a physical pen in it. Kids would steer the robot around, and it would draw as it moved. Seymour said that the Turtle was, “an object to think with”. Many modern programming languages support Turtles, including Python.

    Children playing with a Logo turtle robot that can draw with a pen

    Today, we can play with virtual turtles in a fully-graphical and non-robotic way. Below is a Python program that first creates a Screen object (object of the Screen class) for the turtle to draw on and then creates a Turtle object (object of the Turtle class). The program asks the turtle object to perform some of its behaviors (methods) like moving forward a set amount and turning. The turtle draws as it moves.

    Click the Run button to see what this code does. The drawing will happen below the code.

    csp-5-4-2: Which way does a turtle (object of the Turtle class) face when it is first created?

    Notice that we tell alex the turtle what to do in the code above using dot notation: alex.forward(150) , alex.left(90) , and alex.forward(75) . That is how you communicate with an object. You use the name of the turtle object followed by a . and then the name of the method that you want to execute.

    Just by going forward and turning we can have a turtle object draw many things.

    1.1. What does a left turn of 90 mean?¶

    When we ask a turtle to turn left, it will turn left based on the direction it is currently heading. A turtle object keeps track of its heading (direction it is facing). Use the figure below to help you understand how much the turtle will turn if asked to turn left 90 degrees and it is currently heading east (0 degrees).

    shows what a turn of each degrees means for left and right turns

    2. Practice with Turtles¶

    The following problems have a Help Me button. You can click on the Help Me button after you have made at least 3 full and distinct attempts to solve the problem to make the problem easier.

    The following program uses a turtle to draw a capital L as shown below, but the lines are mixed up. The program should do all necessary set-up: import the turtle module, get the screen/space to draw on, and create the turtle. The turtle should turn to face south, draw a line that is 150 pixels long, then turn to face east, and draw a line that is 75 pixels long. We have added a compass to the picture to indicate the directions north, south, west, and east. Drag the needed blocks of statements from the left column to the right column and put them in the right order. There may be additional blocks that are not needed in a correct solution. Then click on Check to see if you are right. You will be told if any of the lines are in the wrong order or are the wrong blocks.

    ../_images/TurtleL4.png

    from turtle import * --- from turtle Import * #paired --- space = Screen() --- space = screen() #paired --- ella = Turtle() --- ella.right(90) --- ella.turn(90) #paired --- ella.forward(150) --- ella.left(90) --- ella.forward(75) --- ella.go(75) #paired

    The following program uses a turtle to draw a checkmark as shown below but the lines are mixed up. The program should do all necessary set-up: import the turtle module, get the screen/space to draw on, and create the turtle. The turtle should turn to face southeast, draw a line that is 75 pixels long, then turn to face northeast, and draw a line that is 150 pixels long. We have added a compass to the picture to indicate the directions north, south, west, and east. Northeast is between north and east. Southeast is between south and east. Drag the needed blocks of statements from the left column to the right column and put them in the right order. There may be additional blocks that are not needed in a correct solution. Then click on Check to see if you are right. You will be told if any of the lines are in the wrong order or are the wrong blocks.

    ../_images/checkMark.png

    from turtle import * --- space = Screen() --- maria = Turtle() --- maria = Turtle #paired --- maria.right(45) --- maria.left(45) #paired --- maria.forward(75) --- maria.Forward(75) #paired --- maria.left(90) --- maria.right(90) #paired --- maria.forward(150)

    csp-5-4-5: What letter (like A, B, C, D, etc) will the program below draw in block style when you click on the Run button?

    Click the Run button to see what this code does. The drawing will happen below the code.

    The following example has 4 errors. Can you fix the errors so that the code runs correctly to print a capital L?

    Fix the code below. Error messages will shown up below the code. The drawing will also happen below the code.

    The following example has 4 errors. Can you fix the errors so that the code runs correctly to print a capital C?

    Fix the code below. Error messages will shown up below the code. The drawing will also happen below the code.

    Case matters in Python so screen is not the same as Screen . Also the open and close parentheses are required after every function and procedure call, even if it doesn’t take any input.

    Use the area below to try to draw a letter or number. Use block style rather than curves.

    You have attempted of activities on this page

    © Copyright 2020 – Based on Python for Everybody. Created using Runestone .


    B.Tech / MCA

    DBMS tutorial

    Data Structures tutorial

    DAA tutorial

    Operating System

    Javatpoint Services

    JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

    • Website Designing
    • Website Development
    • Java Development
    • PHP Development
    • WordPress
    • Graphic Designing
    • Logo
    • Digital Marketing
    • On Page and Off Page SEO
    • PPC
    • Content Development
    • Corporate Training
    • Classroom and Online Training
    • Data Entry

    Training For College Campus

    JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
    Duration: 1 week to 2 week

    Like/Subscribe us for latest updates or newsletter

    Colin Wynn
    the authorColin Wynn

    Leave a Reply