Class 2 Introduction to turtle graphics Todays Topics

  • Slides: 8
Download presentation
Class 2 Introduction to turtle graphics

Class 2 Introduction to turtle graphics

Today’s Topics First steps u Turtle graphics u Python modules u Python help and

Today’s Topics First steps u Turtle graphics u Python modules u Python help and documentation u for and range u def – creating a function

Computing is a social activity If you’ve got a question, ask it (it’s part

Computing is a social activity If you’ve got a question, ask it (it’s part of computing culture) n We’re working in pairs. It’s more fun, more real and more productive. n Use the online help system and tutorial n Use the textbook n Ask a TA or tutor n

Introduction to Turtle Graphics n n n Type ‘from turtle import *’ and press

Introduction to Turtle Graphics n n n Type ‘from turtle import *’ and press enter u This gives you access to the functions in Python’s turtle module u A module is a collection of functions u ‘*’ matches everything Type: u pendown() and press enter u forward(100) and press enter u A new window pops up and a straight line will be drawn Why are the pendown() parens empty? What is the ‘ 100’ in the parens?

Introduction to Turtle Graphics n Some useful commands u u u u n forward(distance)

Introduction to Turtle Graphics n Some useful commands u u u u n forward(distance) left(degrees) right(degrees) goto(xcoord, ycoord) up() – (no drawing) down() – (pen ‘down’ for drawing) clear() Look up the Turtle module in the Python docs u Python manuals > Global module index > Turtle (Tk)

Practice Drawing Shapes n n Draw the following shapes using forward(), left() or right()

Practice Drawing Shapes n n Draw the following shapes using forward(), left() or right() u Triangle, square or rectangle, pentagon, dodecagon Challenge problem 1: Draw a set of squares rotated at different angles Challenge problem 2: Draw a set of squares offset from each other (hint: use ‘up’) Write down the sequence of Python statements you used before you exit (you’ll need them later)

Write your own function Use def to create your own function n Put this

Write your own function Use def to create your own function n Put this function definition is a file def square(size): forward(size) right(90) n

The for loop Try this >>> for i in 'cat': print(i) n Now try

The for loop Try this >>> for i in 'cat': print(i) n Now try this >>> for i in range(3): print('cat') n Use for in range() to improve your square function n