|
1 | 1 | +++ |
2 | | -title = "Hello, little turtles!" |
| 2 | +title = "Hello Turtles!" |
3 | 3 | weight = 301 |
4 | 4 | +++ |
5 | 5 |
|
6 | | - |
7 | 6 | ## Our first turtle program |
8 | 7 |
|
9 | | -Let’s write a couple of lines of Python program to create a new |
10 | | -turtle and start drawing a rectangle. (We’ll call the variable that |
11 | | -refers to our first turtle ```alex```, but we can choose another |
12 | | -name if we follow the naming rules from the previous chapter). |
13 | | - |
14 | | -To use the turtle library in a Collab Jupyter notebook, we start by installing the library. |
| 8 | +To use the turtle library in a Collab Jupyter notebook, we start by |
| 9 | +installing the library. |
15 | 10 |
|
16 | 11 | **You will have to execute this cell every time you restart a new session.** |
17 | 12 |
|
18 | | -``` |
| 13 | +```Python |
19 | 14 | !pip install ColabTurtlePlus |
20 | 15 | ``` |
21 | 16 |
|
22 | | -Here is a first program we will analyze together. |
| 17 | +With the turtle library, we can draw by moving a virtual turtle around |
| 18 | +the screen. Let’s write a couple of lines of Python program to |
| 19 | +create a new turtle and start drawing a rectangle. (We’ll call |
| 20 | +the variable that refers to our first turtle ```shelly```, but you can |
| 21 | +choose any name you want so long as it follows the Python variable |
| 22 | +naming rules from the previous chapter). |
23 | 23 |
|
24 | | -``` |
25 | | -import ColabTurtlePlus.Turtle as turtle # Allows us to use turtles |
| 24 | +Here is a first program we will analyze together: |
26 | 25 |
|
27 | | -turtle.clearscreen() # Remove drawings from previous runs. |
28 | | -turtle.setup(200,100) # Sets the dimensions of the viewing window. |
| 26 | +```Python |
| 27 | +import ColabTurtlePlus.Turtle as turtle # Imports the turtle library |
29 | 28 |
|
30 | | -alex = turtle.Turtle() # Create a turtle, assign to alex |
| 29 | +turtle.clearscreen() # Remove drawings from previous runs. |
| 30 | +turtle.setup(200, 100) # Sets the dimensions of the viewing window. |
31 | 31 |
|
32 | | -alex.forward(50) # Tell alex to move forward by 50 units |
33 | | -alex.left(90) # Tell alex to turn left by 90 degrees |
34 | | -alex.forward(30) # Complete the second side of a rectangle |
| 32 | +shelly = turtle.Turtle() # Create a turtle, assign to shelly |
35 | 33 |
|
| 34 | +shelly.forward(50) # Tell shelly to move forward by 50 units |
| 35 | +shelly.left(90) # Tell shelly to turn left by 90 degrees |
| 36 | +shelly.forward(30) # Complete the second side of a rectangle |
36 | 37 | ``` |
37 | 38 |
|
38 | 39 | Here are a couple of things we will need to understand about this program. (Do not worry if you do not understand them all now— we will cover them in more detail as we work through the book.) |
39 | 40 |
|
40 | 41 |
|
41 | 42 | The first line, ```import ColabTurtlePlus.Turtle as turtle```, |
42 | | -tells Python to load a module named ```ColabTurtlePlus.Turtle``` that will be used in this notebook with the name ```turtle```. |
43 | | -That module brings us a new type that we can use: the ```Turtle``` type. The dot |
| 43 | +tells Python to load a module (we also this a _library_) named ```ColabTurtlePlus.Turtle``` that will be used in this notebook with the name ```turtle```. |
| 44 | + |
| 45 | +The turtle library defines a new type, ```Turtle``` that has operations like ```forward()``` for moving the a turtle. The dot |
44 | 46 | notation ```turtle.Turtle``` means *“The Turtle type that is defined within |
45 | 47 | the turtle module”*. (Remember that Python is case sensitive, so the |
46 | 48 | module name, with a lowercase *t*, is different from the type ```Turtle```.) |
47 | 49 |
|
48 | | -We then clear the screen to remove drawings from previous runs with ```turtle.clearscreen()``` and set the dimensions to a size that is big enough for the drawing with ```turtle.setup(200,100)```. |
| 50 | +The next line clears the screen to remove drawings from previous runs with ```turtle.clearscreen()``` and set the dimensions to a size that is big enough for the drawing with ```turtle.setup(200,100)```. |
49 | 51 |
|
50 | | -In the next line, ```alex = turtle.Turtle()```, we create a turtle and assign it to the variable ```alex```. |
| 52 | +Then, ```shelly = turtle.Turtle()``` creates a new turtle and assigns it to the variable ```shelly```. |
51 | 53 |
|
52 | | -Now we are ready to draw on the canvas using the turtle. |
53 | | -``` |
54 | | -alex.forward(50) # Tell alex to move forward by 50 units |
55 | | -alex.left(90) # Tell alex to turn left by 90 degrees |
56 | | -alex.forward(30) # Complete the second side of a rectangle |
| 54 | +Now we are ready to draw using the turtle: |
| 55 | +```Python |
| 56 | +shelly.forward(50) # Tell shelly to move forward by 50 units |
| 57 | +shelly.left(90) # Tell shelly to turn left by 90 degrees |
| 58 | +shelly.forward(30) # Complete the second side of a rectangle |
57 | 59 | ``` |
58 | 60 |
|
59 | | -In the last three lines, we instruct the **object** ```alex``` to move, and to turn. We |
60 | | -do this by **invoking**, or activating, ```alex```‘s **methods** — these are |
61 | | -the instructions that all turtles know how to respond to. |
| 61 | +These lines instruct the Turtle **object** that the ```shelly``` |
| 62 | +variable references to move, and to turn. We do this by **invoking**, |
| 63 | +or calling, **methods** of that Turtle object. Methods are functions defined by the turtle library that can be called on Turtle objects. Here, the ```forward``` method takes a parameter that gives the distance, and the Turtle object will move that distance in the direction it is pointing. The ```left``` method causes the Turtle object to turn so it is now facing a new direction. |
62 | 64 |
|
63 | 65 | Running the program produces the output below. |
64 | 66 |
|
|
0 commit comments