Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 ....

21
Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016

Transcript of Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 ....

Page 1: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

Python 3 – Turtle graphics

Lecture 24 – COMPSCI111/111G SS 2016

Page 2: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

Today’s lecture u  Recap

u  The Turtle graphics package u  Brief history u  Basic commands u  Drawing shapes on screen

Page 3: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

Logo and Turtle graphics u  In 1967, Seymour Papert and Wally Feurzeig

created an interpretive programming language called Logo.

u  Papert added commands to Logo so that he could control a turtle robot, which drew shaped on paper, from his computer

u  Turtle graphics is now part of Python

u  Using the Turtle involves instructing the turtle to move on the screen and draw lines to create the desired shape

Page 4: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

The Turtle package u  Some functions are part of Python’s core libraries,

in other words they are ‘built-in’ u  print() u  input() u  float()

u  Other functions need to be imported into your Python program

u  The turtle module needs to be imported at the start of any Python program that uses it: import turtle

Page 5: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

Basic Turtle commands u  There are four basic turtle commands

u  turtle.forward(x) u  Moves turtle forward in direction it is facing by x steps

u  turtle.back(x) u  Moves turtle backward from its facing direction by x steps

u  turtle.left(x) u  Turns the turtle x degrees counterclockwise

u  turtle.right(x) u  Turns the turtle x degrees clockwise

Page 6: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

Turtle example u  Using the Python interpreter in IDLE to

demonstrate how to use Turtle graphics

u  First, import the turtle package

>>> import turtle >>>

Page 7: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

Important Information u  Initial Position:

u  The turtle appears as an icon

u  Initial position is (0,0)

u  Initial direction East (0o)

u  Colour: black

u  Line width: 1 pixel

u  Pen: down (ready to draw)

Page 8: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

Exercise u  Write a Python program that draws a rectangle.

The long sides must be 300 steps long and the short sides must be 150 steps long

import turtle

turtle.forward(300) turtle.left(90) turtle.forward(150) turtle.left(90) turtle.forward(300) turtle.left(90) turtle.forward(150)

Page 9: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

Turtle example u  We can use loops when drawing shapes using

Turtle graphics

u  Write a program that will draw a square using a loop

import turtle

count = 0 while count < 4: turtle.forward(200) turtle.left(90) count = count + 1

Page 10: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

Turtle example u  We are going to draw a right-angled triangle

>>> import turtle >>> >>> turtle.forward(200) >>>

Page 11: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

Turtle example u  Note how the turtle is now facing upward after

being turned 90 degrees left

>>> import turtle >>> >>> turtle.forward(200) >>> turtle.left(90) >>>

Page 12: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

Turtle example

>>> import turtle >>> >>> turtle.forward(200) >>> turtle.left(90) >>> turtle.forward(200) >>>

Page 13: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

Turtle example

>>> import turtle >>> >>> turtle.forward(200) >>> turtle.left(90) >>> turtle.forward(200) >>> turtle.left(135) >>>

Page 14: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

Turtle example u  Working out the length of the longest side using

the Pythagoras’ formula

>>> import turtle >>> >>> turtle.forward(200) >>> turtle.left(90) >>> turtle.forward(200) >>> turtle.left(135) >>> c = ((200**2)+(200**2))**0.5 #around 283 steps

Page 15: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

Turtle example u  The finished image

>>> import turtle >>> >>> turtle.forward(200) >>> turtle.left(90) >>> turtle.forward(200) >>> turtle.left(135) >>> c = ((200**2)+(200**2))**0.5) >>> turtle.forward(c)

Page 16: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

Turtle example u  Write a program that will draw a circle

import turtle count = 0 while(count < 360): turtle.forward(2) turtle.left(1) count = count + 1 print("Finished!")

Page 17: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

What Shape is this? u  import turtle

u  count = 0

u  length = 100

u  while count < 4: u  turtle.forward(length) u  turtle.left(90) u  count = count + 1 u  length = length -10

Page 18: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

How to draw a star import turtle

count = 0

while count < 5: turtle.forward(300) turtle.left(180-36) count = count + 1

Page 19: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

Exercise u  Draw the shape that is produced by the following

Python program: import turtle count = 0 while(count < 180): turtle.forward(2) turtle.right(1) count = count + 1

turtle.right(45) turtle.forward(300) turtle.left(90) turtle.back(150) turtle.right(45) turtle.back(250)

Page 20: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

Exercise import turtle big_line = 100 little_line = 50 angle = 90 turtle.left(angle) turtle.forward(big_line) count = 0 while count < 4:

turtle.right(angle//2) if count != 3: turtle.forward(little_line) else: turtle.forward(big_line) count = count + 1

turtle.right(90) turtle.forward(130)

Page 21: Python 3 – Turtle · Python 3 – Turtle graphics Lecture 24 – COMPSCI111/111G SS 2016 . Today’s lecture ! Recap ! The Turtle graphics package ! Brief history ! Basic commands

Summary u  The Turtle package must be imported into every

Python program that uses it

u  The Turtle has four basic commands; forward, back, left and right