# caioariede/swfk-ptbr

 a1e607f initial caioariede authored Jun 21, 2011 1 % ch3.tex 2 % This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 New Zealand License. 3 % To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/nz 4 % or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. 5 6 7 \chapter{Turtles, and other slow moving creatures}\index{turtle}\label{ch:turtles} 8 9 There are certain similarities between turtles in the real world and a Python turtle. In the real world, a turtle is a (sometimes) green reptile that moves around very slowly and carries its house on its back. In the world of Python, a turtle is a small black arrow that moves very slowly around the screen. No mention of house-carrying though. 10 11 In fact, considering that a Python turtle leaves a trail as it moves around the screen, this makes it less like a real turtle, and more like a snail or a slug. However, I suppose that a module called slug' wouldn't be particularly attractive, so it makes sense to stick with turtles. Just imagine the turtle is carrying a couple of marker pens with it, and drawing as it goes. 12 13 In the deep, dark, and distant past, there was a simple programming language called Logo. Logo was used to control a robot turtle (called Irving). Over time, the turtle evolved from a robot that could move around the floor, to a small arrow moving around a screen. 14 15 \emph{Which just goes to show, things don't always improve as technology advances---a little robot turtle would be a lot more fun.} 16 17 Python's turtle module (we'll come to modules a bit later, but for now just just think of a module as something we can use inside a program) is a little bit like the Logo programming language, but while Logo was (is) fairly limited, Python has many more capabilities. The turtle module itself, is a useful way to learn how computers draw pictures on your computer screen. 18 19 Let's get started and see just how it works. The first step is to tell Python we want to use turtle, by importing the module: 20 21 \begin{listing} 22 \begin{verbatim} 23 >>> import turtle 24 \end{verbatim} 25 \end{listing} 26 27 Then we need to display a canvas to draw on. A canvas is just like the material an artist might use for painting; in this case it's a blank space for drawing on: 28 29 \begin{listing} 30 \begin{verbatim} 31 >>> t = turtle.Pen() 32 \end{verbatim} 33 \end{listing} 34 35 In this code, we call a special function (Pen\index{Pen}) on the module turtle, which automatically creates a canvas we can draw on. A function is a re-useable piece of code (again we'll come to functions later) that does something useful---in this case, an object which represents the turtle is returned by the Pen function---we set that object to the variable t' (in effect we're giving our turtle canvas the name t'). When you type the code into the Python console, you'll see a blank box (the canvas) appear, looking something like figure~\ref{fig10}. 36 37 \begin{figure} 38 \begin{center} 09bb146 cleanup caioariede authored Jun 22, 2011 39 \includegraphics[width=72mm]{eps/figure10.eps} a1e607f initial caioariede authored Jun 21, 2011 40 \end{center} 41 \caption{An arrow representing the turtle.}\label{fig10} 42 \end{figure} 43 44 \emph{Yes, that little arrow in the middle of the screen really is the turtle. And, no, it's not very turtle-like.} 45 46 You can send instructions to the turtle, by using functions on the object that was created (by calling \code{turtle.Pen})---since we assigned that object to the variable \code{t}, we use \code{t} to send the instructions. 47 One turtle instruction is \code{forward}. Forward\index{turtle!forward} tells the turtle to move forward in whatever direction she is facing (I have no idea whether it's a boy or a girl turtle, but let's just assume it's a girl-turtle for the moment). Let's tell the turtle to move forward 50 pixels (we'll talk about pixels in a minute): 48 49 \begin{listing} 50 \begin{verbatim} 51 >>> t.forward(50) 52 \end{verbatim} 53 \end{listing} 54 55 You should see something like figure~\ref{fig11}. 56 57 \begin{figure} 58 \begin{center} 09bb146 cleanup caioariede authored Jun 22, 2011 59 \includegraphics[width=72mm]{eps/figure11.eps} a1e607f initial caioariede authored Jun 21, 2011 60 \end{center} 61 \caption{The turtle draws a line.}\label{fig11} 62 \end{figure} 63 64 From the turtle's point-of-view, she has moved forward 50 steps. From our point-of-view, she has moved 50 pixels. 65 66 \noindent 67 \emph{So, what's a pixel?} 68 69 A pixel\index{pixels} is a dot on the screen. When you look at your computer, everything is made up of tiny (square) dots. The programs you use and the games you play on the computer, or with a Playstation, or an Xbox, or a Wii; are all made up of a whole bunch of different coloured dots, arranged on the screen. In fact, if you look at your computer screen with a magnifying glass, you might just be able to make out some of those dots. So if we zoom in on the canvas and the line that was just drawn by the turtle, we can see the arrow representing the turtle, is also just a bunch of square dots, as you can see in figure~\ref{fig12}. 70 71 \begin{figure} 72 \begin{center} 09bb146 cleanup caioariede authored Jun 22, 2011 73 \includegraphics[width=72mm]{eps/figure12.eps} a1e607f initial caioariede authored Jun 21, 2011 74 \end{center} 75 \caption{Zooming in on the line and the arrow.}\label{fig12} 76 \end{figure} 77 78 We'll talk more about these dots, or pixels, in a later chapter. 79 80 Next, we can tell the turtle to turn left\index{turtle!turning left} or right\index{turtle!turning right}: 81 82 \begin{listing} 83 \begin{verbatim} 84 >>> t.left(90) 85 \end{verbatim} 86 \end{listing} 87 88 This tells the turtle to turn left, 90 degrees. You may not have learned about degrees\index{degrees} in school so far, but the easiest way to think about them, is that they are like the divisions on the face of a clock as seen in figure~\ref{fig13}. 89 90 \begin{figure} 91 \begin{center} 09bb146 cleanup caioariede authored Jun 22, 2011 92 \includegraphics[width=52mm]{eps/figure13.eps} a1e607f initial caioariede authored Jun 21, 2011 93 \end{center} 94 \caption{The divisions' on a clock.}\label{fig13} 95 \end{figure} 96 97 The difference to a clock, is that rather than 12 divisions (or 60, if you're counting minutes rather than hours), there are 360 divisions. So, if you count 360 divisions around the face of a clock, you get 90 where there's normally a 3, 180 where there's normally a 6, and 270 where there's normally a 9; and 0 would be at the top (at the start), where you normally see a 12. Figure~\ref{fig14} shows you the degree divisions. 98 99 \begin{figure} 100 \begin{center} 09bb146 cleanup caioariede authored Jun 22, 2011 101 \includegraphics[width=52mm]{eps/figure14.eps} a1e607f initial caioariede authored Jun 21, 2011 102 \end{center} 103 \caption{Degrees.}\label{fig14} 104 \end{figure} 105 106 So, what does it actually mean when you call \code{left(90)}? 107 \par 108 If you stand and face one direction, point your arm out directly away from your shoulder, THAT is 90 degrees. If you point your left arm, that's 90 degrees left. If you point your right arm, that's 90 degrees right. When Python's turtle turns left, she plants her nose in one spot then swivels her body around the face the new direction (same as if you turned your body to face where your arm is pointing). So, \code{t.left(90)} results in the arrow now pointing upwards, as shown in figure~\ref{fig15}. 109 110 \begin{figure} 111 \begin{center} 09bb146 cleanup caioariede authored Jun 22, 2011 112 \includegraphics[width=72mm]{eps/figure15.eps} a1e607f initial caioariede authored Jun 21, 2011 113 \end{center} 114 \caption{The turtle after turning left.}\label{fig15} 115 \end{figure} 116 117 Let's try the same commands again a few times: 118 119 \begin{listing} 120 \begin{verbatim} 121 >>> t.forward(50) 122 >>> t.left(90) 123 >>> t.forward(50) 124 >>> t.left(90) 125 >>> t.forward(50) 126 >>> t.left(90) 127 \end{verbatim} 128 \end{listing} 129 130 Our turtle has drawn a square and is left facing the same direction as she started (see figure~\ref{fig16}). 131 132 \begin{figure} 133 \begin{center} 09bb146 cleanup caioariede authored Jun 22, 2011 134 \includegraphics[width=72mm]{eps/figure16.eps} a1e607f initial caioariede authored Jun 21, 2011 135 \end{center} 136 \caption{Drawing a square.}\label{fig16} 137 \end{figure} 138 139 We can erase what's on the canvas by using clear\index{turtle!clear}: 140 141 \begin{listing} 142 \begin{verbatim} 143 >>> t.clear() 144 \end{verbatim} 145 \end{listing} 146 147 Some of the other basic functions you can use with your turtle are: \code{reset}\index{turtle!reset}, which also clears the screen, but puts the turtle automatically back into her starting position; \code{backward}\index{turtle!backward}, which moves the turtle backwards; \code{right}, which turns the turtle to the right; \code{up}\index{turtle!up (stop drawing)} which tells the turtle to stop drawing as she moves (in other words pick her pen up off the canvas); and finally \code{down}\index{turtle!down (start drawing)} which tells the turtle to start drawing again. You call these functions in the same way we've used the others: 148 149 \begin{listing} 150 \begin{verbatim} 151 >>> t.reset() 152 >>> t.backward(100) 153 >>> t.right(90) 154 >>> t.up() 155 >>> t.down() 156 \end{verbatim} 157 \end{listing} 158 159 \noindent 160 We'll come back to the turtle module shortly. 161 162 \section{Things to try} 163 164 \emph{In this chapter we saw how to use turtle to draw simple lines, using left and right turns. We saw that turtle uses degrees to turn, a bit like the minute divisions on a clock face.} 165 166 \subsection*{Exercise 1} 167 Create a canvas using turtle's \code{Pen} function, and draw a rectangle. 168 169 \subsection*{Exercise 2} 170 Create another canvas using turtles \code{Pen} function, and draw a triangle. 171 09bb146 cleanup caioariede authored Jun 22, 2011 172 \newpage