Question

In: Computer Science

Write a python program that will allow a user to draw by inputting commands. The program...

Write a python program that will allow a user to draw by inputting commands. The program will load all of the commands first (until it reaches command "exit" or "done"), and then create the drawing.

Must include the following:

change attributes:

  • color [red | green | blue]

  • width [value]

  • heading [value]

  • position [xval] [yval]

drawing:

  • draw_axes

  • draw_tri [x1] [y1] [x2] [y2] [x3] [y3

  • draw_rect [x] [y] [b] [h]

  • draw_poly [x] [y] [n] [s]

  • draw_path [path]

random

  • random [color | width | heading | position]

    • e.g. "random color" would set turtle to a random drawing color

Heres my starter code:

import turtle

#================ command loading =================

lines = [] #initialize empty list

# load up all commands into the list
def load_commands():
  
while(1):
  
line = input('>')
  
words = line.split()
if words[0] == 'exit':
break
  
lines.append(line)

#============== turtle drawing functions ============

def draw_rect(alex, x, y, b, h):
alex.pu()
alex.goto(x,y)
alex.pd()
  
for i in range(2):
alex.fd(b)
alex.lt(90)
alex.fd(h)
alex.lt(90)
  
#==================== process commands (do the drawing)======

def process_commands():
  
# ------ initialize turtle stuff ----------------
  
turtle.TurtleScreen._RUNNING=True
  
wn = turtle.Screen() # Set up the window and its attributes
wn.bgcolor("lightgreen")
wn.title("Alex meets a function")
  
alex = turtle.Turtle() # Create alex
  
#-----------------------------------------
  
# process each line and draw
for line in lines:
  
words = line.split()
  
if words[0] == "draw_rect":
x = int(words[1])
y = int(words[2])
b = int(words[3])
h = int(words[4])
draw_rect(alex,x,y,b,h)
  
  
wn.exitonclick()
  
#================== MAIN ======================
  
def main():
print("loading commands")
load_commands()
print("drawing")
process_commands()
  

main()

Solutions

Expert Solution

PROGRAM:

OUTPUT:

SUMMARY:

Program to load commands and draw based on the commands .

Step1: Load commands()

  • load the command to list till exit or done

Step2: Process command()

  • Set the turtle window and the turtle object
  • Iterate through each command
  • command is divided into 3 - change attributes, drawing and random
  • Change attributes:
    • Set position - using got(x,y)
    • Set color - using turtle.color(color)
    • Set width - using turtle.width(width)
  • Drawing:
    • draw rectangle - xpos,ypos, width and height (90 degree turn)
    • draw triangle - 3 points (120 degree turn )
    • draw polygon - n sides( 360/side degree turn)
    • draw path - (length)
  • Random
    • To use random import random module
    • Set color - shuffle the colorlist - red ,blue, and green - select the one
    • Set position - using randint(start,end) - generate number between start and end
    • Set width - similarly using randint(start,end)

Step 3: Show the final result


Related Solutions

CODE IN PYTHON: Your task is to write a simple program that would allow a user...
CODE IN PYTHON: Your task is to write a simple program that would allow a user to compute the cost of a road trip with a car. User will enter the total distance to be traveled in miles along with the miles per gallon (MPG) information of the car he drives and the per gallon cost of gas. Using these 3 pieces of information you can compute the gas cost of the trip. User will also enter the number of...
Write a GUI based Python program that will allow the user to perform (i) generate RSA...
Write a GUI based Python program that will allow the user to perform (i) generate RSA keys, (ii) encrypt a given message and (iii) decrypt the message without using any cryptographic library. Your program will generate the values of p unless the user provides them manually. Then the program will generate the keys (private and public). Then the program will allow the user to enter his/her message to be encrypted. Finally, the program will perform the decryption operation as well....
In python. Projectile motion: Write a python program that will ask the user for      an...
In python. Projectile motion: Write a python program that will ask the user for      an initial height y0, initial velocity v, launch angle theta, and mass m.      Create two functions, one that will calculate max height      of the projectile, and one that will calculate the range. Ask the     user which one he/she would like to calculate, then present them with the answer. (use kg, m and m/s)
Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
Using Tkinter for GUI, A Python program that will allow the user to perform (i) generate...
Using Tkinter for GUI, A Python program that will allow the user to perform (i) generate RSA keys, (ii) encrypt a given message and (iii) decrypt the message without using any cryptographic library. Your program will generate the values of p unless the user provides them manually. Then the program will generate the keys (private and public). Then the program will allow the user to enter his/her message to be encrypted. Finally, the program will perform the decryption operation as...
create a program that asks user math questions and keeps track of answers... Python Allow the...
create a program that asks user math questions and keeps track of answers... Python Allow the user to decide whether or not to keep playing after each math challenge. Ensure the user’s answer to each math problem is greater than or equal to zero. Keep track of how many math problems have been asked and how many have been answered correctly. When finished, inform the user how they did by displaying the total number of math problems, the number they...
Forms often allow a user to enter an integer. Write a program that takes in a...
Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. Ex: If the input is: 1995 the output is: yes Ex: If the input is: 42,000 or any string with a non-integer character, the output is: no PYTHON 3
Write a program to allow a user to play the game Hangman. DO NOT USE AN...
Write a program to allow a user to play the game Hangman. DO NOT USE AN ARRAY The program will generate a random number (between 1 and 4581) to pick a word from the file - this is the word you then have to guess. Note: if the random number you generate is 42, you need the 42nd word - so loop 41 times picking up the word from the file and not doing anything with it, it is the...
In Python write a program that asks the user to enter the monthly costs for the...
In Python write a program that asks the user to enter the monthly costs for the following expenses incurred from operating his or her automobile: loan payment, insurance, gas, oil, tires, and maintenance the program should then display the total monthly cost of these expenses, and the total annual cost of these expenses. your program MUST have BOTH a main function AND a function named calcExpenses to calculate the expenses. DO NOT display the expenses inside of the calcExpenses function!!...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT