CNET-142 Turtle Graphics

Introduction to Turtle Graphics

  • Part of the default Python graphic package.

  • Import the module using import turtle.

Basic Movements

  • Move the turtle cursor:

    • Forward: Move forward a specified distance.

    • Turning: Change direction:

      • Left turn by a certain angle.

      • Right turn by a certain angle.

Pen Control

  • Pen Up:

    • When the pen is raised, moving the turtle does not draw.

  • Pen Down:

    • When the pen is down, moving the turtle leaves a drawing or line.

Drawing Shapes

  • Ability to draw geometric shapes:

    • Circles: Create circles of a specified radius.

    • Dots: Draw dots of a specified size.

  • Change properties:

    • Change color and size of shapes.

Background and Screen Control

  • Change background color and clear the drawing screen.

Coordinate System

  • Coordinate origin is at the center (0,0).

  • Movement is based on X (horizontal) and Y (vertical) coordinates.

Example Scripts

Simple Movement Example

  • Code example that demonstrates moving the turtle and changing pen status.

    • Moving forward with the pen up results in no drawing.

    • Move to specific coordinates and draw shapes with pen down.

Drawing Circles Example

  • Drawing multiple circles with varying coordinates and colors.

  • Demonstrating the use of pen size and pen color settings.

Using Variables for Dynamic Drawing

  • Use variables for coordinates:

    • Example of adjusting X and Y values dynamically to draw multiple shapes.

Additional Features

Text and Labeling

  • Ability to write text on the screen using turtle graphics.

    • Can change position based on coordinates.

Animating the Turtle

  • Adjust the speed of the turtle's movement:

    • Speed 0 disables animation, allowing for instant movements.

Sophisticated Drawings

Olympic Rings Example

  • Step-by-step guide to drawing the Olympic Rings:

    • Set initial coordinates and pen settings for each ring.

    • Draw and fill shapes, then add text.

Compass Example

  • Drawing a compass using similar techniques:

    • Position markings for North, South, East, and West.

USA Flag Example

  • Drawing the USA flag:

    • Structure to draw various parts of the flag including stripes and stars.

    • Combining all parts into one cohesive picture.

Combining Functions and Graphics

  • Example of combining multiple drawings into one script:

    • How to include different functions for drawing.

    • Importance of handling the main loop to prevent the program from freezing.

Conclusion

  • Turtle graphics is a powerful tool for creating visualizations in Python.

  • Encouragement to explore and modify sample codes for further learning.