12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4Case study: interfacedesign4.1 TurtleWorldTo accompany this book, I have written a suite of modules called Swampy. One of these modulesis TurtleWorld, which provides a set of functions for drawing lines by steering turtles around thescreen.YoucandownloadSwampyfromthinkpython.com/swampy;followtheinstructionstheretoinstallSwampy on your system.Move into the directory that contains TurtleWorld.py, create a file named polygon.py and typeinthefollowing code:from TurtleWorld import *world = TurtleWorld()bob = Turtle()print bobwait_for_user()The first line is a variation of the import statement we saw before; instead of creating a moduleobject, it imports the functions from the module directly, so you can access them without using dotnotation.The next lines create a TurtleWorld assigned to world and a Turtle assigned to bob. Printing bobyields something like:This means that bob refers to an instance of a Turtle as defined in module TurtleWorld. In thiscontext, “instance” means amember of aset; thisTurtleisone of the setof possibleTurtles.wait_for_usertellsTurtleWorldtowaitfortheusertodosomething, although inthiscasethere’snot much forthe user todoexcept closethewindow.TurtleWorldprovidesseveralturtle-steeringfunctions: fdandbkforforwardandbackward,andltand rt for left and right turns. Also, each Turtle is holding a pen, which is either down or up; if the

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!