Students should be able to: use the turtle class to draw a picture Develop algorithms that use simple graphics operations to draw two-dimensional shapes. Turtle graphics originally developed as part of the children's programming language logo. Turtle is an object that has a position in a drawing window this object can be told to turn a number of degrees, move a given distance, move to a new position, change its colour and line width if the turtle's pen is down, it draws a line; otherwise, it just moves
Students should be able to: use the turtle class to draw a picture Develop algorithms that use simple graphics operations to draw two-dimensional shapes. Turtle graphics originally developed as part of the children's programming language logo. Turtle is an object that has a position in a drawing window this object can be told to turn a number of degrees, move a given distance, move to a new position, change its colour and line width if the turtle's pen is down, it draws a line; otherwise, it just moves
Students should be able to: use the turtle class to draw a picture Develop algorithms that use simple graphics operations to draw two-dimensional shapes. Turtle graphics originally developed as part of the children's programming language logo. Turtle is an object that has a position in a drawing window this object can be told to turn a number of degrees, move a given distance, move to a new position, change its colour and line width if the turtle's pen is down, it draws a line; otherwise, it just moves
Principles of Programming Learning outcomes At the end of this lecture, students should be able to: Use the Turtle class to draw a picture Develop algorithms that use simple graphics operations to draw two-dimensional shapes Examples and Exercises: Example 1: Drawing a Triangle Example 2: Drawing three circles Case Study 1: Drawing a Square Case Study 2: Drawing a Polygon Case Study 3: Taking a Random Walk Show Case: Repeating Patterns COMPSCI101 2 Overview of Turtle Graphics Turtle graphics originally developed as part of the childrens programming language Logo Created by Seymour Papert and his colleagues at MIT in the late 1960s Analogy: Turtle crawling on a piece of paper, with a pen tied to its tail Sheet of paper is a window on a display screen Position specified with (x, y) coordinates Cartesian coordinate system, with origin (0, 0) at the centre of a window COMPSCI101 3 Turtle Graphics The turtle is an object that has a position in a drawing window This object can be told to turn a number of degrees, move a given distance, move to a new position, and change its colour and line width If the turtles pen is down, it draws a line; otherwise, it just moves without drawing The turtle Module Includes a Turtle type with methods for getting turtle objects to do things Add an import statement in your py file. COMPSCI101 4 import turtle Instantiating a Turtle Instantiate a Turtle object: The turtle appears as an icon Initial position: (0, 0) Initial direction: East (0) Colour: black Line width: 1 pixel Pen: down (ready to draw) y - a x i s (0,0) 90 180 0 270 x-axis (0,0) t = turtle.Turtle() COMPSCI101 5 Properties of a Turtle The Turtle class provides many methods for setting the properties of a turtle: penup(): pull pen up, no drawing when moving pendown(): put pen down, drawing when moving pensize(width): set the pen line thickness to width pencolor(color): set the pen colour COMPSCI101 6 Moving a Turtle The Turtle class provides many methods for moving the turtle: forward(distance) Move turtle in the direction the turtle is headed by di st ance pixels left(degrees) Rotate turtle counter-clockwise by angle degrees right(degrees) Rotate turtle clockwise by angle degrees goto(x,y) Move turtle to coordinates defined by x, and y; if pen is down, draw line COMPSCI101 7 Example 1: Drawing a Triangle Move a Given Distance Move 50 pixels in the current direction, which is 0 (east) Drawing a line when moving Move to a Given Position Move to location (0, 50) Drawing a line when moving t = turtle.Turtle() t.forward(50) t.goto(0, 50) COMPSCI101 8 Example 1: Drawing a Triangle Set the Direction 270 is due south Note: the direction of the turtles icon has been updated Initial heading is east i.e. 0 Change the Pens Colour Change the RGB value to the brightest red Note: the icons outline colour has been changed t.setheading(270) t.pencolor('red') COMPSCI101 9 Example 1: Drawing a Triangle Move a Given Distance Move 50 pixels in the current direction Returns to the origin, drawing a red line Move Without Drawing Wont draw when moved now t.forward(50) t.penup() t.forward(50) COMPSCI101 10 Demo 1 Some More Turtle Methods The Turtle class provides many methods for drawing: circle(radius) Draw a circle with given radius. The centre is radius units left of the turtle dot(diameter, colour) Draw a dot with given diameter and colour COMPSCI101 11 t = Turtle() t.circle(100) y - a x i s (0,0) 90 180 0 270 x-axis (0,0) Example 2: Drawing three circles Given the following code: COMPSCI101 12 t = turtle.Turtle() t.circle(100) t.penup() t.goto(-50, 50) t.pendown() t.circle(50) t.penup() t.goto(50, 50) t.pendown() t.circle(50) Demo 2 (0,0) (-50,50) (50,50) Case Study 1 Drawing a square Task: Complete the dr aw_squar e( ) function which draws a square with corner point (x, y) and length Arguments: a turtle, x-coordinate, y-coordinate, length Draw a square Case: t = turtle.Turtle() drawSquare(t, 0, 50, 100) 1 2 3 4 COMPSCI101 13 (0,50) Case Study 1 Drawing a square Algorithm: 1 2 3 4 Move to the required position Set the pen up Go to the required position Set direction (South) Set the pen down Repeat the following steps four times Draw a line of size length Rotate counter-clockwise 90 degree COMPSCI101 14 Demo 3 Case Study 2 Drawing a Polygon Task: Complete the dr aw_pol ygon( ) function which draws a polygon from a list of vertices. Arguments: a turtle, a list of vertices Draw a polygon Example: t = turtle.Turtle() drawPolygon(t, [(0,0), (0, 60), (60, 0)]) COMPSCI101 15 (0, 0) Case Study 2 Drawing a Polygon Algorithm: Move to the required position Set the pen up Go to the first vertex Set the pen down Repeat the following steps n-1times Go to the next vertex Go to the first vertex again 1 3 2 COMPSCI101 16 Demo 3 Exercise 1: Drawing For each of the three figures below, write a sequence of turtle graphics function calls which draws that figure COMPSCI101 17 Case Study 3 Taking a Random Walk Animals often appear to wander about randomly, but they are often searching for food, shelter, etc. Task: Complete the r andom_wal k( ) function which takes a turtle, num_steps and distance as parameters and takes a random walk for a step of num_steps. A random walk is a path in which each step is in a randomly chosen direction Arguments: a turtle, num_steps, distance Example: t = turtle.Turtle() randomWalk(t, 30, 50) COMPSCI101 18 Case Study 3 Taking a Random Walk Repeat the following by a step of num_steps: Generate a random degree Rotate by the random degree moves the turtle in the direction it is facing COMPSCI101 19 Demo 4 Show Case Interesting shapes can be created by repeating drawing squares: Repeat the following 36 times COMPSCI101 20 Draw a square Rotate by 10 degrees Increase the length of the square Increase the pensize (<5) Demo 5 Summary "Turtle graphics" were developed for the language LOGO as a tool for helping beginning programmers visualize the effects of their actions. In a turtle graphics environment, the programmer draws figures by controlling a "turtle" which leaves a trail on the screen as it moves. The turtle can be made to move relative to its current position using forward and backward. It can be made to turn relative to its current heading using left and right. You can control whether the turtle leaves a trail as it moves by calling penup and pendown. COMPSCI101 21