This document discusses using the Python Turtle module to draw shapes and diagrams with code. It provides examples of basic turtle commands like forward, left, color, and explains how to import and use the turtle module. The document encourages exploring different turtle methods and provides a link to the Python turtle documentation.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
23 views15 pages
4 Drawing With Code
This document discusses using the Python Turtle module to draw shapes and diagrams with code. It provides examples of basic turtle commands like forward, left, color, and explains how to import and use the turtle module. The document encourages exploring different turtle methods and provides a link to the Python turtle documentation.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15
Drawing with Code
Powered by AFRL NM STEM Academy
Drawing with Code • We will use the Python Turtle module to make drawings with code. • A module is a collection of code that can be used in your program. • Programming with modules makes coding faster and easier because most of the code is already written. • We use pieces of modules that we need to make something new. Modules Using modules is sort of like ordering pizza instead of making it from scratch. It’s possible to build a bicycle by ordering needed parts and assembling the parts at your home. You may also be able to order a bicycle by specifying what parts you want to include. Thankfully, there is no need to make handlebars or seats. Drawing with Code Copy and paste the code below into your Python editor. • The import turtle # Drawing with turtles statement will provide import turtle access to the turtle larry = turtle.Turtle() module code. larry.color('blue') larry.pensize(10) • The next line of code larry.shape('turtle') creates a turtle pen and # gives it a name. larry.forward(50) larry.left(45) larry.forward(100) Drawing with Code • The color, size and shape # Drawing with turtles of the pen can be import turtle selected with functions larry = turtle.Turtle() from the module. larry.color('blue') • The forward function will larry.pensize(10) move the pen forward a larry.shape('turtle') desired number of pixels. # • The left function will turn larry.forward(50) to the left a desired larry.left(45) angle. larry.forward(100) Drawing with Code Click on Save and then Run. # Drawing with turtles import turtle larry = turtle.Turtle() larry.color('blue') larry.pensize(10) larry.shape('turtle') # larry.forward(50) larry.left(45) larry.forward(100) Drawing with Code The Turtle Graphics window will display the results of your code. • The pen starts in the middle of the screen, facing the right of the screen. • The pen should move quickly. • The window will be larger or smaller depending on the editor used.
• All of the above variables( position, pen speed, window
size) can be changed with Turtle functions. Drawing with Code Go to the Python turtle library to learn about how to use all of the options available: https://docs.python.org/3.3/library/turtle.html?highlight =turtle Drawing with Code The following is a description of the shape function or method. You may see mention of methods and functions. For our purposes, methods are functions.
Explore the library at:
https://docs.python.org/3.3/library/turtle.html?highlight =turtle Drawing with Code The speed function may be useful to slow down or speed up the pen.
Explore the library at:
https://docs.python.org/3.3/library/turtle.html?highlight =turtle Drawing with Code The circle function below is fun to play with. There are several other drawing options available. Drawing with Code There is a great search feature on the Python library site. Scroll to the bottom of the page. There is a small search tool on the left side of the page. This makes it easier to find something you may be trying to find. Drawing with Code As you write code to draw shapes, you may want to lift the pen, move it to another place, and continue to draw. The below code shows how to move the pen to a different place in the window to continue drawing.
• The goto() function will move the pen to an X, Y
coordinate in the drawing window. • The middle of the screen is (0,0). Drawing with Code This is your code so far. # Drawing with turtles import turtle larry = turtle.Turtle() You can modify this code to draw larry.color('blue') larry.pensize(10) most shapes. larry.shape('turtle') # Spend some time playing with it. larry.forward(50) larry.left(45) Try to draw some simple shapes. larry.forward(100) # larry.penup() Copy and paste the code into larry.goto(-280,100) your Python editor and run the larry.pendown() larry.forward(200) program. larry.left(90) Your first challenge is to write code that will draw your first name.
Scratch Games Programming for Kids & Students: A Step-by-Step Guide and Design Programs for Creating Thoughtful Animations, Puzzles, and Games with Scratch 3.0