Contents

Introduction to Python

In this lab, we're going to learn about drawing simple graphics in Python using the Turtle library. Like C++, Python is a programming language. However, while C++ belongs to a group of programming languages called compiled languages, Python belongs to the group called interpreted languages. It is also considered a scripting language.

Just like C++, Python has a notion of variables, loops, lists, functions, and classes. However, all of these look a little different in Python. For instance, Python does not require variable type declarations -- types are inferred by whatever you are currently storing in a variable. The type of a variable can also change with each new assignment to it. One of the largest differences is that in Python, indentation matters. All blocks must be indented, and any code that is part of the same block level must be indented the same amount.

(Back to top)

Introduction to Python

For this lab, you're not going to need to know too much Python, and what you will need is provided here or on one of the linked pages. This lab assumes you have Python 2.7 installed. Note that this is installed on each OS on the Macs in the computer lab.

Part 1: Using Turtle in the Python interpreter

Open a terminal on a computer with Python installed—Console2 on Windows or Terminal on OSX. Now type python. You should see something like this:

$ python
Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
Type "help", "copyright", "credits" or "license" for more information.
>>> 

To use the turtle library, we need to import it (much like how we include libraries in C++). Type import turtle. That should cause a new line in the prompt to appear. This line imports an instance of the Turtle class into our environment so we can access its methods. Try entering the following: turtle.forward(100). A graphics window should pop up with a small triangle (that's our turtle!) that moves from the center of the screen to the right by 100 pixels.

Now type turtle.left(90). That will rotate the turtle by 90° counter clockwise. If you type turtle.forward(100) again, the turtle will move 100 pixels in a straight line, but at a 90° angle from the original bearing.

At any point, you can clear what's been drawn and start over. Simple do the following (you don't need to include the lines that start with #—those are comments!):

>>> # Go back to the center of the screen (which has coordinates (0,0)).
>>> turtle.goto(0,0)
>>> # Clear everything that's been drawn.
>>> turtle.clear()

You can change the color of your lines (this is called the Pen color) using turtle.pencolor("red") (you can use most basic color strings, or you can supply a RGB tuple, e.g., red would be turtle.pencolor((1,0,0))). You can prevent the turtle from drawing a line as it moves by issuing the command turtle.up() (this lifts the pen up); doing turtle.down() will put the pen back down. You can also fill in shapes by using the turtle.fill(...) command. See this page for details on fill() and more Turtle methods.

Now, try the following: draw a square, a triangle, and a third shape of your choosing (they should be displayed on the screen at the same time). Make each use a different color pen, and don't allow lines going from one shape to the other.

Keep your interpreter window open—you'll need it for the next part.

Part 2: Writing a Python script

Using the interpreter can be useful when you're playing around or trying something out. However, if you want to re-run something from start to finish, it can be a pain. So, we can write a Python script file and then run that.

To begin, open a new Sublime Text file and save it to the Desktop as lab12-part2.py. Inside of that file, enter the following:

Add your code from part 1 where indicated. You do not need the ">>>" from each command, just the turtle... commands themselves.

Once you've entered that, you can run it from Sublime by hitting Ctr+Shift+B (Windows) or Cmd+Shift+B (OSX). The graphics window may pop up behind Sublime, so if you don't see it at first, try moving the Sublime window.

Part 3: Adding functions

Just like in C++ programs, it's a good idea to break up code into modular chunks. In Part 1, you wrote code to generate three shapes. In this part, you will refactor your code from Part 2. Do the following: create a new file called lab12-part3.py; copy all the code from Part 2 to this new file. Now add a function for each of the shapes you drew. Here's a primer on functions in Python...

Functions in Python need to be defined before they are used. Unlike C++, there is no separate declaration for a function, just its definition. The syntax is simple:

Here's an example of a function that takes a pair of coordinates, a color, and a length and draws a line starting at that point with the given pen color:

Part 4 (Optional): Additional tasks

If you are finished early, investigate how you modify your script so that if the user clicks on the screen, the turtle will move to that position. Then, if you user types a particular key, your script will draw a shape. E.g., if the user presses 't', your program draws a triangle wherever the turtle is.

Hint: the follow program will listen for a click and then draw a line to where the click occurred. Feel free to build from this.

(Back to top)

Submission

Submit your .py files to here. You can view the rubric here.

(Back to top)