17
Python Turtle Graphics ASFA Programming II 2010-2011

Python Turtle Graphics

  • Upload
    chun

  • View
    148

  • Download
    5

Embed Size (px)

DESCRIPTION

Python Turtle Graphics. ASFA Programming II 2010-2011. A first Object: Logo Turtle. Dr. Seymour Papert at MIT invented the Turtle as a graphical and mathematical object to think with for the children’s programming language, Logo (1966). Robot Turtles. - PowerPoint PPT Presentation

Citation preview

Page 1: Python Turtle Graphics

Python Turtle Graphics

ASFA Programming II2010-2011

Page 2: Python Turtle Graphics

A first Object: Logo Turtle

• Dr. Seymour Papert at MIT invented the Turtle as a graphical and mathematical object to think with for the children’s programming language, Logo (1966)

Page 3: Python Turtle Graphics

Robot Turtles

• Children programmed robot turtles to draw pictures

Page 4: Python Turtle Graphics

• A turtle is an object.– Every turtle understands the same methods.– Every turtle has the same fields or instance

variables.• Heading, body color, pen color, X and Y position.• Yet each turtle can have its own values for these fields.

• Many modern programming languages, such as Python, continue to use turtles for drawing

Page 5: Python Turtle Graphics

• Think of a 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

center of a window

Page 6: Python Turtle Graphics

• Imagine a turtle starting at (0, 0) – Give it the command turtle.forward(15), and it

moves (on-screen) 15 pixels in the direction it is facing, drawing a line as it moves.

– Give it the command turtle.left(25), and it rotates in-place 25 degrees counter-clockwise.

Page 7: Python Turtle Graphics

Some Examples of Turtle Programs

• A forest scene created with turtles• http://www.youtube.com/watch?v=Wwzv0FW

J5gQ• A Recursive Turtle Drawing Program• http://www.youtube.com/watch?feature=play

er_embedded&v=YummtrvNC2o

Page 8: Python Turtle Graphics

Some Key Methodsfrom turtle import *

# pen/turtle starts at the center (x=0, y=0) of the turtle display area

shape(“turtle”)color("green")# pen up, don't drawup()# centers the circlegoto(0,-50)# pen down, drawdown()# radius=50 center is 50 radius units above the turtlecircle(50)up()# center the turtle againgoto(0,0)down() tur2.py

Page 9: Python Turtle Graphics

Turtle drawing with repetitionfrom turtle import *def star(): color('red', 'purple') colormode(255) begin_fill() for i in range(36): pencolor(3*i + 100, 0 , 5*i) forward(200) left(85) forward(10) left(45) forward(40) left(-90) forward(40) left(85) forward(10) left(45) end_fill() setpos(100,45) color("white","yellow") shape("turtle")

star()

spyro.py

Page 10: Python Turtle Graphics

Recursive Drawing Algorithms

Recursive Algorithms are used to create fractals

Page 11: Python Turtle Graphics

Documentation available explaining methods available

• If you have trouble getting the docs from Python, you can go to http://docs.python.org/library/turtle.html

Page 12: Python Turtle Graphics

Turtle library contains many methods

Page 13: Python Turtle Graphics

Click on method you want to see

Page 14: Python Turtle Graphics

Method usage documentation and examples

Page 15: Python Turtle Graphics

Endless Artistic Possibilities

Page 16: Python Turtle Graphics

What can you create?

Page 17: Python Turtle Graphics

Assignment• Explore the documentation • Design and program a turtle drawing a picture

Must include:• At least 4 colors• At least 4 shapes• A filled object• Looping

• Show me the drawing creation• Email your program to me after checked off• Enrichment: recreate a picture or animate a story

• Create a Recursive turtle drawing – art work