Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

General Directions In this lab you will work, once again with Turtle graphics and explore how to determine the Turtles location, working with drawing shapes,

General Directions In this lab you will work, once again with Turtle graphics and explore how to determine the Turtles location, working with drawing shapes, filling those shapes and working wit the pen. This assignment will utilize decision structures to help you draw different shapes. Drawing Shapes For this section of the lab you will write code that will draw a circle, square and a triangle. We will also utilize some color in our drawing both the fill and pen color and pen size or thickness of the pen. Rather than us just typing the colors in, we will let the user decide on the pen color and pen size for each shape. Finally we will learn how to move the turtles pen to different parts of the screen. Below summarizes the methods we will use: .forward() .right() .left() .pensize() .pencolor() .fillcolor() .begin_fill() .end_fill() .penup() .pendown() .circle() .goto()

Drawing a circle is pretty easy. We can use the .circle() method. To determine the size of our circle, we send in to this method the radius. For instance to draw a circle with a 90 pixel radius, we would type in: turtle.circle(90)

Drawing the square involves the .forward(), .right() and .left() methods. Drawing the triangle also utilizes these same methods but requires more trial and error to determine the angles of the lines. Follow theses steps to draw your shapes: Open up IDLE and open a new window. Save this file as DrawingShapes.py. At the top of your new window, type in import turtle. This statement imports the turtle library or class. We will work on the circle first. As mentioned in the description above, we will allow the user to determine the pen size, color and fill. For the pen size, we will only allow the user to determine this once so that it is applicable to all three shapes. Additionally we will limit the pen color to red, blue or yellow. Depending upon which of these colors they choose, it will determine the fill color. If the user chooses the color red, fill the circle with yellow. If the user chooses the blue pen color fill the circle with red. Finally if the user chooses yellow, then fill the circle with green.Therefore, we will need to create some variables. Underneath the import turtle option, type in #variables pen_color = str() fill_color = str() pen_size = int()

Next we need to move the pen so our circle starts at the top of the screen. Since we dont want the turtle drawing while we are moving we will use the .penup() and then .pendown() methods. Finally to move the turtle to a specifc part of the screen, we will use the .goto() method. The first argument when we use this method is the horizontal position. The second argument is the vertical position. So, turtle.goto(0,0) is the center of the screen. Type in the following code: #Moving the pen turtle.penup() turtle.goto(0, 150) turtle.pendown()

Type in two input statements that allow the user to determine the pen size and pen color. The pen size can be any integer from 1 to 10. The pen color needs to be red, blue or yellow. Now create a decision structure to determine the fill color. Type in the following cod eo set the pen size, pen color and fill color. turtle.pensize(pen_size) turtle.pencolor(pen_color) turtle.fillcolor(fill_color)

To begin drawing the circle we need to turn on the fill color. As soon as we are done filling our color we need to turn it off. Type in the following code: Run your code. You will be prompted for a pen size and a pen color. For my test I entered 3 for the pen size and red for the pen color. Below is what should print to your screen. Now we move on to the square. Using the code listed in #4 above about moving the pen as a starting point, put the pen up, move turtle to the left 90 degress, move turtle forward 150 pixels and put the pen down. This should move you to the position to start drawing the square. Next ask the user for another pen color and set the turtles pen color to that new color. Using the decision structure you wrote above, copy and paste it below so you can determine the fill color. Change the fill color for the turtle and begin fill. Type in the following code.

Stop filling and test. I selected 3 as the pen size and red for my circle. I selected blue for my square. Your shapes should look like this so far:

On to our last shape, the Triangle. We need to move the pen one last time. Using the moving pen code listed above, put the turtles pen up, move it left 90 degrees, move it forward 150 pixels and then put the pen down. As with the square, you need to ask the user for another pen color and determine the fill color. Copy and paste your working code that does that. Begin filling by typing in the turtles begin fill method. Drawing a triangle is tricky due to the angles of the turns. Here is the code that will draw a triangle. Add code to stop the fill. Run your code to test. When using 3 as the pen size, red for the circle, blue for the square and green for the triangle, my output looked like the following:

On Your Own In this section of the lab you will create code that will draw either a square or a circle in a particular location on the screen. Those locations are Top Left, Top Right, Bottom Left and Bottom Right. The user will determine the shape as well as the pen color and fill. Based on these decisions your code will need to do the following: Top Left Location: the pen size must be 3 Top Right Location: the pen size must be 5 Bottom Left Location: the pen size must be 7 Bottom Right Location: the pen size must be 9 Circles: When the user selects a circle the program needs to ask the user for the pen color. The choices are red, blue or yellow. All of the pen colors will determine a specific fill color. Explore online to find a new fill color. Squares: When the user selects a square the program needs to ask the user for a fill color. The choices are red, blue or yellow. These colors will determine the pen color. Pick a different color. For help with the location of your turtle, check out the section of your book in chapter 2 titled Moving the Turtle to a Specific Location. Also you will want to clear your screen and pause the execution of your code to give a little time between drawing the circle, square and triangle to drawing your new diagrams. To do that import the time library and clear your screen. Here is the code: import time #typed in at the top time.sleep(3) turtle.reset() turtle.setup(600, 600) #sets up the size of the window Additionally, since you have code from the first part of the assignment that works, comment that out while writing the new code. This will save time running and testing your code. Below are a few screen shots displaying what your output should look like. Keep in mind your colors will be different.

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

Database Basics Computer EngineeringInformation Warehouse Basics From Science

Authors: Odiljon Jakbarov ,Anvarkhan Majidov

1st Edition

620675183X, 978-6206751830

Students also viewed these Databases questions