Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python 3.4 Turtle Paint program help please A new graphics file format called tu

ID: 672903 • Letter: P

Question

Python 3.4 Turtle Paint program help please

A new graphics file format called turtle has been created and you are tasked writing a python program to read the data from the file and using the turtle module to draw the shapes from the file. You will be required to use functional decomposition.

Turtle Coordinate System

The turtle module uses a 2d cartesian coordinate system. This means point 0, 0 is at the center of the screen. The y coordinate increases in the up direction and decreases in the down direction. The x coordinate increases going right and decreases going left. The heading of the turtle defaults degrees. Zero degree heading is to the right and runs counter clockwise.

Input file

The input file contains commands to draw with turtle. Each line is a separate action or item to draw. There are five different commands color, rect, circle, and line. The beginning of each line will contain the command followed by the arguments for the command. The arguments are separated by a space.

Color Command

The color has one argument which is a color to set the draw and fill color to. While color won’t draw anything in turtle it will set the colors for any action that does.

Example Line: color red

RECT Command

The rect command is used to draw a rectangle with turtle. It has 4 arguments x, y, width and height. x and y are the top left coordinates of the rectangle. The rectangle drawn should be filled when complete.

Example Line: rect -100 100 200 50

Circle Command

The circle command draws a circle. There are 3 arguments passed to the circle command the x, and y coordinate for the center of the circle and the radius of the circle. The circle drawn should be filled.

Example Line: circle 0 0 100

Line Command

The line command draws a simple line. It is used to draw a line from a given x, y in a particular direction for the given length. There are 4 arguments x, y, heading, and length of line.

Example Line: line 20 20 45 100

Functions

You are going to need to create and use functions for this assignment. To help get you started we’ve created 3 functions definitions for you. You will need to use these in your solution. We’ve only given you the definitions, so you’ll have to write the python code to get them to work.

def set_color(clr): Sets the line and fill color the color being passed

Parameters:

clr: str - the color to set the line and fill color to.

returns: None

def draw_rectangle(x, y, width, height): Draws a rectangle of given width and height at position x, y

Parameters:

x: int - the x position of the top left corner.

y: int - the y position of the top right corner.

width: int - the width of the rectangle to be drawn

height: int - the height of the rectangle to be drawn

returns: None

def draw_circle(x, y, radius): Draws a circle with the turtle at the point x, y with the given

Parameters:

x: int - the x position of the center of the circle.

y: int - the y position of the center of the circle.

radius: int - the radius of the circle to be drawn

returns: None

Requirements:

The program should ask the user for the file to open and draw with turtle.

If the file does not exist, the program should warn the user and ask for a valid filename again.

Read through the file performing the actions specified with the arguments given. The files will be plain text with the command and arguments separated by spaces.

If the line is an invalid command, warn the user and output the line to the shell. The program should continue execution of the text file.

If a command has too few arguments, or an integer argument can’t be converted, you should warn the user in the console and output the line that caused the problem. The program should continue execution of the text file

After the file has been drawn the user can enter another file to be drawn. Before drawing a new image you should clear the current turtle screen.

If the user enters quit for the filename the program ends

Development notes

When moving the turtle to a new location it will continue to draw. You need to find a way to suppress that action. If it was a pen on paper you’d pick the pen up

Circle and rectangle should fill when complete

Set the turtle speed to the fastest speed before submitting your program. While debugging you may want to slow it down if you are having trouble and want to see what the turtle is doing better.

Break this down into small parts

   Create a function to draw a filled circle with the center at a given point and given radius

   Create a function to draw a filled rectangle

Example

>>>================================RESTART================================ >>>

Enter File to open and draw ==> notexists.turtle

Could not open file : notexists.turtle

Enter File to open and draw ==> badcommand.turtle

Bad command given could not interpret

  bigstar 200 200 500 100

Enter File to open and draw ==> rectangle.turtle

Enter File to open and draw ==> rectangle_index_error.turtle

The line did not have proper number of arguments.

  rect50

Enter File to open and draw ==> rectangle_value_error.turtle

Could not convert an argument to int

rect not_int 75 200 50

Enter File to open and draw ==> circle.turtle

Enter File to open and draw ==> star.turtle

Enter File to open and draw ==> pythonlogo.turtle

Enter File to open and draw ==> quit

Explanation / Answer

>>> import os >>> if os.path.exists("myfile.dat"): ... f = file("myfile.dat", "r+") ... else: ... f = file("myfile.dat", "w") import turtle def draw_square(t, sz): """Make turtle t draw a square of sz.""" for i in range(4): t.forward(sz) t.left(90) wn = turtle.Screen() # Set up the window and its attributes wn.bgcolor("lightgreen") wn.title("Alex meets a function") alex = turtle.Turtle() # Create alex draw_square(alex, 50) # Call the function to draw the square wn.mainloop() >>> turtle.home() >>> turtle.left(90) >>> turtle.heading() 90.0 Change angle measurement unit to grad (also known as gon, grade, or gradian and equals 1/100-th of the right angle.) >>> turtle.degrees(400.0) >>> turtle.heading() 100.0 >>> turtle.degrees(360) >>> turtle.heading() 90.0 >>> turtle.home() >>> turtle.left(90) >>> turtle.heading() 90.0 >>> turtle.radians() >>> turtle.heading() 1.5707963267948966