Turtle Paint A new graphics file format called turtle has been created and you a
ID: 673142 • Letter: T
Question
Turtle Paint 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. This assignment will have you using your skills with file handling, error handling, string manipulation and using the turtle module. To make this easier you should use your newfound knowledge of functions to break the program down into smaller testable portions. You will be required to use functional decomposition in your assignments from this assignment on The Turtle Module.
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.
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 colorred 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 ( Color was previously set to red ) rect-10010020050
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 ( Color was previously set to blue ) circle00100 Result Note : You will want to take note of how turtle draws a circle and what the starting point and direction is. It does not assume the turtle is at the center of the circle.
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 ( Color was previously set to green ) line202045100 Result
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. Three functions aren’t enough for a program of this size. You will still need to create some of your own.
defset_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
"""
defdraw_rectangle(x,y,width,height):
"""Draws a rectangle of given width and heigh tat 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
"""
defdraw_circle(x,y,radius):
"""Draws a circle with turtle at the poin tx,y with the given radius
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. Debug each part thoroughly before moving on.
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
et. 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
bigstar200200500100
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_int75 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
A python program for read the data from the file.
import turtle
def drawQuakes():
filename = input("Please enter the quake file: ")
readfile = open(filename, "r")
readlines = readfile.read()
start = readlines.find("type")
g = readlines[start]
Type = g.split(",")
tracy = turtle.Turtle()
tracy.up()
for points in Type:
print(points)
x = float(Type[1])
y = float(Type[2])
tracy.goto(x,y)
tracy.write(".")
drawQuakes()
import turtle
s = turtle.Screen()
t = turtle.Turtle()
length = None
while not length:
try:
length = float(input('Enter your preferred turtle line length: '))
except ValueError:
print('You need to enter a number')
width = None
while not width:
try:
width = int(input('Enter your preferred turtle line width: '))
except ValueError:
print('You need to enter a positive integer')
else:
if width < 1:
print('You need to enter a positive integer')
width = None
color = None
while not color:
color = input('Enter your preferred turtle line color: ')
try:
t.pencolor(color)
except:
print('You need to enter a color that I know.')
color = None
shape = None
while not shape:
shape = input('Specify whether you want to draw a line, triangle, or square: ')
if shape.lower() not in ['line', 'triangle', 'square']:
shape = None
print('I only draw lines, triangles and squares!')
t.pensize(width)
if shape == 'line':
t.forward(length)
elif shape == 'triangle':
t.forward(length)
t.right(120)
t.forward(length)
t.right(120)
t.forward(length)
else:
t.forward(length)
t.right(90)
t.forward(length)
t.right(90)
t.forward(length)
t.right(90)
t.forward(length)
s.exitonclick()
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.
import os
def get_file_existency(filename, directory):
path = os.path.join(directory, filename)
realpath = os.path.realpath(path)
exists = os.path.exists(path)
if exists:
file = open(realpath, 'w')
else:
file = open(realpath, 'r')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.