Lab Exercise 2: Turtle Graphics
The purpose of this lab time is to give you more practice with python and writing programs, an introduction to turtle graphics and practice with the concepts of variables, operators, and functions.
- Connect to your network directory. In the Finder under the Go menu, select Go to server. Enter fileserver1 into the server name field and then click connect. a window should pop up with several volumes on it, select Personal. A finder window should then come up with a folder with your username on it. You can do whatever you want inside that folder, such as creating directories or putting files. It's just like working with a flash stick. Since you can access those files from any networked computer on campus, please use your network directory to store all of your work from now on.
- Create a new file in BBEdit, or whatever editor you wish. Save the file as square.py
- Download the turtleUtils.py module and save it in your working directory. We'll use the turtleStart() and turtleWait() functions defined in that module to create the turtle window and keep it open after drawing is done.
- Using turtle graphics, write code for a square that is 50 pixels on a side. Test it out.
- Create a new variable called dx at the top of your code. Give it the value 50. Now modify your forward() instructions to use the parameter for the square size instead of the hard-coded number. Test it out.
- Create a new variable called dy at the top of your code. Interleave the use of dx and dy when you draw the square so that dx parameterizes the horizontal sides of the square and dy the vertical. Test it out.
- Create a new variable called angle at the top of your code. Before you make the first forward() call left() with angle as its argument. Test it out.
- Create two new variables called x0 and y0 at the top of your code. Give them initial values of 0. Before the call to left() have the pen lift up, use the function goto(x, y) to send the turtle to the location given by x0 and y0. Then put the pen down using the down() function.
- Create three new variables, red, green, and blue. Set each variable to a value between 0.0 and 1.0, where 1.0 is the maximum brightness for that color channel. Before any of the square is drawn, set the color by using the color(red, green, blue) function.
- Call the fill() function with an argument of 1 just before you draw the square. Then call it again after the square is done drawing. What happens?
Put the following line just before you define the first variable.
Then tab in the rest of the commands that draw the square. All of the python commands that are tabbed in after the def statement are inside the function. You can now use the function square() to draw a square. After the function, make a call to the function square() by calling it (but don't tab it in, or it will be inside the function.
Unfortunately, the funciton square() with no parameters draws
the same square over and ver again because the size, location,
orientation, and color are all hard-coded inside the function. Start
to parameterize the function by first adding the parameters
x0 and y0 to the def statement. The remove the two
lines where hard-coded values are assigned to them within the
function. Now the function will use the values passed in from
def square(x0, y0):
Now you can call the function square() with two arguments such as square(0, 0) and square(50, 50) and get squares at different locations.
- Add more parameters to the function so that none of the values are hard-coded inside the function. Then have your program draw many rectangles of differing parameter values.
- Try making a Mondrian style image.
Once you've finished the above exercise, get started on the next project.