CSC 226

CSC 226 logo

Software Design and Implementation


A6: Turtle Houses, Animals, People, etc.

Assignment A6 should be complete individually.

In this assignment, you will learn to use functions, more about image color in addition to gaining practice with turtle graphics.

Useful Examples using Functions and Turtle Methods

Here are some examples of code which is organized into functions:

Image Colors

Images displayed on a screen use light for the display.  Any three colors (or frequencies) of light that produce white light when combined with full intensity are called primary colors of light. The most commonly used set of primary colors of light is the set Red (R), Green (G), and Blue (B).  Using a term borrowed from neuroscience, each color is typically called a color channel.

The following is a widget we created for your exploration of color channels.  Try various RGB color channel values between 0 and 255:

Object-Oriented Programming (OOP) Design

Python supports an important kind of program design called object-oriented programming.   We will learn much more about this later, but we want to take this opportunity to introduce some important terminology: classes, objects, states, and methods.

At this point, we have used two different classes: the random class and the turtle class.

A class can be defined as a template or a blue print that describes the states and the behaviors that an object of its type supports. An object is simply an instance of a class, and a method is used to change a state or initiate a behavior.

Our text created two objects (two instances of the turtle class): Tess and Alex. 

Recall that objects have states and behaviors. Example: A turtle object has states such as color, orientation, penup, pendown, etc as well as behaviors such as moving forward, turning left, etc which are changed or initiated with by calling methods.

Methods of the Turtle Class

A summary of all turtle methods for Python 2.7 can be found at: http://docs.python.org/2.7/library/turtle.html

Some turtle methods which may be particularly useful for this assignment assuming that myturtle has been instantiated as a turtle object.
  • myturtle.begin_fill(), myturtle.end_fill() # The figure drawn between the two fill commands is filled with the present color setting.
  • myturtle.circle(radius) # Draws a circle of the indicated radius. Assuming the radius is positive, the circle is drawn tangent to the direction of myturtle in a clockwise direction.
  • myturtle.clear() # Erase everything written by myturtle.
  • myturtle.fillcolor(r, g, b), myturtle.fillcolor(s) # Set the color for filling figures--see widget above for exporation. In fillcolor(r,g,b), each argument is a floating point number between 0.0-1.0; the fraction of red, the fraction of green, and the fraction of blue. In fillcolor(s) the argument is a string indicating a color by name or by its hexadecimal code, e.g., “green”, “red”, “#6600FF”. Arguments are the same for pencolor.
  • myturtle.forward(distance), myturtle.backward(distance) # Move myturtle forward or backward the amount of distance indicated. Oriented on the direction myturtle is facing. Draws a line if myturtle is down, not if myturtle is up.
  • myturtle.pencolor(r,g,b), myturtle.pencolor(s) # Set the color that myturtle. In pencolor(r,g,b), each
    argument is a floating point number between 0.0-1.0; the fraction of red, the fraction of green and the fraction of blue. In pencolor(s) the argument is a string indicating a color by name or by its hexadecimal code, e.g., “green”, “red”, “#6600FF”.
  • myturtle.pensize(width) # Set the line thickness to width.
  • myturtle.right(degrees), myturtle.left(degrees) # Turn the given direction oriented by myturtle.
  • myturtle.setpos(x,y) # Move myturtle to the specified point, drawing a line along the way if myturtle is down, and not drawing if myturtle is up.
  • myturtle.shape("circle") # May use the following polygon shapes: "arrow", "circle", "classic", "square", "triangle", "turtle"
  • myturtle.shapesize(stretch_wid=2, stretch_len=None, outline=None), myturtle.turtlesize(stretch_wid=2, stretch_len=None, outline=None) # myturtle will be displayed stretched according to its stretchfactors: stretch_wid is perpendicular to its orientation, stretch_len is in the direction of its orientation, outline determines the width of the shapes’s outline.
  • myturtle.stamp() # Stamp a copy of the turtle shape onto the canvas at the current turtle position.
  • myturtle.write(string) # Write a string starting at the present myturtle point.
  • myturtle.up(), myturtle.down(), myturtle.penup(), myturtle.pendown(), # Set myturtle state to be up (for not drawing) or down (for drawing)
We have already seen a very minimal house drawn with turtle graphics: turtle-house.py (right-click to download). 
Here is an example which "upgrades" Dr. Jan's house to a rather nice chateau with a roof and a deck: Dr. Jan's Chateau. Be sure to look through the code to see how we added a background image, roof image, and deck image.
We know you can do far better than this level of graphics!


The instructions

This assignment should be completed individually, but consulting with each other is fine. 

In this assignment, you will draw something complex like a house, animal, or person using turtle graphics and using functions.

  1. You must use functions for encapsulating each "mental chunk".
  2. Include a main() function.
  3. Make effective use of functions and to use docstrings to help clearly explain what each function is designed to do (hopefully, what the functions do will match what you wrote they would do).
  4. The highest level of your program (i.e., no indenting) should only contain the following:
    • the header
    • any import statements
    • function definitions
    • a call to the main() function
  5. All of your own function definitions should come before the def main(): function definition AND the call to the main() function. In other words, the last lines of your code should be:

    def main ():
        # your code inside of main

    main()

  6. All of your own function definitions should be at the highest level of your program (i.e., no indenting). Though it is possible to do, functions should really not be DEFINED inside of other functions (they can be CALLED inside other functions though).
  7. Make sure that the thing you draw:
    • Has at least one complex thing which looks like some sort of building, animal or person.
    • Is set against a background which is not white. You can use an image or a color as your background.
    • Has some embellishments such as windows, text, trees, flowers in front of a house or something--these are not all required--they are just suggestions.
    • Uses an unnamed color via either RGB or Hexadecimal.
    • Has at least one interesting detail such as  intricate windows, smoke out of the chimney, clothing, etc.
    • Uses creativity (such as the use of color, an intricate shape, a cool design…)
    • Be sure to include our standard header at the top of your program with name, username, assignment number, purpose and acknowledgements.
    • Also, please be sure to include comments for the sections in your code which draw the different shapes.
Note that this assignment was adapted from "http://www.garfieldcs.com/2012/03/python-turtle-house-practice/".

Copyright © 2016 | http://cs.berea.edu/courses/CSC226/ | Licensed under a Creative Commons Attribution-Share Alike 3.0 United States License