Python 3 – Turtle Graphics

Transcription

Python 3 – Turtle graphicsLecture 25 – COMPSCI111/111G SS 20181

Today’s lecture The Turtle graphics package 2Brief historyBasic commandsDrawing shapes on screen

Logo and Turtle graphics 3In 1967, Seymour Papert and Wally Feurzeig created aninterpretive programming language called Logo.Papert added commands to Logo so that he could control aturtle robot, which drew shaped on paper, from his computerTurtle graphics is now part of PythonUsing the Turtle involves instructing the turtle to move on thescreen and draw lines to create the desired shape

The Turtle package Some functions are part of Python’s core libraries, inother words they are ‘built-in’ 4print()input()float()Other functions need to be imported into yourPython programThe turtle module needs to be imported at thestart of any Python program that uses it:import turtle

Basic Turtle commands There are four basic turtle commandsturtle.forward(x) turtle.back(x) Turns the turtle x degrees counterclockwiseturtle.right(x) 5Moves turtle backward from its facing direction by xstepsturtle.left(x) Moves turtle forward in direction it is facing by x stepsTurns the turtle x degrees clockwise

Turtle example Using the Python interpreter in IDLE to demonstrate how touse Turtle graphicsFirst, import the turtle package import turtle 6

Turtle example We are going to draw a right-angled triangle90 Important information: 7x-axis(0,0)180 0 270 y-axis The turtle appears as an iconInitial position: (0, 0)Initial direction: East (0 )Colour: blackLine width: 1 pixelPen: down (ready to draw)

Algorithmdraw a lineTurn 90 degrees left (anti-clockwise)draw a lineTurn 135 degrees left (anti-clockwise)8draw a line

Turtle example Step 1: Draw a line import turtle turtle.forward(200) 1. Draw a line9

Turtle example Initial direction: 0Note how the turtle is now facing upward after being turned90 degrees left import turtle turtle.forward(200) turtle.left(90) 1090degree

Turtle example Step 3: draw a line 11import ward(200)

current directionTurtle example Step 4: turn 135 degree left (anti-clockwise) 12135degreeimport ward(200)turtle.left(135)

Turtle example Working out the length of the longest side using thePythagoras’ formula 13import ward(200)turtle.left(135)c ((200**2) (200**2))**0.5#around 283 steps

Turtle example Step 6: draw a lineThe finished image 14import ward(200)turtle.left(135)c ((200**2) (200**2))**0.5)turtle.forward(c)

Turtle example We can use loops when drawing shapes usingTurtle graphicsWrite a program that will draw a square using aloopDraw aline15Turn 90degree leftX 4 times

Turtle example We can use loops when drawing shapes usingTurtle graphicsWrite a program that will draw a square using aloopimport turtlecount 0while count 4:turtle.forward(200)turtle.left(90)count count 116

TRY IT OUT!Exercise 1 Write a Python program that draws a rectangle. The longsides must be 300 steps long and the short sides must be 150steps longDraw along lineTurn 90degree leftDraw ashort lineTurn 90degree leftDraw along line17Turn 90degree leftDraw ashort lineTurn 90degree left

Turtle example Write a program that will draw a circle Steps: 18Draw a short line (2 pixels)Turn 1 degreeRepeat the above steps 360 times

Turtle example Write a program that will draw a circleimport turtlecount 0while(count 360):turtle.forward(2)turtle.left(1)count count 1print("Finished!")19

QuestionConsider the following program:import turtlecount 0length 100while count 4:turtle.forward(length)turtle.left(90)count count 1length length - 10 20Which of the following pictures demonstrates the outputgenerated by the program above?

Exercise 2 How to draw a star? 21How many steps do you need?What is the size/length for each step? E.g. 400 pixelsWhat is the turning angle for each step?

Exercise 3TRY IT OUT!Draw the shape that is produced by the following Python program:import turtlecount 0while(count 180):turtle.forward(2)turtle.right(1)count count )turtle.back(150)turtle.right(45)turtle.back(250) 22

Exercise 4 TRY IT OUT!Draw the shape that is produced by the following Pythonprogram:import turtlebig line 100little line 50angle 90turtle.left(angle)turtle.forward(big line)count 0while count 4:turtle.right(angle//2)if count ! 3:turtle.forward(little line)else:turtle.forward(big line)count count 1turtle.right(90)23 turtle.forward(130)

Summary 24The Turtle package must be imported into every Pythonprogram that uses itThe Turtle has four basic commands; forward, back, left andright

Logo and Turtle graphics In 1967, Seymour Papert and Wally Feurzeig created an interpretive programming language called Logo. Papert added commands to Logo so that he could control a turtle robot, which drew shaped on paper, from his computer Turtle graphics is now part of Python Using