Detailed Turtle Graphics Study Notes

Program Initialization

  • Importing the Turtle Module

    • Use the command: import turtle

    • Initializes the turtle graphics module for use.

  • Creating a Screen Object

    • Syntax: screen = turtle.Screen()

    • Sets up a screen where graphics will be drawn.

  • Creating a Turtle Object

    • Syntax: turtle = turtle.Turtle()

    • Initializes a turtle that can draw on the screen.

  • Exiting the Program

    • Use: screen.exitonclick()

    • Keeps the window open until clicked, useful for debugging.

General Program Structure

  • Order of Commands

    • Import turtle module > Initialize screen > Initialize turtle > Your drawing commands > screen.exitonclick().

    • Important to get this order correct for the program to function properly.

  • Cartesian Plane Explanation

    • The turtle starts at the origin (0,0).

    • The x-axis runs horizontally (right is positive, left is negative).

    • The y-axis runs vertically (up is positive, down is negative).

Basic Turtle Movement Methods

  • Basic Movement Methods

    • All use dot notation: turtle_name.method(arguments)

    • Common methods include:

    • forward(pixels)

      • Moves the turtle forward by a specified number of pixels in the direction it is currently facing.

    • left(degrees)

      • Rotates the turtle left (counterclockwise) by the specified number of degrees.

    • right(degrees)

      • Rotates the turtle right (clockwise) by the specified number of degrees.

  • Heading Control

    • setheading(angle)

    • Sets the orientation of the turtle to a specific angle measured from the (0,0) point (where 0 degrees is to the right).

    • Angles:

      • 0°: Right

      • 90°: Up

      • 180°: Left

      • 270°: Down

  • Pen Control

    • penup() - Lifts the turtle's pen (no line drawn).

    • pendown() - Lowers the turtle's pen (draws a line).

Example of Movement

  • Example Drawing Sequence:

    • Initial Position: Turtle (e.g., Fred) at origin facing right.

    • Commands:

    • fred.forward(150) moves forward by 150 pixels.

    • fred.left(90) turns left 90 degrees.

    • fred.forward(150) moves up 150 pixels.

    • fred.right(90) turns right, facing up.

    • fred.forward(150) moves forward 150 pixels.

    • Results in a shape drawn based on these commands.

Advanced Turtle Methods

  • Creating Shapes

    • circle(radius)

    • Draws a circle of specified radius.

    • Circle direction influenced by current turtle heading.

  • Pen Size and Color

    • pensize(width)

    • Sets the width of the pen in pixels.

    • pencolor(color)

    • Sets the pen color (accepts string name of color).

    • Available Colors: red, orange, yellow, green, blue, purple, pink, etc.

  • Positioning with goto(x, y)

    • Moves turtle to specified x and y coordinates on the Cartesian plane.

Filling Shapes with Color

  • Filling Shapes

    • Methods:

    • begin_fill() - Marks beginning of a shape to be filled.

    • end_fill() - Marks end of the shape to be filled.

    • fillcolor(color) - Sets the fill color of the shape.

    • Must draw a closed shape for filling to be effective.

User Interaction

  • Getting User Input

  • Input Function

    • num_input(title, prompt)

    • Displays a dialog box prompting for numerical input.

    • title: Title of the dialog box.

    • prompt: Prompt shown inside the dialog box asking for input.

Utilizing Loops with Turtle Graphics

  • Using Loops to Simplify Code

    • Advantages of using loops over repetitive action coding.

    • For instance, to draw a square:

    • Instead of writing forward and right multiple times, use a loop to repeat this sequence.

    • Example:
      python for i in range(4): turtle.forward(100) turtle.right(90)

Homework Activity

  • The assignment is to draw a hexagon using turtle graphics with repetition structures.

  • Each side of the hexagon should be 100 pixels in length.

  • Change the screen's background color to any preferred color.

Additional Notes

  • All turtle methods follow the same dot notation pattern (turtle_name.method()).

  • Remember to ensure that all closures of shapes and commands are properly aligned to avoid errors in rendering.