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...
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...
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!!...
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
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
Hello! Working with python This program asks user math questions and keeps track of answers... Allow...
Hello! Working with python This program asks user math questions and keeps track of answers... 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...
Please write in python Use modular design to write a program that asks the user to...
Please write in python Use modular design to write a program that asks the user to enter his or her weight and the name of a planet. The program then outputs how much the user would weigh on that planet. The following table gives the factor by which the weight must be multiplied for each planet. PLANET CONVERSION FACTOR Mercury 0.4155 Venus 0.8975 Earth 1.0000 Moon 0.1660 Mars 0.3507 Jupiter 2.5374 Saturn 1.0677 Uranus 0.8947 Neptune 1.1794 Pluto 0.0899 The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT