turtle module Simple graphics programming How to use

  • Slides: 4
Download presentation
turtle module Simple graphics programming How to use it ? Import ‘turtle’ module by

turtle module Simple graphics programming How to use it ? Import ‘turtle’ module by including import turtle Need to create a turtle, and NAME it to manipulate Say, you named a turtle ‘xxx. ’ xxx = turtle. Turtle() Opens up a turtle window Can have more than one turtles at a time – tell them apart by names

turtle module >>> import turtle >>> t = turtle. Turtle() What is “. ”

turtle module >>> import turtle >>> t = turtle. Turtle() What is “. ” between turtle and Turtle() ? turtle => class (generic object referring to turtle module) Turtle() => method (action, function, constructor) Rough interpretation: get ‘turtle’ type (class) and do ‘Turtle()’ >>> t. color(“red”) Get turtle ‘t’ and color it red Attribute Position, heading (direction), color, tail position

turtle module Actions t. forward(100), t. backward(100) t. right(90), t. left(45) t. goto(-200, 90),

turtle module Actions t. forward(100), t. backward(100) t. right(90), t. left(45) t. goto(-200, 90), t. circle(50), t. color(“red”) t. up(), t. down() t. write(“Hello!”) References http: //www. eg. bucknell. edu/~hyde/Python 3/Turtle. Directions. html Official Python turtle page https: //docs. python. org/release/3/library/turtle. html

turtle module Suppose turtle ‘t’ is created Draw a circle --- t. circle(100) Draw

turtle module Suppose turtle ‘t’ is created Draw a circle --- t. circle(100) Draw a thicker circle – t. width(5) Find out the location (x, y) of t – t. position() Move t to (100, -50) – t. goto(100, -50) Stop drawing – t. up() Find out the screen sizes screen=t. getscreen() screen. window_width() and screen. window_height()