Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Turtle graphics is a popular way for introducing programming in Python. It was part of the original Logo programming language developed by Wally Feurzig and

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Turtle graphics is a popular way for introducing programming in Python. It was part of the original Logo programming language developed by Wally Feurzig and Seymour Papert in 1966. Imagine a robotic turtle starting at (0,0) in the xy plane. After an import turtle, we can give it the command turtle.forward(15), and it moves (on-screen!) 15 pixels in the direction it is facing, drawing a line as it moves. If we give it the command turtle.right(25), and it rotates in-place 25 degrees clockwise. By combining together these and similar commands _, we can draw intricate shapes and pictures. Turtle Module Python has a built in module that supports turtle graphics called the "turtle" module. Importing this module gives you access to all the turtle graphics functions you will need to draw vector graphics on the screen. We do this by saying: This allows us to control a cursor, also know as a "turtle", which has the following properties: - A position in 2D space - An orientation, or heading - A pen that can be up or down and can lay color on the canvas You create a turtle by doing the following \[ \text { my_turtle }=\text { turtle. } \text { Turtle( }) \] Yes, that's a lot of turtle words! my_turtle is our variable name (so this could have been anything), turtle refers to Python's turtle module and Turtle() means make a new turtle for us to interact with (this is technically called a constructor, but more on that later...) Turtle Functions You can move your turtle forward or backwards by saying: You can turn your turtle right or left by saying: By default, turtles continuously draw to the screen (the pen starts down). You can change the state of the pen by saying: Example import turtle \#=====main===== my_turtle = turtle.Turtle() size = 100 my_turtle.speed(1) my_turtle.forward(size) my_turtle.right( 90) my_turtle.forward(size) my_turtle.right (90) my_turtle.forward(size) my_turtle.right( 90) my_turtle.forward(size) my_turtle.right( 90) Try playing with it yourself: Draw Box Turtle Example Another Example Let's look at an example: Draw Boxes Tuttle Example import turtle \# get python's turtle "stuff" \# this is a function we are defining called "box" \# - this code is only being defined here, it will not run until invoked \# - it can be invoked (called) by saying "box()" \# - the function will draw a 5050 pixel box on the screen def box(t): t.pendown() t. forward (50) t. Left (90) t. forward (50) t. Left (90) t. forward (50) t. Left (90) t. forward (50) t. left(90) t. penup() #=====main==== \# make a turtle called my_turtle my_turtle = turtle. Turtle() \# pick up pen, so no drawing when repositioned my_turtle.penup() \# move turtle to a specific position on the screen my_turtle.setpos (200,0) \# draw a box - this is repeated 3x box(my_turtle) my_turtle.forward (100) box(my_turtle) my_turtle. forward (100) box(my_turtle) \# draw a box - this is repeated 3x box(my_turtle) my_turtle.forward (100) box(my_turtle) my_turtle.forward (100) box(my_turtle) my_turtle.forward (100) Related Textbook Reading How to Think Like a Computer Scientist: Chapter 4: Turtle Graphics - The notes above include an introduction to for-loops and lists. We will cover these topics in much more depth at a later time, but you should use this opportunity to see a "preview" of the concepts. This means that you should still read and try all the material contained in this chapter, but also know that this is only the first time that you will see the concepts, not the last, and that we_will_delve into them further later in the quarter

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Access Patterns Database Interactions In Object Oriented Applications

Authors: Clifton Nock

1st Edition

0321555627, 978-0321555625

More Books

Students also viewed these Databases questions