Chapter 4. Case Study: Interface Design
Code examples from this chapter are available from http://thinkpython.com/code/polygon.py.
TurtleWorld
To accompany this book, I have written a package called Swampy. You can download Swampy from http://thinkpython.com/swampy; follow the instructions there to install Swampy on your system.
A package is a collection of
modules; one of the modules in Swampy is TurtleWorld, which provides a set of functions
for drawing lines by steering turtles around the screen.
If Swampy is installed as a package on your system, you can import
TurtleWorld like this:
fromswampy.TurtleWorldimport*
If you downloaded the Swampy modules but did not install them as a
package, you can either work in the directory that contains the Swampy
files, or add that directory to Python’s search path. Then you can
import TurtleWorld like this:
fromTurtleWorldimport*
The details of the installation process and setting Python’s search path depend on your system, so rather than include those details here, I will try to maintain current information for several systems at http://thinkpython.com/swampy
Create a file named mypolygon.py and type in the following
code:
fromswampy.TurtleWorldimport*world=TurtleWorld()bob=Turtle()bobwait_for_user()
The first line imports everything from the TurtleWorld module in the swampy package.
The next lines create a TurtleWorld assigned to world and a Turtle assigned to bob. Printing bob yields something like:
<TurtleWorld.Turtle instance ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access